Skip to content

Commit 387355b

Browse files
committed
test, refactor: rpc_rawtransaction PEP8
1 parent 7d5cec2 commit 387355b

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

test/functional/rpc_rawtransaction.py

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ def run_test(self):
7878
self.sync_all()
7979
self.nodes[0].generate(COINBASE_MATURITY + 1)
8080
self.sync_all()
81-
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.5)
82-
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),1.0)
83-
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(),5.0)
81+
for amount in [1.5, 1.0, 5.0]:
82+
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), amount)
8483
self.sync_all()
8584
self.nodes[0].generate(5)
8685
self.sync_all()
@@ -192,7 +191,8 @@ def createrawtransaction_tests(self):
192191
assert_raises_rpc_error(-1, "JSON value is not an object as expected", self.nodes[0].createrawtransaction, ['foo'], {})
193192
assert_raises_rpc_error(-1, "JSON value is not a string as expected", self.nodes[0].createrawtransaction, [{}], {})
194193
assert_raises_rpc_error(-8, "txid must be of length 64 (not 3, for 'foo')", self.nodes[0].createrawtransaction, [{'txid': 'foo'}], {})
195-
assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844')", self.nodes[0].createrawtransaction, [{'txid': 'ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844'}], {})
194+
txid = "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844"
195+
assert_raises_rpc_error(-8, f"txid must be hexadecimal string (not '{txid}')", self.nodes[0].createrawtransaction, [{'txid': txid}], {})
196196
assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': TXID}], {})
197197
assert_raises_rpc_error(-8, "Invalid parameter, missing vout key", self.nodes[0].createrawtransaction, [{'txid': TXID, 'vout': 'foo'}], {})
198198
assert_raises_rpc_error(-8, "Invalid parameter, vout cannot be negative", self.nodes[0].createrawtransaction, [{'txid': TXID, 'vout': -1}], {})
@@ -264,9 +264,9 @@ def signrawtransactionwithwallet_tests(self):
264264
addr = self.nodes[0].getnewaddress("", type)
265265
addrinfo = self.nodes[0].getaddressinfo(addr)
266266
pubkey = addrinfo["scriptPubKey"]
267-
inputs = [ {'txid' : TXID, 'vout' : 3, 'sequence' : 1000}]
268-
outputs = { self.nodes[0].getnewaddress() : 1 }
269-
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
267+
inputs = [{'txid': TXID, 'vout': 3, 'sequence': 1000}]
268+
outputs = {self.nodes[0].getnewaddress(): 1}
269+
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
270270

271271
prevtx = dict(txid=TXID, scriptPubKey=pubkey, vout=3, amount=1)
272272
succ = self.nodes[0].signrawtransactionwithwallet(rawtx, [prevtx])
@@ -309,23 +309,25 @@ def signrawtransactionwithwallet_tests(self):
309309

310310
def sendrawtransaction_tests(self):
311311
self.log.info("Test sendrawtransaction with missing input")
312-
inputs = [{'txid' : TXID, 'vout' : 1}] # won't exist
313-
outputs = { self.nodes[0].getnewaddress() : 4.998 }
314-
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
315-
rawtx = self.nodes[2].signrawtransactionwithwallet(rawtx)
312+
inputs = [{'txid': TXID, 'vout': 1}] # won't exist
313+
outputs = {self.nodes[0].getnewaddress(): 4.998}
314+
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
315+
rawtx = self.nodes[2].signrawtransactionwithwallet(rawtx)
316316
assert_raises_rpc_error(-25, "bad-txns-inputs-missingorspent", self.nodes[2].sendrawtransaction, rawtx['hex'])
317317

318318
def sendrawtransaction_testmempoolaccept_tests(self):
319319
self.log.info("Test sendrawtransaction/testmempoolaccept with maxfeerate")
320+
fee_exceeds_max = "Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)"
321+
320322
# Test a transaction with a small fee.
321323
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
322324
rawTx = self.nodes[0].getrawtransaction(txId, True)
323325
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
324326

