Skip to content

Commit 1cfc887

Browse files
committed
Remove CChain dependency in node/blockstorage
1 parent fe86a7c commit 1cfc887

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
618618
return BlockFileSeq().FileName(pos);
619619
}
620620

621-
bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown)
621+
bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown)
622622
{
623623
LOCK(cs_LastBlockFile);
624624

@@ -841,7 +841,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
841841
return true;
842842
}
843843

844-
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const FlatFilePos* dbp)
844+
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
845845
{
846846
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
847847
FlatFilePos blockPos;
@@ -854,7 +854,7 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
854854
// we add BLOCK_SERIALIZATION_HEADER_SIZE only for new blocks since they will have the serialization header added when written to disk.
855855
nBlockSize += static_cast<unsigned int>(BLOCK_SERIALIZATION_HEADER_SIZE);
856856
}
857-
if (!FindBlockPos(blockPos, nBlockSize, nHeight, active_chain, block.GetBlockTime(), position_known)) {
857+
if (!FindBlockPos(blockPos, nBlockSize, nHeight, block.GetBlockTime(), position_known)) {
858858
error("%s: FindBlockPos failed", __func__);
859859
return FlatFilePos();
860860
}

src/node/blockstorage.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class BlockValidationState;
2424
class CBlock;
2525
class CBlockFileInfo;
2626
class CBlockUndo;
27-
class CChain;
2827
class CChainParams;
2928
class Chainstate;
3029
class ChainstateManager;
@@ -94,7 +93,7 @@ class BlockManager
9493
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
9594
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
9695
void FlushUndoFile(int block_file, bool finalize = false);
97-
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown);
96+
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown);
9897
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
9998

10099
FlatFileSeq BlockFileSeq() const;
@@ -215,7 +214,7 @@ class BlockManager
215214
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
216215

217216
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
218-
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const FlatFilePos* dbp);
217+
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp);
219218

220219
/** Whether running in -prune mode. */
221220
[[nodiscard]] bool IsPruneMode() const { return m_prune_mode; }

src/test/blockmanager_tests.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,21 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
3030
.notifications = notifications,
3131
};
3232
BlockManager blockman{m_node.kernel->interrupt, blockman_opts};
33-
CChain chain {};
3433
// simulate adding a genesis block normally
35-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
34+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
3635
// simulate what happens during reindex
3736
// simulate a well-formed genesis block being found at offset 8 in the blk00000.dat file
3837
// the block is found at offset 8 because there is an 8 byte serialization header
3938
// consisting of 4 magic bytes + 4 length bytes before each block in a well-formed blk file.
4039
FlatFilePos pos{0, BLOCK_SERIALIZATION_HEADER_SIZE};
41-
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
40+
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, &pos).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);
4241
// now simulate what happens after reindex for the first new block processed
4342
// the actual block contents don't matter, just that it's a block.
4443
// verify that the write position is at offset 0x12d.
4544
// this is a check to make sure that https://github.com/bitcoin/bitcoin/issues/21379 does not recur
4645
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
4746
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
48-
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, chain, nullptr)};
47+
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, nullptr)};
4948
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE);
5049
}
5150

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3973,7 +3973,7 @@ bool Chainstate::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, BlockV
39733973
// Write block to history file
39743974
if (fNewBlock) *fNewBlock = true;
39753975
try {
3976-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, m_chain, dbp)};
3976+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, pindex->nHeight, dbp)};
39773977
if (blockPos.IsNull()) {
39783978
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
39793979
return false;
@@ -4496,7 +4496,7 @@ bool Chainstate::LoadGenesisBlock()
44964496

44974497
try {
44984498
const CBlock& block = params.GenesisBlock();
4499-
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, m_chain, nullptr)};
4499+
FlatFilePos blockPos{m_blockman.SaveBlockToDisk(block, 0, nullptr)};
45004500
if (blockPos.IsNull()) {
45014501
return error("%s: writing genesis block to disk failed", __func__);
45024502
}

0 commit comments

Comments
 (0)