@@ -195,24 +195,16 @@ def __init__(self):
195
195
self .setup_clean_chain = True
196
196
self .num_nodes = 3
197
197
198
- def add_options (self , parser ):
199
- parser .add_option ("--oldbinary" , dest = "oldbinary" ,
200
- default = None ,
201
- help = "pre-segwit bitcoind binary for upgrade testing" )
202
-
203
198
def setup_network (self ):
204
199
self .nodes = []
205
200
self .nodes .append (start_node (0 , self .options .tmpdir , ["-debug" , "-logtimemicros=1" , "-whitelist=127.0.0.1" ]))
206
201
# Start a node for testing IsStandard rules.
207
202
self .nodes .append (start_node (1 , self .options .tmpdir , ["-debug" , "-logtimemicros=1" , "-whitelist=127.0.0.1" , "-acceptnonstdtxn=0" ]))
208
203
connect_nodes (self .nodes [0 ], 1 )
209
204
210
- # If an old bitcoind is given, do the upgrade-after-activation test.
211
- self .test_upgrade = False
212
- if (self .options .oldbinary != None ):
213
- self .nodes .append (start_node (2 , self .options .tmpdir , ["-debug" , "-whitelist=127.0.0.1" ], binary = self .options .oldbinary ))
214
- connect_nodes (self .nodes [0 ], 2 )
215
- self .test_upgrade = True
205
+ # Disable segwit's bip9 parameter to simulate upgrading after activation.
206
+ self .nodes .append (start_node (2 , self .options .tmpdir , ["-debug" , "-whitelist=127.0.0.1" , "-bip9params=segwit:0:0" ]))
207
+ connect_nodes (self .nodes [0 ], 2 )
216
208
217
209
''' Helpers '''
218
210
# Build a block on top of node0's tip.
@@ -1177,7 +1169,7 @@ def test_standardness_v0(self, segwit_activated):
1177
1169
if segwit_activated :
1178
1170
# tx and tx2 were both accepted. Don't bother trying to reclaim the
1179
1171
# P2PKH output; just send tx's first output back to an anyone-can-spend.
1180
- sync_mempools (self .nodes )
1172
+ sync_mempools ([ self .nodes [ 0 ], self . nodes [ 1 ]] )
1181
1173
tx3 .vin = [CTxIn (COutPoint (tx .sha256 , 0 ), b"" )]
1182
1174
tx3 .vout = [CTxOut (tx .vout [0 ].nValue - 1000 , CScript ([OP_TRUE ]))]
1183
1175
tx3 .wit .vtxinwit .append (CTxInWitness ())
@@ -1706,19 +1698,53 @@ def test_witness_sigops(self):
1706
1698
1707
1699
def test_getblocktemplate_before_lockin (self ):
1708
1700
print ("\t Testing getblocktemplate setting of segwit versionbit (before lockin)" )
1709
- block_version = (self .nodes [0 ].getblocktemplate ())['version' ]
1710
- assert_equal (block_version & (1 << VB_WITNESS_BIT ), 0 )
1701
+ # Node0 is segwit aware, node2 is not.
1702
+ for node in [self .nodes [0 ], self .nodes [2 ]]:
1703
+ gbt_results = node .getblocktemplate ()
1704
+ block_version = gbt_results ['version' ]
1705
+ # If we're not indicating segwit support, we should not be signalling
1706
+ # for segwit activation, nor should we get a witness commitment.
1707
+ assert_equal (block_version & (1 << VB_WITNESS_BIT ), 0 )
1708
+ assert ('default_witness_commitment' not in gbt_results )
1711
1709
1712
1710
# Workaround:
1713
1711
# Can either change the tip, or change the mempool and wait 5 seconds
1714
1712
# to trigger a recomputation of getblocktemplate.
1715
- self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 )
1713
+ txid = int ( self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 ), 16 )
1716
1714
# Using mocktime lets us avoid sleep()
1715
+ sync_mempools (self .nodes )
1717
1716
self .nodes [0 ].setmocktime (int (time .time ())+ 10 )
1718
-
1719
- block_version = self .nodes [0 ].getblocktemplate ({"rules" : ["segwit" ]})['version' ]
1720
- assert (block_version & (1 << VB_WITNESS_BIT ) != 0 )
1721
- self .nodes [0 ].setmocktime (0 ) # undo mocktime
1717
+ self .nodes [2 ].setmocktime (int (time .time ())+ 10 )
1718
+
1719
+ for node in [self .nodes [0 ], self .nodes [2 ]]:
1720
+ gbt_results = node .getblocktemplate ({"rules" : ["segwit" ]})
1721
+ block_version = gbt_results ['version' ]
1722
+ if node == self .nodes [2 ]:
1723
+ # If this is a non-segwit node, we should still not get a witness
1724
+ # commitment, nor a version bit signalling segwit.
1725
+ assert_equal (block_version & (1 << VB_WITNESS_BIT ), 0 )
1726
+ assert ('default_witness_commitment' not in gbt_results )
1727
+ else :
1728
+ # For segwit-aware nodes, check the version bit and the witness
1729
+ # commitment are correct.
1730
+ assert (block_version & (1 << VB_WITNESS_BIT ) != 0 )
1731
+ assert ('default_witness_commitment' in gbt_results )
1732
+ witness_commitment = gbt_results ['default_witness_commitment' ]
1733
+
1734
+ # TODO: this duplicates some code from blocktools.py, would be nice
1735
+ # to refactor.
1736
+ # Check that default_witness_commitment is present.
1737
+ block = CBlock ()
1738
+ witness_root = block .get_merkle_root ([ser_uint256 (0 ), ser_uint256 (txid )])
1739
+ check_commitment = uint256_from_str (hash256 (ser_uint256 (witness_root )+ ser_uint256 (0 )))
1740
+ from test_framework .blocktools import WITNESS_COMMITMENT_HEADER
1741
+ output_data = WITNESS_COMMITMENT_HEADER + ser_uint256 (check_commitment )
1742
+ script = CScript ([OP_RETURN , output_data ])
1743
+ assert_equal (witness_commitment , bytes_to_hex_str (script ))
1744
+
1745
+ # undo mocktime
1746
+ self .nodes [0 ].setmocktime (0 )
1747
+ self .nodes [2 ].setmocktime (0 )
1722
1748
1723
1749
# Uncompressed pubkeys are no longer supported in default relay policy,
1724
1750
# but (for now) are still valid in blocks.
@@ -1958,6 +1984,7 @@ def run_test(self):
1958
1984
1959
1985
# Advance to segwit being 'started'
1960
1986
self .advance_to_segwit_started ()
1987
+ sync_blocks (self .nodes )
1961
1988
self .test_getblocktemplate_before_lockin ()
1962
1989
1963
1990
sync_blocks (self .nodes )
@@ -2000,10 +2027,7 @@ def run_test(self):
2000
2027
self .test_signature_version_1 ()
2001
2028
self .test_non_standard_witness ()
2002
2029
sync_blocks (self .nodes )
2003
- if self .test_upgrade :
2004
- self .test_upgrade_after_activation (self .nodes [2 ], 2 )
2005
- else :
2006
- print ("\t Skipping upgrade-after-activation test (use --oldbinary to enable)" )
2030
+ self .test_upgrade_after_activation (self .nodes [2 ], 2 )
2007
2031
self .test_witness_sigops ()
2008
2032
2009
2033
0 commit comments