refactor: Remove useless CBlock::GetBlockHeader
What changed, and why it matters
This commit is a simple code cleanup. It removes a helper function called GetBlockHeader that copied a few fields from a full block into a block header object. Because a full block already is a block header in this codebase, the function was unnecessary. All call sites now use the block directly instead of making a copy. There is no security-relevant change.
No security action required. Treat as ordinary refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit removes CBlock::GetBlockHeader(), which manually copied nVersion, hashPrevBlock, hashMerkleRoot, nTime, nBits, and nNonce into a new CBlockHeader. Since CBlock publicly inherits from CBlockHeader, the conversion can be expressed as a cast/reference. Callers are updated to pass the CBlock itself where a CBlockHeader is expected (e.g., static_cast
Changed components
src/primitives/block.hsrc/merkleblock.cppsrc/net_processing.cppsrc/test/blockfilter_index_tests.cppsrc/test/fuzz/block_header.cppsrc/test/fuzz/utxo_snapshot.cppsrc/test/validation_block_tests.cppInspect captured patch +8 / −21
diff --git a/src/merkleblock.cpp b/src/merkleblock.cpp
index 34ff06e5..f5aaddba 100644
--- a/src/merkleblock.cpp
+++ b/src/merkleblock.cpp
@@ -29,7 +29,7 @@ std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<Txid>* txids)
{
- header = block.GetBlockHeader();
+ header = static_cast<const CBlockHeader&>(block);
std::vector<bool> vMatch;
std::vector<Txid> vHashes;
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 5fd11d3c..3a712f2f 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -4666,7 +4666,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
mapBlockSource.emplace(hash, std::make_pair(pfrom.GetId(), true));
// Check claimed work on this block against our anti-dos thresholds.
- if (prev_block && prev_block->nChainWork + GetBlockProof(pblock->GetBlockHeader()) >= GetAntiDoSWorkThreshold()) {
+ if (prev_block && prev_block->nChainWork + GetBlockProof(*pblock) >= GetAntiDoSWorkThreshold()) {
min_pow_checked = true;
}
}
diff --git a/src/primitives/block.h b/src/primitives/block.h
index 207d2b29..afaeba0d 100644
--- a/src/primitives/block.h
+++ b/src/primitives/block.h
@@ -101,18 +101,6 @@ public:
m_checked_merkle_root = false;
}
- CBlockHeader GetBlockHeader() const
- {
- CBlockHeader block;
- block.nVersion = nVersion;
- block.hashPrevBlock = hashPrevBlock;
- block.hashMerkleRoot = hashMerkleRoot;
- block.nTime = nTime;
- block.nBits = nBits;
- block.nNonce = nNonce;
- return block;
- }
-
std::string ToString() const;
};
diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp
index 224acb8b..4a3f4d42 100644
--- a/src/test/blockfilter_index_tests.cpp
+++ b/src/test/blockfilter_index_tests.cpp
@@ -102,10 +102,9 @@ bool BuildChainTestingSetup::BuildChain(const CBlockIndex* pindex,
chain.resize(length);
for (auto& block : chain) {
block = std::make_shared<CBlock>(CreateBlock(pindex, no_txns, coinbase_script_pub_key));
- CBlockHeader header = block->GetBlockHeader();
BlockValidationState state;
- if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{header}}, true, state, &pindex)) {
+ if (!Assert(m_node.chainman)->ProcessNewBlockHeaders({{*block}}, true, state, &pindex)) {
return false;
}
}
diff --git a/src/test/fuzz/block_header.cpp b/src/test/fuzz/block_header.cpp
index 2e446b16..12a9e1a2 100644
--- a/src/test/fuzz/block_header.cpp
+++ b/src/test/fuzz/block_header.cpp
@@ -33,10 +33,10 @@ FUZZ_TARGET(block_header)
mut_block_header.SetNull();
assert(mut_block_header.IsNull());
CBlock block{*block_header};
- assert(block.GetBlockHeader().GetHash() == block_header->GetHash());
+ assert(block.GetHash() == block_header->GetHash());
(void)block.ToString();
block.SetNull();
- assert(block.GetBlockHeader().GetHash() == mut_block_header.GetHash());
+ assert(block.GetHash() == mut_block_header.GetHash());
}
{
std::optional<CBlockLocator> block_locator = ConsumeDeserializable<CBlockLocator>(fuzzed_data_provider);
diff --git a/src/test/fuzz/utxo_snapshot.cpp b/src/test/fuzz/utxo_snapshot.cpp
index 56d36155..35faa73a 100644
--- a/src/test/fuzz/utxo_snapshot.cpp
+++ b/src/test/fuzz/utxo_snapshot.cpp
@@ -89,7 +89,7 @@ void initialize_chain()
auto& chainman{*setup->m_node.chainman};
for (const auto& block : chain) {
BlockValidationState dummy;
- bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, true, dummy)};
+ bool processed{chainman.ProcessNewBlockHeaders({{*block}}, true, dummy)};
Assert(processed);
const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
Assert(index);
@@ -171,7 +171,7 @@ void utxo_snapshot_fuzz(FuzzBufferType buffer)
if constexpr (!INVALID) {
for (const auto& block : *g_chain) {
BlockValidationState dummy;
- bool processed{chainman.ProcessNewBlockHeaders({{block->GetBlockHeader()}}, true, dummy)};
+ bool processed{chainman.ProcessNewBlockHeaders({{*block}}, true, dummy)};
Assert(processed);
const auto* index{WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block->GetHash()))};
Assert(index);
diff --git a/src/test/validation_block_tests.cpp b/src/test/validation_block_tests.cpp
index a0b23f5d..6497a243 100644
--- a/src/test/validation_block_tests.cpp
+++ b/src/test/validation_block_tests.cpp
@@ -104,7 +104,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock>
// submit block header, so that miner can get the block height from the
// global state and the node has the topology of the chain
BlockValidationState ignored;
- BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({{pblock->GetBlockHeader()}}, true, ignored));
+ BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({{*pblock}}, true, ignored));
return pblock;
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.