Skip to content

Commit f6df6e4

Browse files
committed
Add calls to CWallet::BlockUntilSyncedToCurrentChain() in RPCs
Adaptation from btc@5d67a7868db188f7e43ce9028f215034d057790d This prevents the wallet-RPCs-return-stale-info issue from being re-introduced when new-block callbacks no longer happen in the block-connection cs_main lock
1 parent 24a3ce4 commit f6df6e4

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
518518
if (!pwalletMain)
519519
throw std::runtime_error("wallet not initialized");
520520

521+
// Make sure the results are valid at least up to the most recent block
522+
// the user could have gotten from another RPC command prior to now
523+
pwalletMain->BlockUntilSyncedToCurrentChain();
524+
521525
RPCTypeCheck(request.params, {UniValue::VSTR});
522526

523527
CTxDestination changeAddress = CNoDestination();

src/wallet/rpcwallet.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,10 @@ UniValue sethdseed(const JSONRPCRequest& request)
440440
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Cannot set a new HD seed while still in Initial Block Download");
441441
}
442442

443+
// Make sure the results are valid at least up to the most recent block
444+
// the user could have gotten from another RPC command prior to now
445+
pwalletMain->BlockUntilSyncedToCurrentChain();
446+
443447
LOCK2(cs_main, pwallet->cs_wallet);
444448

445449
// Do not do anything to non-HD wallets
@@ -1033,6 +1037,10 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
10331037

10341038
EnsureWalletIsUnlocked();
10351039

1040+
// Make sure the results are valid at least up to the most recent block
1041+
// the user could have gotten from another RPC command prior to now
1042+
pwalletMain->BlockUntilSyncedToCurrentChain();
1043+
10361044
bool isStaking = false, isShielded = false;
10371045
const std::string addrStr = request.params[0].get_str();
10381046
const CWDestination& destination = Standard::DecodeDestination(addrStr, isStaking, isShielded);
@@ -1209,6 +1217,10 @@ UniValue delegatestake(const JSONRPCRequest& request)
12091217
HelpExampleCli("delegatestake", "\"S1t2a3kab9c8c71VA78xxxy4MxZg6vgeS6\" 1000 \"DMJRSsuU9zfyrvxVaAEFQqK4MxZg34fk\"") +
12101218
HelpExampleRpc("delegatestake", "\"S1t2a3kab9c8c71VA78xxxy4MxZg6vgeS6\", 1000, \"DMJRSsuU9zfyrvxVaAEFQqK4MxZg34fk\""));
12111219

1220+
// Make sure the results are valid at least up to the most recent block
1221+
// the user could have gotten from another RPC command prior to now
1222+
pwalletMain->BlockUntilSyncedToCurrentChain();
1223+
12121224
LOCK2(cs_main, pwalletMain->cs_wallet);
12131225

12141226
CTransactionRef wtx;
@@ -1251,6 +1263,10 @@ UniValue rawdelegatestake(const JSONRPCRequest& request)
12511263
HelpExampleCli("rawdelegatestake", "\"S1t2a3kab9c8c71VA78xxxy4MxZg6vgeS6\" 1000 \"DMJRSsuU9zfyrvxVaAEFQqK4MxZg34fk\"") +
12521264
HelpExampleRpc("rawdelegatestake", "\"S1t2a3kab9c8c71VA78xxxy4MxZg6vgeS6\", 1000, \"DMJRSsuU9zfyrvxVaAEFQqK4MxZg34fk\""));
12531265

1266+
// Make sure the results are valid at least up to the most recent block
1267+
// the user could have gotten from another RPC command prior to now
1268+
pwalletMain->BlockUntilSyncedToCurrentChain();
1269+
12541270
LOCK2(cs_main, pwalletMain->cs_wallet);
12551271

12561272
CTransactionRef wtx;
@@ -1375,6 +1391,11 @@ UniValue viewshieldtransaction(const JSONRPCRequest& request)
13751391
}
13761392

13771393
EnsureWalletIsUnlocked();
1394+
1395+
// Make sure the results are valid at least up to the most recent block
1396+
// the user could have gotten from another RPC command prior to now
1397+
pwalletMain->BlockUntilSyncedToCurrentChain();
1398+
13781399
LOCK2(cs_main, pwalletMain->cs_wallet);
13791400

13801401
uint256 hash;
@@ -1687,6 +1708,10 @@ UniValue shieldsendmany(const JSONRPCRequest& request)
16871708
"\"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\", [{\"address\": \"ps1ra969yfhvhp73rw5ak2xvtcm9fkuqsnmad7qln79mphhdrst3lwu9vvv03yuyqlh42p42st47qd\" ,\"amount\": 5.0}]")
16881709
);
16891710