325327
self.sync_all()
326-
inputs = [{ "txid" : txId, "vout" : vout['n'] }]
328+
inputs = [{"txid": txId, "vout": vout['n']}]
327329
# Fee 10,000 satoshis, (1 - (10000 sat * 0.00000001 BTC/sat)) = 0.9999
328-
outputs = { self.nodes[0].getnewaddress() : Decimal("0.99990000") }
330+
outputs = {self.nodes[0].getnewaddress(): Decimal("0.99990000")}
329331
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
330332
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
331333
assert_equal(rawTxSigned['complete'], True)
@@ -335,7 +337,7 @@ def sendrawtransaction_testmempoolaccept_tests(self):
335337
assert_equal(testres['allowed'], False)
336338
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
337339
# and sendrawtransaction should throw
338-
assert_raises_rpc_error(-25, 'Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)', self.nodes[2].sendrawtransaction, rawTxSigned['hex'], 0.00001000)
340+
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'], 0.00001000)
339341
# and the following calls should both succeed
340342
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']])[0]
341343
assert_equal(testres['allowed'], True)
@@ -347,9 +349,9 @@ def sendrawtransaction_testmempoolaccept_tests(self):
347349
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
348350

349351
self.sync_all()
350-
inputs = [{ "txid" : txId, "vout" : vout['n'] }]
352+
inputs = [{"txid": txId, "vout": vout['n']}]
351353
# Fee 2,000,000 satoshis, (1 - (2000000 sat * 0.00000001 BTC/sat)) = 0.98
352-
outputs = { self.nodes[0].getnewaddress() : Decimal("0.98000000") }
354+
outputs = {self.nodes[0].getnewaddress() : Decimal("0.98000000")}
353355
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
354356
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
355357
assert_equal(rawTxSigned['complete'], True)
@@ -359,7 +361,7 @@ def sendrawtransaction_testmempoolaccept_tests(self):
359361
assert_equal(testres['allowed'], False)
360362
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
361363
# and sendrawtransaction should throw
362-
assert_raises_rpc_error(-25, 'Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)', self.nodes[2].sendrawtransaction, rawTxSigned['hex'])
364+
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'])
363365
# and the following calls should both succeed
364366
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']], maxfeerate='0.20000000')[0]
365367
assert_equal(testres['allowed'], True)
@@ -378,20 +380,22 @@ def decoderawtransaction_tests(self):
378380
self.log.info("Test decoderawtransaction")
379381
# witness transaction
380382
encrawtx = "010000000001010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f50500000000000102616100000000"
381-
decrawtx = self.nodes[0].decoderawtransaction(encrawtx, True) # decode as witness transaction
383+
decrawtx = self.nodes[0].decoderawtransaction(encrawtx, True) # decode as witness transaction
382384
assert_equal(decrawtx['vout'][0]['value'], Decimal('1.00000000'))
383385
assert_raises_rpc_error(-22, 'TX decode failed', self.nodes[0].decoderawtransaction, encrawtx, False) # force decode as non-witness transaction
384386
# non-witness transaction
385387
encrawtx = "01000000010000000000000072c1a6a246ae63f74f931e8365e15a089c68d61900000000000000000000ffffffff0100e1f505000000000000000000"
386-
decrawtx = self.nodes[0].decoderawtransaction(encrawtx, False) # decode as non-witness transaction
388+
decrawtx = self.nodes[0].decoderawtransaction(encrawtx, False) # decode as non-witness transaction
387389
assert_equal(decrawtx['vout'][0]['value'], Decimal('1.00000000'))
388390
# known ambiguous transaction in the chain (see https://github.com/bitcoin/bitcoin/issues/20579)
389-
encrawtx = "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4b03c68708046ff8415c622f4254432e434f4d2ffabe6d6de1965d02c68f928e5b244ab1965115a36f56eb997633c7f690124bbf43644e23080000000ca3d3af6d005a65ff0200fd00000000ffffffff03f4c1fb4b0000000016001497cfc76442fe717f2a3f0cc9c175f7561b6619970000000000000000266a24aa21a9ed957d1036a80343e0d1b659497e1b48a38ebe876a056d45965fac4a85cda84e1900000000000000002952534b424c4f434b3a8e092581ab01986cbadc84f4b43f4fa4bb9e7a2e2a0caf9b7cf64d939028e22c0120000000000000000000000000000000000000000000000000000000000000000000000000"
391+
coinbase = "03c68708046ff8415c622f4254432e434f4d2ffabe6d6de1965d02c68f928e5b244ab1965115a36f56eb997633c7f690124bbf43644e23080000000ca3d3af6d005a65ff0200fd00000000"
392+
encrawtx = f"020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff4b{coinbase}" \
393+
"ffffffff03f4c1fb4b0000000016001497cfc76442fe717f2a3f0cc9c175f7561b6619970000000000000000266a24aa21a9ed957d1036a80343e0d1b659497e1b48a38ebe876a056d45965fac4a85cda84e1900000000000000002952534b424c4f434b3a8e092581ab01986cbadc84f4b43f4fa4bb9e7a2e2a0caf9b7cf64d939028e22c0120000000000000000000000000000000000000000000000000000000000000000000000000"
390394
decrawtx = self.nodes[0].decoderawtransaction(encrawtx)
391395
decrawtx_wit = self.nodes[0].decoderawtransaction(encrawtx, True)
392-
assert_raises_rpc_error(-22, 'TX decode failed', self.nodes[0].decoderawtransaction, encrawtx, False) # fails to decode as non-witness transaction
393-
assert_equal(decrawtx, decrawtx_wit) # the witness interpretation should be chosen
394-
assert_equal(decrawtx['vin'][0]['coinbase'], "03c68708046ff8415c622f4254432e434f4d2ffabe6d6de1965d02c68f928e5b244ab1965115a36f56eb997633c7f690124bbf43644e23080000000ca3d3af6d005a65ff0200fd00000000")
396+
assert_raises_rpc_error(-22, 'TX decode failed', self.nodes[0].decoderawtransaction, encrawtx, False) # fails to decode as non-witness transaction
397+
assert_equal(decrawtx, decrawtx_wit) # the witness interpretation should be chosen
398+
assert_equal(decrawtx['vin'][0]['coinbase'], coinbase)
395399

