Merge #17381: LegacyScriptPubKeyMan code cleanups

05b224a175065aee4d6d9c471722bc4503f01fdf Add missing SetupGeneration error handling in EncryptWallet (Russell Yanofsky)
bfd826a675445801adec86a469040f3ceb8172ee Clean up nested scope in GetReservedDestination (Russell Yanofsky)
491a599b37f3e3a648e52aebed677ca11b0615e2 Get rid of confusing LegacyScriptPubKeyMan::TopUpKeyPool method (Russell Yanofsky)
4a0abf694ee10cf186f25a67ca35c3fce0c10874 Pass CTxDestination to ScriptPubKeyMan::GetMetadata (Russell Yanofsky)
b07b07cd8779355ba1dd16e7eb4af42e0ae1c587 Add EnsureLegacyScriptPubKeyMan and use in rpcwallet.cpp (Russell Yanofsky)

Pull request description:

  This PR implements suggested code cleanups from #17300 and #17304 review comments

ACKs for top commit:
  Sjors:
    re-ACK 05b224a
  laanwj:
    Code review ACK 05b224a175065aee4d6d9c471722bc4503f01fdf

Tree-SHA512: 12fd86637088515b744c028e0501c5d21a9cf9ee9c9cfd70e9cb65d44611ea5643abd5f6f101105caa5aff015d74de606f074f08af7dae8429f929d21288ab45
This commit is contained in:
Wladimir J. van der Laan 2019-11-06 17:14:45 +01:00 committed by PastaPastaPasta
parent aff510d914
commit 041620beb8
6 changed files with 53 additions and 57 deletions

View File

