Skip to content

Commit

Permalink
merge bitcoin#25349: CBlockIndex/CDiskBlockIndex improvements for saf…
Browse files Browse the repository at this point in the history
…ety, consistent behavior
  • Loading branch information
kwvg committed Aug 2, 2024
1 parent 39fea85 commit 16d0732
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 20 deletions.
14 changes: 1 addition & 13 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@

#include <tinyformat.h>

std::string CDiskBlockIndex::ToString() const
{
std::string str = "CDiskBlockIndex(";
str += CBlockIndex::ToString();
str += strprintf("\n hashBlock=%s, hashPrev=%s)",
GetBlockHash().ToString(),
hashPrev.ToString());
return str;
}

std::string CBlockIndex::ToString() const
{
return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
pprev, nHeight,
hashMerkleRoot.ToString(),
GetBlockHash().ToString());
pprev, nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class CBlockIndex

uint256 GetBlockHash() const
{
assert(phashBlock != nullptr);
return *phashBlock;
}

Expand Down Expand Up @@ -393,7 +394,7 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(obj.nNonce);
}

uint256 GetBlockHash() const
uint256 ConstructBlockHash() const
{
if(hash != uint256()) return hash;
// should never really get here, keeping this as a fallback
Expand All @@ -407,8 +408,8 @@ class CDiskBlockIndex : public CBlockIndex
return block.GetHash();
}

std::string ToString() const;

uint256 GetBlockHash() = delete;
std::string ToString() = delete;
};

/** An in-memory indexed chain of blocks. */
Expand Down
3 changes: 1 addition & 2 deletions src/test/fuzz/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ FUZZ_TARGET(chain)
disk_block_index->phashBlock = &zero;
{
LOCK(::cs_main);
(void)disk_block_index->GetBlockHash();
(void)disk_block_index->ConstructBlockHash();
(void)disk_block_index->GetBlockPos();
(void)disk_block_index->GetBlockTime();
(void)disk_block_index->GetBlockTimeMax();
(void)disk_block_index->GetMedianTimePast();
(void)disk_block_index->GetUndoPos();
(void)disk_block_index->HaveTxsDownloaded();
(void)disk_block_index->IsValid();
(void)disk_block_index->ToString();
}

const CBlockHeader block_header = disk_block_index->GetBlockHeader();
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
CDiskBlockIndex diskindex;
if (pcursor->GetValue(diskindex)) {
// Construct block index object
CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
CBlockIndex* pindexNew = insertBlockIndex(diskindex.ConstructBlockHash());
pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
pindexNew->nHeight = diskindex.nHeight;
pindexNew->nFile = diskindex.nFile;
Expand Down
1 change: 0 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,6 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
int64_t nTime7 = GetTimeMicros(); nTimeIndexWrite += nTime7 - nTime6;
LogPrint(BCLog::BENCHMARK, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime7 - nTime6), nTimeIndexWrite * MICRO, nTimeIndexWrite * MILLI / nBlocksTotal);

assert(pindex->phashBlock);
// add this block to the view's block chain
view.SetBestBlock(pindex->GetBlockHash());
m_evoDb.WriteBestBlock(pindex->GetBlockHash());
Expand Down

0 comments on commit 16d0732

Please sign in to comment.