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,14 @@ 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
+ block .nVersion = 4
132
+ if segwit :
133
+ add_witness_commitment (block )
131
134
block .solve ()
132
135
return block
133
136
@@ -380,11 +383,11 @@ def test_compactblock_construction(self, node, test_node, version, use_witness_a
380
383
# Post-segwit: upgraded nodes would only make this request of cb-version-2,
381
384
# NODE_WITNESS peers. Unupgraded nodes would still make this request of
382
385
# any cb-version-1-supporting peer.
383
- def test_compactblock_requests (self , node , test_node ):
386
+ def test_compactblock_requests (self , node , test_node , version , segwit ):
384
387
# Try announcing a block with an inv or header, expect a compactblock
385
388
# request
386
389
for announce in ["inv" , "header" ]:
387
- block = self .build_block_on_tip (node )
390
+ block = self .build_block_on_tip (node , segwit = segwit )
388
391
with mininode_lock :
389
392
test_node .last_getdata = None
390
393
@@ -403,8 +406,11 @@ def test_compactblock_requests(self, node, test_node):
403
406
comp_block .header = CBlockHeader (block )
404
407
comp_block .nonce = 0
405
408
[k0 , k1 ] = comp_block .get_siphash_keys ()
409
+ coinbase_hash = block .vtx [0 ].sha256
410
+ if version == 2 :
411
+ coinbase_hash = block .vtx [0 ].calc_sha256 (True )
406
412
comp_block .shortids = [
407
- calculate_shortid (k0 , k1 , block . vtx [ 0 ]. sha256 ) ]
413
+ calculate_shortid (k0 , k1 , coinbase_hash ) ]
408
414
test_node .send_and_ping (msg_cmpctblock (comp_block .to_p2p ()))
409
415
assert_equal (int (node .getbestblockhash (), 16 ), block .hashPrevBlock )
410
416
# Expect a getblocktxn message.
@@ -414,7 +420,10 @@ def test_compactblock_requests(self, node, test_node):
414
420
assert_equal (absolute_indexes , [0 ]) # should be a coinbase request
415
421
416
422
# Send the coinbase, and verify that the tip advances.
417
- msg = msg_blocktxn ()
423
+ if version == 2 :
424
+ msg = msg_witness_blocktxn ()
425
+ else :
426
+ msg = msg_blocktxn ()
418
427
msg .block_transactions .blockhash = block .sha256
419
428
msg .block_transactions .transactions = [block .vtx [0 ]]
420
429
test_node .send_and_ping (msg )
@@ -750,9 +759,9 @@ def run_test(self):
750
759
sync_blocks (self .nodes )
751
760
752
761
print ("\t Testing compactblock requests... " )
753
- self .test_compactblock_requests (self .nodes [0 ], self .test_node )
762
+ self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , False )
754
763
sync_blocks (self .nodes )
755
- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node )
764
+ self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , False )
756
765
sync_blocks (self .nodes )
757
766
758
767
print ("\t Testing getblocktxn requests..." )
@@ -800,7 +809,7 @@ def run_test(self):
800
809
sync_blocks (self .nodes )
801
810
802
811
print ("\t Testing compactblock requests (unupgraded node)... " )
803
- self .test_compactblock_requests (self .nodes [0 ], self .test_node )
812
+ self .test_compactblock_requests (self .nodes [0 ], self .test_node , 1 , True )
804
813
805
814
print ("\t Testing getblocktxn requests (unupgraded node)..." )
806
815
self .test_getblocktxn_requests (self .nodes [0 ], self .test_node , 1 )
@@ -815,7 +824,7 @@ def run_test(self):
815
824
assert_equal (self .nodes [0 ].getbestblockhash (), self .nodes [1 ].getbestblockhash ())
816
825
817
826
print ("\t Testing compactblock requests (segwit node)... " )
818
- self .test_compactblock_requests (self .nodes [1 ], self .segwit_node )
827
+ self .test_compactblock_requests (self .nodes [1 ], self .segwit_node , 2 , True )
819
828
820
829
print ("\t Testing getblocktxn requests (segwit node)..." )
821
830
self .test_getblocktxn_requests (self .nodes [1 ], self .segwit_node , 2 )
0 commit comments