@@ -69,6 +69,11 @@ def getutxo(txid):
69
69
utxo ["txid" ] = txid
70
70
return utxo
71
71
72
+ def find_unspent (node , min_value ):
73
+ for utxo in node .listunspent ():
74
+ if utxo ['amount' ] >= min_value :
75
+ return utxo
76
+
72
77
class SegWitTest (BitcoinTestFramework ):
73
78
74
79
def setup_chain (self ):
@@ -117,8 +122,21 @@ def fail_mine(self, node, txid, sign, redeem_script=""):
117
122
sync_blocks (self .nodes )
118
123
119
124
def run_test (self ):
120
- self .nodes [0 ].generate (160 ) #block 160
121
-
125
+ self .nodes [0 ].generate (161 ) #block 161
126
+
127
+ print ("Verify sigops are counted in GBT with pre-BIP141 rules before the fork" )
128
+ txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 )
129
+ tmpl = self .nodes [0 ].getblocktemplate ({})
130
+ assert (tmpl ['sigoplimit' ] == 20000 )
131
+ assert (tmpl ['transactions' ][0 ]['hash' ] == txid )
132
+ assert (tmpl ['transactions' ][0 ]['sigops' ] == 2 )
133
+ tmpl = self .nodes [0 ].getblocktemplate ({'rules' :['segwit' ]})
134
+ assert (tmpl ['sigoplimit' ] == 20000 )
135
+ assert (tmpl ['transactions' ][0 ]['hash' ] == txid )
136
+ assert (tmpl ['transactions' ][0 ]['sigops' ] == 2 )
137
+ self .nodes [0 ].generate (1 ) #block 162
138
+
139
+ balance_presetup = self .nodes [0 ].getbalance ()
122
140
self .pubkey = []
123
141
p2sh_ids = [] # p2sh_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE embedded in p2sh
124
142
wit_ids = [] # wit_ids[NODE][VER] is an array of txids that spend to a witness version VER pkscript to an address for NODE via bare witness
@@ -137,18 +155,18 @@ def run_test(self):
137
155
for i in range (5 ):
138
156
for n in range (3 ):
139
157
for v in range (2 ):
140
- wit_ids [n ][v ].append (send_to_witness (v , self .nodes [0 ], self .nodes [0 ]. listunspent ()[ 0 ] , self .pubkey [n ], False , Decimal ("49.999" )))
141
- p2sh_ids [n ][v ].append (send_to_witness (v , self .nodes [0 ], self .nodes [0 ]. listunspent ()[ 0 ] , self .pubkey [n ], True , Decimal ("49.999" )))
158
+ wit_ids [n ][v ].append (send_to_witness (v , self .nodes [0 ], find_unspent ( self .nodes [0 ], 50 ) , self .pubkey [n ], False , Decimal ("49.999" )))
159
+ p2sh_ids [n ][v ].append (send_to_witness (v , self .nodes [0 ], find_unspent ( self .nodes [0 ], 50 ) , self .pubkey [n ], True , Decimal ("49.999" )))
142
160
143
- self .nodes [0 ].generate (1 ) #block 161
161
+ self .nodes [0 ].generate (1 ) #block 163
144
162
sync_blocks (self .nodes )
145
163
146
164
# Make sure all nodes recognize the transactions as theirs
147
- assert_equal (self .nodes [0 ].getbalance (), 60 * 50 - 60 * 50 + 20 * Decimal ("49.999" ) + 50 )
165
+ assert_equal (self .nodes [0 ].getbalance (), balance_presetup - 60 * 50 + 20 * Decimal ("49.999" ) + 50 )
148
166
assert_equal (self .nodes [1 ].getbalance (), 20 * Decimal ("49.999" ))
149
167
assert_equal (self .nodes [2 ].getbalance (), 20 * Decimal ("49.999" ))
150
168
151
- self .nodes [0 ].generate (262 ) #block 423
169
+ self .nodes [0 ].generate (260 ) #block 423
152
170
sync_blocks (self .nodes )
153
171
154
172
print ("Verify default node can't accept any witness format txs before fork" )
@@ -205,5 +223,25 @@ def run_test(self):
205
223
self .success_mine (self .nodes [0 ], p2sh_ids [NODE_0 ][WIT_V0 ][0 ], True ) #block 434
206
224
self .success_mine (self .nodes [0 ], p2sh_ids [NODE_0 ][WIT_V1 ][0 ], True ) #block 435
207
225
226
+ print ("Verify sigops are counted in GBT with BIP141 rules after the fork" )
227
+ txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 1 )
228
+ tmpl = self .nodes [0 ].getblocktemplate ({'rules' :['segwit' ]})
229
+ assert (tmpl ['sigoplimit' ] == 80000 )
230
+ assert (tmpl ['transactions' ][0 ]['txid' ] == txid )
231
+ assert (tmpl ['transactions' ][0 ]['sigops' ] == 8 )
232
+
233
+ print ("Verify non-segwit miners get a valid GBT response after the fork" )
234
+ send_to_witness (1 , self .nodes [0 ], find_unspent (self .nodes [0 ], 50 ), self .pubkey [0 ], False , Decimal ("49.998" ))
235
+ try :
236
+ tmpl = self .nodes [0 ].getblocktemplate ({})
237
+ assert (len (tmpl ['transactions' ]) == 1 ) # Doesn't include witness tx
238
+ assert (tmpl ['sigoplimit' ] == 20000 )
239
+ assert (tmpl ['transactions' ][0 ]['hash' ] == txid )
240
+ assert (tmpl ['transactions' ][0 ]['sigops' ] == 2 )
241
+ assert (('!segwit' in tmpl ['rules' ]) or ('segwit' not in tmpl ['rules' ]))
242
+ except JSONRPCException :
243
+ # This is an acceptable outcome
244
+ pass
245
+
208
246
if __name__ == '__main__' :
209
247
SegWitTest ().main ()
0 commit comments