Skip to content

Commit fa5362a

Browse files
author
MarcoFalke
committed
rpc: Add missing BlockUntilSyncedToCurrentChain to wallet RPCs
Wallet RPCs that allow a rescan based on block-timestamp or block-height need to sync with the active chain first, because the user might assume the wallet is up-to-date with the latest block they got reported via a blockchain RPC.
1 parent 8b1de78 commit fa5362a

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/wallet/rpc/backup.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@ RPCHelpMan importmulti()
13461346
{
13471347
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(mainRequest);
13481348
if (!pwallet) return NullUniValue;
1349+
CWallet& wallet{*pwallet};
1350+
1351+
// Make sure the results are valid at least up to the most recent block
1352+
// the user could have gotten from another RPC command prior to now
1353+
wallet.BlockUntilSyncedToCurrentChain();
13491354

13501355
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13511356

@@ -1649,6 +1654,11 @@ RPCHelpMan importdescriptors()
16491654
{
16501655
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(main_request);
16511656
if (!pwallet) return NullUniValue;
1657+
CWallet& wallet{*pwallet};
1658+
1659+
// Make sure the results are valid at least up to the most recent block
1660+
// the user could have gotten from another RPC command prior to now
1661+
wallet.BlockUntilSyncedToCurrentChain();
16521662

16531663
// Make sure wallet is a descriptor wallet
16541664
if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,6 +3304,11 @@ static RPCHelpMan rescanblockchain()
33043304
{
33053305
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
33063306
if (!pwallet) return NullUniValue;
3307+
CWallet& wallet{*pwallet};
3308+
3309+
// Make sure the results are valid at least up to the most recent block
3310+
// the user could have gotten from another RPC command prior to now
3311+
wallet.BlockUntilSyncedToCurrentChain();
33073312

33083313
WalletRescanReserver reserver(*pwallet);
33093314
if (!reserver.reserve()) {

test/functional/wallet_importmulti.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def run_test(self):
6565
self.generate(self.nodes[0], 1, sync_fun=self.no_op)
6666
self.generate(self.nodes[1], 1, sync_fun=self.no_op)
6767
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
68-
self.nodes[1].syncwithvalidationinterfacequeue() # Sync the timestamp to the wallet, so that importmulti works
6968

7069
node0_address1 = self.nodes[0].getaddressinfo(self.nodes[0].getnewaddress())
7170

@@ -260,7 +259,6 @@ def run_test(self):
260259
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
261260
self.generate(self.nodes[1], 1, sync_fun=self.no_op)
262261
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
263-
self.nodes[1].syncwithvalidationinterfacequeue()
264262

265263
self.log.info("Should import a p2sh")
266264
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr},
@@ -281,7 +279,6 @@ def run_test(self):
281279
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
282280
self.generate(self.nodes[1], 1, sync_fun=self.no_op)
283281
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
284-
self.nodes[1].syncwithvalidationinterfacequeue()
285282

286283
self.log.info("Should import a p2sh with respective redeem script")
287284
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr},
@@ -302,7 +299,6 @@ def run_test(self):
302299
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
303300
self.generate(self.nodes[1], 1, sync_fun=self.no_op)
304301
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
305-
self.nodes[1].syncwithvalidationinterfacequeue()
306302

307303
self.log.info("Should import a p2sh with respective redeem script and private keys")
308304
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr},
@@ -328,7 +324,6 @@ def run_test(self):
328324
self.nodes[1].sendtoaddress(multisig.p2sh_addr, 10.00)
329325
self.generate(self.nodes[1], 1, sync_fun=self.no_op)
330326
timestamp = self.nodes[1].getblock(self.nodes[1].getbestblockhash())['mediantime']
331-
self.nodes[1].syncwithvalidationinterfacequeue()
332327

333328
self.log.info("Should import a p2sh with respective redeem script and private keys")
334329
self.test_importmulti({"scriptPubKey": {"address": multisig.p2sh_addr},

0 commit comments

Comments
 (0)