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 <seb.kung@gmail.com> Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com>
This commit is contained in:
1 parent
d91c718a68
commit
5d235d50d6
1 file changed
+4
-4
@@ -2298,7 +2298,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<const CBlock> pblock;
|
std::shared_ptr<const CBlock> pblock;
|
||||||
if (a_recent_block && a_recent_block->GetHash() == pindex->GetBlockHash()) {
|
if (a_recent_block && a_recent_block->GetHash() == inv.hash) {
|
||||||
pblock = a_recent_block;
|
pblock = a_recent_block;
|
||||||
} else if (inv.IsMsgWitnessBlk()) {
|
} else if (inv.IsMsgWitnessBlk()) {
|
||||||
// Fast-path: in this case it is possible to serve the block directly from disk,
|
// 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&
|
|||||||
} else {
|
} else {
|
||||||
// Send block from disk
|
// Send block from disk
|
||||||
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
|
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
|
||||||
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos)) {
|
if (!m_chainman.m_blockman.ReadBlock(*pblockRead, block_pos, inv.hash)) {
|
||||||
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
|
if (WITH_LOCK(m_chainman.GetMutex(), return m_chainman.m_blockman.IsBlockPruned(*pindex))) {
|
||||||
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
|
LogDebug(BCLog::NET, "Block was pruned before it could be read, %s\n", pfrom.DisconnectMsg(fLogIPs));
|
||||||
} else {
|
} else {
|
||||||
@@ -2364,7 +2364,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
|
|||||||
// and we don't feel like constructing the object for them, so
|
// and we don't feel like constructing the object for them, so
|
||||||
// instead we respond with the full, non-compact block.
|
// instead we respond with the full, non-compact block.
|
||||||
if (can_direct_fetch && pindex->nHeight >= tip->nHeight - MAX_CMPCTBLOCK_DEPTH) {
|
if (can_direct_fetch && pindex->nHeight >= tip->nHeight - MAX_CMPCTBLOCK_DEPTH) {
|
||||||
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == pindex->GetBlockHash()) {
|
if (a_recent_compact_block && a_recent_compact_block->header.GetHash() == inv.hash) {
|
||||||
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
|
MakeAndPushMessage(pfrom, NetMsgType::CMPCTBLOCK, *a_recent_compact_block);
|
||||||
} else {
|
} else {
|
||||||
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, m_rng.rand64()};
|
CBlockHeaderAndShortTxIDs cmpctblock{*pblock, m_rng.rand64()};
|
||||||
@@ -4173,7 +4173,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||||||
|
|
||||||
if (!block_pos.IsNull()) {
|
if (!block_pos.IsNull()) {
|
||||||
CBlock block;
|
CBlock block;
|
||||||
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos)};
|
const bool ret{m_chainman.m_blockman.ReadBlock(block, block_pos, req.blockhash)};
|
||||||
// If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
|
// If height is above MAX_BLOCKTXN_DEPTH then this block cannot get
|
||||||
// pruned after we release cs_main above, so this read should never fail.
|
// pruned after we release cs_main above, so this read should never fail.
|
||||||
assert(ret);
|
assert(ret);
|
||||||
|
|||||||
Reference in new issue
Block a user