@ -66,15 +66,6 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
}
}
static LegacyScriptPubKeyMan& GetLegacyScriptPubKeyMan(CWallet& wallet)
{
LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
if (!spk_man) {
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
}
return *spk_man;
}
UniValue importprivkey(const JSONRPCRequest& request)
{
RPCHelpMan{"importprivkey",
@ -110,7 +101,7 @@ UniValue importprivkey(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot import private keys to a wallet with private keys disabled");
}
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
WalletBatch batch(pwallet->GetDBHandle());
WalletRescanReserver reserver(pwallet);
@ -221,6 +212,7 @@ UniValue importaddress(const JSONRPCRequest& request)
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
EnsureLegacyScriptPubKeyMan(*pwallet);
std::string strLabel;
if (!request.params[1].isNull())
@ -410,6 +402,7 @@ UniValue importpubkey(const JSONRPCRequest& request)
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
EnsureLegacyScriptPubKeyMan(*wallet);
std::string strLabel;
if (!request.params[1].isNull())
@ -486,6 +479,7 @@ UniValue importwallet(const JSONRPCRequest& request)
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
EnsureLegacyScriptPubKeyMan(*wallet);
if (pwallet->chain().havePruned()) {
// Exit early and print an error.
@ -653,7 +647,7 @@ UniValue importelectrumwallet(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Private keys are disabled for this wallet");
}
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
LOCK(pwallet->cs_wallet);
AssertLockHeld(spk_man.cs_wallet);
@ -822,7 +816,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
LOCK(pwallet->cs_wallet);
@ -871,7 +865,7 @@ UniValue dumphdinfo(const JSONRPCRequest& request)
EnsureWalletIsUnlocked(pwallet);
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
CHDChain hdChainCurrent;
if (!spk_man.GetHDChain(hdChainCurrent))
throw JSONRPCError(RPC_WALLET_ERROR, "This wallet is not a HD wallet.");
@ -919,7 +913,7 @@ UniValue dumpwallet(const JSONRPCRequest& request)
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
LegacyScriptPubKeyMan& spk_man = GetLegacyScriptPubKeyMan(*wallet);
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*wallet);
LOCK(pwallet->cs_wallet);
AssertLockHeld(spk_man.cs_wallet);
@ -1515,7 +1509,7 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
GetLegacyScriptPubKeyMan(*wallet);
EnsureLegacyScriptPubKeyMan(*wallet);
//Default options
bool fRescan = true;

View File

@ -129,6 +129,15 @@ void EnsureWalletIsUnlocked(const CWallet* pwallet)
}
}
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet)
{
LegacyScriptPubKeyMan* spk_man = wallet.GetLegacyScriptPubKeyMan();
if (!spk_man) {
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
}
return *spk_man;
}
WalletContext& EnsureWalletContext(const CoreContext& context)
{
auto* wallet_context = GetContext<WalletContext>(context);
@ -995,10 +1004,7 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
if (!spk_man) {
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
}
LegacyScriptPubKeyMan& spk_man = EnsureLegacyScriptPubKeyMan(*pwallet);
LOCK(pwallet->cs_wallet);
@ -1015,14 +1021,14 @@ static UniValue addmultisigaddress(const JSONRPCRequest& request)
if (IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) {
pubkeys.push_back(HexToPubKey(keys_or_addrs[i].get_str()));
} else {
pubkeys.push_back(AddrToPubKey(spk_man, keys_or_addrs[i].get_str()));
pubkeys.push_back(AddrToPubKey(&spk_man, keys_or_addrs[i].get_str()));
}
}
// Construct using pay-to-script-hash:
CScript inner = CreateMultisigRedeemscript(required, pubkeys);
CScriptID innerID(inner);
spk_man->AddCScript(inner);
spk_man.AddCScript(inner);
pwallet->SetAddressBook(innerID, label, "send");
@ -3691,14 +3697,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
ScriptPubKeyMan* spk_man = pwallet->GetScriptPubKeyMan();
if (spk_man) {
const CKeyID *key_id = std::get_if<CKeyID>(&dest);
const CKeyMetadata* meta = nullptr;
if (key_id != nullptr && !key_id->IsNull()) {
meta = spk_man->GetMetadata(*key_id);
}
if (!meta) {
meta = spk_man->GetMetadata(CScriptID(scriptPubKey));
}
if (meta) {
if (const CKeyMetadata* meta = spk_man->GetMetadata(dest)) {
ret.pushKV("timestamp", meta->nCreateTime);
CHDChain hdChainCurrent;
LegacyScriptPubKeyMan* legacy_spk_man = pwallet->GetLegacyScriptPubKeyMan();

View File

@ -15,6 +15,7 @@
class CRPCCommand;
class CWallet;
class JSONRPCRequest;
class LegacyScriptPubKeyMan;
class UniValue;
class CTransaction;
struct PartiallySignedTransaction;
@ -32,8 +33,9 @@ Span<const CRPCCommand> GetWalletRPCCommands();
*/
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
void EnsureWalletIsUnlocked(const CWallet*);
void EnsureWalletIsUnlocked(const CWallet *);
WalletContext& EnsureWalletContext(const CoreContext& context);
LegacyScriptPubKeyMan& EnsureLegacyScriptPubKeyMan(CWallet& wallet);
UniValue getaddressinfo(const JSONRPCRequest& request);
UniValue signrawtransactionwithwallet(const JSONRPCRequest& request);

View File

@ -15,7 +15,7 @@
bool LegacyScriptPubKeyMan::GetNewDestination(CTxDestination& dest, std::string& error)
{
error.clear();
TopUpKeyPool();
TopUp();
// Generate a new key that is added to wallet
CPubKey new_key;
@ -265,10 +265,8 @@ bool LegacyScriptPubKeyMan::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
bool LegacyScriptPubKeyMan::GetReservedDestination(bool internal, int64_t& index, CKeyPool& keypool)
{
{
if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
return false;
}
if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
return false;
}
return true;
}
@ -283,11 +281,6 @@ void LegacyScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, cons
ReturnKey(index, internal, pubkey);
}
bool LegacyScriptPubKeyMan::TopUp(unsigned int size)
{
return TopUpKeyPool(size);
}
void LegacyScriptPubKeyMan::MarkUnusedAddresses(WalletBatch &batch, const CScript& script, const uint256& hashBlock)
{
AssertLockHeld(cs_wallet);
@ -298,7 +291,7 @@ void LegacyScriptPubKeyMan::MarkUnusedAddresses(WalletBatch &batch, const CScrip
WalletLogPrintf("%s: Detected a used keypool key, mark all keypool key up to this key as used\n", __func__);
MarkReserveKeysAsUsed(mi->second);
if (!TopUpKeyPool()) {
if (!TopUp()) {
WalletLogPrintf("%s: Topping up keypool failed (locked wallet)\n", __func__);
}
}
@ -666,7 +659,6 @@ bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal)
return keypool_has_keys;
}
bool LegacyScriptPubKeyMan::HavePrivateKeys() const
{
LOCK(cs_KeyStore);
@ -736,18 +728,24 @@ int64_t LegacyScriptPubKeyMan::GetTimeFirstKey() const
return nTimeFirstKey;
}
const CKeyMetadata* LegacyScriptPubKeyMan::GetMetadata(uint160 id) const
const CKeyMetadata* LegacyScriptPubKeyMan::GetMetadata(const CTxDestination& dest) const
{
AssertLockHeld(cs_wallet);
auto it = mapKeyMetadata.find(CKeyID(id));
if (it != mapKeyMetadata.end()) {
return &it->second;
} else {
auto it2 = m_script_metadata.find(CScriptID(id));
if (it2 != m_script_metadata.end()) {
return &it2->second;
const CKeyID *key_id = std::get_if<CKeyID>(&dest);
if (key_id != nullptr && !key_id->IsNull()) {
auto it = mapKeyMetadata.find(*key_id);
if (it != mapKeyMetadata.end()) {
return &it->second;
}
}
CScript scriptPubKey = GetScriptForDestination(dest);
auto it = m_script_metadata.find(CScriptID(scriptPubKey));
if (it != m_script_metadata.end()) {
return &it->second;
}
return nullptr;
}
@ -1357,15 +1355,16 @@ bool LegacyScriptPubKeyMan::NewKeyPool()
m_pool_key_to_index.clear();
if (!TopUpKeyPool())
if (!TopUp()) {
return false;
}
WalletLogPrintf("LegacyScriptPubKeyMan::NewKeyPool rewrote keypool\n");
}
return true;
}
bool LegacyScriptPubKeyMan::TopUpKeyPool(unsigned int kpSize)
bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
{
if (!CanGenerateKeys()) {
return false;
@ -1508,7 +1507,7 @@ bool LegacyScriptPubKeyMan::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& key
{
LOCK(cs_wallet);
TopUpKeyPool();
TopUp();
bool fReturningInternal = fRequestedInternal;
fReturningInternal &= IsHDEnabled() || m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);

View File

@ -173,7 +173,8 @@ public:
virtual int64_t GetTimeFirstKey() const { return 0; }
virtual const CKeyMetadata* GetMetadata(uint160 id) const { return nullptr; }
//! Return address metadata
virtual const CKeyMetadata* GetMetadata(const CTxDestination& dest) const { return nullptr; }
};
class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProvider
@ -287,7 +288,7 @@ public:
int64_t GetTimeFirstKey() const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
const CKeyMetadata* GetMetadata(uint160 id) const override;
const CKeyMetadata* GetMetadata(const CTxDestination& dest) const override;
bool CanGetAddresses(bool internal = false) override;
@ -361,7 +362,6 @@ public:
bool AddKeyOrigin(const CPubKey& pubkey, const KeyOriginInfo& info);
void LoadKeyPool(int64_t nIndex, const CKeyPool &keypool) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool TopUpKeyPool(unsigned int kpSize = 0);
bool NewKeyPool();
// Seems as not used now anywhere in code
// void AddKeypoolPubkey(const CPubKey& pubkey, const bool internal);

View File

@ -672,7 +672,9 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
// if we are not using HD, generate new keypool
if(m_spk_man->IsHDEnabled()) {
m_spk_man->TopUpKeyPool();
if (!m_spk_man->TopUp()) {
return false;
}
}
else {
m_spk_man->NewKeyPool();