32
32
assert_raises_rpc_error ,
33
33
softfork_active ,
34
34
)
35
+ from test_framework .wallet import MiniWallet
35
36
36
37
SCRIPT_W0_SH_OP_TRUE = script_to_p2wsh_script (CScript ([OP_TRUE ]))
37
38
@@ -58,14 +59,12 @@ def set_test_params(self):
58
59
],
59
60
]
60
61
61
- def skip_test_if_missing_module (self ):
62
- self .skip_if_no_wallet ()
63
-
64
62
def run_test (self ):
65
63
self .relayfee = self .nodes [0 ].getnetworkinfo ()["relayfee" ]
64
+ self .wallet = MiniWallet (self .nodes [0 ])
66
65
67
66
# Generate some coins
68
- self .generate (self .nodes [ 0 ] , 110 )
67
+ self .generate (self .wallet , 110 )
69
68
70
69
self .log .info ("Running test disable flag" )
71
70
self .test_disable_flag ()
@@ -92,16 +91,10 @@ def run_test(self):
92
91
# the first sequence bit is set.
93
92
def test_disable_flag (self ):
94
93
# Create some unconfirmed inputs
95
- new_addr = self .nodes [0 ].getnewaddress ()
96
- self .nodes [0 ].sendtoaddress (new_addr , 2 ) # send 2 BTC
97
-
98
- utxos = self .nodes [0 ].listunspent (0 , 0 )
99
- assert len (utxos ) > 0
100
-
101
- utxo = utxos [0 ]
94
+ utxo = self .wallet .send_self_transfer (from_node = self .nodes [0 ])["new_utxo" ]
102
95
103
96
tx1 = CTransaction ()
104
- value = int ((utxo ["amount " ] - self .relayfee ) * COIN )
97
+ value = int ((utxo ["value " ] - self .relayfee ) * COIN )
105
98
106
99
# Check that the disable flag disables relative locktime.
107
100
# If sequence locks were used, this would require 1 block for the
@@ -110,8 +103,8 @@ def test_disable_flag(self):
110
103
tx1 .vin = [CTxIn (COutPoint (int (utxo ["txid" ], 16 ), utxo ["vout" ]), nSequence = sequence_value )]
111
104
tx1 .vout = [CTxOut (value , SCRIPT_W0_SH_OP_TRUE )]
112
105
113
- tx1_signed = self .nodes [ 0 ]. signrawtransactionwithwallet ( tx1 . serialize (). hex ())[ "hex" ]
114
- tx1_id = self .nodes [0 ]. sendrawtransaction ( tx1_signed )
106
+ self .wallet . sign_tx ( tx = tx1 )
107
+ tx1_id = self .wallet . sendrawtransaction ( from_node = self . nodes [0 ], tx_hex = tx1 . serialize (). hex () )
115
108
tx1_id = int (tx1_id , 16 )
116
109
117
110
# This transaction will enable sequence-locks, so this transaction should
@@ -125,13 +118,13 @@ def test_disable_flag(self):
125
118
tx2 .vout = [CTxOut (int (value - self .relayfee * COIN ), SCRIPT_W0_SH_OP_TRUE )]
126
119
tx2 .rehash ()
127
120
128
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ]. sendrawtransaction , tx2 .serialize ().hex ())
121
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .wallet . sendrawtransaction , from_node = self . nodes [0 ], tx_hex = tx2 .serialize ().hex ())
129
122
130
123
# Setting the version back down to 1 should disable the sequence lock,
131
124
# so this should be accepted.
132
125
tx2 .nVersion = 1
133
126
134
- self .nodes [0 ]. sendrawtransaction ( tx2 .serialize ().hex ())
127
+ self .wallet . sendrawtransaction ( from_node = self . nodes [0 ], tx_hex = tx2 .serialize ().hex ())
135
128
136
129
# Calculate the median time past of a prior block ("confirmations" before
137
130
# the current tip).
@@ -144,20 +137,13 @@ def test_sequence_lock_confirmed_inputs(self):
144
137
# Create lots of confirmed utxos, and use them to generate lots of random
145
138
# transactions.
146
139
max_outputs = 50
147
- addresses = []
148
- while len (addresses ) < max_outputs :
149
- addresses .append (self .nodes [0 ].getnewaddress ())
150
- while len (self .nodes [0 ].listunspent ()) < 200 :
140
+ while len (self .wallet .get_utxos (include_immature_coinbase = False , mark_as_spent = False )) < 200 :
151
141
import random
152
- random .shuffle (addresses )
153
142
num_outputs = random .randint (1 , max_outputs )
154
- outputs = {}
155
- for i in range (num_outputs ):
156
- outputs [addresses [i ]] = random .randint (1 , 20 )* 0.01
157
- self .nodes [0 ].sendmany ("" , outputs )
158
- self .generate (self .nodes [0 ], 1 )
143
+ self .wallet .send_self_transfer_multi (from_node = self .nodes [0 ], num_outputs = num_outputs )
144
+ self .generate (self .wallet , 1 )
159
145
160
- utxos = self .nodes [ 0 ]. listunspent ( )
146
+ utxos = self .wallet . get_utxos ( include_immature_coinbase = False )
161
147
162
148
# Try creating a lot of random transactions.
163
149
# Each time, choose a random number of inputs, and randomly set
@@ -214,19 +200,20 @@ def test_sequence_lock_confirmed_inputs(self):
214
200
sequence_value = ((cur_time - orig_time ) >> SEQUENCE_LOCKTIME_GRANULARITY )+ 1
215
201
sequence_value |= SEQUENCE_LOCKTIME_TYPE_FLAG
216
202
tx .vin .append (CTxIn (COutPoint (int (utxos [j ]["txid" ], 16 ), utxos [j ]["vout" ]), nSequence = sequence_value ))
217
- value += utxos [j ]["amount " ]* COIN
203
+ value += utxos [j ]["value " ]* COIN
218
204
# Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output
219
205
tx_size = len (tx .serialize ().hex ())// 2 + 120 * num_inputs + 50
220
206
tx .vout .append (CTxOut (int (value - self .relayfee * tx_size * COIN / 1000 ), SCRIPT_W0_SH_OP_TRUE ))
221
- rawtx = self .nodes [ 0 ]. signrawtransactionwithwallet (tx . serialize (). hex ())[ "hex" ]
207
+ self .wallet . sign_tx (tx = tx )
222
208
223
209
if (using_sequence_locks and not should_pass ):
224
210
# This transaction should be rejected
225
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ]. sendrawtransaction , rawtx )
211
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .wallet . sendrawtransaction , from_node = self . nodes [0 ], tx_hex = tx . serialize (). hex () )
226
212
else :
227
213
# This raw transaction should be accepted
228
- self .nodes [0 ].sendrawtransaction (rawtx )
229
- utxos = self .nodes [0 ].listunspent ()
214
+ self .wallet .sendrawtransaction (from_node = self .nodes [0 ], tx_hex = tx .serialize ().hex ())
215
+ self .wallet .rescan_utxos ()
216
+ utxos = self .wallet .get_utxos (include_immature_coinbase = False )
230
217
231
218
# Test that sequence locks on unconfirmed inputs must have nSequence
232
219
# height or time of 0 to be accepted.
@@ -237,8 +224,8 @@ def test_sequence_lock_unconfirmed_inputs(self):
237
224
cur_height = self .nodes [0 ].getblockcount ()
238
225
239
226
# Create a mempool tx.
240
- txid = self .nodes [ 0 ]. sendtoaddress ( self . nodes [ 0 ]. getnewaddress (), 2 )
241
- tx1 = tx_from_hex ( self .nodes [0 ]. getrawtransaction ( txid ))
227
+ self .wallet . rescan_utxos ( )
228
+ tx1 = self . wallet . send_self_transfer ( from_node = self .nodes [0 ])[ "tx" ]
242
229
tx1 .rehash ()
243
230
244
231
# Anyone-can-spend mempool tx.
@@ -247,11 +234,11 @@ def test_sequence_lock_unconfirmed_inputs(self):
247
234
tx2 .nVersion = 2
248
235
tx2 .vin = [CTxIn (COutPoint (tx1 .sha256 , 0 ), nSequence = 0 )]
249
236
tx2 .vout = [CTxOut (int (tx1 .vout [0 ].nValue - self .relayfee * COIN ), SCRIPT_W0_SH_OP_TRUE )]
250
- tx2_raw = self .nodes [ 0 ]. signrawtransactionwithwallet ( tx2 . serialize (). hex ())[ "hex" ]
251
- tx2 = tx_from_hex ( tx2_raw )
237
+ self .wallet . sign_tx ( tx = tx2 )
238
+ tx2_raw = tx2 . serialize (). hex ( )
252
239
tx2 .rehash ()
253
240
254
- self .nodes [0 ]. sendrawtransaction ( tx2_raw )
241
+ self .wallet . sendrawtransaction ( from_node = self . nodes [0 ], tx_hex = tx2_raw )
255
242
256
243
# Create a spend of the 0th output of orig_tx with a sequence lock
257
244
# of 1, and test what happens when submitting.
@@ -271,10 +258,10 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
271
258
272
259
if (orig_tx .hash in node .getrawmempool ()):
273
260
# sendrawtransaction should fail if the tx is in the mempool
274
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , node . sendrawtransaction , tx .serialize ().hex ())
261
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self . wallet . sendrawtransaction , from_node = node , tx_hex = tx .serialize ().hex ())
275
262
else :
276
263
# sendrawtransaction should succeed if the tx is not in the mempool
277
- node . sendrawtransaction (tx .serialize ().hex ())
264
+ self . wallet . sendrawtransaction (from_node = node , tx_hex = tx .serialize ().hex ())
278
265
279
266
return tx
280
267
@@ -287,7 +274,7 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
287
274
cur_time = int (time .time ())
288
275
for _ in range (10 ):
289
276
self .nodes [0 ].setmocktime (cur_time + 600 )
290
- self .generate (self .nodes [ 0 ] , 1 , sync_fun = self .no_op )
277
+ self .generate (self .wallet , 1 , sync_fun = self .no_op )
291
278
cur_time += 600
292
279
293
280
assert tx2 .hash in self .nodes [0 ].getrawmempool ()
@@ -321,12 +308,12 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
321
308
tx5 = test_nonzero_locks (tx4 , self .nodes [0 ], self .relayfee , use_height_lock = True )
322
309
assert tx5 .hash not in self .nodes [0 ].getrawmempool ()
323
310
324
- utxos = self .nodes [ 0 ]. listunspent ()
325
- tx5 .vin .append (CTxIn (COutPoint (int (utxos [ 0 ][ "txid" ], 16 ), utxos [ 0 ] ["vout" ]), nSequence = 1 ))
326
- tx5 .vout [0 ].nValue += int (utxos [ 0 ][ "amount " ]* COIN )
327
- raw_tx5 = self .nodes [ 0 ]. signrawtransactionwithwallet ( tx5 . serialize (). hex ())[ "hex" ]
311
+ utxo = self .wallet . get_utxo ()
312
+ tx5 .vin .append (CTxIn (COutPoint (int (utxo [ "txid" ], 16 ), utxo ["vout" ]), nSequence = 1 ))
313
+ tx5 .vout [0 ].nValue += int (utxo [ "value " ]* COIN )
314
+ self .wallet . sign_tx ( tx = tx5 )
328
315
329
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ]. sendrawtransaction , raw_tx5 )
316
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .wallet . sendrawtransaction , from_node = self . nodes [0 ], tx_hex = tx5 . serialize (). hex () )
330
317
331
318
# Test mempool-BIP68 consistency after reorg
332
319
#
@@ -362,17 +349,16 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
362
349
# Reset the chain and get rid of the mocktimed-blocks
363
350
self .nodes [0 ].setmocktime (0 )
364
351
self .nodes [0 ].invalidateblock (self .nodes [0 ].getblockhash (cur_height + 1 ))
365
- self .generate (self .nodes [ 0 ] , 10 , sync_fun = self .no_op )
352
+ self .generate (self .wallet , 10 , sync_fun = self .no_op )
366
353
367
354
# Make sure that BIP68 isn't being used to validate blocks prior to
368
355
# activation height. If more blocks are mined prior to this test
369
356
# being run, then it's possible the test has activated the soft fork, and
370
357
# this test should be moved to run earlier, or deleted.
371
358
def test_bip68_not_consensus (self ):
372
359
assert not softfork_active (self .nodes [0 ], 'csv' )
373
- txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 2 )
374
360
375
- tx1 = tx_from_hex ( self .nodes [0 ]. getrawtransaction ( txid ))
361
+ tx1 = self . wallet . send_self_transfer ( from_node = self .nodes [0 ])[ "tx" ]
376
362
tx1 .rehash ()
377
363
378
364
# Make an anyone-can-spend transaction
@@ -382,11 +368,12 @@ def test_bip68_not_consensus(self):
382
368
tx2 .vout = [CTxOut (int (tx1 .vout [0 ].nValue - self .relayfee * COIN ), SCRIPT_W0_SH_OP_TRUE )]
383
369
384
370
# sign tx2
385
- tx2_raw = self .nodes [0 ].signrawtransactionwithwallet (tx2 .serialize ().hex ())["hex" ]
371
+ self .wallet .sign_tx (tx = tx2 )
372
+ tx2_raw = tx2 .serialize ().hex ()
386
373
tx2 = tx_from_hex (tx2_raw )
387
374
tx2 .rehash ()
388
375
389
- self .nodes [ 0 ] .sendrawtransaction (tx2 . serialize (). hex () )
376
+ self .wallet .sendrawtransaction (from_node = self . nodes [ 0 ], tx_hex = tx2_raw )
390
377
391
378
# Now make an invalid spend of tx2 according to BIP68
392
379
sequence_value = 100 # 100 block relative locktime
@@ -399,7 +386,7 @@ def test_bip68_not_consensus(self):
399
386
tx3 .vout = [CTxOut (int (tx2 .vout [0 ].nValue - self .relayfee * COIN ), SCRIPT_W0_SH_OP_TRUE )]
400
387
tx3 .rehash ()
401
388
402
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ]. sendrawtransaction , tx3 .serialize ().hex ())
389
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .wallet . sendrawtransaction , from_node = self . nodes [0 ], tx_hex = tx3 .serialize ().hex ())
403
390
404
391
# make a block that violates bip68; ensure that the tip updates
405
392
block = create_block (tmpl = self .nodes [0 ].getblocktemplate (NORMAL_GBT_REQUEST_PARAMS ), txlist = [tx1 , tx2 , tx3 ])
@@ -415,22 +402,19 @@ def activateCSV(self):
415
402
min_activation_height = 432
416
403
height = self .nodes [0 ].getblockcount ()
417
404
assert_greater_than (min_activation_height - height , 2 )
418
- self .generate (self .nodes [ 0 ] , min_activation_height - height - 2 , sync_fun = self .no_op )
405
+ self .generate (self .wallet , min_activation_height - height - 2 , sync_fun = self .no_op )
419
406
assert not softfork_active (self .nodes [0 ], 'csv' )
420
- self .generate (self .nodes [ 0 ] , 1 , sync_fun = self .no_op )
407
+ self .generate (self .wallet , 1 , sync_fun = self .no_op )
421
408
assert softfork_active (self .nodes [0 ], 'csv' )
422
409
self .sync_blocks ()
423
410
424
411
# Use self.nodes[1] to test that version 2 transactions are standard.
425
412
def test_version2_relay (self ):
426
- inputs = [ ]
427
- outputs = { self .nodes [1 ].getnewaddress () : 1.0 }
428
- rawtx = self .nodes [1 ].createrawtransaction (inputs , outputs )
429
- rawtxfund = self .nodes [1 ].fundrawtransaction (rawtx )['hex' ]
430
- tx = tx_from_hex (rawtxfund )
413
+ mini_wallet = MiniWallet (self .nodes [1 ])
414
+ mini_wallet .rescan_utxos ()
415
+ tx = mini_wallet .create_self_transfer ()["tx" ]
431
416
tx .nVersion = 2
432
- tx_signed = self .nodes [1 ].signrawtransactionwithwallet (tx .serialize ().hex ())["hex" ]
433
- self .nodes [1 ].sendrawtransaction (tx_signed )
417
+ mini_wallet .sendrawtransaction (from_node = self .nodes [1 ], tx_hex = tx .serialize ().hex ())
434
418
435
419
if __name__ == '__main__' :
436
420
BIP68Test ().main ()
0 commit comments