mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-23 18:32:45 +02:00
indexes, refactor: Remove CBlockIndex* uses in index WriteBlock methods
Replace WriteBlock method with CustomAppend and pass BlockInfo struct instead of CBlockIndex* pointer This commit does not change behavior in any way.
This commit is contained in:
parent
bef4e405f3
commit
dc971be083
@ -884,6 +884,7 @@ libbitcoinkernel_la_SOURCES = \
|
|||||||
flatfile.cpp \
|
flatfile.cpp \
|
||||||
fs.cpp \
|
fs.cpp \
|
||||||
hash.cpp \
|
hash.cpp \
|
||||||
|
kernel/chain.cpp \
|
||||||
kernel/checks.cpp \
|
kernel/checks.cpp \
|
||||||
kernel/coinstats.cpp \
|
kernel/coinstats.cpp \
|
||||||
kernel/context.cpp \
|
kernel/context.cpp \
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <chainparams.h>
|
#include <chainparams.h>
|
||||||
#include <index/base.h>
|
#include <index/base.h>
|
||||||
#include <interfaces/chain.h>
|
#include <interfaces/chain.h>
|
||||||
|
#include <kernel/chain.h>
|
||||||
#include <node/blockstorage.h>
|
#include <node/blockstorage.h>
|
||||||
#include <node/context.h>
|
#include <node/context.h>
|
||||||
#include <node/interface_ui.h>
|
#include <node/interface_ui.h>
|
||||||
@ -180,12 +181,15 @@ void BaseIndex::ThreadSync()
|
|||||||
}
|
}
|
||||||
|
|
||||||
CBlock block;
|
CBlock block;
|
||||||
|
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
|
||||||
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
|
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
|
||||||
FatalError("%s: Failed to read block %s from disk",
|
FatalError("%s: Failed to read block %s from disk",
|
||||||
__func__, pindex->GetBlockHash().ToString());
|
__func__, pindex->GetBlockHash().ToString());
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
block_info.data = █
|
||||||
}
|
}
|
||||||
if (!WriteBlock(block, pindex)) {
|
if (!CustomAppend(block_info)) {
|
||||||
FatalError("%s: Failed to write block %s to index database",
|
FatalError("%s: Failed to write block %s to index database",
|
||||||
__func__, pindex->GetBlockHash().ToString());
|
__func__, pindex->GetBlockHash().ToString());
|
||||||
return;
|
return;
|
||||||
@ -273,8 +277,8 @@ void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get());
|
||||||
if (WriteBlock(*block, pindex)) {
|
if (CustomAppend(block_info)) {
|
||||||
SetBestBlockIndex(pindex);
|
SetBestBlockIndex(pindex);
|
||||||
} else {
|
} else {
|
||||||
FatalError("%s: Failed to write block %s to index",
|
FatalError("%s: Failed to write block %s to index",
|
||||||
|
@ -97,7 +97,7 @@ protected:
|
|||||||
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
|
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
|
||||||
|
|
||||||
/// Write update index entries for a newly connected block.
|
/// Write update index entries for a newly connected block.
|
||||||
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
|
[[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
|
||||||
|
|
||||||
/// Virtual method called internally by Commit that can be overridden to atomically
|
/// Virtual method called internally by Commit that can be overridden to atomically
|
||||||
/// commit more index state.
|
/// commit more index state.
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include <index/blockfilterindex.h>
|
#include <index/blockfilterindex.h>
|
||||||
#include <node/blockstorage.h>
|
#include <node/blockstorage.h>
|
||||||
#include <util/system.h>
|
#include <util/system.h>
|
||||||
|
#include <validation.h>
|
||||||
|
|
||||||
using node::UndoReadFromDisk;
|
using node::UndoReadFromDisk;
|
||||||
|
|
||||||
@ -214,22 +215,25 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
|
|||||||
return data_size;
|
return data_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
|
||||||
{
|
{
|
||||||
CBlockUndo block_undo;
|
CBlockUndo block_undo;
|
||||||
uint256 prev_header;
|
uint256 prev_header;
|
||||||
|
|
||||||
if (pindex->nHeight > 0) {
|
if (block.height > 0) {
|
||||||
|
// pindex variable gives indexing code access to node internals. It
|
||||||
|
// will be removed in upcoming commit
|
||||||
|
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
|
||||||
if (!UndoReadFromDisk(block_undo, pindex)) {
|
if (!UndoReadFromDisk(block_undo, pindex)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<uint256, DBVal> read_out;
|
std::pair<uint256, DBVal> read_out;
|
||||||
if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
|
if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 expected_block_hash = pindex->pprev->GetBlockHash();
|
uint256 expected_block_hash = *Assert(block.prev_hash);
|
||||||
if (read_out.first != expected_block_hash) {
|
if (read_out.first != expected_block_hash) {
|
||||||
return error("%s: previous block header belongs to unexpected block %s; expected %s",
|
return error("%s: previous block header belongs to unexpected block %s; expected %s",
|
||||||
__func__, read_out.first.ToString(), expected_block_hash.ToString());
|
__func__, read_out.first.ToString(), expected_block_hash.ToString());
|
||||||
@ -238,18 +242,18 @@ bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex
|
|||||||
prev_header = read_out.second.header;
|
prev_header = read_out.second.header;
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockFilter filter(m_filter_type, block, block_undo);
|
BlockFilter filter(m_filter_type, *Assert(block.data), block_undo);
|
||||||
|
|
||||||
size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
|
size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
|
||||||
if (bytes_written == 0) return false;
|
if (bytes_written == 0) return false;
|
||||||
|
|
||||||
std::pair<uint256, DBVal> value;
|
std::pair<uint256, DBVal> value;
|
||||||
value.first = pindex->GetBlockHash();
|
value.first = block.hash;
|
||||||
value.second.hash = filter.GetHash();
|
value.second.hash = filter.GetHash();
|
||||||
value.second.header = filter.ComputeHeader(prev_header);
|
value.second.header = filter.ComputeHeader(prev_header);
|
||||||
value.second.pos = m_next_filter_pos;
|
value.second.pos = m_next_filter_pos;
|
||||||
|
|
||||||
if (!m_db->Write(DBHeightKey(pindex->nHeight), value)) {
|
if (!m_db->Write(DBHeightKey(block.height), value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ protected:
|
|||||||
|
|
||||||
bool CommitInternal(CDBBatch& batch) override;
|
bool CommitInternal(CDBBatch& batch) override;
|
||||||
|
|
||||||
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
|
bool CustomAppend(const interfaces::BlockInfo& block) override;
|
||||||
|
|
||||||
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
|
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
|
||||||
|
|
||||||
|
@ -111,24 +111,27 @@ CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t
|
|||||||
m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
|
m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
|
||||||
{
|
{
|
||||||
CBlockUndo block_undo;
|
CBlockUndo block_undo;
|
||||||
const CAmount block_subsidy{GetBlockSubsidy(pindex->nHeight, Params().GetConsensus())};
|
const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
|
||||||
m_total_subsidy += block_subsidy;
|
m_total_subsidy += block_subsidy;
|
||||||
|
|
||||||
// Ignore genesis block
|
// Ignore genesis block
|
||||||
if (pindex->nHeight > 0) {
|
if (block.height > 0) {
|
||||||
|
// pindex variable gives indexing code access to node internals. It
|
||||||
|
// will be removed in upcoming commit
|
||||||
|
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
|
||||||
if (!UndoReadFromDisk(block_undo, pindex)) {
|
if (!UndoReadFromDisk(block_undo, pindex)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<uint256, DBVal> read_out;
|
std::pair<uint256, DBVal> read_out;
|
||||||
if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
|
if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint256 expected_block_hash{pindex->pprev->GetBlockHash()};
|
uint256 expected_block_hash{*Assert(block.prev_hash)};
|
||||||
if (read_out.first != expected_block_hash) {
|
if (read_out.first != expected_block_hash) {
|
||||||
LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
|
LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
|
||||||
read_out.first.ToString(), expected_block_hash.ToString());
|
read_out.first.ToString(), expected_block_hash.ToString());
|
||||||
@ -140,12 +143,13 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Deduplicate BIP30 related code
|
// TODO: Deduplicate BIP30 related code
|
||||||
bool is_bip30_block{(pindex->nHeight == 91722 && pindex->GetBlockHash() == uint256S("0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
|
bool is_bip30_block{(block.height == 91722 && block.hash == uint256S("0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
|
||||||
(pindex->nHeight == 91812 && pindex->GetBlockHash() == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
|
(block.height == 91812 && block.hash == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
|
||||||
|
|
||||||
// Add the new utxos created from the block
|
// Add the new utxos created from the block
|
||||||
for (size_t i = 0; i < block.vtx.size(); ++i) {
|
assert(block.data);
|
||||||
const auto& tx{block.vtx.at(i)};
|
for (size_t i = 0; i < block.data->vtx.size(); ++i) {
|
||||||
|
const auto& tx{block.data->vtx.at(i)};
|
||||||
|
|
||||||
// Skip duplicate txid coinbase transactions (BIP30).
|
// Skip duplicate txid coinbase transactions (BIP30).
|
||||||
if (is_bip30_block && tx->IsCoinBase()) {
|
if (is_bip30_block && tx->IsCoinBase()) {
|
||||||
@ -156,7 +160,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|||||||
|
|
||||||
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
|
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
|
||||||
const CTxOut& out{tx->vout[j]};
|
const CTxOut& out{tx->vout[j]};
|
||||||
Coin coin{out, pindex->nHeight, tx->IsCoinBase()};
|
Coin coin{out, block.height, tx->IsCoinBase()};
|
||||||
COutPoint outpoint{tx->GetHash(), j};
|
COutPoint outpoint{tx->GetHash(), j};
|
||||||
|
|
||||||
// Skip unspendable coins
|
// Skip unspendable coins
|
||||||
@ -212,7 +216,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|||||||
m_total_unspendables_unclaimed_rewards += unclaimed_rewards;
|
m_total_unspendables_unclaimed_rewards += unclaimed_rewards;
|
||||||
|
|
||||||
std::pair<uint256, DBVal> value;
|
std::pair<uint256, DBVal> value;
|
||||||
value.first = pindex->GetBlockHash();
|
value.first = block.hash;
|
||||||
value.second.transaction_output_count = m_transaction_output_count;
|
value.second.transaction_output_count = m_transaction_output_count;
|
||||||
value.second.bogo_size = m_bogo_size;
|
value.second.bogo_size = m_bogo_size;
|
||||||
value.second.total_amount = m_total_amount;
|
value.second.total_amount = m_total_amount;
|
||||||
@ -232,7 +236,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|||||||
|
|
||||||
// Intentionally do not update DB_MUHASH here so it stays in sync with
|
// Intentionally do not update DB_MUHASH here so it stays in sync with
|
||||||
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
|
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
|
||||||
return m_db->Write(DBHeightKey(pindex->nHeight), value);
|
return m_db->Write(DBHeightKey(block.height), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
|
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
|
||||||
|
@ -43,7 +43,7 @@ protected:
|
|||||||
|
|
||||||
bool CommitInternal(CDBBatch& batch) override;
|
bool CommitInternal(CDBBatch& batch) override;
|
||||||
|
|
||||||
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
|
bool CustomAppend(const interfaces::BlockInfo& block) override;
|
||||||
|
|
||||||
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
|
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
|
||||||
|
|
||||||
|
@ -54,17 +54,16 @@ TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size,
|
|||||||
|
|
||||||
TxIndex::~TxIndex() = default;
|
TxIndex::~TxIndex() = default;
|
||||||
|
|
||||||
bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
|
||||||
{
|
{
|
||||||
// Exclude genesis block transaction because outputs are not spendable.
|
// Exclude genesis block transaction because outputs are not spendable.
|
||||||
if (pindex->nHeight == 0) return true;
|
if (block.height == 0) return true;
|
||||||
|
|
||||||
CDiskTxPos pos{
|
assert(block.data);
|
||||||
WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
|
CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
|
||||||
GetSizeOfCompactSize(block.vtx.size())};
|
|
||||||
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
||||||
vPos.reserve(block.vtx.size());
|
vPos.reserve(block.data->vtx.size());
|
||||||
for (const auto& tx : block.vtx) {
|
for (const auto& tx : block.data->vtx) {
|
||||||
vPos.emplace_back(tx->GetHash(), pos);
|
vPos.emplace_back(tx->GetHash(), pos);
|
||||||
pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION);
|
pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION);
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ private:
|
|||||||
bool AllowPrune() const override { return false; }
|
bool AllowPrune() const override { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
|
bool CustomAppend(const interfaces::BlockInfo& block) override;
|
||||||
|
|
||||||
BaseIndex::DB& GetDB() const override;
|
BaseIndex::DB& GetDB() const override;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user