Skip to content

Commit c13d3c3

Browse files
committed
rpc: replace raw pointers with shared_ptrs
Co-authored-by: MarcoFalke <[email protected]> Co-authored-by: João Barbosa <[email protected]>
1 parent 7bcc42b commit c13d3c3

File tree

3 files changed

+146
-199
lines changed

3 files changed

+146
-199
lines changed

src/wallet/rpcdump.cpp

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ static std::string DecodeDumpString(const std::string &str) {
5656
return ret.str();
5757
}
5858

59-
static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWallet* const pwallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
59+
static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
6060
{
6161
bool fLabelFound = false;
6262
CKey key;
6363
spk_man->GetKey(keyid, key);
6464
for (const auto& dest : GetAllDestinationsForKey(key.GetPubKey())) {
65-
const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
65+
const auto* address_book_entry = wallet.FindAddressBookEntry(dest);
6666
if (address_book_entry) {
6767
if (!strAddr.empty()) {
6868
strAddr += ",";
@@ -73,7 +73,7 @@ static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWall
7373
}
7474
}
7575
if (!fLabelFound) {
76-
strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), pwallet->m_default_address_type));
76+
strAddr = EncodeDestination(GetDestinationForKey(key.GetPubKey(), wallet.m_default_address_type));
7777
}
7878
return fLabelFound;
7979
}
@@ -92,8 +92,7 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
9292

9393
UniValue importprivkey(const JSONRPCRequest& request)
9494
{
95-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
96-
CWallet* const pwallet = wallet.get();
95+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
9796
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
9897
return NullUniValue;
9998
}
@@ -128,14 +127,14 @@ UniValue importprivkey(const JSONRPCRequest& request)
128127
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
129128
}
130129

131-
EnsureLegacyScriptPubKeyMan(*wallet, true);
130+
EnsureLegacyScriptPubKeyMan(*pwallet, true);
132131

133132
WalletRescanReserver reserver(*pwallet);
134133
bool fRescan = true;
135134
{
136135
LOCK(pwallet->cs_wallet);
137136

138-
EnsureWalletIsUnlocked(pwallet);
137+
EnsureWalletIsUnlocked(*pwallet);
139138

140139
std::string strSecret = request.params[0].get_str();
141140
std::string strLabel = "";
@@ -195,8 +194,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
195194

196195
UniValue abortrescan(const JSONRPCRequest& request)
197196
{
198-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
199-
CWallet* const pwallet = wallet.get();
197+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
200198
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
201199
return NullUniValue;
202200
}
@@ -223,8 +221,7 @@ UniValue abortrescan(const JSONRPCRequest& request)
223221

224222
UniValue importaddress(const JSONRPCRequest& request)
225223
{
226-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
227-
CWallet* const pwallet = wallet.get();
224+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
228225
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
229226
return NullUniValue;
230227
}
@@ -325,8 +322,7 @@ UniValue importaddress(const JSONRPCRequest& request)
325322

326323
UniValue importprunedfunds(const JSONRPCRequest& request)
327324
{
328-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
329-
CWallet* const pwallet = wallet.get();
325+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
330326
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
331327
return NullUniValue;
332328
}
@@ -383,8 +379,7 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
383379

384380
UniValue removeprunedfunds(const JSONRPCRequest& request)
385381
{
386-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
387-
CWallet* const pwallet = wallet.get();
382+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
388383
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
389384
return NullUniValue;
390385
}
@@ -422,8 +417,7 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
422417

423418
UniValue importpubkey(const JSONRPCRequest& request)
424419
{
425-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
426-
CWallet* const pwallet = wallet.get();
420+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
427421
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
428422
return NullUniValue;
429423
}
@@ -450,7 +444,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
450444
},
451445
}.Check(request);
452446

453-
EnsureLegacyScriptPubKeyMan(*wallet, true);
447+
EnsureLegacyScriptPubKeyMan(*pwallet, true);
454448

455449
std::string strLabel;
456450
if (!request.params[1].isNull())
@@ -509,8 +503,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
509503

510504
UniValue importwallet(const JSONRPCRequest& request)
511505
{
512-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
513-
CWallet* const pwallet = wallet.get();
506+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
514507
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
515508
return NullUniValue;
516509
}
@@ -532,7 +525,7 @@ UniValue importwallet(const JSONRPCRequest& request)
532525
},
533526
}.Check(request);
534527

535-
EnsureLegacyScriptPubKeyMan(*wallet, true);
528+
EnsureLegacyScriptPubKeyMan(*pwallet, true);
536529

