Skip to content

Commit 100fa0a

Browse files
author
MarcoFalke
committed
Merge #17300: LegacyScriptPubKeyMan code cleanups
53fe0b7 Fix missing strFailReason in CreateTransaction (Russell Yanofsky) 4b28a05 Fix misplaced AssertLockHeld (Russell Yanofsky) 2632b1f doc: Clarify WalletStorage / Wallet relation (Russell Yanofsky) 628d11b Add back mistakenly removed AssertLockHeld (Russell Yanofsky) 52cf68f Refactor: Add GetLegacyScriptPubKeyMan helper (Russell Yanofsky) Pull request description: This PR implements suggested code cleanups from bitcoin/bitcoin#17260 review comments ACKs for top commit: Sjors: ACK 53fe0b7 achow101: ACK 53fe0b7 MarcoFalke: ACK 53fe0b7 Tree-SHA512: a577b96cb21a9aa7185d7d900e4db0665c302adcd12097957b9d8e838a8548c7de8f901bcb83e7c46d231b841221345c9264f5e29ed069f3d9236896430f959b
2 parents 1c5e0cc + 53fe0b7 commit 100fa0a

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

src/wallet/rpcdump.cpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
8787
}
8888
}
8989

90+
static LegacyScriptPubKeyMan& GetLegacyScriptPubKeyMan(CWallet& wallet)
91+
{
92+
LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
93+
if (!spk_man) {
94+
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
95+
}
96+
return *spk_man;
97+
}
98+
9099
UniValue importprivkey(const JSONRPCRequest& request)
91100
{
92101
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -125,10 +134,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
125134
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
126135
}
127136

128-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
129-
if (!spk_man) {
130-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
131-
}
137+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
132138

133139
WalletRescanReserver reserver(pwallet);
134140
bool fRescan = true;
@@ -256,10 +262,7 @@ UniValue importaddress(const JSONRPCRequest& request)
256262
},
257263
}.Check(request);
258264

259-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
260-
if (!spk_man) {
261-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
262-
}
265+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*pwallet);
263266

264267
std::string strLabel;
265268
if (!request.params[1].isNull())
@@ -462,10 +465,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
462465
},
463466
}.Check(request);
464467

465-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
466-
if (!spk_man) {
467-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
468-
}
468+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
469469

470470
std::string strLabel;
471471
if (!request.params[1].isNull())
@@ -549,10 +549,7 @@ UniValue importwallet(const JSONRPCRequest& request)
549549
},
550550
}.Check(request);
551551

552-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
553-
if (!spk_man) {
554-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
555-
}
552+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
556553

557554
if (pwallet->chain().havePruned()) {
558555
// Exit early and print an error.
@@ -711,10 +708,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
711708
},
712709
}.Check(request);
713710

714-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
715-
if (!spk_man) {
716-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
717-
}
711+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
718712

719713
auto locked_chain = pwallet->chain().lock();
720714
LOCK(pwallet->cs_wallet);
@@ -726,12 +720,12 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
726720
if (!IsValidDestination(dest)) {
727721
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
728722
}
729-
auto keyid = GetKeyForDestination(*spk_man, dest);
723+
auto keyid = GetKeyForDestination(spk_man, dest);
730724
if (keyid.IsNull()) {
731725
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to a key");
732726
}
733727
CKey vchSecret;
734-
if (!spk_man->GetKey(keyid, vchSecret)) {
728+
if (!spk_man.GetKey(keyid, vchSecret)) {
735729
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
736730
}
737731
return EncodeSecret(vchSecret);
@@ -765,14 +759,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
765759
},
766760
}.Check(request);
767761

768-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
769-
if (!spk_man) {
770-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
771-
}
762+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
772763

773764
auto locked_chain = pwallet->chain().lock();
774765
LOCK(pwallet->cs_wallet);
775-
AssertLockHeld(spk_man->cs_wallet);
766+
AssertLockHeld(spk_man.cs_wallet);
776767

777768
EnsureWalletIsUnlocked(pwallet);
778769

@@ -794,10 +785,10 @@ UniValue dumpwallet(const JSONRPCRequest& request)
794785
throw JSONRPCError(RPC_INVALID_PARAMETER, "Cannot open wallet dump file");
795786

796787
std::map<CKeyID, int64_t> mapKeyBirth;
797-
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man->GetAllReserveKeys();
788+
const std::map<CKeyID, int64_t>& mapKeyPool = spk_man.GetAllReserveKeys();
798789
pwallet->GetKeyBirthTimes(*locked_chain, mapKeyBirth);
799790

800-
std::set<CScriptID> scripts = spk_man->GetCScripts();
791+
std::set<CScriptID> scripts = spk_man.GetCScripts();
801792

802793
// sort time/key pairs
803794
std::vector<std::pair<int64_t, CKeyID> > vKeyBirth;
@@ -816,11 +807,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
816807
file << "\n";
817808

