11 #include <validation.h>
25 explicit DB(
size_t n_cache_size,
bool f_memory =
false,
bool f_wipe =
false);
32 bool WriteTxs(
const std::vector<std::pair<uint256, CDiskTxPos>>& v_pos);
45 return Read(std::make_pair(
DB_TXINDEX, txid), pos);
51 for (
const auto& tuple : v_pos) {
54 return WriteBatch(batch);
63 const std::pair<unsigned char, uint256>& begin_key,
64 const std::pair<unsigned char, uint256>& end_key)
91 bool f_legacy_flag =
false;
92 block_tree_db.
ReadFlag(
"txindex", f_legacy_flag);
95 return error(
"%s: cannot write block indicator", __func__);
97 if (!block_tree_db.
WriteFlag(
"txindex",
false)) {
98 return error(
"%s: cannot write block index db flag", __func__);
108 LogPrintf(
"Upgrading txindex database... [0%%]\n");
109 uiInterface.ShowProgress(
_(
"Upgrading txindex database").translated, 0,
true);
111 const size_t batch_size = 1 << 24;
114 CDBBatch batch_olddb(block_tree_db);
116 std::pair<unsigned char, uint256> key;
118 std::pair<unsigned char, uint256> prev_key = begin_key;
120 bool interrupted =
false;
121 std::unique_ptr<CDBIterator> cursor(block_tree_db.
NewIterator());
122 for (cursor->Seek(begin_key); cursor->Valid(); cursor->Next()) {
128 if (!cursor->GetKey(key)) {
129 return error(
"%s: cannot get key from valid cursor", __func__);
136 if (++
count % 256 == 0) {
139 const uint256& txid = key.second;
140 uint32_t high_nibble =
141 (
static_cast<uint32_t
>(*(txid.
begin() + 0)) << 8) +
142 (
static_cast<uint32_t
>(*(txid.
begin() + 1)) << 0);
143 int percentage_done = (int)(high_nibble * 100.0 / 65536.0 + 0.5);
146 if (report_done < percentage_done/10) {
147 LogPrintf(
"Upgrading txindex database... [%d%%]\n", percentage_done);
148 report_done = percentage_done/10;
153 if (!cursor->GetValue(value)) {
154 return error(
"%s: cannot parse txindex record", __func__);
156 batch_newdb.
Write(key, value);
157 batch_olddb.
Erase(key);
164 batch_newdb, batch_olddb,
180 batch_newdb, batch_olddb,
217 if (pindex->
nHeight == 0)
return true;
220 std::vector<std::pair<uint256, CDiskTxPos>> vPos;
221 vPos.reserve(block.
vtx.size());
222 for (
const auto& tx : block.
vtx) {
223 vPos.emplace_back(tx->GetHash(), pos);
226 return m_db->WriteTxs(vPos);
234 if (!
m_db->ReadTxPos(tx_hash, postx)) {
240 return error(
"%s: OpenBlockFile failed", __func__);
246 return error(
"%s: fseek(...) failed", __func__);
249 }
catch (
const std::exception& e) {
250 return error(
"%s: Deserialize or I/O error - %s", __func__, e.what());
252 if (tx->GetHash() != tx_hash) {
253 return error(
"%s: txid mismatch", __func__);
The database stores a block locator of the chain the database is synced to so that the index can effi...
Base class for indices of blockchain data.
virtual bool Init()
Initialize internal state from the database and block index.
Non-refcounted RAII wrapper for FILE*.
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
FlatFilePos GetBlockPos() const
int nHeight
height of the entry in the chain. The genesis block has height 0
Access to the block database (blocks/index/)
bool ReadFlag(const std::string &name, bool &fValue)
bool WriteFlag(const std::string &name, bool fValue)
Batch of changes queued to be written to a CDBWrapper.
size_t SizeEstimate() const
void Write(const K &key, const V &value)
CDBIterator * NewIterator()
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
bool Write(const K &key, const V &value, bool fSync=false)
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
Access to the txindex database (indexes/txindex/)
bool WriteTxs(const std::vector< std::pair< uint256, CDiskTxPos >> &v_pos)
Write a batch of transaction positions to the DB.
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
bool MigrateData(CBlockTreeDB &block_tree_db, const CBlockLocator &best_locator)
Migrate txindex data from the block tree DB, where it may be for older nodes that have not been upgra...
bool ReadTxPos(const uint256 &txid, CDiskTxPos &pos) const
Read the disk location of the transaction data with the given hash.
TxIndex is used to look up transactions included in the blockchain by hash.
TxIndex(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
bool FindTx(const uint256 &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
BaseIndex::DB & GetDB() const override
bool Init() override
Override base class init to migrate from old database.
virtual ~TxIndex() override
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
const std::unique_ptr< DB > m_db
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
static void LogPrintf(const char *fmt, const Args &... args)
std::unique_ptr< T > MakeUnique(Args &&... args)
Substitute for C++14 std::make_unique.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
std::shared_ptr< const CTransaction > CTransactionRef
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 byt...
size_t GetSerializeSize(const T &t, int nVersion=0)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
const fs::path & GetDataDir(bool fNetSpecific)
bool error(const char *fmt, const Args &... args)
bilingual_str _(const char *psz)
Translation function.
static void WriteTxIndexMigrationBatches(CDBWrapper &newdb, CDBWrapper &olddb, CDBBatch &batch_newdb, CDBBatch &batch_olddb, const std::pair< unsigned char, uint256 > &begin_key, const std::pair< unsigned char, uint256 > &end_key)
constexpr char DB_TXINDEX
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
constexpr char DB_BEST_BLOCK
constexpr char DB_TXINDEX_BLOCK
CClientUIInterface uiInterface
std::unique_ptr< CBlockTreeDB > pblocktree
Global variable that points to the active block tree (protected by cs_main)
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)