@@ -757,6 +757,54 @@ def request_cb_announcements(self, peer, node, version):
757757 msg .announce = True
758758 peer .send_and_ping (msg )
759759
760+ def test_compactblock_reconstruction_multiple_peers (self , node , stalling_peer , delivery_peer ):
761+ assert (len (self .utxos ))
762+
763+ def announce_cmpct_block (node , peer ):
764+ utxo = self .utxos .pop (0 )
765+ block = self .build_block_with_transactions (node , utxo , 5 )
766+
767+ cmpct_block = HeaderAndShortIDs ()
768+ cmpct_block .initialize_from_block (block )
769+ msg = msg_cmpctblock (cmpct_block .to_p2p ())
770+ peer .send_and_ping (msg )
771+ with mininode_lock :
772+ assert (peer .last_getblocktxn is not None )
773+ return block , cmpct_block
774+
775+ block , cmpct_block = announce_cmpct_block (node , stalling_peer )
776+
777+ for tx in block .vtx [1 :]:
778+ delivery_peer .send_message (msg_tx (tx ))
779+ delivery_peer .sync_with_ping ()
780+ mempool = node .getrawmempool ()
781+ for tx in block .vtx [1 :]:
782+ assert (tx .hash in mempool )
783+
784+ delivery_peer .send_and_ping (msg_cmpctblock (cmpct_block .to_p2p ()))
785+ assert_equal (int (node .getbestblockhash (), 16 ), block .sha256 )
786+
787+ self .utxos .append ([block .vtx [- 1 ].sha256 , 0 , block .vtx [- 1 ].vout [0 ].nValue ])
788+
789+ # Now test that delivering an invalid compact block won't break relay
790+
791+ block , cmpct_block = announce_cmpct_block (node , stalling_peer )
792+ for tx in block .vtx [1 :]:
793+ delivery_peer .send_message (msg_tx (tx ))
794+ delivery_peer .sync_with_ping ()
795+
796+ cmpct_block .prefilled_txn [0 ].tx .wit .vtxinwit = [ CTxInWitness () ]
797+ cmpct_block .prefilled_txn [0 ].tx .wit .vtxinwit [0 ].scriptWitness .stack = [ser_uint256 (0 )]
798+
799+ cmpct_block .use_witness = True
800+ delivery_peer .send_and_ping (msg_cmpctblock (cmpct_block .to_p2p ()))
801+ assert (int (node .getbestblockhash (), 16 ) != block .sha256 )
802+
803+ msg = msg_blocktxn ()
804+ msg .block_transactions .blockhash = block .sha256
805+ msg .block_transactions .transactions = block .vtx [1 :]
806+ stalling_peer .send_and_ping (msg )
807+ assert_equal (int (node .getbestblockhash (), 16 ), block .sha256 )
760808
761809 def run_test (self ):
762810 # Setup the p2p connections and start up the network thread.
@@ -841,6 +889,10 @@ def run_test(self):
841889 self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .segwit_node , False )
842890 self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node , False )
843891
892+ print ("\t Testing reconstructing compact blocks from all peers..." )
893+ self .test_compactblock_reconstruction_multiple_peers (self .nodes [1 ], self .segwit_node , self .old_node )
894+ sync_blocks (self .nodes )
895+
844896 # Advance to segwit activation
845897 print ("\n Advancing to segwit activation\n " )
846898 self .activate_segwit (self .nodes [1 ])
0 commit comments