Skip to content

Commit e7feb73

Browse files
committed
Merge bitcoin/bitcoin#22805: refactor: use CWallet const shared pointers in dump{privkey,wallet}
d150fe3 refactor: use `CWallet` const shared pointers in dump{privkey,wallet} RPCs (Sebastian Falbesoner) ec2792d refactor: use const `LegacyScriptPubKeyMan` references in dump{privkey,wallet} RPCs (Sebastian Falbesoner) 29905c0 refactor: avoid multiple key->metadata lookups in dumpwallet RPC (Sebastian Falbesoner) Pull request description: ~~This PR is based on #22787 ("refactor: actual immutable pointing"), which should be reviewed first.~~ (merged by now) It aims to make the CWallet shared pointers actually immutable also for the `dumpprivkey` and `dumpwallet` RPC methods. For doing that, some more preparations are needed; we need a const-counterpart to the helper `EnsureLegacyScriptPubKeyMan` that accepts a const CWallet pointer and accordingly also returns a const `LegacyScriptPubKeyMan` instance. The metadata lookup in `dumpwallet` is changed to not need a mutable `ScriptPubKeyMan` instance by avoiding using the `operator[]` in its mapKeyMetadata map, which also avoids repeated lookups. ACKs for top commit: laanwj: Code review ACK d150fe3 Tree-SHA512: 90ac05e21f40c6d0ebb479a71c545da2fd89940b9ca3409d9f932abc983bf8830d34c45086e68880d0d1e994846fbefee7534eec142ff4268e0fa28a1a411d36
2 parents 4a87077 + d150fe3 commit e7feb73

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/wallet/rpcdump.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static std::string DecodeDumpString(const std::string &str) {
5757
return ret.str();
5858
}
5959

60-
static bool GetWalletAddressesForKey(LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
60+
static bool GetWalletAddressesForKey(const LegacyScriptPubKeyMan* spk_man, const CWallet& wallet, const CKeyID& keyid, std::string& strAddr, std::string& strLabel) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
6161
{
6262
bool fLabelFound = false;
6363
CKey key;
@@ -681,10 +681,10 @@ RPCHelpMan dumpprivkey()
681681
},
682682
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
683683
{
684-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
684+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
685685
if (!pwallet) return NullUniValue;
686686

687-
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
687+
const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(*pwallet);
688688

689689
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
690690

@@ -731,11 +731,11 @@ RPCHelpMan dumpwallet()
731731
},
732732
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
733733
{
734-
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
734+
const std::shared_ptr<const CWallet> pwallet = GetWalletForJSONRPCRequest(request);
735735
if (!pwallet) return NullUniValue;
736736

737-
CWallet& wallet = *pwallet;
738-
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(wallet);
737+
const CWallet& wallet = *pwallet;
738+
const LegacyScriptPubKeyMan& spk_man = EnsureConstLegacyScriptPubKeyMan(wallet);
739739

740740
// Make sure the results are valid at least up to the most recent block
741741
// the user could have gotten from another RPC command prior to now
@@ -809,19 +809,22 @@ RPCHelpMan dumpwallet()
809809
std::string strLabel;
810810
CKey key;
811811
if (spk_man.GetKey(keyid, key)) {
812+
CKeyMetadata metadata;
813+
const auto it{spk_man.mapKeyMetadata.find(keyid)};
814+
if (it != spk_man.mapKeyMetadata.end()) metadata = it->second;
812815
file << strprintf("%s %s ", EncodeSecret(key), strTime);
813816
if (GetWalletAddressesForKey(&spk_man, wallet, keyid, strAddr, strLabel)) {
814817
file << strprintf("label=%s", strLabel);
815818
} else if (keyid == seed_id) {
816819
file << "hdseed=1";
817820
} else if (mapKeyPool.count(keyid)) {
818821
file << "reserve=1";
819-
} else if (spk_man.mapKeyMetadata[keyid].hdKeypath == "s") {
822+
} else if (metadata.hdKeypath == "s") {
820823
file << "inactivehdseed=1";
821824
} else {
822825
file << "change=1";
823826
}
824-
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man.mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man.mapKeyMetadata[keyid].key_origin.path) : ""));
827+
file << strprintf(" # addr=%s%s\n", strAddr, (metadata.has_key_origin ? " hdkeypath="+WriteHDKeypath(metadata.key_origin.path) : ""));
825828
}
826829
}
827830
file << "\n";

src/wallet/rpcwallet.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_cr
150150
return *spk_man;
151151
}
152152

153+
const LegacyScriptPubKeyMan& EnsureConstLegacyScriptPubKeyMan(const CWallet& wallet)
154+
{
155+
const LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
156+
if (!spk_man) {
157+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
158+
}
159+
return *spk_man;
160+
}
161+
153162
static void WalletTxToJSON(const CWallet& wallet, const CWalletTx& wtx, UniValue& entry)
154163
{
155164
interfaces::Chain& chain = wallet.chain();

src/wallet/rpcwallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
3434
void EnsureWalletIsUnlocked(const CWallet&);
3535
WalletContext& EnsureWalletContext(const std::any& context);
3636
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet, bool also_create = false);
37+
const LegacyScriptPubKeyMan& EnsureConstLegacyScriptPubKeyMan(const CWallet& wallet);
3738

3839
RPCHelpMan getaddressinfo();
3940
RPCHelpMan signrawtransactionwithwallet();

0 commit comments

Comments
 (0)