Skip to content

Commit 5d67a78

Browse files
committed
Add calls to CWallet::BlockUntilSyncedToCurrentChain() in RPCs
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 5ee3172 commit 5d67a78

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
455455
);
456456

457457
ObserveSafeMode();
458+
459+
// Make sure the results are valid at least up to the most recent block
460+
// the user could have gotten from another RPC command prior to now
461+
pwallet->BlockUntilSyncedToCurrentChain();
462+
458463
LOCK2(cs_main, pwallet->cs_wallet);
459464

460465
CTxDestination dest = DecodeDestination(request.params[0].get_str());
@@ -533,6 +538,11 @@ UniValue listaddressgroupings(const JSONRPCRequest& request)
533538
);
534539

535540
ObserveSafeMode();
541+
542+
// Make sure the results are valid at least up to the most recent block
543+
// the user could have gotten from another RPC command prior to now
544+
pwallet->BlockUntilSyncedToCurrentChain();
545+
536546
LOCK2(cs_main, pwallet->cs_wallet);
537547

538548
UniValue jsonGroupings(UniValue::VARR);
@@ -645,6 +655,11 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
645655
);
646656

647657
ObserveSafeMode();
658+
659+
// Make sure the results are valid at least up to the most recent block
660+
// the user could have gotten from another RPC command prior to now
661+
pwallet->BlockUntilSyncedToCurrentChain();
662+
648663
LOCK2(cs_main, pwallet->cs_wallet);
649664

650665
// Bitcoin address
@@ -707,6 +722,11 @@ UniValue getreceivedbyaccount(const JSONRPCRequest& request)
707722
);
708723

709724
ObserveSafeMode();
725+
726+
// Make sure the results are valid at least up to the most recent block
727+
// the user could have gotten from another RPC command prior to now
728+
pwallet->BlockUntilSyncedToCurrentChain();
729+
710730
LOCK2(cs_main, pwallet->cs_wallet);
711731

712732
// Minimum confirmations
@@ -780,6 +800,11 @@ UniValue getbalance(const JSONRPCRequest& request)
780800
);
781801

782802
ObserveSafeMode();
803+
804+
// Make sure the results are valid at least up to the most recent block
805+
// the user could have gotten from another RPC command prior to now
806+
pwallet->BlockUntilSyncedToCurrentChain();
807+
783808
LOCK2(cs_main, pwallet->cs_wallet);
784809

785810
const UniValue& account_value = request.params[0];
@@ -825,6 +850,11 @@ UniValue getunconfirmedbalance(const JSONRPCRequest &request)
825850
"Returns the server's total unconfirmed balance\n");
826851

827852
ObserveSafeMode();
853+
854+
// Make sure the results are valid at least up to the most recent block
855+
// the user could have gotten from another RPC command prior to now
856+
pwallet->BlockUntilSyncedToCurrentChain();
857+
828858
LOCK2(cs_main, pwallet->cs_wallet);
829859

830860
return ValueFromAmount(pwallet->GetUnconfirmedBalance());
@@ -919,6 +949,11 @@ UniValue sendfrom(const JSONRPCRequest& request)
919949
);
920950

921951
ObserveSafeMode();
952+
953+
// Make sure the results are valid at least up to the most recent block
954+
// the user could have gotten from another RPC command prior to now
955+
pwallet->BlockUntilSyncedToCurrentChain();
956+
922957
LOCK2(cs_main, pwallet->cs_wallet);
923958

924959
std::string strAccount = AccountFromValue(request.params[0]);
@@ -1004,6 +1039,11 @@ UniValue sendmany(const JSONRPCRequest& request)
10041039
);
10051040

10061041
ObserveSafeMode();
1042+
1043+
// Make sure the results are valid at least up to the most recent block
1044+
// the user could have gotten from another RPC command prior to now
1045+
pwallet->BlockUntilSyncedToCurrentChain();
1046+
10071047
LOCK2(cs_main, pwallet->cs_wallet);
10081048

10091049
if (pwallet->GetBroadcastTransactions() && !g_connman) {
@@ -1455,6 +1495,11 @@ UniValue listreceivedbyaddress(const JSONRPCRequest& request)
14551495
);
14561496

14571497
ObserveSafeMode();
1498+
1499+
// Make sure the results are valid at least up to the most recent block
1500+
// the user could have gotten from another RPC command prior to now
1501+
pwallet->BlockUntilSyncedToCurrentChain();
1502+
14581503
LOCK2(cs_main, pwallet->cs_wallet);
14591504

14601505
return ListReceived(pwallet, request.params, false);
@@ -1495,6 +1540,11 @@ UniValue listreceivedbyaccount(const JSONRPCRequest& request)
14951540
);
14961541

14971542
ObserveSafeMode();
1543+
1544+
// Make sure the results are valid at least up to the most recent block
1545+
// the user could have gotten from another RPC command prior to now
1546+
pwallet->BlockUntilSyncedToCurrentChain();
1547+
14981548
LOCK2(cs_main, pwallet->cs_wallet);
14991549

15001550
return ListReceived(pwallet, request.params, true);
@@ -1683,6 +1733,11 @@ UniValue listtransactions(const JSONRPCRequest& request)
16831733
);
16841734

16851735
ObserveSafeMode();
1736+
1737+
// Make sure the results are valid at least up to the most recent block
1738+
// the user could have gotten from another RPC command prior to now
1739+
pwallet->BlockUntilSyncedToCurrentChain();
1740+
16861741
LOCK2(cs_main, pwallet->cs_wallet);
16871742

