Skip to content

Commit 54011e7

Browse files
committed
refactor: use CWallet const shared pointers when possible
While const shared_ptr<X> gives us an immutable shared pointer to a mutable X (we can't set it to some other X later), shared_ptr<const X> gives us a shared pointer to an immutable X. Importantly, we can recast shared_ptr<X> into shared_ptr<const X>, but not the other way around. We do this for two reasons: because it makes the code safer to guarantee the wallet is not modified, and because it further dispells the misconception that const shared_ptr<X> gives immutability to X.
1 parent 9646198 commit 54011e7

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/wallet/rpcdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ RPCHelpMan listdescriptors()
17881788
},
17891789
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
17901790
{
1791-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
1791+
const std::shared_ptr<const CWallet> wallet = GetWalletForJSONRPCRequest(request);
17921792
if (!wallet) return NullUniValue;
17931793

17941794
if (!wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {

src/wallet/rpcwallet.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ static RPCHelpMan listaddressgroupings()
570570
},
571571
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
572572
{
573-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
573+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
574574
if (!pwallet) return NullUniValue;
575575

576576
// Make sure the results are valid at least up to the most recent block
@@ -627,7 +627,7 @@ static RPCHelpMan signmessage()
627627
},
628628
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
629629
{
630-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
630+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
631631
if (!pwallet) return NullUniValue;
632632

633633
LOCK(pwallet->cs_wallet);
@@ -729,7 +729,7 @@ static RPCHelpMan getreceivedbyaddress()
729729
},
730730
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
731731
{
732-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
732+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
733733
if (!pwallet) return NullUniValue;
734734

735735
// Make sure the results are valid at least up to the most recent block
@@ -767,7 +767,7 @@ static RPCHelpMan getreceivedbylabel()
767767
},
768768
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
769769
{
770-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
770+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
771771
if (!pwallet) return NullUniValue;
772772

773773
// Make sure the results are valid at least up to the most recent block
@@ -807,7 +807,7 @@ static RPCHelpMan getbalance()
807807
},
808808
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
809809
{
810-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
810+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
811811
if (!pwallet) return NullUniValue;
812812

813813
// Make sure the results are valid at least up to the most recent block
@@ -846,7 +846,7 @@ static RPCHelpMan getunconfirmedbalance()
846846
RPCExamples{""},
847847
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
848848
{
849-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
849+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
850850
if (!pwallet) return NullUniValue;
851851

852852
// Make sure the results are valid at least up to the most recent block
@@ -1234,7 +1234,7 @@ static RPCHelpMan listreceivedbyaddress()
12341234
},
12351235
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
12361236
{
1237-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1237+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
12381238
if (!pwallet) return NullUniValue;
12391239

12401240
// Make sure the results are valid at least up to the most recent block
@@ -1276,7 +1276,7 @@ static RPCHelpMan listreceivedbylabel()
12761276
},
12771277
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
12781278
{
1279-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1279+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
12801280
if (!pwallet) return NullUniValue;
12811281

12821282
// Make sure the results are valid at least up to the most recent block
@@ -1461,7 +1461,7 @@ static RPCHelpMan listtransactions()
14611461
},
14621462
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
14631463
{
1464-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1464+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
14651465
if (!pwallet) return NullUniValue;
14661466

14671467
// Make sure the results are valid at least up to the most recent block
@@ -1577,7 +1577,7 @@ static RPCHelpMan listsinceblock()
15771577
},
15781578
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
15791579
{
1580-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1580+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
15811581
if (!pwallet) return NullUniValue;
15821582

15831583
const CWallet& wallet = *pwallet;
@@ -1718,7 +1718,7 @@ static RPCHelpMan gettransaction()
17181718
},
17191719
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
17201720
{
1721-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1721+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
17221722
if (!pwallet) return NullUniValue;
17231723

17241724
// Make sure the results are valid at least up to the most recent block
@@ -1829,7 +1829,7 @@ static RPCHelpMan backupwallet()
18291829
},
18301830
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
18311831
{
1832-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
1832+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
18331833
if (!pwallet) return NullUniValue;
18341834

18351835
// Make sure the results are valid at least up to the most recent block
@@ -2331,7 +2331,7 @@ static RPCHelpMan listlockunspent()
23312331
},
23322332
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
23332333
{
2334-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
2334+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
23352335
if (!pwallet) return NullUniValue;
23362336

23372337
LOCK(pwallet->cs_wallet);
@@ -2424,9 +2424,9 @@ static RPCHelpMan getbalances()
24242424
HelpExampleRpc("getbalances", "")},
24252425
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
24262426
{
2427-
std::shared_ptr<CWallet> const rpc_wallet = GetWalletForJSONRPCRequest(request);
2427+
const std::shared_ptr<const CWallet> rpc_wallet = GetWalletForJSONRPCRequest(request);
24282428
if (!rpc_wallet) return NullUniValue;
2429-
CWallet& wallet = *rpc_wallet;
2429+
const CWallet& wallet = *rpc_wallet;
24302430

24312431
// Make sure the results are valid at least up to the most recent block
24322432
// the user could have gotten from another RPC command prior to now
@@ -2500,7 +2500,7 @@ static RPCHelpMan getwalletinfo()
25002500
},
25012501
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
25022502
{
2503-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
2503+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
25042504
if (!pwallet) return NullUniValue;
25052505

25062506
// Make sure the results are valid at least up to the most recent block
@@ -3030,7 +3030,7 @@ static RPCHelpMan listunspent()
30303030
},
30313031
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
30323032
{
3033-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
3033+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
30343034
if (!pwallet) return NullUniValue;
30353035

30363036
int nMinDepth = 1;
@@ -3593,7 +3593,7 @@ RPCHelpMan signrawtransactionwithwallet()
35933593
},
35943594
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
35953595
{
3596-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
3596+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
35973597
if (!pwallet) return NullUniValue;
35983598

35993599
RPCTypeCheck(request.params, {UniValue::VSTR, UniValue::VARR, UniValue::VSTR}, true);
@@ -4058,7 +4058,7 @@ RPCHelpMan getaddressinfo()
40584058
},
40594059
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
40604060
{
4061-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
4061+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
40624062
if (!pwallet) return NullUniValue;
40634063

40644064
LOCK(pwallet->cs_wallet);
@@ -4165,7 +4165,7 @@ static RPCHelpMan getaddressesbylabel()
41654165
},
41664166
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
41674167
{
4168-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
4168+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
41694169
if (!pwallet) return NullUniValue;
41704170

41714171
LOCK(pwallet->cs_wallet);
@@ -4226,7 +4226,7 @@ static RPCHelpMan listlabels()
42264226
},
42274227
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
42284228
{
4229-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
4229+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
42304230
if (!pwallet) return NullUniValue;
42314231

42324232
LOCK(pwallet->cs_wallet);
@@ -4555,7 +4555,7 @@ static RPCHelpMan walletprocesspsbt()
45554555
},
45564556
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
45574557
{
4558-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
4558+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
45594559
if (!pwallet) return NullUniValue;
45604560

45614561
const CWallet& wallet{*pwallet};

0 commit comments

Comments
 (0)