396400
def transaction_version_number_tests(self):
397401
self.log.info("Test transaction version numbers")
@@ -415,6 +419,7 @@ def raw_multisig_transaction_legacy_tests(self):
415419
self.log.info("Test raw multisig transactions (legacy)")
416420
# The traditional multisig workflow does not work with descriptor wallets so these are legacy only.
417421
# The multisig workflow with descriptor wallets uses PSBTs and is tested elsewhere, no need to do them here.
422+
418423
# 2of2 test
419424
addr1 = self.nodes[2].getnewaddress()
420425
addr2 = self.nodes[2].getnewaddress()
@@ -424,20 +429,23 @@ def raw_multisig_transaction_legacy_tests(self):
424429

425430
# Tests for createmultisig and addmultisigaddress
426431
assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 1, ["01020304"])
427-
self.nodes[0].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']]) # createmultisig can only take public keys
428-
assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 2, [addr1Obj['pubkey'], addr1]) # addmultisigaddress can take both pubkeys and addresses so long as they are in the wallet, which is tested here.
432+
# createmultisig can only take public keys
433+
self.nodes[0].createmultisig(2, [addr1Obj['pubkey'], addr2Obj['pubkey']])
434+
# addmultisigaddress can take both pubkeys and addresses so long as they are in the wallet, which is tested here
435+
assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 2, [addr1Obj['pubkey'], addr1])
429436

430437
mSigObj = self.nodes[2].addmultisigaddress(2, [addr1Obj['pubkey'], addr1])['address']
431438

432-
#use balance deltas instead of absolute values
439+
# use balance deltas instead of absolute values
433440
bal = self.nodes[2].getbalance()
434441

435442
# send 1.2 BTC to msig adr
436443
txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
437444
self.sync_all()
438445
self.nodes[0].generate(1)
439446
self.sync_all()
440-
assert_equal(self.nodes[2].getbalance(), bal+Decimal('1.20000000')) #node2 has both keys of the 2of2 ms addr., tx should affect the balance
447+
# node2 has both keys of the 2of2 ms addr, tx should affect the balance
448+
assert_equal(self.nodes[2].getbalance(), bal + Decimal('1.20000000'))
441449

442450

443451
# 2of3 test from different nodes
@@ -459,29 +467,29 @@ def raw_multisig_transaction_legacy_tests(self):
459467
self.nodes[0].generate(1)
460468
self.sync_all()
461469

462-
#THIS IS AN INCOMPLETE FEATURE
463-
#NODE2 HAS TWO OF THREE KEY AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
464-
assert_equal(self.nodes[2].getbalance(), bal) #for now, assume the funds of a 2of3 multisig tx are not marked as spendable
470+
# THIS IS AN INCOMPLETE FEATURE
471+
# NODE2 HAS TWO OF THREE KEYS AND THE FUNDS SHOULD BE SPENDABLE AND COUNT AT BALANCE CALCULATION
472+
assert_equal(self.nodes[2].getbalance(), bal) # for now, assume the funds of a 2of3 multisig tx are not marked as spendable
465473

466474
txDetails = self.nodes[0].gettransaction(txId, True)
467475
rawTx = self.nodes[0].decoderawtransaction(txDetails['hex'])
468476
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('2.20000000'))
469477