16881743
std::string strAccount = "*";
@@ -1777,6 +1832,11 @@ UniValue listaccounts(const JSONRPCRequest& request)
17771832
);
17781833

17791834
ObserveSafeMode();
1835+
1836+
// Make sure the results are valid at least up to the most recent block
1837+
// the user could have gotten from another RPC command prior to now
1838+
pwallet->BlockUntilSyncedToCurrentChain();
1839+
17801840
LOCK2(cs_main, pwallet->cs_wallet);
17811841

17821842
int nMinDepth = 1;
@@ -1886,6 +1946,11 @@ UniValue listsinceblock(const JSONRPCRequest& request)
18861946
);
18871947

18881948
ObserveSafeMode();
1949+
1950+
// Make sure the results are valid at least up to the most recent block
1951+
// the user could have gotten from another RPC command prior to now
1952+
pwallet->BlockUntilSyncedToCurrentChain();
1953+
18891954
LOCK2(cs_main, pwallet->cs_wallet);
18901955

18911956
const CBlockIndex* pindex = nullptr; // Block index of the specified block or the common ancestor, if the block provided was in a deactivated chain.
@@ -2018,6 +2083,11 @@ UniValue gettransaction(const JSONRPCRequest& request)
20182083
);
20192084

20202085
ObserveSafeMode();
2086+
2087+
// Make sure the results are valid at least up to the most recent block
2088+
// the user could have gotten from another RPC command prior to now
2089+
pwallet->BlockUntilSyncedToCurrentChain();
2090+
20212091
LOCK2(cs_main, pwallet->cs_wallet);
20222092

20232093
uint256 hash;
@@ -2080,6 +2150,11 @@ UniValue abandontransaction(const JSONRPCRequest& request)
20802150
);
20812151

20822152
ObserveSafeMode();
2153+
2154+
// Make sure the results are valid at least up to the most recent block
2155+
// the user could have gotten from another RPC command prior to now
2156+
pwallet->BlockUntilSyncedToCurrentChain();
2157+
20832158
LOCK2(cs_main, pwallet->cs_wallet);
20842159

20852160
uint256 hash;
@@ -2114,6 +2189,10 @@ UniValue backupwallet(const JSONRPCRequest& request)
21142189
+ HelpExampleRpc("backupwallet", "\"backup.dat\"")
21152190
);
21162191

2192+
// Make sure the results are valid at least up to the most recent block
2193+
// the user could have gotten from another RPC command prior to now
2194+
pwallet->BlockUntilSyncedToCurrentChain();
2195+
21172196
LOCK2(cs_main, pwallet->cs_wallet);
21182197

21192198
std::string strDest = request.params[0].get_str();
@@ -2433,6 +2512,10 @@ UniValue lockunspent(const JSONRPCRequest& request)
24332512
+ HelpExampleRpc("lockunspent", "false, \"[{\\\"txid\\\":\\\"a08e6907dbbd3d809776dbfc5d82e371b764ed838b5655e72f463568df1aadf0\\\",\\\"vout\\\":1}]\"")
24342513
);
24352514

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

24382521
RPCTypeCheckArgument(request.params[0], UniValue::VBOOL);
@@ -2592,6 +2675,11 @@ UniValue getwalletinfo(const JSONRPCRequest& request)
25922675
);
25932676

25942677
ObserveSafeMode();
2678+
2679+
// Make sure the results are valid at least up to the most recent block
2680+
// the user could have gotten from another RPC command prior to now
2681+
pwallet->BlockUntilSyncedToCurrentChain();
2682+
25952683
LOCK2(cs_main, pwallet->cs_wallet);
25962684

25972685
UniValue obj(UniValue::VOBJ);
@@ -2801,6 +2889,10 @@ UniValue listunspent(const JSONRPCRequest& request)
28012889
nMaximumCount = options["maximumCount"].get_int64();
28022890
}
28032891

2892+
// Make sure the results are valid at least up to the most recent block
2893+
// the user could have gotten from another RPC command prior to now
2894+
pwallet->BlockUntilSyncedToCurrentChain();
2895+
28042896
UniValue results(UniValue::VARR);
28052897
std::vector<COutput> vecOutputs;
28062898
assert(pwallet != nullptr);
@@ -2911,6 +3003,10 @@ UniValue fundrawtransaction(const JSONRPCRequest& request)
29113003
ObserveSafeMode();
29123004
RPCTypeCheck(request.params, {UniValue::VSTR});
29133005

3006+
// Make sure the results are valid at least up to the most recent block
3007+
// the user could have gotten from another RPC command prior to now
3008+
pwallet->BlockUntilSyncedToCurrentChain();
3009+
29143010
CCoinControl coinControl;
29153011
int changePosition = -1;
29163012
bool lockUnspents = false;
@@ -3121,6 +3217,10 @@ UniValue bumpfee(const JSONRPCRequest& request)
31213217
}
31223218
}
31233219

3220+
// Make sure the results are valid at least up to the most recent block
3221+
// the user could have gotten from another RPC command prior to now
3222+
pwallet->BlockUntilSyncedToCurrentChain();
3223+
31243224
LOCK2(cs_main, pwallet->cs_wallet);
31253225
EnsureWalletIsUnlocked(pwallet);
31263226

0 commit comments

Comments
 (0)