@@ -708,6 +708,33 @@ def test_end_to_end_block_relay(self, node, listeners):
708708 l .last_cmpctblock .header_and_shortids .header .calc_sha256 ()
709709 assert_equal (l .last_cmpctblock .header_and_shortids .header .sha256 , block .sha256 )
710710
711+ # Test that we don't get disconnected if we relay a compact block with valid header,
712+ # but invalid transactions.
713+ def test_invalid_tx_in_compactblock (self , node , test_node , use_segwit ):
714+ assert (len (self .utxos ))
715+ utxo = self .utxos [0 ]
716+
717+ block = self .build_block_with_transactions (node , utxo , 5 )
718+ del block .vtx [3 ]
719+ block .hashMerkleRoot = block .calc_merkle_root ()
720+ if use_segwit :
721+ # If we're testing with segwit, also drop the coinbase witness,
722+ # but include the witness commitment.
723+ add_witness_commitment (block )
724+ block .vtx [0 ].wit .vtxinwit = []
725+ block .solve ()
726+
727+ # Now send the compact block with all transactions prefilled, and
728+ # verify that we don't get disconnected.
729+ comp_block = HeaderAndShortIDs ()
730+ comp_block .initialize_from_block (block , prefill_list = [0 , 1 , 2 , 3 , 4 ], use_witness = use_segwit )
731+ msg = msg_cmpctblock (comp_block .to_p2p ())
732+ test_node .send_and_ping (msg )
733+
734+ # Check that the tip didn't advance
735+ assert (int (node .getbestblockhash (), 16 ) is not block .sha256 )
736+ test_node .sync_with_ping ()
737+
711738 # Helper for enabling cb announcements
712739 # Send the sendcmpct request and sync headers
713740 def request_cb_announcements (self , peer , node , version ):
@@ -798,6 +825,11 @@ def run_test(self):
798825 self .test_end_to_end_block_relay (self .nodes [0 ], [self .segwit_node , self .test_node , self .old_node ])
799826 self .test_end_to_end_block_relay (self .nodes [1 ], [self .segwit_node , self .test_node , self .old_node ])
800827
828+ print ("\t Testing handling of invalid compact blocks..." )
829+ self .test_invalid_tx_in_compactblock (self .nodes [0 ], self .test_node , False )
830+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .segwit_node , False )
831+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node , False )
832+
801833 # Advance to segwit activation
802834 print ("\n Advancing to segwit activation\n " )
803835 self .activate_segwit (self .nodes [1 ])
@@ -844,6 +876,11 @@ def run_test(self):
844876 self .request_cb_announcements (self .segwit_node , self .nodes [1 ], 2 )
845877 self .test_end_to_end_block_relay (self .nodes [1 ], [self .segwit_node , self .test_node , self .old_node ])
846878
879+ print ("\t Testing handling of invalid compact blocks..." )
880+ self .test_invalid_tx_in_compactblock (self .nodes [0 ], self .test_node , False )
881+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .segwit_node , True )
882+ self .test_invalid_tx_in_compactblock (self .nodes [1 ], self .old_node , True )
883+
847884 print ("\t Testing invalid index in cmpctblock message..." )
848885 self .test_invalid_cmpctblock_message ()
849886
0 commit comments