Skip to content

Commit 3668bb3

Browse files
author
MarcoFalke
committed
Merge #14468: [wallet] Deprecate generate RPC method
ab9aca2 [rpc] add 'getnewaddress' hint to 'generatetoaddress' help text. (John Newbery) c9f0295 [wallet] Deprecate the generate RPC method (John Newbery) aab8172 [tests] Add generate method to TestNode (John Newbery) c269209 [tests] Small fixups before deprecating generate (John Newbery) Pull request description: Deprecates the `generate` RPC method. For concept discussion, see #14299. Fixes #14299. Tree-SHA512: 16a3b8b742932e4f0476c06b23de07a34d9d215b41d9272c1c9d1e39966b0c2406f17c5ab3cc568947620c08171ebe5eb74fd7ed4b62151363e305ee2937cc80
2 parents b3f377d + ab9aca2 commit 3668bb3

File tree

9 files changed

+68
-17
lines changed

9 files changed

+68
-17
lines changed

doc/release-notes-14468.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Wallet `generate` RPC method deprecated
2+
---------------------------------------
3+
4+
The wallet's `generate` RPC method has been deprecated and will be fully
5+
removed in v0.19.
6+
7+
`generate` is only used for testing. The RPC call reaches across multiple
8+
subsystems (wallet and mining), so is deprecated to simplify the wallet-node
9+
interface. Projects that are using `generate` for testing purposes should
10+
transition to using the `generatetoaddress` call, which does not require or use
11+
the wallet component. Calling `generatetoaddress` with an address returned by
12+
`getnewaddress` gives the same functionality as the old `generate` method.
13+
14+
To continue using `generate` in v0.18, restart bitcoind with the
15+
`-deprecatedrpc=generate` configuration.

src/rpc/mining.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
167167
"\nExamples:\n"
168168
"\nGenerate 11 blocks to myaddress\n"
169169
+ HelpExampleCli("generatetoaddress", "11 \"myaddress\"")
170+
+ "If you are running the bitcoin core wallet, you can get a new address to send the newly generated bitcoin to with:\n"
171+
+ HelpExampleCli("getnewaddress", "")
170172
);
171173

172174
int nGenerate = request.params[0].get_int();

src/wallet/rpcwallet.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,6 +3321,12 @@ UniValue generate(const JSONRPCRequest& request)
33213321
);
33223322
}
33233323

