Skip to content

Commit 9745e18

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24670: test: move-only: Move all generate* tests to a single file
0000ff0 test: move-only: Move all generate* tests to a single file (MarcoFalke) Pull request description: Seems a bit overkill to spread tests for the `generate*` methods over several files. Combining them into a single file has also a nice side-effect of requiring less node (re)starts, which are expensive in valgrind. ACKs for top commit: glozow: utACK 0000ff0 Tree-SHA512: 8269eb05649a871011bbfbd1838d0f7d1dac4a35b3b198fc43fe85131fda8a53803b75da78cbf422eabf086006dee4421e622fbe706f6781a3848b989024001b
2 parents 7878c86 + 0000ff0 commit 9745e18

File tree

4 files changed

+91
-107
lines changed

4 files changed

+91
-107
lines changed

test/functional/rpc_generate.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
# Copyright (c) 2020-2021 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test generate RPC."""
5+
"""Test generate* RPCs."""
66

77
from test_framework.test_framework import BitcoinTestFramework
8+
from test_framework.wallet import MiniWallet
89
from test_framework.util import (
910
assert_equal,
1011
assert_raises_rpc_error,
@@ -16,6 +17,94 @@ def set_test_params(self):
1617
self.num_nodes = 1
1718

1819
def run_test(self):
20+
self.test_generatetoaddress()
21+
self.test_generate()
22+
self.test_generateblock()
23+
24+
def test_generatetoaddress(self):
25+
self.generatetoaddress(self.nodes[0], 1, 'mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ')
26+
assert_raises_rpc_error(-5, "Invalid address", self.generatetoaddress, self.nodes[0], 1, '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy')
27+
28+
def test_generateblock(self):
29+
node = self.nodes[0]
30+
miniwallet = MiniWallet(node)
31+
miniwallet.rescan_utxos()
32+
33+
self.log.info('Generate an empty block to address')
34+
address = miniwallet.get_address()
35+
hash = self.generateblock(node, output=address, transactions=[])['hash']
36+
block = node.getblock(blockhash=hash, verbose=2)
37+
assert_equal(len(block['tx']), 1)
38+
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['address'], address)
39+
40+
self.log.info('Generate an empty block to a descriptor')
41+
hash = self.generateblock(node, 'addr(' + address + ')', [])['hash']
42+
block = node.getblock(blockhash=hash, verbosity=2)
43+
assert_equal(len(block['tx']), 1)
44+
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['address'], address)
45+
46+
self.log.info('Generate an empty block to a combo descriptor with compressed pubkey')
47+
combo_key = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'
48+
combo_address = 'bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kygt080'
49+
hash = self.generateblock(node, 'combo(' + combo_key + ')', [])['hash']
50+
block = node.getblock(hash, 2)
51+
assert_equal(len(block['tx']), 1)
52+
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['address'], combo_address)
53+
54+
self.log.info('Generate an empty block to a combo descriptor with uncompressed pubkey')
55+
combo_key = '0408ef68c46d20596cc3f6ddf7c8794f71913add807f1dc55949fa805d764d191c0b7ce6894c126fce0babc6663042f3dde9b0cf76467ea315514e5a6731149c67'
56+
combo_address = 'mkc9STceoCcjoXEXe6cm66iJbmjM6zR9B2'
57+
hash = self.generateblock(node, 'combo(' + combo_key + ')', [])['hash']
58+
block = node.getblock(hash, 2)
59+
assert_equal(len(block['tx']), 1)
60+
assert_equal(block['tx'][0]['vout'][0]['scriptPubKey']['address'], combo_address)
61+
62+
# Generate some extra mempool transactions to verify they don't get mined
63+
for _ in range(10):
64+
miniwallet.send_self_transfer(from_node=node)
65+
66+
self.log.info('Generate block with txid')
67+
txid = miniwallet.send_self_transfer(from_node=node)['txid']
68+
hash = self.generateblock(node, address, [txid])['hash']
69+
block = node.getblock(hash, 1)
70+
assert_equal(len(block['tx']), 2)
71+
assert_equal(block['tx'][1], txid)
72+
73+
self.log.info('Generate block with raw tx')
74+
rawtx = miniwallet.create_self_transfer()['hex']
75+
hash = self.generateblock(node, address, [rawtx])['hash']
76+
77+
block = node.getblock(hash, 1)
78+
assert_equal(len(block['tx']), 2)
79+
txid = block['tx'][1]
80+
assert_equal(node.getrawtransaction(txid=txid, verbose=False, blockhash=hash), rawtx)
81+
82+
self.log.info('Fail to generate block with out of order txs')
83+
txid1 = miniwallet.send_self_transfer(from_node=node)['txid']
84+
utxo1 = miniwallet.get_utxo(txid=txid1)
85+
rawtx2 = miniwallet.create_self_transfer(utxo_to_spend=utxo1)['hex']
86+
assert_raises_rpc_error(-25, 'TestBlockValidity failed: bad-txns-inputs-missingorspent', self.generateblock, node, address, [rawtx2, txid1])
87+
88+
self.log.info('Fail to generate block with txid not in mempool')
89+
missing_txid = '0000000000000000000000000000000000000000000000000000000000000000'
90+
assert_raises_rpc_error(-5, 'Transaction ' + missing_txid + ' not in mempool.', self.generateblock, node, address, [missing_txid])
91+
92+
self.log.info('Fail to generate block with invalid raw tx')
93+
invalid_raw_tx = '0000'
94+
assert_raises_rpc_error(-22, 'Transaction decode failed for ' + invalid_raw_tx, self.generateblock, node, address, [invalid_raw_tx])
95+
96+
self.log.info('Fail to generate block with invalid address/descriptor')
97+
assert_raises_rpc_error(-5, 'Invalid address or descriptor', self.generateblock, node, '1234', [])
98+
99+
self.log.info('Fail to generate block with a ranged descriptor')
100+
ranged_descriptor = 'pkh(tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp/0/*)'
101+
assert_raises_rpc_error(-8, 'Ranged descriptor not accepted. Maybe pass through deriveaddresses first?', self.generateblock, node, ranged_descriptor, [])
102+
103+
self.log.info('Fail to generate block with a descriptor missing a private key')
104+
child_descriptor = 'pkh(tpubD6NzVbkrYhZ4XgiXtGrdW5XDAPFCL9h7we1vwNCpn8tGbBcgfVYjXyhWo4E1xkh56hjod1RhGjxbaTLV3X4FyWuejifB9jusQ46QzG87VKp/0\'/0)'
105+
assert_raises_rpc_error(-5, 'Cannot derive script without private keys', self.generateblock, node, child_descriptor, [])
106+
107+
def test_generate(self):
19108
message = (
20109
"generate\n\n"
21110
"has been replaced by the -generate "

test/functional/rpc_generateblock.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

test/functional/test_runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@
235235
'p2p_eviction.py',
236236
'wallet_signmessagewithaddress.py',
237237
'rpc_signmessagewithprivkey.py',
238-
'rpc_generateblock.py',
239238
'rpc_generate.py',
240239
'wallet_balance.py --legacy-wallet',
241240
'wallet_balance.py --descriptors',

test/functional/wallet_disable.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def run_test (self):
2626
x = self.nodes[0].validateaddress('mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ')
2727
assert x['isvalid'] == True
2828

29-
# Checking mining to an address without a wallet. Generating to a valid address should succeed
30-
# but generating to an invalid address will fail.
31-
self.generatetoaddress(self.nodes[0], 1, 'mneYUmWYsuk7kySiURxCi3AGxrAqZxLgPZ')
32-
assert_raises_rpc_error(-5, "Invalid address", self.generatetoaddress, self.nodes[0], 1, '3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy')
3329

3430
if __name__ == '__main__':
35-
DisableWalletTest ().main ()
31+
DisableWalletTest().main()

0 commit comments

Comments
 (0)