1711+
// Make sure the results are valid at least up to the most recent block
1712+
// the user could have gotten from another RPC command prior to now
1713+
pwalletMain->BlockUntilSyncedToCurrentChain();
1714+
16901715
SaplingOperation operation = CreateShieldedTransaction(request);
16911716
std::string txHash;
16921717
auto res = operation.send(txHash);
@@ -1731,6 +1756,10 @@ UniValue rawshieldsendmany(const JSONRPCRequest& request)
17311756
"\"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\", [{\"address\": \"ps1ra969yfhvhp73rw5ak2xvtcm9fkuqsnmad7qln79mphhdrst3lwu9vvv03yuyqlh42p42st47qd\" ,\"amount\": 5.0}]")
17321757
);
17331758

1759+
// Make sure the results are valid at least up to the most recent block
1760+
// the user could have gotten from another RPC command prior to now
1761+
pwalletMain->BlockUntilSyncedToCurrentChain();
1762+
17341763
CTransaction tx = CreateShieldedTransaction(request).getFinalTx();
17351764
return EncodeHexTx(tx);
17361765
}
@@ -1760,6 +1789,10 @@ UniValue listaddressgroupings(const JSONRPCRequest& request)
17601789
"\nExamples:\n" +
17611790
HelpExampleCli("listaddressgroupings", "") + HelpExampleRpc("listaddressgroupings", ""));
17621791

1792+
// Make sure the results are valid at least up to the most recent block
1793+
// the user could have gotten from another RPC command prior to now
1794+
pwalletMain->BlockUntilSyncedToCurrentChain();
1795+
17631796
LOCK2(cs_main, pwalletMain->cs_wallet);
17641797

17651798
UniValue jsonGroupings(UniValue::VARR);
@@ -1860,6 +1893,10 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
18601893
"\nAs a json rpc call\n" +
18611894
HelpExampleRpc("getreceivedbyaddress", "\"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\", 6"));
18621895

1896+
// Make sure the results are valid at least up to the most recent block
1897+
// the user could have gotten from another RPC command prior to now
1898+
pwalletMain->BlockUntilSyncedToCurrentChain();
1899+
18631900
LOCK2(cs_main, pwalletMain->cs_wallet);
18641901

18651902
// pivx address
@@ -1916,6 +1953,10 @@ UniValue getreceivedbylabel(const JSONRPCRequest& request)
19161953
"\nAs a json rpc call\n" +
19171954
HelpExampleRpc("getreceivedbylabel", "\"tabby\", 6"));
19181955

1956+
// Make sure the results are valid at least up to the most recent block
1957+
// the user could have gotten from another RPC command prior to now
1958+
pwalletMain->BlockUntilSyncedToCurrentChain();
1959+
19191960
LOCK2(cs_main, pwalletMain->cs_wallet);
19201961

19211962
// Minimum confirmations
@@ -1971,6 +2012,10 @@ UniValue getbalance(const JSONRPCRequest& request)
19712012
"\nAs a json rpc call\n" +
19722013
HelpExampleRpc("getbalance", "6"));
19732014

2015+
// Make sure the results are valid at least up to the most recent block
2016+
// the user could have gotten from another RPC command prior to now
2017+
pwalletMain->BlockUntilSyncedToCurrentChain();
2018+
19742019
LOCK2(cs_main, pwalletMain->cs_wallet);
19752020

19762021
const int paramsSize = request.params.size();
@@ -2002,6 +2047,10 @@ UniValue getcoldstakingbalance(const JSONRPCRequest& request)
20022047
"\nAs a json rpc call\n" +
20032048
HelpExampleRpc("getcoldstakingbalance", "\"*\""));
20042049

2050+
// Make sure the results are valid at least up to the most recent block
2051+
// the user could have gotten from another RPC command prior to now
2052+
pwalletMain->BlockUntilSyncedToCurrentChain();
2053+
20052054
LOCK2(cs_main, pwalletMain->cs_wallet);
20062055

20072056
return ValueFromAmount(pwalletMain->GetColdStakingBalance());
@@ -2024,6 +2073,10 @@ UniValue getdelegatedbalance(const JSONRPCRequest& request)
20242073
"\nAs a json rpc call\n" +
20252074
HelpExampleRpc("getdelegatedbalance", "\"*\""));
20262075

2076+
// Make sure the results are valid at least up to the most recent block
2077+
// the user could have gotten from another RPC command prior to now
2078+
pwalletMain->BlockUntilSyncedToCurrentChain();
2079+
20272080
LOCK2(cs_main, pwalletMain->cs_wallet);
20282081

20292082
return ValueFromAmount(pwalletMain->GetDelegatedBalance());
@@ -2036,6 +2089,10 @@ UniValue getunconfirmedbalance(const JSONRPCRequest& request)
20362089
"getunconfirmedbalance\n"
20372090
"Returns the server's total unconfirmed balance\n");
20382091