3324+
if (!IsDeprecatedRPCEnabled("generate")) {
3325+
throw JSONRPCError(RPC_METHOD_DEPRECATED, "The wallet generate rpc method is deprecated and will be fully removed in v0.19. "
3326+
"To use generate in v0.18, restart bitcoind with -deprecatedrpc=generate.\n"
3327+
"Clients should transition to using the node rpc method generatetoaddress\n");
3328+
}
3329+
33243330
int num_generate = request.params[0].get_int();
33253331
uint64_t max_tries = 1000000;
33263332
if (!request.params[1].isNull()) {

test/functional/rpc_deprecated.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test deprecation of RPC calls."""
66
from test_framework.test_framework import BitcoinTestFramework
7+
from test_framework.util import assert_raises_rpc_error
78

89
class DeprecatedRpcTest(BitcoinTestFramework):
910
def set_test_params(self):
1011
self.num_nodes = 2
1112
self.setup_clean_chain = True
12-
self.extra_args = [[], ["-deprecatedrpc=validateaddress"]]
13+
self.extra_args = [[], ["-deprecatedrpc=generate"]]
14+
15+
def skip_test_if_missing_module(self):
16+
# The generate RPC method requires the wallet to be compiled
17+
self.skip_if_no_wallet()
1318

1419
def run_test(self):
1520
# This test should be used to verify correct behaviour of deprecated
@@ -18,7 +23,10 @@ def run_test(self):
1823
# self.log.info("Make sure that -deprecatedrpc=createmultisig allows it to take addresses")
1924
# assert_raises_rpc_error(-5, "Invalid public key", self.nodes[0].createmultisig, 1, [self.nodes[0].getnewaddress()])
2025
# self.nodes[1].createmultisig(1, [self.nodes[1].getnewaddress()])
21-
pass
26+
27+
self.log.info("Test generate RPC")
28+
assert_raises_rpc_error(-32, 'The wallet generate rpc method is deprecated', self.nodes[0].rpc.generate, 1)
29+
self.nodes[1].generate(1)
2230

2331
if __name__ == '__main__':
2432
DeprecatedRpcTest().main()

test/functional/test_framework/test_node.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,25 @@ def wait_for_rpc_connection(self):
197197
time.sleep(1.0 / poll_per_s)
198198
self._raise_assertion_error("Unable to connect to bitcoind")
199199

200+
def generate(self, nblocks, maxtries=1000000):
201+
self.log.debug("TestNode.generate() dispatches `generate` call to `generatetoaddress`")
202+
# Try to import the node's deterministic private key. This is a no-op if the private key
203+
# has already been imported.
204+
try:
205+
self.rpc.importprivkey(privkey=self.get_deterministic_priv_key().key, label='coinbase', rescan=False)
206+
except JSONRPCException as e:
207+
# This may fail if:
208+
# - wallet is disabled ('Method not found')
209+
# - there are multiple wallets to import to ('Wallet file not specified')
210+
# - wallet is locked ('Error: Please enter the wallet passphrase with walletpassphrase first')
211+
# Just ignore those errors. We can make this tidier by importing the privkey during TestFramework.setup_nodes
212+
# TODO: tidy up deterministic privkey import.
213+
assert str(e).startswith('Method not found') or \
214+
str(e).startswith('Wallet file not specified') or \
215+
str(e).startswith('Error: Please enter the wallet passphrase with walletpassphrase first')
216+
217+
return self.generatetoaddress(nblocks=nblocks, address=self.get_deterministic_priv_key().address, maxtries=maxtries)
218+
200219
def get_wallet_rpc(self, wallet_name):
201220
if self.use_cli:
202221
return self.cli("-rpcwallet={}".format(wallet_name))

test/functional/wallet_basic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
assert_array_result,
1212
assert_equal,
1313
assert_fee_amount,
14+
assert_greater_than,
1415
assert_raises_rpc_error,
1516
connect_nodes_bi,
1617
sync_blocks,
@@ -92,13 +93,13 @@ def run_test(self):
9293
assert_equal(txout['value'], 50)
9394

9495
# Send 21 BTC from 0 to 2 using sendtoaddress call.
95-
# Locked memory should use at least 32 bytes to sign each transaction
96+
# Locked memory should increase to sign transactions
9697
self.log.info("test getmemoryinfo")
9798
memory_before = self.nodes[0].getmemoryinfo()
9899
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)
99100
mempool_txid = self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 10)
100101
memory_after = self.nodes[0].getmemoryinfo()
101-
assert(memory_before['locked']['used'] + 64 <= memory_after['locked']['used'])
102+
assert_greater_than(memory_after['locked']['used'], memory_before['locked']['used'])
102103

103104
self.log.info("test gettxout (second part)")
104105
# utxo spent in mempool should be visible if you exclude mempool

test/functional/wallet_keypool.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@ def run_test(self):
7373
time.sleep(1.1)
7474
assert_equal(nodes[0].getwalletinfo()["unlocked_until"], 0)
7575

76-
# drain them by mining
77-
nodes[0].generate(1)
78-
nodes[0].generate(1)
79-
nodes[0].generate(1)
80-
assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].generate, 1)
76+
# drain the keypool
77+
for _ in range(3):
78+
nodes[0].getnewaddress()
79+
assert_raises_rpc_error(-12, "Keypool ran out", nodes[0].getnewaddress)
8180

8281
nodes[0].walletpassphrase('test', 100)
8382
nodes[0].keypoolrefill(100)

test/functional/wallet_labels.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def run_test(self):
2929

3030
# Note each time we call generate, all generated coins go into
3131
# the same address, so we call twice to get two addresses w/50 each
32-
node.generate(1)
33-
node.generate(101)
32+
node.generatetoaddress(nblocks=1, address=node.getnewaddress(label='coinbase'))
33+
node.generatetoaddress(nblocks=101, address=node.getnewaddress(label='coinbase'))
3434
assert_equal(node.getbalance(), 100)
3535

3636
# there should be 2 address groups
@@ -42,8 +42,9 @@ def run_test(self):
4242
linked_addresses = set()
4343
for address_group in address_groups:
4444
assert_equal(len(address_group), 1)
45-
assert_equal(len(address_group[0]), 2)
45+
assert_equal(len(address_group[0]), 3)
4646
assert_equal(address_group[0][1], 50)
47+
assert_equal(address_group[0][2], 'coinbase')
4748
linked_addresses.add(address_group[0][0])
4849

4950
# send 50 from each address to a third address not in this wallet
@@ -77,7 +78,7 @@ def run_test(self):
7778
label.verify(node)
7879

7980
# Check all labels are returned by listlabels.
80-
assert_equal(node.listlabels(), [label.name for label in labels])
81+
assert_equal(node.listlabels(), sorted(['coinbase'] + [label.name for label in labels]))
8182

8283
# Send a transaction to each label.
8384
for label in labels:

test/functional/wallet_multiwallet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def wallet_file(name):
129129
self.start_node(0, ['-wallet=w4', '-wallet=w5'])
130130
assert_equal(set(node.listwallets()), {"w4", "w5"})
131131
w5 = wallet("w5")
132-
w5.generate(1)
132+
node.generatetoaddress(nblocks=1, address=w5.getnewaddress())
133133

134134
# now if wallets/ exists again, but the rootdir is specified as the walletdir, w4 and w5 should still be loaded
135135
os.rename(wallet_dir2, wallet_dir())
@@ -153,7 +153,7 @@ def wallet_file(name):
153153
wallet_bad = wallet("bad")
154154

155155
# check wallet names and balances
156-
wallets[0].generate(1)
156+
node.generatetoaddress(nblocks=1, address=wallets[0].getnewaddress())
157157
for wallet_name, wallet in zip(wallet_names, wallets):
158158
info = wallet.getwalletinfo()
159159
assert_equal(info['immature_balance'], 50 if wallet is wallets[0] else 0)
@@ -166,7 +166,7 @@ def wallet_file(name):
166166
assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)
167167

168168
w1, w2, w3, w4, *_ = wallets
169-
w1.generate(101)
169+
node.generatetoaddress(nblocks=101, address=w1.getnewaddress())
170170
assert_equal(w1.getbalance(), 100)
171171
assert_equal(w2.getbalance(), 0)
172172
assert_equal(w3.getbalance(), 0)
@@ -175,7 +175,7 @@ def wallet_file(name):
175175
w1.sendtoaddress(w2.getnewaddress(), 1)
176176
w1.sendtoaddress(w3.getnewaddress(), 2)
177177
w1.sendtoaddress(w4.getnewaddress(), 3)
178-
w1.generate(1)
178+
node.generatetoaddress(nblocks=1, address=w1.getnewaddress())
179179
assert_equal(w2.getbalance(), 1)
180180
assert_equal(w3.getbalance(), 2)
181181
assert_equal(w4.getbalance(), 3)

0 commit comments

Comments
 (0)