@@ -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 (['invalid compact block/non-matching block transactions' ]):
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