2092+
// Make sure the results are valid at least up to the most recent block
2093+
// the user could have gotten from another RPC command prior to now
2094+
pwalletMain->BlockUntilSyncedToCurrentChain();
2095+
20392096
LOCK2(cs_main, pwalletMain->cs_wallet);
20402097

20412098
return ValueFromAmount(pwalletMain->GetUnconfirmedBalance());
@@ -2156,6 +2213,10 @@ UniValue sendmany(const JSONRPCRequest& request)
21562213

21572214
EnsureWalletIsUnlocked();
21582215

2216+
// Make sure the results are valid at least up to the most recent block
2217+
// the user could have gotten from another RPC command prior to now
2218+
pwalletMain->BlockUntilSyncedToCurrentChain();
2219+
21592220
// Read Params
21602221
if (!request.params[0].isNull() && !request.params[0].get_str().empty()) {
21612222
throw JSONRPCError(RPC_INVALID_PARAMETER, "Dummy value must be set to \"\"");
@@ -2414,6 +2475,10 @@ UniValue listreceivedbyaddress(const JSONRPCRequest& request)
24142475
HelpExampleRpc("listreceivedbyaddress", "6, true, true") +
24152476
HelpExampleRpc("listreceivedbyaddress", "6, true, true, \"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\""));
24162477

2478+
// Make sure the results are valid at least up to the most recent block
2479+
// the user could have gotten from another RPC command prior to now
2480+
pwalletMain->BlockUntilSyncedToCurrentChain();
2481+
24172482
LOCK2(cs_main, pwalletMain->cs_wallet);
24182483

24192484
return ListReceived(request.params, false);
@@ -2448,6 +2513,10 @@ UniValue listreceivedbyshieldaddress(const JSONRPCRequest& request)
24482513
+ HelpExampleRpc("listreceivedbyshieldaddress", "\"ps1ra969yfhvhp73rw5ak2xvtcm9fkuqsnmad7qln79mphhdrst3lwu9vvv03yuyqlh42p42st47qd\"")
24492514
);
24502515

2516+
// Make sure the results are valid at least up to the most recent block
2517+
// the user could have gotten from another RPC command prior to now
2518+
pwalletMain->BlockUntilSyncedToCurrentChain();
2519+
24512520
LOCK2(cs_main, pwalletMain->cs_wallet);
24522521

24532522
int nMinDepth = 1;
@@ -2542,6 +2611,10 @@ UniValue listreceivedbylabel(const JSONRPCRequest& request)
25422611
"\nExamples:\n" +
25432612
HelpExampleCli("listreceivedbylabel", "") + HelpExampleCli("listreceivedbylabel", "6 true") + HelpExampleRpc("listreceivedbylabel", "6, true, true"));
25442613

2614+
// Make sure the results are valid at least up to the most recent block
2615+
// the user could have gotten from another RPC command prior to now
2616+
pwalletMain->BlockUntilSyncedToCurrentChain();
2617+
25452618
LOCK2(cs_main, pwalletMain->cs_wallet);
25462619

25472620
return ListReceived(request.params, true);
@@ -2574,6 +2647,10 @@ UniValue listcoldutxos(const JSONRPCRequest& request)
25742647
"\nExamples:\n" +
25752648
HelpExampleCli("listcoldutxos", "") + HelpExampleCli("listcoldutxos", "true"));
25762649

2650+
// Make sure the results are valid at least up to the most recent block
2651+
// the user could have gotten from another RPC command prior to now
2652+
pwalletMain->BlockUntilSyncedToCurrentChain();
2653+
25772654
LOCK2(cs_main, pwalletMain->cs_wallet);
25782655

25792656
bool fExcludeWhitelisted = false;
@@ -2740,6 +2817,10 @@ UniValue listtransactions(const JSONRPCRequest& request)
27402817
HelpExampleRpc("listtransactions", "\"*\", 20, 100")
27412818
);
27422819

2820+
// Make sure the results are valid at least up to the most recent block
2821+
// the user could have gotten from another RPC command prior to now
2822+
pwalletMain->BlockUntilSyncedToCurrentChain();
2823+
27432824
LOCK2(cs_main, pwalletMain->cs_wallet);
27442825

27452826
if (!request.params[0].isNull() && request.params[0].get_str() != "*") {
@@ -2841,6 +2922,10 @@ UniValue listsinceblock(const JSONRPCRequest& request)
28412922
HelpExampleCli("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\" 6") +
28422923
HelpExampleRpc("listsinceblock", "\"000000000000000bacf66f7497b7dc45ef753ee9a7d38571037cdb1a57f663ad\", 6"));
28432924

