Skip to content

Commit dc971be

Browse files
committed
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.
1 parent bef4e40 commit dc971be

File tree

9 files changed

+45
-33
lines changed

9 files changed

+45
-33
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ libbitcoinkernel_la_SOURCES = \
884884
flatfile.cpp \
885885
fs.cpp \
886886
hash.cpp \
887+
kernel/chain.cpp \
887888
kernel/checks.cpp \
888889
kernel/coinstats.cpp \
889890
kernel/context.cpp \

src/index/base.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chainparams.h>
66
#include <index/base.h>
77
#include <interfaces/chain.h>
8+
#include <kernel/chain.h>
89
#include <node/blockstorage.h>
910
#include <node/context.h>
1011
#include <node/interface_ui.h>
@@ -180,12 +181,15 @@ void BaseIndex::ThreadSync()
180181
}
181182

182183
CBlock block;
184+
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex);
183185
if (!ReadBlockFromDisk(block, pindex, consensus_params)) {
184186
FatalError("%s: Failed to read block %s from disk",
185187
__func__, pindex->GetBlockHash().ToString());
186188
return;
189+
} else {
190+
block_info.data = &block;
187191
}
188-
if (!WriteBlock(block, pindex)) {
192+
if (!CustomAppend(block_info)) {
189193
FatalError("%s: Failed to write block %s to index database",
190194
__func__, pindex->GetBlockHash().ToString());
191195
return;
@@ -273,8 +277,8 @@ void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const
273277
return;
274278
}
275279
}
276-
277-
if (WriteBlock(*block, pindex)) {
280+
interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get());
281+
if (CustomAppend(block_info)) {
278282
SetBestBlockIndex(pindex);
279283
} else {
280284
FatalError("%s: Failed to write block %s to index",

src/index/base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class BaseIndex : public CValidationInterface
9797
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
9898

9999
/// Write update index entries for a newly connected block.
100-
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
100+
[[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
101101

102102
/// Virtual method called internally by Commit that can be overridden to atomically
103103
/// commit more index state.

src/index/blockfilterindex.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <index/blockfilterindex.h>
1010
#include <node/blockstorage.h>
1111
#include <util/system.h>
12+
#include <validation.h>
1213

1314
using node::UndoReadFromDisk;
1415

@@ -214,22 +215,25 @@ size_t BlockFilterIndex::WriteFilterToDisk(FlatFilePos& pos, const BlockFilter&
214215
return data_size;
215216
}
216217

217-
bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
218+
bool BlockFilterIndex::CustomAppend(const interfaces::BlockInfo& block)
218219
{
219220
CBlockUndo block_undo;
220221
uint256 prev_header;
221222

222-
if (pindex->nHeight > 0) {
223+
if (block.height > 0) {
224+
// pindex variable gives indexing code access to node internals. It
225+
// will be removed in upcoming commit
226+
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
223227
if (!UndoReadFromDisk(block_undo, pindex)) {
224228
return false;
225229
}
226230

227231
std::pair<uint256, DBVal> read_out;
228-
if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
232+
if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
229233
return false;
230234
}
231235

232-
uint256 expected_block_hash = pindex->pprev->GetBlockHash();
236+
uint256 expected_block_hash = *Assert(block.prev_hash);
233237
if (read_out.first != expected_block_hash) {
234238
return error("%s: previous block header belongs to unexpected block %s; expected %s",
235239
__func__, read_out.first.ToString(), expected_block_hash.ToString());
@@ -238,18 +242,18 @@ bool BlockFilterIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex
238242
prev_header = read_out.second.header;
239243
}
240244

241-
BlockFilter filter(m_filter_type, block, block_undo);
245+
BlockFilter filter(m_filter_type, *Assert(block.data), block_undo);
242246

243247
size_t bytes_written = WriteFilterToDisk(m_next_filter_pos, filter);
244248
if (bytes_written == 0) return false;
245249

246250
std::pair<uint256, DBVal> value;
247-
value.first = pindex->GetBlockHash();
251+
value.first = block.hash;
248252
value.second.hash = filter.GetHash();
249253
value.second.header = filter.ComputeHeader(prev_header);
250254
value.second.pos = m_next_filter_pos;
251255

252-
if (!m_db->Write(DBHeightKey(pindex->nHeight), value)) {
256+
if (!m_db->Write(DBHeightKey(block.height), value)) {
253257
return false;
254258
}
255259

src/index/blockfilterindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BlockFilterIndex final : public BaseIndex
4545

4646
bool CommitInternal(CDBBatch& batch) override;
4747

48-
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
48+
bool CustomAppend(const interfaces::BlockInfo& block) override;
4949

5050
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
5151

src/index/coinstatsindex.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,27 @@ CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t
111111
m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
112112
}
113113

114-
bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
114+
bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
115115
{
116116
CBlockUndo block_undo;
117-
const CAmount block_subsidy{GetBlockSubsidy(pindex->nHeight, Params().GetConsensus())};
117+
const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
118118
m_total_subsidy += block_subsidy;
119119

120120
// Ignore genesis block
121-
if (pindex->nHeight > 0) {
121+
if (block.height > 0) {
122+
// pindex variable gives indexing code access to node internals. It
123+
// will be removed in upcoming commit
124+
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
122125
if (!UndoReadFromDisk(block_undo, pindex)) {
123126
return false;
124127
}
125128

126129
std::pair<uint256, DBVal> read_out;
127-
if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
130+
if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
128131
return false;
129132
}
130133

131-
uint256 expected_block_hash{pindex->pprev->GetBlockHash()};
134+
uint256 expected_block_hash{*Assert(block.prev_hash)};
132135
if (read_out.first != expected_block_hash) {
133136
LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
134137
read_out.first.ToString(), expected_block_hash.ToString());
@@ -140,12 +143,13 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
140143
}
141144

142145
// TODO: Deduplicate BIP30 related code
143-
bool is_bip30_block{(pindex->nHeight == 91722 && pindex->GetBlockHash() == uint256S("0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
144-
(pindex->nHeight == 91812 && pindex->GetBlockHash() == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
146+
bool is_bip30_block{(block.height == 91722 && block.hash == uint256S("0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
147+
(block.height == 91812 && block.hash == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
145148

146149
// Add the new utxos created from the block
147-
for (size_t i = 0; i < block.vtx.size(); ++i) {
148-
const auto& tx{block.vtx.at(i)};
150+
assert(block.data);
151+
for (size_t i = 0; i < block.data->vtx.size(); ++i) {
152+
const auto& tx{block.data->vtx.at(i)};
149153

150154
// Skip duplicate txid coinbase transactions (BIP30).
151155
if (is_bip30_block && tx->IsCoinBase()) {
@@ -156,7 +160,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
156160

157161
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
158162
const CTxOut& out{tx->vout[j]};
159-
Coin coin{out, pindex->nHeight, tx->IsCoinBase()};
163+
Coin coin{out, block.height, tx->IsCoinBase()};
160164
COutPoint outpoint{tx->GetHash(), j};
161165

162166
// Skip unspendable coins
@@ -212,7 +216,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
212216
m_total_unspendables_unclaimed_rewards += unclaimed_rewards;
213217

214218
std::pair<uint256, DBVal> value;
215-
value.first = pindex->GetBlockHash();
219+
value.first = block.hash;
216220
value.second.transaction_output_count = m_transaction_output_count;
217221
value.second.bogo_size = m_bogo_size;
218222
value.second.total_amount = m_total_amount;
@@ -232,7 +236,7 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
232236

233237
// Intentionally do not update DB_MUHASH here so it stays in sync with
234238
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
235-
return m_db->Write(DBHeightKey(pindex->nHeight), value);
239+
return m_db->Write(DBHeightKey(block.height), value);
236240
}
237241

238242
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,

src/index/coinstatsindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CoinStatsIndex final : public BaseIndex
4343

4444
bool CommitInternal(CDBBatch& batch) override;
4545

46-
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
46+
bool CustomAppend(const interfaces::BlockInfo& block) override;
4747

4848
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
4949

src/index/txindex.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size,
5454

5555
TxIndex::~TxIndex() = default;
5656

57-
bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
57+
bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
5858
{
5959
// Exclude genesis block transaction because outputs are not spendable.
60-
if (pindex->nHeight == 0) return true;
60+
if (block.height == 0) return true;
6161

62-
CDiskTxPos pos{
63-
WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
64-
GetSizeOfCompactSize(block.vtx.size())};
62+
assert(block.data);
63+
CDiskTxPos pos({block.file_number, block.data_pos}, GetSizeOfCompactSize(block.data->vtx.size()));
6564
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
66-
vPos.reserve(block.vtx.size());
67-
for (const auto& tx : block.vtx) {
65+
vPos.reserve(block.data->vtx.size());
66+
for (const auto& tx : block.data->vtx) {
6867
vPos.emplace_back(tx->GetHash(), pos);
6968
pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION);
7069
}

src/index/txindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class TxIndex final : public BaseIndex
2323
bool AllowPrune() const override { return false; }
2424

2525
protected:
26-
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
26+
bool CustomAppend(const interfaces::BlockInfo& block) override;
2727

2828
BaseIndex::DB& GetDB() const override;
2929

0 commit comments

Comments
 (0)