6
6
from test_framework .mininode import *
7
7
from test_framework .test_framework import BitcoinTestFramework
8
8
from 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
10
10
from test_framework .siphash import siphash256
11
11
from test_framework .script import CScript , OP_TRUE
12
12
@@ -123,11 +123,13 @@ def setup_network(self):
123
123
["-debug" , "-logtimemicros" , "-txindex" ]])
124
124
connect_nodes (self .nodes [0 ], 1 )
125
125
126
- def build_block_on_tip (self , node ):
126
+ def build_block_on_tip (self , node , segwit = False ):
127
127
height = node .getblockcount ()
128
128
tip = node .getbestblockhash ()
129
129
mtp = node .getblockheader (tip )['mediantime' ]
130
130
block = create_block (int (tip , 16 ), create_coinbase (height + 1 ), mtp + 1 )
131
+ if segwit :
132
+ add_witness_commitment (block )
131
133
block .solve ()
132
134
return block
133
135
@@ -380,11 +382,11 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
380
382
# Post-segwit: upgraded nodes would only make this request of cb-version-2,
381
383
# NODE_WITNESS peers. Unupgraded nodes would still make this request of
382
384
# 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 ):
384
386
# Try announcing a block with an inv or header, expect a compactblock
385
387
# request
386
388
for announce in ["inv" , "header" ]:
387
- block = self .build_block_on_tip (node )
389
+ block = self .build_block_on_tip (node , segwit = segwit )
388
390
with mininode_lock :
389
391
test_node .last_getdata = None
390
392
@@ -403,8 +405,11 @@ def test_compactblock_requests(self, node, test_node):
403
405
comp_block .header = CBlockHeader (block )
404
406
comp_block .nonce = 0
405
407
[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 )
406
411
comp_block .shortids = [
407
- calculate_shortid (k0 , k1 , block . vtx [ 0 ]. sha256 ) ]
412
+ calculate_shortid (k0 , k1 , coinbase_hash ) ]
408
413
test_node .send_and_ping (msg_cmpctblock (comp_block .to_p2p ()))
409
414
assert_equal (int (node .getbestblockhash (), 16 ), block .hashPrevBlock )
410
415
# Expect a getblocktxn message.
@@ -414,7 +419,10 @@ def test_compactblock_requests(self, node, test_node):
414
419
assert_equal (absolute_indexes , [0 ]) # should be a coinbase request
415
420
416
421
# 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 ()
418
426
msg .block_transactions .blockhash = block .sha256
419
427
msg .block_transactions .transactions = [block .vtx [0 ]]
420
428
test_node .send_and_ping (msg )
@@ -750,9 +758,9 @@ def run_test(self):
750
758
sync_blocks (self .nodes )
751
759
752
760
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 )
754
762
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 )
756
764
sync_blocks (self .nodes )
757
765
758
766
print ("\t Testing getblocktxn requests..." )
@@ -800,7 +808,7 @@ def run_test(self):
800
808
sync_blocks (self .nodes )
801
809
802
810
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 )
804
812
805
813
print ("\t Testing getblocktxn requests (unupgraded node)..." )
806
814
self .test_getblocktxn_requests (self .nodes [0 ], self .test_node , 1 )
@@ -815,7 +823,7 @@ def run_test(self):
815
823
assert_equal (self .nodes [0 ].getbestblockhash (), self .nodes [1 ].getbestblockhash ())
816
824
817
825
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 )
819
827
820
828
print ("\t Testing getblocktxn requests (segwit node)..." )
821
829
self .test_getblocktxn_requests (self .nodes [1 ], self .segwit_node , 2 )
0 commit comments