Skip to content

Commit 5d235d5

Browse files
l0rincTheCharlatanhodlinator
committed
net: assert block hash in ProcessGetBlockData and ProcessMessage
The non-recent-block code path in `ProcessGetBlockData` already has `inv.hash` available (equaling `pindex->GetBlockHash()`). Pass it to `ReadBlock()` and assert that the on-disk header matches the requested hash. The `GETBLOCKTXN` message handler in `ProcessMessage` receives `req.blockhash` from the peer (equaling `pindex->GetBlockHash()`). Pass this hash to `ReadBlock()` for verification and assert that the index lookup matches. Co-authored-by: TheCharlatan <[email protected]> Co-authored-by: Hodlinator <[email protected]>
1 parent d91c718 commit 5d235d5

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/net_processing.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
22982298
}
22992299

23002300
std::shared_ptr<const CBlock> pblock;
2301-
if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
2301+
if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
23022302
pblock = a_recent_block;
23032303
} else if (inv.IsMsgWitnessBlk()) {
23042304
// Fast-path: in this case it is possible to serve the block directly from disk,
@@ -2318,7 +2318,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23182318
} else {
23192319
// Send block from disk
23202320
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
2321-
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos)) {
2321+
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
23222322
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
23232323
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
23242324
} else {
@@ -2364,7 +2364,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
23642364
// and we don't feel like constructing the object for them, so
23652365
// instead we respond with the full, non-compact block.
23662366
if (can_direct_fetch && pindex->nHeight >= tip->nHeight - MAX_CMPCTBLOCK_DEPTH) {
2367-
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
2367+
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == inv.hash) {
23682368
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
23692369
} else {
23702370
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, m_rng.rand64()};
@@ -4173,7 +4173,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
41734173

41744174
if (!block_pos.IsNull()) {
41754175
CBlock block;
4176-
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos)};
4176+
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos, req.blockhash)};
41774177
// If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
41784178
// pruned after we release cs_main above, so this read should never fail.
41794179
assert(ret);

0 commit comments

Comments
 (0)