818809
// add the base58check encoded extended master if the wallet uses HD
819-
CKeyID seed_id = spk_man->GetHDChain().seed_id;
810+
CKeyID seed_id = spk_man.GetHDChain().seed_id;
820811
if (!seed_id.IsNull())
821812
{
822813
CKey seed;
823-
if (spk_man->GetKey(seed_id, seed)) {
814+
if (spk_man.GetKey(seed_id, seed)) {
824815
CExtKey masterKey;
825816
masterKey.SetSeed(seed.begin(), seed.size());
826817

@@ -833,20 +824,20 @@ UniValue dumpwallet(const JSONRPCRequest& request)
833824
std::string strAddr;
834825
std::string strLabel;
835826
CKey key;
836-
if (spk_man->GetKey(keyid, key)) {
827+
if (spk_man.GetKey(keyid, key)) {
837828
file << strprintf("%s %s ", EncodeSecret(key), strTime);
838-
if (GetWalletAddressesForKey(spk_man, pwallet, keyid, strAddr, strLabel)) {
829+
if (GetWalletAddressesForKey(&spk_man, pwallet, keyid, strAddr, strLabel)) {
839830
file << strprintf("label=%s", strLabel);
840831
} else if (keyid == seed_id) {
841832
file << "hdseed=1";
842833
} else if (mapKeyPool.count(keyid)) {
843834
file << "reserve=1";
844-
} else if (spk_man->mapKeyMetadata[keyid].hdKeypath == "s") {
835+
} else if (spk_man.mapKeyMetadata[keyid].hdKeypath == "s") {
845836
file << "inactivehdseed=1";
846837
} else {
847838
file << "change=1";
848839
}
849-
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man->mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man->mapKeyMetadata[keyid].key_origin.path) : ""));
840+
file << strprintf(" # addr=%s%s\n", strAddr, (spk_man.mapKeyMetadata[keyid].has_key_origin ? " hdkeypath="+WriteHDKeypath(spk_man.mapKeyMetadata[keyid].key_origin.path) : ""));
850841
}
851842
}
852843
file << "\n";
@@ -855,11 +846,11 @@ UniValue dumpwallet(const JSONRPCRequest& request)
855846
std::string create_time = "0";
856847
std::string address = EncodeDestination(ScriptHash(scriptid));
857848
// get birth times for scripts with metadata
858-
auto it = spk_man->m_script_metadata.find(scriptid);
859-
if (it != spk_man->m_script_metadata.end()) {
849+
auto it = spk_man.m_script_metadata.find(scriptid);
850+
if (it != spk_man.m_script_metadata.end()) {
860851
create_time = FormatISO8601DateTime(it->second.nCreateTime);
861852
}
862-
if(spk_man->GetCScript(scriptid, script)) {
853+
if(spk_man.GetCScript(scriptid, script)) {
863854
file << strprintf("%s %s script=1", HexStr(script.begin(), script.end()), create_time);
864855
file << strprintf(" # addr=%s\n", address);
865856
}
@@ -1355,10 +1346,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
13551346

13561347
RPCTypeCheck(mainRequest.params, {UniValue::VARR, UniValue::VOBJ});
13571348

1358-
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
1359-
if (!spk_man) {
1360-
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
1361-
}
1349+
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
13621350

13631351
const UniValue& requests = mainRequest.params[0];
13641352

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ bool LegacyScriptPubKeyMan::AddKeyPubKey(const CKey& secret, const CPubKey &pubk
386386

387387
bool LegacyScriptPubKeyMan::AddKeyPubKeyWithDB(WalletBatch& batch, const CKey& secret, const CPubKey& pubkey)
388388
{
389+
AssertLockHeld(cs_wallet);
390+
389391
// Make sure we aren't adding private keys to private key disabled wallets
390392
assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
391393

src/wallet/scriptpubkeyman.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum class OutputType;
2020
// It provides access to things that are part of the entire wallet and not specific to a ScriptPubKeyMan such as
2121
// wallet flags, wallet version, encryption keys, encryption status, and the database itself. This allows a
2222
// ScriptPubKeyMan to have callbacks into CWallet without causing a circular dependency.
23-
// WalletStorage should be the same for all ScriptPubKeyMans.
23+
// WalletStorage should be the same for all ScriptPubKeyMans of a wallet.
2424
class WalletStorage
2525
{
2626
public:

src/wallet/wallet.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
249249

250250
void CWallet::UpgradeKeyMetadata()
251251
{
252-
AssertLockHeld(m_spk_man->cs_wallet);
253252
if (m_spk_man) {
253+
AssertLockHeld(m_spk_man->cs_wallet);
254254
m_spk_man->UpgradeKeyMetadata();
255255
}
256256
}
@@ -2783,11 +2783,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
27832783
SignatureData sigdata;
27842784

27852785
const SigningProvider* provider = GetSigningProvider();
2786-
if (!provider) {
2787-
return false;
2788-
}
2789-
2790-
if (!ProduceSignature(*provider, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
2786+
if (!provider || !ProduceSignature(*provider, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
27912787
{
27922788
strFailReason = _("Signing transaction failed").translated;
27932789
return false;

0 commit comments

Comments
 (0)