6
6
7
7
import time
8
8
9
- from test_framework .blocktools import create_block , NORMAL_GBT_REQUEST_PARAMS , add_witness_commitment
10
- from test_framework .messages import COIN , COutPoint , CTransaction , CTxIn , CTxOut , FromHex , ToHex
9
+ from test_framework .blocktools import (
10
+ NORMAL_GBT_REQUEST_PARAMS ,
11
+ add_witness_commitment ,
12
+ create_block ,
13
+ )
14
+ from test_framework .messages import (
15
+ COIN ,
16
+ COutPoint ,
17
+ CTransaction ,
18
+ CTxIn ,
19
+ CTxOut ,
20
+ tx_from_hex ,
21
+ )
11
22
from test_framework .test_framework import BitcoinTestFramework
12
23
from test_framework .util import (
13
24
assert_equal ,
@@ -89,7 +100,7 @@ def test_disable_flag(self):
89
100
tx1 .vin = [CTxIn (COutPoint (int (utxo ["txid" ], 16 ), utxo ["vout" ]), nSequence = sequence_value )]
90
101
tx1 .vout = [CTxOut (value , DUMMY_P2WPKH_SCRIPT )]
91
102
92
- tx1_signed = self .nodes [0 ].signrawtransactionwithwallet (ToHex ( tx1 ))["hex" ]
103
+ tx1_signed = self .nodes [0 ].signrawtransactionwithwallet (tx1 . serialize (). hex ( ))["hex" ]
93
104
tx1_id = self .nodes [0 ].sendrawtransaction (tx1_signed )
94
105
tx1_id = int (tx1_id , 16 )
95
106
@@ -102,13 +113,13 @@ def test_disable_flag(self):
102
113
tx2 .vout = [CTxOut (int (value - self .relayfee * COIN ), DUMMY_P2WPKH_SCRIPT )]
103
114
tx2 .rehash ()
104
115
105
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ].sendrawtransaction , ToHex ( tx2 ))
116
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ].sendrawtransaction , tx2 . serialize (). hex ( ))
106
117
107
118
# Setting the version back down to 1 should disable the sequence lock,
108
119
# so this should be accepted.
109
120
tx2 .nVersion = 1
110
121
111
- self .nodes [0 ].sendrawtransaction (ToHex ( tx2 ))
122
+ self .nodes [0 ].sendrawtransaction (tx2 . serialize (). hex ( ))
112
123
113
124
# Calculate the median time past of a prior block ("confirmations" before
114
125
# the current tip).
@@ -193,9 +204,9 @@ def test_sequence_lock_confirmed_inputs(self):
193
204
tx .vin .append (CTxIn (COutPoint (int (utxos [j ]["txid" ], 16 ), utxos [j ]["vout" ]), nSequence = sequence_value ))
194
205
value += utxos [j ]["amount" ]* COIN
195
206
# Overestimate the size of the tx - signatures should be less than 120 bytes, and leave 50 for the output
196
- tx_size = len (ToHex ( tx ))// 2 + 120 * num_inputs + 50
207
+ tx_size = len (tx . serialize (). hex ( ))// 2 + 120 * num_inputs + 50
197
208
tx .vout .append (CTxOut (int (value - self .relayfee * tx_size * COIN / 1000 ), DUMMY_P2WPKH_SCRIPT ))
198
- rawtx = self .nodes [0 ].signrawtransactionwithwallet (ToHex ( tx ))["hex" ]
209
+ rawtx = self .nodes [0 ].signrawtransactionwithwallet (tx . serialize (). hex ( ))["hex" ]
199
210
200
211
if (using_sequence_locks and not should_pass ):
201
212
# This transaction should be rejected
@@ -215,7 +226,7 @@ def test_sequence_lock_unconfirmed_inputs(self):
215
226
216
227
# Create a mempool tx.
217
228
txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 2 )
218
- tx1 = FromHex ( CTransaction (), self .nodes [0 ].getrawtransaction (txid ))
229
+ tx1 = tx_from_hex ( self .nodes [0 ].getrawtransaction (txid ))
219
230
tx1 .rehash ()
220
231
221
232
# Anyone-can-spend mempool tx.
@@ -224,8 +235,8 @@ def test_sequence_lock_unconfirmed_inputs(self):
224
235
tx2 .nVersion = 2
225
236
tx2 .vin = [CTxIn (COutPoint (tx1 .sha256 , 0 ), nSequence = 0 )]
226
237
tx2 .vout = [CTxOut (int (tx1 .vout [0 ].nValue - self .relayfee * COIN ), DUMMY_P2WPKH_SCRIPT )]
227
- tx2_raw = self .nodes [0 ].signrawtransactionwithwallet (ToHex ( tx2 ))["hex" ]
228
- tx2 = FromHex ( tx2 , tx2_raw )
238
+ tx2_raw = self .nodes [0 ].signrawtransactionwithwallet (tx2 . serialize (). hex ( ))["hex" ]
239
+ tx2 = tx_from_hex ( tx2_raw )
229
240
tx2 .rehash ()
230
241
231
242
self .nodes [0 ].sendrawtransaction (tx2_raw )
@@ -246,10 +257,10 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
246
257
247
258
if (orig_tx .hash in node .getrawmempool ()):
248
259
# sendrawtransaction should fail if the tx is in the mempool
249
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , node .sendrawtransaction , ToHex ( tx ))
260
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , node .sendrawtransaction , tx . serialize (). hex ( ))
250
261
else :
251
262
# sendrawtransaction should succeed if the tx is not in the mempool
252
- node .sendrawtransaction (ToHex ( tx ))
263
+ node .sendrawtransaction (tx . serialize (). hex ( ))
253
264
254
265
return tx
255
266
@@ -299,7 +310,7 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
299
310
utxos = self .nodes [0 ].listunspent ()
300
311
tx5 .vin .append (CTxIn (COutPoint (int (utxos [0 ]["txid" ], 16 ), utxos [0 ]["vout" ]), nSequence = 1 ))
301
312
tx5 .vout [0 ].nValue += int (utxos [0 ]["amount" ]* COIN )
302
- raw_tx5 = self .nodes [0 ].signrawtransactionwithwallet (ToHex ( tx5 ))["hex" ]
313
+ raw_tx5 = self .nodes [0 ].signrawtransactionwithwallet (tx5 . serialize (). hex ( ))["hex" ]
303
314
304
315
assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ].sendrawtransaction , raw_tx5 )
305
316
@@ -325,7 +336,7 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
325
336
block .rehash ()
326
337
block .solve ()
327
338
tip = block .sha256
328
- assert_equal (None if i == 1 else 'inconclusive' , self .nodes [0 ].submitblock (ToHex ( block )))
339
+ assert_equal (None if i == 1 else 'inconclusive' , self .nodes [0 ].submitblock (block . serialize (). hex ( )))
329
340
tmpl = self .nodes [0 ].getblocktemplate (NORMAL_GBT_REQUEST_PARAMS )
330
341
tmpl ['previousblockhash' ] = '%x' % tip
331
342
tmpl ['transactions' ] = []
@@ -348,7 +359,7 @@ def test_bip68_not_consensus(self):
348
359
assert not softfork_active (self .nodes [0 ], 'csv' )
349
360
txid = self .nodes [0 ].sendtoaddress (self .nodes [0 ].getnewaddress (), 2 )
350
361
351
- tx1 = FromHex ( CTransaction (), self .nodes [0 ].getrawtransaction (txid ))
362
+ tx1 = tx_from_hex ( self .nodes [0 ].getrawtransaction (txid ))
352
363
tx1 .rehash ()
353
364
354
365
# Make an anyone-can-spend transaction
@@ -358,11 +369,11 @@ def test_bip68_not_consensus(self):
358
369
tx2 .vout = [CTxOut (int (tx1 .vout [0 ].nValue - self .relayfee * COIN ), DUMMY_P2WPKH_SCRIPT )]
359
370
360
371
# sign tx2
361
- tx2_raw = self .nodes [0 ].signrawtransactionwithwallet (ToHex ( tx2 ))["hex" ]
362
- tx2 = FromHex ( tx2 , tx2_raw )
372
+ tx2_raw = self .nodes [0 ].signrawtransactionwithwallet (tx2 . serialize (). hex ( ))["hex" ]
373
+ tx2 = tx_from_hex ( tx2_raw )
363
374
tx2 .rehash ()
364
375
365
- self .nodes [0 ].sendrawtransaction (ToHex ( tx2 ))
376
+ self .nodes [0 ].sendrawtransaction (tx2 . serialize (). hex ( ))
366
377
367
378
# Now make an invalid spend of tx2 according to BIP68
368
379
sequence_value = 100 # 100 block relative locktime
@@ -373,7 +384,7 @@ def test_bip68_not_consensus(self):
373
384
tx3 .vout = [CTxOut (int (tx2 .vout [0 ].nValue - self .relayfee * COIN ), DUMMY_P2WPKH_SCRIPT )]
374
385
tx3 .rehash ()
375
386
376
- assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ].sendrawtransaction , ToHex ( tx3 ))
387
+ assert_raises_rpc_error (- 26 , NOT_FINAL_ERROR , self .nodes [0 ].sendrawtransaction , tx3 . serialize (). hex ( ))
377
388
378
389
# make a block that violates bip68; ensure that the tip updates
379
390
block = create_block (tmpl = self .nodes [0 ].getblocktemplate (NORMAL_GBT_REQUEST_PARAMS ))
@@ -404,9 +415,9 @@ def test_version2_relay(self):
404
415
outputs = { self .nodes [1 ].getnewaddress () : 1.0 }
405
416
rawtx = self .nodes [1 ].createrawtransaction (inputs , outputs )
406
417
rawtxfund = self .nodes [1 ].fundrawtransaction (rawtx )['hex' ]
407
- tx = FromHex ( CTransaction (), rawtxfund )
418
+ tx = tx_from_hex ( rawtxfund )
408
419
tx .nVersion = 2
409
- tx_signed = self .nodes [1 ].signrawtransactionwithwallet (ToHex ( tx ))["hex" ]
420
+ tx_signed = self .nodes [1 ].signrawtransactionwithwallet (tx . serialize (). hex ( ))["hex" ]
410
421
self .nodes [1 ].sendrawtransaction (tx_signed )
411
422
412
423
if __name__ == '__main__' :
0 commit comments