66from test_framework .mininode import *
77from test_framework .test_framework import BitcoinTestFramework
88from test_framework .util import *
9- from test_framework .blocktools import create_block , create_coinbase
9+ from test_framework .blocktools import create_block , create_coinbase , add_witness_commitment
1010from test_framework .siphash import siphash256
1111from test_framework .script import CScript , OP_TRUE
1212
@@ -123,11 +123,13 @@ def setup_network(self):
123123 ["-debug" , "-logtimemicros" , "-txindex" ]])
124124 connect_nodes (self .nodes [0 ], 1 )
125125
126- def build_block_on_tip (self , node ):
126+ def build_block_on_tip (self , node , segwit = False ):
127127 height = node .getblockcount ()
128128 tip = node .getbestblockhash ()
129129 mtp = node .getblockheader (tip )['mediantime' ]
130130 block = create_block (int (tip , 16 ), create_coinbase (height + 1 ), mtp + 1 )
131+ if segwit :
132+ add_witness_commitment (block )
131133 block .solve ()
132134 return block
133135
@@ -380,11 +382,11 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
380382 # Post-segwit: upgraded nodes would only make this request of cb-version-2,
381383 # NODE_WITNESS peers. Unupgraded nodes would still make this request of
382384 # any cb-version-1-supporting peer.
383- def test_compactblock_requests (self , node , test_node ):
385+ def test_compactblock_requests (self , node , test_node , version , segwit ):
384386 # Try announcing a block with an inv or header, expect a compactblock
385387 # request
386388 for announce in ["inv" , "header" ]:
387- block = self .build_block_on_tip (node )
389+ block = self .build_block_on_tip (node , segwit = segwit )
388390 with mininode_lock :
389391 test_node .last_getdata = None
390392
@@ -403,8 +405,11 @@ def test_compactblock_requests(self, node, test_node):
403405 comp_block .header = CBlockHeader (block )
404406 comp_block .nonce = 0
405407 [k0 , k1 ] = comp_block .get_siphash_keys ()
408+ coinbase_hash = block .vtx [0 ].sha256
409+ if version == 2 :
410+ coinbase_hash = block .vtx [0 ].calc_sha256 (True )
406411 comp_block .shortids = [
407- calculate_shortid (k0 , k1 , block . vtx [ 0 ]. sha256 ) ]
412+ calculate_shortid (k0 , k1 , coinbase_hash ) ]
408413 test_node .send_and_ping (msg_cmpctblock (comp_block .to_p2p ()))
409414 assert_equal (int (node .getbestblockhash (), 16 ), block .hashPrevBlock )
410415 # Expect a getblocktxn message.
@@ -414,7 +419,10 @@ def test_compactblock_requests(self, node, test_node):
414419 assert_equal (absolute_indexes , [0 ]) # should be a coinbase request
415420
416421 # Send the coinbase, and verify that the tip advances.
417- msg = msg_blocktxn ()
422+ if version == 2 :
423+ msg = msg_witness_blocktxn ()
424+ else :
425+ msg = msg_blocktxn ()
418426 msg .block_transactions .blockhash = block .sha256
419427 msg .block_transactions .transactions = [block .vtx [0 ]]
420428 test_node .send_and_ping (msg )
@@ -750,9 +758,9 @@ def run_test(self):
750758 sync_blocks (self .nodes )
751759
752760 print ("\t Testing compactblock requests... " )
753- self .test_compactblock_requests (self .nodes [0 ], self .test_node )
761+ self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , False )
754762 sync_blocks (self .nodes )
755- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node )
763+ self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , False )
756764 sync_blocks (self .nodes )
757765
758766 print ("\t Testing getblocktxn requests..." )
@@ -800,7 +808,7 @@ def run_test(self):
800808 sync_blocks (self .nodes )
801809
802810 print ("\t Testing compactblock requests (unupgraded node)... " )
803- self .test_compactblock_requests (self .nodes [0 ], self .test_node )
811+ self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , True )
804812
805813 print ("\t Testing getblocktxn requests (unupgraded node)..." )
806814 self .test_getblocktxn_requests (self .nodes [0 ], self .test_node , 1 )
@@ -815,7 +823,7 @@ def run_test(self):
815823 assert_equal (self .nodes [0 ].getbestblockhash (), self .nodes [1 ].getbestblockhash ())
816824
817825 print ("\t Testing compactblock requests (segwit node)... " )
818- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node )
826+ self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , True )
819827
820828 print ("\t Testing getblocktxn requests (segwit node)..." )
821829 self .test_getblocktxn_requests (self .nodes [1 ], self .segwit_node , 2 )
0 commit comments