44
44
from test_framework .test_framework import BitcoinTestFramework
45
45
from test_framework .util import (
46
46
assert_equal ,
47
+ assert_greater_than_or_equal ,
47
48
assert_is_hex_string ,
48
49
assert_raises_rpc_error ,
49
50
try_rpc ,
54
55
P2WPKH = 0
55
56
P2WSH = 1
56
57
58
+
57
59
def getutxo (txid ):
58
60
utxo = {}
59
61
utxo ["vout" ] = 0
60
62
utxo ["txid" ] = txid
61
63
return utxo
62
64
65
+
63
66
def find_spendable_utxo (node , min_value ):
64
67
for utxo in node .listunspent (query_options = {'minimumAmount' : min_value }):
65
68
if utxo ['spendable' ]:
66
69
return utxo
67
70
68
71
raise AssertionError (f"Unspent output equal or higher than { min_value } not found" )
69
72
70
- txs_mined = {} # txindex from txid to blockhash
73
+
74
+ txs_mined = {} # txindex from txid to blockhash
75
+
71
76
72
77
class SegWitTest (BitcoinTestFramework ):
73
78
def set_test_params (self ):
@@ -124,18 +129,18 @@ def run_test(self):
124
129
self .log .info ("Verify sigops are counted in GBT with pre-BIP141 rules before the fork" )
125
130
txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 )
126
131
tmpl = self .nodes [0 ].getblocktemplate ({'rules' : ['segwit' ]})
127
- assert tmpl ['sizelimit' ] == 1000000
132
+ assert_equal ( tmpl ['sizelimit' ], 1000000 )
128
133
assert 'weightlimit' not in tmpl
129
- assert tmpl ['sigoplimit' ] == 20000
130
- assert tmpl ['transactions' ][0 ]['hash' ] == txid
131
- assert tmpl ['transactions' ][0 ]['sigops' ] == 2
134
+ assert_equal ( tmpl ['sigoplimit' ], 20000 )
135
+ assert_equal ( tmpl ['transactions' ][0 ]['hash' ], txid )
136
+ assert_equal ( tmpl ['transactions' ][0 ]['sigops' ], 2 )
132
137
assert '!segwit' not in tmpl ['rules' ]
133
138
self .generate (self .nodes [0 ], 1 ) # block 162
134
139
135
140
balance_presetup = self .nodes [0 ].getbalance ()
136
141
self .pubkey = []
137
- p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
138
- wit_ids = [] # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
142
+ p2sh_ids = [] # p2sh_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE embedded in p2sh
143
+ wit_ids = [] # wit_ids[NODE][TYPE] is an array of txids that spend to P2WPKH (TYPE=0) or P2WSH (TYPE=1) scripts to an address for NODE via bare witness
139
144
for i in range (3 ):
140
145
newaddress = self .nodes [i ].getnewaddress ()
141
146
self .pubkey .append (self .nodes [i ].getaddressinfo (newaddress )["pubkey" ])
@@ -215,7 +220,7 @@ def run_test(self):
215
220
witnesses = coinbase_tx ["decoded" ]["vin" ][0 ]["txinwitness" ]
216
221
assert_equal (len (witnesses ), 1 )
217
222
assert_is_hex_string (witnesses [0 ])
218
- assert_equal (witnesses [0 ], '00' * 32 )
223
+ assert_equal (witnesses [0 ], '00' * 32 )
219
224
220
225
self .log .info ("Verify witness txs without witness data are invalid after the fork" )
221
226
self .fail_accept (self .nodes [2 ], 'non-mandatory-script-verify-flag (Witness program hash mismatch)' , wit_ids [NODE_2 ][P2WPKH ][2 ], sign = False )
@@ -232,11 +237,11 @@ def run_test(self):
232
237
self .log .info ("Verify sigops are counted in GBT with BIP141 rules after the fork" )
233
238
txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 )
234
239
tmpl = self .nodes [0 ].getblocktemplate ({'rules' : ['segwit' ]})
235
- assert tmpl ['sizelimit' ] >= 3999577 # actual maximum size is lower due to minimum mandatory non-witness data
236
- assert tmpl ['weightlimit' ] == 4000000
237
- assert tmpl ['sigoplimit' ] == 80000
238
- assert tmpl ['transactions' ][0 ]['txid' ] == txid
239
- assert tmpl ['transactions' ][0 ]['sigops' ] == 8
240
+ assert_greater_than_or_equal ( tmpl ['sizelimit' ], 3999577 ) # actual maximum size is lower due to minimum mandatory non-witness data
241
+ assert_equal ( tmpl ['weightlimit' ], 4000000 )
242
+ assert_equal ( tmpl ['sigoplimit' ], 80000 )
243
+ assert_equal ( tmpl ['transactions' ][0 ]['txid' ], txid )
244
+ assert_equal ( tmpl ['transactions' ][0 ]['sigops' ], 8 )
240
245
assert '!segwit' in tmpl ['rules' ]
241
246
242
247
self .generate (self .nodes [0 ], 1 ) # Mine a block to clear the gbt cache
@@ -356,7 +361,7 @@ def run_test(self):
356
361
357
362
for i in compressed_spendable_address :
358
363
v = self .nodes [0 ].getaddressinfo (i )
359
- if ( v ['isscript' ]) :
364
+ if v ['isscript' ]:
360
365
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
361
366
# p2sh multisig with compressed keys should always be spendable
362
367
spendable_anytime .extend ([p2sh ])
@@ -375,7 +380,7 @@ def run_test(self):
375
380
376
381
for i in uncompressed_spendable_address :
377
382
v = self .nodes [0 ].getaddressinfo (i )
378
- if ( v ['isscript' ]) :
383
+ if v ['isscript' ]:
379
384
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
380
385
# p2sh multisig with uncompressed keys should always be spendable
381
386
spendable_anytime .extend ([p2sh ])
@@ -394,7 +399,7 @@ def run_test(self):
394
399
395
400
for i in compressed_solvable_address :
396
401
v = self .nodes [0 ].getaddressinfo (i )
397
- if ( v ['isscript' ]) :
402
+ if v ['isscript' ]:
398
403
# Multisig without private is not seen after addmultisigaddress, but seen after importaddress
399
404
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
400
405
solvable_after_importaddress .extend ([bare , p2sh , p2wsh , p2sh_p2wsh ])
@@ -407,7 +412,7 @@ def run_test(self):
407
412
408
413
for i in uncompressed_solvable_address :
409
414
v = self .nodes [0 ].getaddressinfo (i )
410
- if ( v ['isscript' ]) :
415
+ if v ['isscript' ]:
411
416
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
412
417
# Base uncompressed multisig without private is not seen after addmultisigaddress, but seen after importaddress
413
418
solvable_after_importaddress .extend ([bare , p2sh ])
@@ -446,7 +451,7 @@ def run_test(self):
446
451
importlist = []
447
452
for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address :
448
453
v = self .nodes [0 ].getaddressinfo (i )
449
- if ( v ['isscript' ]) :
454
+ if v ['isscript' ]:
450
455
bare = bytes .fromhex (v ['hex' ])
451
456
importlist .append (bare .hex ())
452
457
importlist .append (script_to_p2wsh_script (bare ).hex ())
@@ -509,7 +514,7 @@ def run_test(self):
509
514
510
515
for i in compressed_spendable_address :
511
516
v = self .nodes [0 ].getaddressinfo (i )
512
- if ( v ['isscript' ]) :
517
+ if v ['isscript' ]:
513
518
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
514
519
premature_witaddress .append (script_to_p2sh (p2wsh ))
515
520
else :
@@ -519,7 +524,7 @@ def run_test(self):
519
524
520
525
for i in uncompressed_spendable_address + uncompressed_solvable_address :
521
526
v = self .nodes [0 ].getaddressinfo (i )
522
- if ( v ['isscript' ]) :
527
+ if v ['isscript' ]:
523
528
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
524
529
# P2WSH and P2SH(P2WSH) multisig with uncompressed keys are never seen
525
530
unseen_anytime .extend ([p2wsh , p2sh_p2wsh ])
@@ -530,7 +535,7 @@ def run_test(self):
530
535
531
536
for i in compressed_solvable_address :
532
537
v = self .nodes [0 ].getaddressinfo (i )
533
- if ( v ['isscript' ]) :
538
+ if v ['isscript' ]:
534
539
[bare , p2sh , p2wsh , p2sh_p2wsh ] = self .p2sh_address_to_script (v )
535
540
premature_witaddress .append (script_to_p2sh (p2wsh ))
536
541
else :
@@ -597,13 +602,13 @@ def mine_and_test_listunspent(self, script_list, ismine):
597
602
watchcount = 0
598
603
spendcount = 0
599
604
for i in self .nodes [0 ].listunspent ():
600
- if ( i ['txid' ] == txid ) :
605
+ if i ['txid' ] == txid :
601
606
watchcount += 1
602
607
if i ['spendable' ]:
603
608
spendcount += 1
604
- if ( ismine == 2 ) :
609
+ if ismine == 2 :
605
610
assert_equal (spendcount , len (script_list ))
606
- elif ( ismine == 1 ) :
611
+ elif ismine == 1 :
607
612
assert_equal (watchcount , len (script_list ))
608
613
assert_equal (spendcount , 0 )
609
614
else :
@@ -615,7 +620,7 @@ def p2sh_address_to_script(self, v):
615
620
p2sh = CScript (bytes .fromhex (v ['scriptPubKey' ]))
616
621
p2wsh = script_to_p2wsh_script (bare )
617
622
p2sh_p2wsh = script_to_p2sh_script (p2wsh )
618
- return ( [bare , p2sh , p2wsh , p2sh_p2wsh ])
623
+ return [bare , p2sh , p2wsh , p2sh_p2wsh ]
619
624
620
625
def p2pkh_address_to_script (self , v ):
621
626
pubkey = bytes .fromhex (v ['pubkey' ])
0 commit comments