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);
51 for (
const auto& tuple : v_pos) {
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);
145 uiInterface.ShowProgress(
_(
"Upgrading txindex database").translated, percentage_done,
true);
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__);
std::shared_ptr< const CTransaction > CTransactionRef
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)
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
virtual bool Init()
Initialize internal state from the database and block index.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
constexpr char DB_TXINDEX_BLOCK
Batch of changes queued to be written to a CDBWrapper.
CChain & ChainActive()
Please prefer the identical ChainstateManager::ActiveChain.
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
constexpr char DB_TXINDEX
static void LogPrintf(const char *fmt, const Args &... args)
FlatFilePos GetBlockPos() const
std::unique_ptr< T > MakeUnique(Args &&... args)
Substitute for C++14 std::make_unique.
const std::unique_ptr< DB > m_db
bool Init() override
Override base class init to migrate from old database.
CDBIterator * NewIterator()
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Access to the txindex database (indexes/txindex/)
bool ReadTxPos(const uint256 &txid, CDiskTxPos &pos) const
Read the disk location of the transaction data with the given hash.
size_t GetSerializeSize(const T &t, int nVersion=0)
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.
bool FindTx(const uint256 &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Access to the block database (blocks/index/)
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...
std::unique_ptr< TxIndex > g_txindex
The global transaction index, used in GetTransaction. May be null.
bilingual_str _(const char *psz)
Translation function.
const fs::path & GetDataDir(bool fNetSpecific)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
void Write(const K &key, const V &value)
BaseIndex::DB & GetDB() const override
size_t SizeEstimate() const
bool Read(const K &key, V &value) const
bool ReadFlag(const std::string &name, bool &fValue)
TxIndex(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
virtual ~TxIndex() override
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.
std::vector< CTransactionRef > vtx
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
The block chain is a tree shaped structure starting with the genesis block at the root...
bool WriteTxs(const std::vector< std::pair< uint256, CDiskTxPos >> &v_pos)
Write a batch of transaction positions to the DB.
FILE * OpenBlockFile(const FlatFilePos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
DB(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
constexpr char DB_BEST_BLOCK
std::unique_ptr< CBlockTreeDB > pblocktree
Global variable that points to the active block tree (protected by cs_main)
bool WriteFlag(const std::string &name, bool fValue)
bool WriteBatch(CDBBatch &batch, bool fSync=false)
CClientUIInterface uiInterface
int nHeight
height of the entry in the chain. The genesis block has height 0
static const int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
bool error(const char *fmt, const Args &... args)
TxIndex is used to look up transactions included in the blockchain by hash.
Non-refcounted RAII wrapper for FILE*.