470478
bal = self.nodes[0].getbalance()
471-
inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "amount" : vout['value']}]
472-
outputs = { self.nodes[0].getnewaddress() : 2.19 }
479+
inputs = [{"txid": txId, "vout": vout['n'], "scriptPubKey": vout['scriptPubKey']['hex'], "amount": vout['value']}]
480+
outputs = {self.nodes[0].getnewaddress(): 2.19}
473481
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
474482
rawTxPartialSigned = self.nodes[1].signrawtransactionwithwallet(rawTx, inputs)
475-
assert_equal(rawTxPartialSigned['complete'], False) #node1 only has one key, can't comp. sign the tx
483+
assert_equal(rawTxPartialSigned['complete'], False) # node1 only has one key, can't comp. sign the tx
476484

477485
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx, inputs)
478-
assert_equal(rawTxSigned['complete'], True) #node2 can sign the tx compl., own two of three keys
486+
assert_equal(rawTxSigned['complete'], True) # node2 can sign the tx compl., own two of three keys
479487
self.nodes[2].sendrawtransaction(rawTxSigned['hex'])
480488
rawTx = self.nodes[0].decoderawtransaction(rawTxSigned['hex'])
481489
self.sync_all()
482490
self.nodes[0].generate(1)
483491
self.sync_all()
484-
assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
492+
assert_equal(self.nodes[0].getbalance(), bal + Decimal('50.00000000') + Decimal('2.19000000')) # block reward + tx
485493

486494
# 2of2 test for combining transactions
487495
bal = self.nodes[2].getbalance()
@@ -502,31 +510,31 @@ def raw_multisig_transaction_legacy_tests(self):
502510
self.nodes[0].generate(1)
503511
self.sync_all()
504512

505-
assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable
513+
assert_equal(self.nodes[2].getbalance(), bal) # the funds of a 2of2 multisig tx should not be marked as spendable
506514

507515
txDetails = self.nodes[0].gettransaction(txId, True)
508516
rawTx2 = self.nodes[0].decoderawtransaction(txDetails['hex'])
509517
vout = next(o for o in rawTx2['vout'] if o['value'] == Decimal('2.20000000'))
510518

511519
bal = self.nodes[0].getbalance()
512-
inputs = [{ "txid" : txId, "vout" : vout['n'], "scriptPubKey" : vout['scriptPubKey']['hex'], "redeemScript" : mSigObjValid['hex'], "amount" : vout['value']}]
513-
outputs = { self.nodes[0].getnewaddress() : 2.19 }
520+
inputs = [{"txid": txId, "vout": vout['n'], "scriptPubKey": vout['scriptPubKey']['hex'], "redeemScript": mSigObjValid['hex'], "amount": vout['value']}]
521+
outputs = {self.nodes[0].getnewaddress(): 2.19}
514522
rawTx2 = self.nodes[2].createrawtransaction(inputs, outputs)
515523
rawTxPartialSigned1 = self.nodes[1].signrawtransactionwithwallet(rawTx2, inputs)
516524
self.log.debug(rawTxPartialSigned1)
517-
assert_equal(rawTxPartialSigned1['complete'], False) #node1 only has one key, can't comp. sign the tx
525+
assert_equal(rawTxPartialSigned1['complete'], False) # node1 only has one key, can't comp. sign the tx
518526

519527
rawTxPartialSigned2 = self.nodes[2].signrawtransactionwithwallet(rawTx2, inputs)
520528
self.log.debug(rawTxPartialSigned2)
521-
assert_equal(rawTxPartialSigned2['complete'], False) #node2 only has one key, can't comp. sign the tx
529+
assert_equal(rawTxPartialSigned2['complete'], False) # node2 only has one key, can't comp. sign the tx
522530
rawTxComb = self.nodes[2].combinerawtransaction([rawTxPartialSigned1['hex'], rawTxPartialSigned2['hex']])
523531
self.log.debug(rawTxComb)
524532
self.nodes[2].sendrawtransaction(rawTxComb)
525533
rawTx2 = self.nodes[0].decoderawtransaction(rawTxComb)
526534
self.sync_all()
527535
self.nodes[0].generate(1)
528536
self.sync_all()
529-
assert_equal(self.nodes[0].getbalance(), bal+Decimal('50.00000000')+Decimal('2.19000000')) #block reward + tx
537+
assert_equal(self.nodes[0].getbalance(), bal + Decimal('50.00000000') + Decimal('2.19000000')) # block reward + tx
530538

531539

532540
if __name__ == '__main__':

0 commit comments

Comments
 (0)