Skip to content

Commit 7c90c67

Browse files
fanquakeMarcoFalkepromag
committed
rpc: refactor rpc wallet functions to take references instead of pointers
Co-authored-by: MarcoFalke <[email protected]> Co-authored-by: João Barbosa <[email protected]>
1 parent 4866934 commit 7c90c67

File tree

3 files changed

+85
-86
lines changed

3 files changed

+85
-86
lines changed

src/wallet/rpcdump.cpp

Lines changed: 28 additions & 28 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
}
@@ -132,7 +132,7 @@ RPCHelpMan importprivkey()
132132
{
133133
LOCK(pwallet->cs_wallet);
134134

135-
EnsureWalletIsUnlocked(pwallet.get());
135+
EnsureWalletIsUnlocked(*pwallet);
136136

137137
std::string strSecret = request.params[0].get_str();
138138
std::string strLabel = "";
@@ -543,7 +543,7 @@ RPCHelpMan importwallet()
543543
{
544544
LOCK(pwallet->cs_wallet);
545545

546-
EnsureWalletIsUnlocked(pwallet.get());
546+
EnsureWalletIsUnlocked(*pwallet);
547547

548548
fsbridge::ifstream file;
549549
file.open(request.params[0].get_str(), std::ios::in | std::ios::ate);
@@ -684,7 +684,7 @@ RPCHelpMan dumpprivkey()
684684

685685
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
686686

687-
EnsureWalletIsUnlocked(pwallet.get());
687+
EnsureWalletIsUnlocked(*pwallet);
688688

689689
std::string strAddress = request.params[0].get_str();
690690
CTxDestination dest = DecodeDestination(strAddress);
@@ -739,7 +739,7 @@ RPCHelpMan dumpwallet()
739739

740740
LOCK2(wallet.cs_wallet, spk_man.cs_KeyStore);
741741

742-
EnsureWalletIsUnlocked(&wallet);
742+
EnsureWalletIsUnlocked(wallet);
743743

744744
fs::path filepath = request.params[0].get_str();
745745
filepath = fs::absolute(filepath);
@@ -801,7 +801,7 @@ RPCHelpMan dumpwallet()
801801
CKey key;
802802
if (spk_man.GetKey(keyid, key)) {
803803
file << strprintf("%s %s ", EncodeSecret(key), strTime);
804-
if (GetWalletAddressesForKey(&spk_man, &wallet, keyid, strAddr, strLabel)) {
804+
if (GetWalletAddressesForKey(&spk_man, wallet, keyid, strAddr, strLabel)) {
805805
file << strprintf("label=%s", strLabel);
806806
} else if (keyid == seed_id) {
807807
file << "hdseed=1";
@@ -1161,7 +1161,7 @@ static UniValue ProcessImportDescriptor(ImportData& import_data, std::map<CKeyID
11611161
return warnings;
11621162
}
11631163

1164-
static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1164+
static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
11651165
{
11661166
UniValue warnings(UniValue::VARR);
11671167
UniValue result(UniValue::VOBJ);
@@ -1176,7 +1176,7 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
11761176
const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
11771177

11781178
// Add to keypool only works with privkeys disabled
1179-
if (add_keypool && !pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1179+
if (add_keypool && !wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
11801180
throw JSONRPCError(RPC_INVALID_PARAMETER, "Keys can only be imported to the keypool when private keys are disabled");
11811181
}
11821182

@@ -1198,29 +1198,29 @@ static UniValue ProcessImport(CWallet * const pwallet, const UniValue& data, con
11981198
}
11991199

12001200
// If private keys are disabled, abort if private keys are being imported
1201-
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
1201+
if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !privkey_map.empty()) {
12021202
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
12031203
}
12041204

12051205
// Check whether we have any work to do
12061206
for (const CScript& script : script_pub_keys) {
1207-
if (pwallet->IsMine(script) & ISMINE_SPENDABLE) {
1207+
if (wallet.IsMine(script) & ISMINE_SPENDABLE) {
12081208
throw JSONRPCError(RPC_WALLET_ERROR, "The wallet already contains the private key for this address or script (\"" + HexStr(script) + "\")");
12091209
}
12101210
}
12111211

12121212
// All good, time to import
1213-
pwallet->MarkDirty();
1214-
if (!pwallet->ImportScripts(import_data.import_scripts, timestamp)) {
1213+
wallet.MarkDirty();
1214+
if (!wallet.ImportScripts(import_data.import_scripts, timestamp)) {
12151215
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding script to wallet");
12161216
}
1217-
if (!pwallet->ImportPrivKeys(privkey_map, timestamp)) {
1217+
if (!wallet.ImportPrivKeys(privkey_map, timestamp)) {
12181218
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding key to wallet");
12191219
}
1220-
if (!pwallet->ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
1220+
if (!wallet.ImportPubKeys(ordered_pubkeys, pubkey_map, import_data.key_origins, add_keypool, internal, timestamp)) {
12211221
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
12221222
}
1223-
if (!pwallet->ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
1223+
if (!wallet.ImportScriptPubKeys(label, script_pub_keys, have_solving_data, !internal, timestamp)) {
12241224
throw JSONRPCError(RPC_WALLET_ERROR, "Error adding address to wallet");
12251225
}
12261226

@@ -1359,7 +1359,7 @@ RPCHelpMan importmulti()
13591359
UniValue response(UniValue::VARR);
13601360
{
13611361
LOCK(pwallet->cs_wallet);
1362-
EnsureWalletIsUnlocked(pwallet.get());
1362+
EnsureWalletIsUnlocked(*pwallet);
13631363

13641364
// Verify all timestamps are present before importing any keys.
13651365
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
@@ -1371,7 +1371,7 @@ RPCHelpMan importmulti()
13711371

13721372
for (const UniValue& data : requests.getValues()) {
13731373
const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
1374-
const UniValue result = ProcessImport(pwallet.get(), data, timestamp);
1374+
const UniValue result = ProcessImport(*pwallet, data, timestamp);
13751375
response.push_back(result);
13761376

13771377
if (!fRescan) {
@@ -1438,7 +1438,7 @@ RPCHelpMan importmulti()
14381438
};
14391439
}
14401440

1441-
static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1441+
static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
14421442
{
14431443
UniValue warnings(UniValue::VARR);
14441444
UniValue result(UniValue::VOBJ);
@@ -1507,7 +1507,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15071507
}
15081508

15091509
// If the wallet disabled private keys, abort if private keys exist
1510-
if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
1510+
if (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && !keys.keys.empty()) {
15111511
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
15121512
}
15131513

@@ -1531,7 +1531,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15311531
}
15321532

15331533
// If private keys are enabled, check some things.
1534-
if (!pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
1534+
if (!wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
15351535
if (keys.keys.empty()) {
15361536
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import descriptor without private keys to a wallet with private keys enabled");
15371537
}
@@ -1543,7 +1543,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15431543
WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index);
15441544

15451545
// Check if the wallet already contains the descriptor
1546-
auto existing_spk_manager = pwallet->GetDescriptorScriptPubKeyMan(w_desc);
1546+
auto existing_spk_manager = wallet.GetDescriptorScriptPubKeyMan(w_desc);
15471547
if (existing_spk_manager) {
15481548
LOCK(existing_spk_manager->cs_desc_man);
15491549
if (range_start > existing_spk_manager->GetWalletDescriptor().range_start) {
@@ -1552,7 +1552,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15521552
}
15531553

15541554
// Add descriptor to the wallet
1555-
auto spk_manager = pwallet->AddWalletDescriptor(w_desc, keys, label, internal);
1555+
auto spk_manager = wallet.AddWalletDescriptor(w_desc, keys, label, internal);
15561556
if (spk_manager == nullptr) {
15571557
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Could not add descriptor '%s'", descriptor));
15581558
}
@@ -1562,7 +1562,7 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15621562
if (!w_desc.descriptor->GetOutputType()) {
15631563
warnings.push_back("Unknown output type, cannot set descriptor to active.");
15641564
} else {
1565-
pwallet->AddActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
1565+
wallet.AddActiveScriptPubKeyMan(spk_manager->GetID(), *w_desc.descriptor->GetOutputType(), internal);
15661566
}
15671567
}
15681568

@@ -1655,15 +1655,15 @@ RPCHelpMan importdescriptors()
16551655
UniValue response(UniValue::VARR);
16561656
{
16571657
LOCK(pwallet->cs_wallet);
1658-
EnsureWalletIsUnlocked(pwallet.get());
1658+
EnsureWalletIsUnlocked(*pwallet);
16591659

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

16621662
// Get all timestamps and extract the lowest timestamp
16631663
for (const UniValue& request : requests.getValues()) {
16641664
// This throws an error if "timestamp" doesn't exist
16651665
const int64_t timestamp = std::max(GetImportTimestamp(request, now), minimum_timestamp);
1666-
const UniValue result = ProcessDescriptorImport(pwallet.get(), request, timestamp);
1666+
const UniValue result = ProcessDescriptorImport(*pwallet, request, timestamp);
16671667
response.push_back(result);
16681668

16691669
if (lowest_timestamp > timestamp ) {
@@ -1765,7 +1765,7 @@ RPCHelpMan listdescriptors()
17651765
throw JSONRPCError(RPC_WALLET_ERROR, "listdescriptors is not available for non-descriptor wallets");
17661766
}
17671767

1768-
EnsureWalletIsUnlocked(wallet.get());
1768+
EnsureWalletIsUnlocked(*wallet);
17691769

17701770
LOCK(wallet->cs_wallet);
17711771

0 commit comments

Comments
 (0)