Skip to content

Commit 0ba44d9

Browse files
committed
Merge bitcoin/bitcoin#33296: net: check for empty header before calling FillBlock
8b62647 test: send duplicate blocktxn message in p2p_compactblocks.py (Eugene Siegel) 5e585a0 net: check for empty header before calling FillBlock (Eugene Siegel) Pull request description: This avoids an Assume crash if multiple blocktxn messages are received. The first call to `FillBlock` would make the header empty via `SetNull` and the call right before the second `FillBlock` would crash [here](https://github.com/bitcoin/bitcoin/blob/689a32197638e92995dd8eb071425715f5fdc3a4/src/net_processing.cpp#L3333) since `LookupBlockIndex` won't find anything. Fix that by checking for an empty header before the Assume. ACKs for top commit: instagibbs: reACK bitcoin/bitcoin@8b62647 fjahr: tACK 8b62647 achow101: ACK 8b62647 mzumsande: Code Review ACK 8b62647 Tree-SHA512: d43a6f652161d4f7e6137f207a3e95259fc51509279d20347b1698c91179c39c8fcb75d2668b13a6b220f478a03578573208a415804be1d8843acb057fa1a73a
2 parents 1861030 + 8b62647 commit 0ba44d9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/net_processing.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3329,6 +3329,16 @@ void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const Bl
33293329

33303330
PartiallyDownloadedBlock& partialBlock = *range_flight.first->second.second->partialBlock;
33313331

3332+
if (partialBlock.header.IsNull()) {
3333+
// It is possible for the header to be empty if a previous call to FillBlock wiped the header, but left
3334+
// the PartiallyDownloadedBlock pointer around (i.e. did not call RemoveBlockRequest). In this case, we
3335+
// should not call LookupBlockIndex below.
3336+
RemoveBlockRequest(block_transactions.blockhash, pfrom.GetId());
3337+
Misbehaving(peer, "previous compact block reconstruction attempt failed");
3338+
LogDebug(BCLog::NET, "Peer %d sent compact block transactions multiple times", pfrom.GetId());
3339+
return;
3340+
}
3341+
33323342
// We should not have gotten this far in compact block processing unless it's attached to a known header
33333343
const CBlockIndex* prev_block{Assume(m_chainman.m_blockman.LookupBlockIndex(partialBlock.header.hashPrevBlock))};
33343344
ReadStatus status = partialBlock.FillBlock(*pblock, block_transactions.txn,
@@ -3340,6 +3350,9 @@ void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const Bl
33403350
} else if (status == READ_STATUS_FAILED) {
33413351
if (first_in_flight) {
33423352
// Might have collided, fall back to getdata now :(
3353+
// We keep the failed partialBlock to disallow processing another compact block announcement from the same
3354+
// peer for the same block. We let the full block download below continue under the same m_downloading_since
3355+
// timer.
33433356
std::vector<CInv> invs;
33443357
invs.emplace_back(MSG_BLOCK | GetFetchFlags(peer), block_transactions.blockhash);
33453358
MakeAndPushMessage(pfrom, NetMsgType::GETDATA, invs);

test/functional/p2p_compactblocks.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,42 @@ def test_incorrect_blocktxn_response(self, test_node):
558558
test_node.send_and_ping(msg_block(block))
559559
assert_equal(node.getbestblockhash(), block.hash_hex)
560560

561+
# Multiple blocktxn responses will cause a node to get disconnected.
562+
def test_multiple_blocktxn_response(self, test_node):
563+
node = self.nodes[0]
564+
utxo = self.utxos[0]
565+
566+
block = self.build_block_with_transactions(node, utxo, 2)
567+
568+
# Send compact block
569+
comp_block = HeaderAndShortIDs()
570+
comp_block.initialize_from_block(block, prefill_list=[0], use_witness=True)
571+
test_node.send_and_ping(msg_cmpctblock(comp_block.to_p2p()))
572+
absolute_indexes = []
573+
with p2p_lock:
574+
assert "getblocktxn" in test_node.last_message
575+
absolute_indexes = test_node.last_message["getblocktxn"].block_txn_request.to_absolute()
576+
assert_equal(absolute_indexes, [1, 2])
577+
578+
# Send a blocktxn that does not succeed in reconstruction, triggering
579+
# getdata fallback.
580+
msg = msg_blocktxn()
581+
msg.block_transactions = BlockTransactions(block.hash_int, [block.vtx[2]] + [block.vtx[1]])
582+
test_node.send_and_ping(msg)
583+
584+
# Tip should not have updated
585+
assert_equal(int(node.getbestblockhash(), 16), block.hashPrevBlock)
586+
587+
# We should receive a getdata request
588+
test_node.wait_for_getdata([block.hash_int], timeout=10)
589+
assert test_node.last_message["getdata"].inv[0].type == MSG_BLOCK or \
590+
test_node.last_message["getdata"].inv[0].type == MSG_BLOCK | MSG_WITNESS_FLAG
591+
592+
# Send the same blocktxn and assert the sender gets disconnected.
593+
with node.assert_debug_log(['previous compact block reconstruction attempt failed']):
594+
test_node.send_without_ping(msg)
595+
test_node.wait_for_disconnect()
596+
561597
def test_getblocktxn_handler(self, test_node):
562598
node = self.nodes[0]
563599
# bitcoind will not send blocktxn responses for blocks whose height is
@@ -977,6 +1013,12 @@ def run_test(self):
9771013
# The previous test will lead to a disconnection. Reconnect before continuing.
9781014
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())
9791015

1016+
self.log.info("Testing handling of multiple blocktxn responses...")
1017+
self.test_multiple_blocktxn_response(self.segwit_node)
1018+
1019+
# The previous test will lead to a disconnection. Reconnect before continuing.
1020+
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn())
1021+
9801022
self.log.info("Testing invalid index in cmpctblock message...")
9811023
self.test_invalid_cmpctblock_message()
9821024

0 commit comments

Comments
 (0)