537530
if (pwallet->chain().havePruned()) {
538531
// Exit early and print an error.
@@ -551,7 +544,7 @@ UniValue importwallet(const JSONRPCRequest& request)
551544
{
552545
LOCK(pwallet->cs_wallet);
553546

554-
EnsureWalletIsUnlocked(pwallet);
547+
EnsureWalletIsUnlocked(*pwallet);
555548

556549
fsbridge::ifstream file;
557550
file.open(request.params[0].get_str(), std::ios::in | std::ios::ate);
@@ -667,8 +660,7 @@ UniValue importwallet(const JSONRPCRequest& request)
667660

668661
UniValue dumpprivkey(const JSONRPCRequest& request)
669662
{
670-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
671-
const CWallet* const pwallet = wallet.get();
663+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
672664
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
673665
return NullUniValue;
674666
}
@@ -689,11 +681,11 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
689681
},
690682
}.Check(request);
691683

692-
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
684+
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
693685

694686
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
695687

696-
EnsureWalletIsUnlocked(pwallet);
688+
EnsureWalletIsUnlocked(*pwallet);
697689

698690
std::string strAddress = request.params[0].get_str();
699691
CTxDestination dest = DecodeDestination(strAddress);
@@ -715,7 +707,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
715707
UniValue dumpwallet(const JSONRPCRequest& request)
716708
{
717709
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
718-
if (!EnsureWalletIsAvailable(pwallet.get(), request.fHelp)) {
710+
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
719711
return NullUniValue;
720712
}
721713

@@ -748,7 +740,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
748740

749741
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
750742

751-
EnsureWalletIsUnlocked(&wallet);
743+
EnsureWalletIsUnlocked(wallet);
752744

753745
fs::path filepath = request.params[0].get_str();
754746
filepath = fs::absolute(filepath);
@@ -810,7 +802,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
810802
CKey key;
811803
if (spk_man.GetKey(keyid, key)) {
812804
file << strprintf("%s %s ", EncodeSecret(key), strTime);
813-
if (GetWalletAddressesForKey(&spk_man, &wallet, keyid, strAddr, strLabel)) {
805+
if (GetWalletAddressesForKey(&spk_man, *pwallet, keyid, strAddr, strLabel)) {
814806
file << strprintf("label=%s", strLabel);
815807
} else if (keyid == seed_id) {
816808
file << "hdseed=1";
@@ -1167,7 +1159,7 @@ static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID
11671159
return warnings;
11681160
}
11691161

1170-
static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1162+
static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
11711163
{
11721164
UniValue warnings(UniValue::VARR);
11731165
UniValue result(UniValue::VOBJ);
@@ -1182,7 +1174,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
11821174
const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
11831175

11841176
// Add to keypool only works with privkeys disabled
1185-
if (add_keypool && !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1177+
if (add_keypool && !wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
11861178
throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled");
11871179
}
11881180

@@ -1204,29 +1196,29 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
12041196
}
12051197

12061198
// If private keys are disabled, abort if private keys are being imported
1207-
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
1199+
if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
12081200
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
12091201
}
12101202

12111203
// Check whether we have any work to do
12121204
for (const CScript& script : script_pub_keys) {
1213-
if (pwallet->IsMine(script) & ISMINE_SPENDABLE) {
1205+
if (wallet.IsMine(script) & ISMINE_SPENDABLE) {
12141206
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script.begin(), script.end()) + "\")");
12151207
}
12161208
}
12171209

12181210
// All good, time to import
1219-
pwallet->MarkDirty();
1220-
if (!pwallet->ImportScripts(import_data.import_scripts, timestamp)) {
1211+
wallet.MarkDirty();
1212+
if (!wallet.ImportScripts(import_data.import_scripts, timestamp)) {
12211213
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet");
12221214
}
1223-
if (!pwallet->ImportPrivKeys(privkey_map, timestamp)) {
1215+
if (!wallet.ImportPrivKeys(privkey_map, timestamp)) {
12241216
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
12251217
}
1226-
if (!pwallet->ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
1218+
if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
12271219
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
12281220
}
1229-
if (!pwallet->ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
1221+
if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
12301222
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
12311223
}
12321224

@@ -1259,8 +1251,7 @@ static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
12591251

12601252
UniValue importmulti(const JSONRPCRequest& mainRequest)
12611253
{
1262-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(mainRequest);
1263-
CWallet* const pwallet = wallet.get();
1254+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(mainRequest);
12641255
if (!EnsureWalletIsAvailable(pwallet, mainRequest.fHelp)) {
12651256
return NullUniValue;
12661257
}
@@ -1343,7 +1334,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13431334

13441335
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13451336

1346-
EnsureLegacyScriptPubKeyMan(*wallet, true);
1337+
EnsureLegacyScriptPubKeyMan(*pwallet, true);
13471338

13481339
const UniValue& requests = mainRequest.params[0];
13491340

@@ -1369,7 +1360,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13691360
UniValue response(UniValue::VARR);
13701361
{
13711362
LOCK(pwallet->cs_wallet);
1372-
EnsureWalletIsUnlocked(pwallet);
1363+
EnsureWalletIsUnlocked(*pwallet);
13731364

13741365
// Verify all timestamps are present before importing any keys.
13751366
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
@@ -1381,7 +1372,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13811372

13821373
for (const UniValue& data : requests.getValues()) {
13831374
const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
1384-
const UniValue result = ProcessImport(pwallet, data, timestamp);
1375+
const UniValue result = ProcessImport(*pwallet, data, timestamp);
13851376
response.push_back(result);
13861377

13871378
if (!fRescan) {
@@ -1446,7 +1437,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
14461437
return response;
14471438
}
14481439

1449-
static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1440+
static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
14501441
{
14511442
UniValue warnings(UniValue::VARR);
14521443
UniValue result(UniValue::VOBJ);
@@ -1515,7 +1506,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15151506
}
15161507

15171508
// If the wallet disabled private keys, abort if private keys exist
1518-
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
1509+
if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
15191510
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
15201511
}
15211512

@@ -1537,7 +1528,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15371528
}
15381529

15391530
// If private keys are enabled, check some things.
1540-
if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1531+
if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
15411532
if (keys.keys.empty()) {
15421533
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
15431534
}
@@ -1549,7 +1540,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15491540
WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
15501541

15511542
// Check if the wallet already contains the descriptor
1552-
auto existing_spk_manager = pwallet->GetDescriptorScriptPubKeyMan(w_desc);
1543+
auto existing_spk_manager = wallet.GetDescriptorScriptPubKeyMan(w_desc);
15531544
if (existing_spk_manager) {
15541545
LOCK(existing_spk_manager->cs_desc_man);
15551546
if (range_start > existing_spk_manager->GetWalletDescriptor().range_start) {
@@ -1558,7 +1549,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15581549
}
15591550

15601551
// Add descriptor to the wallet
1561-
auto spk_manager = pwallet->AddWalletDescriptor(w_desc, keys, label);
1552+
auto spk_manager = wallet.AddWalletDescriptor(w_desc, keys, label);
15621553
if (spk_manager == nullptr) {
15631554
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s'", descriptor));
15641555
}
@@ -1568,7 +1559,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15681559
if (!w_desc.descriptor->GetOutputType()) {
15691560
warnings.push_back("Unknown output type, cannot set descriptor to active.");
15701561
} else {
1571-
pwallet->SetActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
1562+
wallet.SetActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
15721563
}
15731564
}
15741565

@@ -1587,8 +1578,8 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15871578

15881579
UniValue importdescriptors(const JSONRPCRequest& main_request) {
15891580
// Acquire the wallet
1590-
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(main_request);
1591-
CWallet* const pwallet = wallet.get();
1581+
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(main_request);
1582+
15921583
if (!EnsureWalletIsAvailable(pwallet, main_request.fHelp)) {
15931584
return NullUniValue;
15941585
}
@@ -1664,15 +1655,15 @@ UniValue importdescriptors(const JSONRPCRequest& main_request) {
16641655
UniValue response(UniValue::VARR);
16651656
{
16661657
LOCK(pwallet->cs_wallet);
1667-
EnsureWalletIsUnlocked(pwallet);
1658+
EnsureWalletIsUnlocked(*pwallet);
16681659

16691660
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(lowest_timestamp).mtpTime(now)));
16701661

16711662
// Get all timestamps and extract the lowest timestamp
16721663
for (const UniValue& request : requests.getValues()) {
16731664
// This throws an error if "timestamp" doesn't exist
16741665
const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
1675-
const UniValue result = ProcessDescriptorImport(pwallet, request, timestamp);
1666+
const UniValue result = ProcessDescriptorImport(*pwallet, request, timestamp);
16761667
response.push_back(result);
16771668

16781669
if (lowest_timestamp > timestamp ) {

0 commit comments

Comments
 (0)