mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-19 05:45:05 +01:00
test: Use MiniWallet in rpc_rawtransaction.py
This test was previously run twice, once with `--legacy-wallet` and once with `--descriptors`. Now we run it only with `--legacy-wallet`, as all the tests has been ported to the MiniWallet but `raw_multisig_transaction_legacy_tests`, which can be run only with the legacy wallet. We also decrease the number of nodes used from 4 to 3, making the test run slightly faster.
This commit is contained in:
parent
e93046c10b
commit
e8959000b6
@ -24,7 +24,10 @@ from test_framework.test_framework import BitcoinTestFramework
|
|||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
assert_equal,
|
assert_equal,
|
||||||
assert_raises_rpc_error,
|
assert_raises_rpc_error,
|
||||||
find_vout_for_address,
|
)
|
||||||
|
from test_framework.wallet import (
|
||||||
|
getnewdestination,
|
||||||
|
MiniWallet,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -52,9 +55,8 @@ class multidict(dict):
|
|||||||
class RawTransactionsTest(BitcoinTestFramework):
|
class RawTransactionsTest(BitcoinTestFramework):
|
||||||
def set_test_params(self):
|
def set_test_params(self):
|
||||||
self.setup_clean_chain = True
|
self.setup_clean_chain = True
|
||||||
self.num_nodes = 4
|
self.num_nodes = 3
|
||||||
self.extra_args = [
|
self.extra_args = [
|
||||||
["-txindex"],
|
|
||||||
["-txindex"],
|
["-txindex"],
|
||||||
["-txindex"],
|
["-txindex"],
|
||||||
[],
|
[],
|
||||||
@ -62,24 +64,19 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
# whitelist all peers to speed up tx relay / mempool sync
|
# whitelist all peers to speed up tx relay / mempool sync
|
||||||
for args in self.extra_args:
|
for args in self.extra_args:
|
||||||
args.append("-whitelist=noban@127.0.0.1")
|
args.append("-whitelist=noban@127.0.0.1")
|
||||||
|
self.requires_wallet = self.is_specified_wallet_compiled()
|
||||||
|
|
||||||
self.supports_cli = False
|
self.supports_cli = False
|
||||||
|
|
||||||
def skip_test_if_missing_module(self):
|
|
||||||
self.skip_if_no_wallet()
|
|
||||||
|
|
||||||
def setup_network(self):
|
def setup_network(self):
|
||||||
super().setup_network()
|
super().setup_network()
|
||||||
self.connect_nodes(0, 2)
|
self.connect_nodes(0, 2)
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
|
self.wallet = MiniWallet(self.nodes[0])
|
||||||
self.log.info("Prepare some coins for multiple *rawtransaction commands")
|
self.log.info("Prepare some coins for multiple *rawtransaction commands")
|
||||||
self.generate(self.nodes[2], 1)
|
self.generate(self.wallet, 10)
|
||||||
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
||||||
for amount in [1.5, 1.0, 5.0]:
|
|
||||||
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), amount)
|
|
||||||
self.sync_all()
|
|
||||||
self.generate(self.nodes[0], 5)
|
|
||||||
|
|
||||||
self.getrawtransaction_tests()
|
self.getrawtransaction_tests()
|
||||||
self.createrawtransaction_tests()
|
self.createrawtransaction_tests()
|
||||||
@ -87,43 +84,38 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
self.sendrawtransaction_testmempoolaccept_tests()
|
self.sendrawtransaction_testmempoolaccept_tests()
|
||||||
self.decoderawtransaction_tests()
|
self.decoderawtransaction_tests()
|
||||||
self.transaction_version_number_tests()
|
self.transaction_version_number_tests()
|
||||||
if not self.options.descriptors:
|
if self.requires_wallet and not self.options.descriptors:
|
||||||
self.raw_multisig_transaction_legacy_tests()
|
self.raw_multisig_transaction_legacy_tests()
|
||||||
|
|
||||||
def getrawtransaction_tests(self):
|
def getrawtransaction_tests(self):
|
||||||
addr = self.nodes[1].getnewaddress()
|
tx = self.wallet.send_self_transfer(from_node=self.nodes[0])
|
||||||
txid = self.nodes[0].sendtoaddress(addr, 10)
|
|
||||||
self.generate(self.nodes[0], 1)
|
self.generate(self.nodes[0], 1)
|
||||||
vout = find_vout_for_address(self.nodes[1], txid, addr)
|
txId = tx['txid']
|
||||||
rawTx = self.nodes[1].createrawtransaction([{'txid': txid, 'vout': vout}], {self.nodes[1].getnewaddress(): 9.999})
|
|
||||||
rawTxSigned = self.nodes[1].signrawtransactionwithwallet(rawTx)
|
|
||||||
txId = self.nodes[1].sendrawtransaction(rawTxSigned['hex'])
|
|
||||||
self.generateblock(self.nodes[0], output=self.nodes[0].getnewaddress(), transactions=[rawTxSigned['hex']])
|
|
||||||
err_msg = (
|
err_msg = (
|
||||||
"No such mempool transaction. Use -txindex or provide a block hash to enable"
|
"No such mempool transaction. Use -txindex or provide a block hash to enable"
|
||||||
" blockchain transaction queries. Use gettransaction for wallet transactions."
|
" blockchain transaction queries. Use gettransaction for wallet transactions."
|
||||||
)
|
)
|
||||||
|
|
||||||
for n in [0, 3]:
|
for n in [0, 2]:
|
||||||
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex")
|
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex")
|
||||||
|
|
||||||
if n == 0:
|
if n == 0:
|
||||||
# With -txindex.
|
# With -txindex.
|
||||||
# 1. valid parameters - only supply txid
|
# 1. valid parameters - only supply txid
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId), rawTxSigned['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId), tx['hex'])
|
||||||
|
|
||||||
# 2. valid parameters - supply txid and 0 for non-verbose
|
# 2. valid parameters - supply txid and 0 for non-verbose
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId, 0), rawTxSigned['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId, 0), tx['hex'])
|
||||||
|
|
||||||
# 3. valid parameters - supply txid and False for non-verbose
|
# 3. valid parameters - supply txid and False for non-verbose
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId, False), rawTxSigned['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId, False), tx['hex'])
|
||||||
|
|
||||||
# 4. valid parameters - supply txid and 1 for verbose.
|
# 4. valid parameters - supply txid and 1 for verbose.
|
||||||
# We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
|
# We only check the "hex" field of the output so we don't need to update this test every time the output format changes.
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId, 1)["hex"], rawTxSigned['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId, 1)["hex"], tx['hex'])
|
||||||
|
|
||||||
# 5. valid parameters - supply txid and True for non-verbose
|
# 5. valid parameters - supply txid and True for non-verbose
|
||||||
assert_equal(self.nodes[n].getrawtransaction(txId, True)["hex"], rawTxSigned['hex'])
|
assert_equal(self.nodes[n].getrawtransaction(txId, True)["hex"], tx['hex'])
|
||||||
else:
|
else:
|
||||||
# Without -txindex, expect to raise.
|
# Without -txindex, expect to raise.
|
||||||
for verbose in [None, 0, False, 1, True]:
|
for verbose in [None, 0, False, 1, True]:
|
||||||
@ -140,9 +132,9 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txId, {})
|
assert_raises_rpc_error(-1, "not a boolean", self.nodes[n].getrawtransaction, txId, {})
|
||||||
|
|
||||||
# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
|
# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
|
||||||
tx = self.nodes[2].sendtoaddress(self.nodes[1].getnewaddress(), 1)
|
tx = self.wallet.send_self_transfer(from_node=self.nodes[2])['txid']
|
||||||
block1, block2 = self.generate(self.nodes[2], 2)
|
block1, block2 = self.generate(self.nodes[2], 2)
|
||||||
for n in [0, 3]:
|
for n in [0, 2]:
|
||||||
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex, with blockhash")
|
self.log.info(f"Test getrawtransaction {'with' if n == 0 else 'without'} -txindex, with blockhash")
|
||||||
# We should be able to get the raw transaction by providing the correct block
|
# We should be able to get the raw transaction by providing the correct block
|
||||||
gottx = self.nodes[n].getrawtransaction(txid=tx, verbose=True, blockhash=block1)
|
gottx = self.nodes[n].getrawtransaction(txid=tx, verbose=True, blockhash=block1)
|
||||||
@ -199,20 +191,21 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
# sequence number out of range
|
# sequence number out of range
|
||||||
for invalid_seq in [-1, 4294967296]:
|
for invalid_seq in [-1, 4294967296]:
|
||||||
inputs = [{'txid': TXID, 'vout': 1, 'sequence': invalid_seq}]
|
inputs = [{'txid': TXID, 'vout': 1, 'sequence': invalid_seq}]
|
||||||
outputs = {self.nodes[0].getnewaddress(): 1}
|
address = getnewdestination()[2]
|
||||||
|
outputs = {address: 1}
|
||||||
assert_raises_rpc_error(-8, 'Invalid parameter, sequence number is out of range',
|
assert_raises_rpc_error(-8, 'Invalid parameter, sequence number is out of range',
|
||||||
self.nodes[0].createrawtransaction, inputs, outputs)
|
self.nodes[0].createrawtransaction, inputs, outputs)
|
||||||
# with valid sequence number
|
# with valid sequence number
|
||||||
for valid_seq in [1000, 4294967294]:
|
for valid_seq in [1000, 4294967294]:
|
||||||
inputs = [{'txid': TXID, 'vout': 1, 'sequence': valid_seq}]
|
inputs = [{'txid': TXID, 'vout': 1, 'sequence': valid_seq}]
|
||||||
outputs = {self.nodes[0].getnewaddress(): 1}
|
address = getnewdestination()[2]
|
||||||
|
outputs = {address: 1}
|
||||||
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
|
||||||
decrawtx = self.nodes[0].decoderawtransaction(rawtx)
|
decrawtx = self.nodes[0].decoderawtransaction(rawtx)
|
||||||
assert_equal(decrawtx['vin'][0]['sequence'], valid_seq)
|
assert_equal(decrawtx['vin'][0]['sequence'], valid_seq)
|
||||||
|
|
||||||
# Test `createrawtransaction` invalid `outputs`
|
# Test `createrawtransaction` invalid `outputs`
|
||||||
address = self.nodes[0].getnewaddress()
|
address = getnewdestination()[2]
|
||||||
address2 = self.nodes[0].getnewaddress()
|
|
||||||
assert_raises_rpc_error(-1, "JSON value is not an array as expected", self.nodes[0].createrawtransaction, [], 'foo')
|
assert_raises_rpc_error(-1, "JSON value is not an array as expected", self.nodes[0].createrawtransaction, [], 'foo')
|
||||||
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
|
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
|
||||||
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
|
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
|
||||||
@ -244,6 +237,7 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=[{address: 99}]),
|
self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=[{address: 99}]),
|
||||||
)
|
)
|
||||||
# Two outputs
|
# Two outputs
|
||||||
|
address2 = getnewdestination()[2]
|
||||||
tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=OrderedDict([(address, 99), (address2, 99)])))
|
tx = tx_from_hex(self.nodes[2].createrawtransaction(inputs=[{'txid': TXID, 'vout': 9}], outputs=OrderedDict([(address, 99), (address2, 99)])))
|
||||||
assert_equal(len(tx.vout), 2)
|
assert_equal(len(tx.vout), 2)
|
||||||
assert_equal(
|
assert_equal(
|
||||||
@ -261,70 +255,50 @@ class RawTransactionsTest(BitcoinTestFramework):
|
|||||||
def sendrawtransaction_tests(self):
|
def sendrawtransaction_tests(self):
|
||||||
self.log.info("Test sendrawtransaction with missing input")
|
self.log.info("Test sendrawtransaction with missing input")
|
||||||
inputs = [{'txid': TXID, 'vout': 1}] # won't exist
|
inputs = [{'txid': TXID, 'vout': 1}] # won't exist
|
||||||
outputs = {self.nodes[0].getnewaddress(): 4.998}
|
address = getnewdestination()[2]
|
||||||
|
outputs = {address: 4.998}
|
||||||
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
|
||||||
rawtx = self.nodes[2].signrawtransactionwithwallet(rawtx)
|
assert_raises_rpc_error(-25, "bad-txns-inputs-missingorspent", self.nodes[2].sendrawtransaction, rawtx)
|
||||||
assert_raises_rpc_error(-25, "bad-txns-inputs-missingorspent", self.nodes[2].sendrawtransaction, rawtx['hex'])
|
|
||||||
|
|
||||||
def sendrawtransaction_testmempoolaccept_tests(self):
|
def sendrawtransaction_testmempoolaccept_tests(self):
|
||||||
self.log.info("Test sendrawtransaction/testmempoolaccept with maxfeerate")
|
self.log.info("Test sendrawtransaction/testmempoolaccept with maxfeerate")
|
||||||
fee_exceeds_max = "Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)"
|
fee_exceeds_max = "Fee exceeds maximum configured by user (e.g. -maxtxfee, maxfeerate)"
|
||||||
|
|
||||||
# Test a transaction with a small fee.
|
# Test a transaction with a small fee.
|
||||||
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
|
# Fee rate is 0.00100000 BTC/kvB
|
||||||
rawTx = self.nodes[0].getrawtransaction(txId, True)
|
tx = self.wallet.create_self_transfer(fee_rate=Decimal('0.00100000'))
|
||||||
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
|
|
||||||
|
|
||||||
self.sync_all()
|
|
||||||
inputs = [{"txid": txId, "vout": vout['n']}]
|
|
||||||
# Fee 10,000 satoshis, (1 - (10000 sat * 0.00000001 BTC/sat)) = 0.9999
|
|
||||||
outputs = {self.nodes[0].getnewaddress(): Decimal("0.99990000")}
|
|
||||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
||||||
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
|
|
||||||
assert_equal(rawTxSigned['complete'], True)
|
|
||||||
# Fee 10,000 satoshis, ~100 b transaction, fee rate should land around 100 sat/byte = 0.00100000 BTC/kB
|
|
||||||
# Thus, testmempoolaccept should reject
|
# Thus, testmempoolaccept should reject
|
||||||
testres = self.nodes[2].testmempoolaccept([rawTxSigned['hex']], 0.00001000)[0]
|
testres = self.nodes[2].testmempoolaccept([tx['hex']], 0.00001000)[0]
|
||||||
assert_equal(testres['allowed'], False)
|
assert_equal(testres['allowed'], False)
|
||||||
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
|
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
|
||||||
# and sendrawtransaction should throw
|
# and sendrawtransaction should throw
|
||||||
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'], 0.00001000)
|
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, tx['hex'], 0.00001000)
|
||||||
# and the following calls should both succeed
|
# and the following calls should both succeed
|
||||||
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']])[0]
|
testres = self.nodes[2].testmempoolaccept(rawtxs=[tx['hex']])[0]
|
||||||
assert_equal(testres['allowed'], True)
|
assert_equal(testres['allowed'], True)
|
||||||
self.nodes[2].sendrawtransaction(hexstring=rawTxSigned['hex'])
|
self.nodes[2].sendrawtransaction(hexstring=tx['hex'])
|
||||||
|
|
||||||
# Test a transaction with a large fee.
|
# Test a transaction with a large fee.
|
||||||
txId = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 1.0)
|
# Fee rate is 0.20000000 BTC/kvB
|
||||||
rawTx = self.nodes[0].getrawtransaction(txId, True)
|
tx = self.wallet.create_self_transfer(mempool_valid=False, from_node=self.nodes[0], fee_rate=Decimal('0.20000000'))
|
||||||
vout = next(o for o in rawTx['vout'] if o['value'] == Decimal('1.00000000'))
|
|
||||||
|
|
||||||
self.sync_all()
|
|
||||||
inputs = [{"txid": txId, "vout": vout['n']}]
|
|
||||||
# Fee 2,000,000 satoshis, (1 - (2000000 sat * 0.00000001 BTC/sat)) = 0.98
|
|
||||||
outputs = {self.nodes[0].getnewaddress() : Decimal("0.98000000")}
|
|
||||||
rawTx = self.nodes[2].createrawtransaction(inputs, outputs)
|
|
||||||
rawTxSigned = self.nodes[2].signrawtransactionwithwallet(rawTx)
|
|
||||||
assert_equal(rawTxSigned['complete'], True)
|
|
||||||
# Fee 2,000,000 satoshis, ~100 b transaction, fee rate should land around 20,000 sat/byte = 0.20000000 BTC/kB
|
|
||||||
# Thus, testmempoolaccept should reject
|
# Thus, testmempoolaccept should reject
|
||||||
testres = self.nodes[2].testmempoolaccept([rawTxSigned['hex']])[0]
|
testres = self.nodes[2].testmempoolaccept([tx['hex']])[0]
|
||||||
assert_equal(testres['allowed'], False)
|
assert_equal(testres['allowed'], False)
|
||||||
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
|
assert_equal(testres['reject-reason'], 'max-fee-exceeded')
|
||||||
# and sendrawtransaction should throw
|
# and sendrawtransaction should throw
|
||||||
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, rawTxSigned['hex'])
|
assert_raises_rpc_error(-25, fee_exceeds_max, self.nodes[2].sendrawtransaction, tx['hex'])
|
||||||
# and the following calls should both succeed
|
# and the following calls should both succeed
|
||||||
testres = self.nodes[2].testmempoolaccept(rawtxs=[rawTxSigned['hex']], maxfeerate='0.20000000')[0]
|
testres = self.nodes[2].testmempoolaccept(rawtxs=[tx['hex']], maxfeerate='0.20000000')[0]
|
||||||
assert_equal(testres['allowed'], True)
|
assert_equal(testres['allowed'], True)
|
||||||
self.nodes[2].sendrawtransaction(hexstring=rawTxSigned['hex'], maxfeerate='0.20000000')
|
self.nodes[2].sendrawtransaction(hexstring=tx['hex'], maxfeerate='0.20000000')
|
||||||
|
|
||||||
self.log.info("Test sendrawtransaction/testmempoolaccept with tx already in the chain")
|
self.log.info("Test sendrawtransaction/testmempoolaccept with tx already in the chain")
|
||||||
self.generate(self.nodes[2], 1)
|
self.generate(self.nodes[2], 1)
|
||||||
for node in self.nodes:
|
for node in self.nodes:
|
||||||
testres = node.testmempoolaccept([rawTxSigned['hex']])[0]
|
testres = node.testmempoolaccept([tx['hex']])[0]
|
||||||
assert_equal(testres['allowed'], False)
|
assert_equal(testres['allowed'], False)
|
||||||
assert_equal(testres['reject-reason'], 'txn-already-known')
|
assert_equal(testres['reject-reason'], 'txn-already-known')
|
||||||
assert_raises_rpc_error(-27, 'Transaction already in block chain', node.sendrawtransaction, rawTxSigned['hex'])
|
assert_raises_rpc_error(-27, 'Transaction already in block chain', node.sendrawtransaction, tx['hex'])
|
||||||
|
|
||||||
def decoderawtransaction_tests(self):
|
def decoderawtransaction_tests(self):
|
||||||
self.log.info("Test decoderawtransaction")
|
self.log.info("Test decoderawtransaction")
|
||||||
|
@ -183,7 +183,6 @@ BASE_SCRIPTS = [
|
|||||||
'rpc_signrawtransaction.py --legacy-wallet',
|
'rpc_signrawtransaction.py --legacy-wallet',
|
||||||
'rpc_signrawtransaction.py --descriptors',
|
'rpc_signrawtransaction.py --descriptors',
|
||||||
'rpc_rawtransaction.py --legacy-wallet',
|
'rpc_rawtransaction.py --legacy-wallet',
|
||||||
'rpc_rawtransaction.py --descriptors',
|
|
||||||
'wallet_groups.py --legacy-wallet',
|
'wallet_groups.py --legacy-wallet',
|
||||||
'wallet_transactiontime_rescan.py --descriptors',
|
'wallet_transactiontime_rescan.py --descriptors',
|
||||||
'wallet_transactiontime_rescan.py --legacy-wallet',
|
'wallet_transactiontime_rescan.py --legacy-wallet',
|
||||||
|
Loading…
Reference in New Issue
Block a user