Skip to content

Commit fee7334

Browse files
committed
validation: Pass in chain to FindBlockPos+SaveBlockToDisk
1 parent a9d28bc commit fee7334

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/validation.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
32263226
}
32273227
}
32283228

3229-
static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
3229+
static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false)
32303230
{
32313231
LOCK(cs_LastBlockFile);
32323232

@@ -3241,7 +3241,8 @@ static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int n
32413241
// when the undo file is keeping up with the block file, we want to flush it explicitly
32423242
// when it is lagging behind (more blocks arrive than are being connected), we let the
32433243
// undo block write case handle it
3244-
finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)ChainActive().Tip()->nHeight);
3244+
assert(std::addressof(::ChainActive()) == std::addressof(active_chain));
3245+
finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)active_chain.Tip()->nHeight);
32453246
nFile++;
32463247
if (vinfoBlockFile.size() <= nFile) {
32473248
vinfoBlockFile.resize(nFile + 1);
@@ -3703,12 +3704,12 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
37033704
}
37043705

37053706
/** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
3706-
static FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const CChainParams& chainparams, const FlatFilePos* dbp) {
3707+
static FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp) {
37073708
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
37083709
FlatFilePos blockPos;
37093710
if (dbp != nullptr)
37103711
blockPos = *dbp;
3711-
if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr)) {
3712+
if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) {
37123713
error("%s: FindBlockPos failed", __func__);
37133714
return FlatFilePos();
37143715
}
@@ -3787,7 +3788,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
37873788
// Write block to history file
37883789
if (fNewBlock) *fNewBlock = true;
37893790
try {
3790-
FlatFilePos blockPos = SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp);
3791+
FlatFilePos blockPos = SaveBlockToDisk(block, pindex->nHeight, ::ChainActive(), chainparams, dbp);
37913792
if (blockPos.IsNull()) {
37923793
state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
37933794
return false;
@@ -4629,7 +4630,7 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
46294630

46304631
try {
46314632
const CBlock& block = chainparams.GenesisBlock();
4632-
FlatFilePos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr);
4633+
FlatFilePos blockPos = SaveBlockToDisk(block, 0, ::ChainActive(), chainparams, nullptr);
46334634
if (blockPos.IsNull())
46344635
return error("%s: writing genesis block to disk failed", __func__);
46354636
CBlockIndex *pindex = m_blockman.AddToBlockIndex(block);

0 commit comments

Comments
 (0)