2925+
// Make sure the results are valid at least up to the most recent block
2926+
// the user could have gotten from another RPC command prior to now
2927+
pwalletMain->BlockUntilSyncedToCurrentChain();
2928+
28442929
LOCK2(cs_main, pwalletMain->cs_wallet);
28452930

28462931
CBlockIndex* pindex = NULL;
@@ -2928,6 +3013,10 @@ UniValue gettransaction(const JSONRPCRequest& request)
29283013
HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true") +
29293014
HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\""));
29303015

3016+
// Make sure the results are valid at least up to the most recent block
3017+
// the user could have gotten from another RPC command prior to now
3018+
pwalletMain->BlockUntilSyncedToCurrentChain();
3019+
29313020
LOCK2(cs_main, pwalletMain->cs_wallet);
29323021

29333022
uint256 hash;
@@ -2984,6 +3073,10 @@ UniValue abandontransaction(const JSONRPCRequest& request)
29843073

29853074
EnsureWalletIsUnlocked();
29863075

3076+
// Make sure the results are valid at least up to the most recent block
3077+
// the user could have gotten from another RPC command prior to now
3078+
pwalletMain->BlockUntilSyncedToCurrentChain();
3079+
29873080
LOCK2(cs_main, pwalletMain->cs_wallet);
29883081

29893082
uint256 hash;
@@ -3011,6 +3104,10 @@ UniValue backupwallet(const JSONRPCRequest& request)
30113104
"\nExamples:\n" +
30123105
HelpExampleCli("backupwallet", "\"backup.dat\"") + HelpExampleRpc("backupwallet", "\"backup.dat\""));
30133106

3107+
// Make sure the results are valid at least up to the most recent block
3108+
// the user could have gotten from another RPC command prior to now
3109+
pwalletMain->BlockUntilSyncedToCurrentChain();
3110+
30143111
LOCK2(cs_main, pwalletMain->cs_wallet);
30153112

30163113
std::string strDest = request.params[0].get_str();
@@ -3199,6 +3296,10 @@ UniValue walletlock(const JSONRPCRequest& request)
31993296
"\nAs json rpc call\n" +
32003297
HelpExampleRpc("walletlock", ""));
32013298

3299+
// Make sure the results are valid at least up to the most recent block
3300+
// the user could have gotten from another RPC command prior to now
3301+
pwalletMain->BlockUntilSyncedToCurrentChain();
3302+
32023303
LOCK2(cs_main, pwalletMain->cs_wallet);
32033304

32043305
if (request.fHelp)
@@ -3315,6 +3416,10 @@ UniValue listunspent(const JSONRPCRequest& request)
33153416

33163417
RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM, UniValue::VARR, UniValue::VNUM});
33173418

3419+
// Make sure the results are valid at least up to the most recent block
3420+
// the user could have gotten from another RPC command prior to now
3421+
pwalletMain->BlockUntilSyncedToCurrentChain();
3422+
33183423
int nMinDepth = 1;
33193424
if (request.params.size() > 0)
33203425
nMinDepth = request.params[0].get_int();
@@ -3439,6 +3544,10 @@ UniValue lockunspent(const JSONRPCRequest& request)
34393544
"\nAs a json rpc call\n" +
34403545
HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\""));
34413546

3547+
// Make sure the results are valid at least up to the most recent block
3548+
// the user could have gotten from another RPC command prior to now
3549+
pwalletMain->BlockUntilSyncedToCurrentChain();
3550+
34423551
LOCK2(cs_main, pwalletMain->cs_wallet);
34433552

34443553
if (request.params.size() == 1)
@@ -3622,6 +3731,10 @@ UniValue getwalletinfo(const JSONRPCRequest& request)
36223731
"\nExamples:\n" +
36233732
HelpExampleCli("getwalletinfo", "") + HelpExampleRpc("getwalletinfo", ""));
36243733

3734+
// Make sure the results are valid at least up to the most recent block
3735+
// the user could have gotten from another RPC command prior to now
3736+
pwalletMain->BlockUntilSyncedToCurrentChain();
3737+
36253738
LOCK2(cs_main, pwalletMain->cs_wallet);
36263739

36273740
UniValue obj(UniValue::VOBJ);
@@ -4052,6 +4165,10 @@ UniValue getsaplingnotescount(const JSONRPCRequest& request)
40524165
+ HelpExampleRpc("getsaplingnotescount", "0")
40534166
);
40544167

4168+
// Make sure the results are valid at least up to the most recent block
4169+
// the user could have gotten from another RPC command prior to now
4170+
pwalletMain->BlockUntilSyncedToCurrentChain();
4171+
40554172
LOCK2(cs_main, pwalletMain->cs_wallet);
40564173

40574174
int nMinDepth = !request.params.empty() ? request.params[0].get_int() : 1;

0 commit comments

Comments
 (0)