Skip to content

Commit aab8172

Browse files
committed
[tests] Add generate method to TestNode
Adds a generate() method to the TestNode class in the test framework. This method intercepts calls to generate, imports a dewterministic private key to the node and then calls generatetoaddress to generate the block to that address. Note that repeated calls to importprivkey for the same private keys are no-ops, so it's fine to call the generate() method many times.
1 parent c269209 commit aab8172

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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))

0 commit comments

Comments
 (0)