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
77from test_framework .test_framework import BitcoinTestFramework
8+ from test_framework .wallet import MiniWallet
89from 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 "
0 commit comments