Skip to content

Commit 6a97e8a

Browse files
author
MarcoFalke
committed
Merge bitcoin#17260: Split some CWallet functions into new LegacyScriptPubKeyMan
f201ba5 Refactor: Split up CWallet and LegacyScriptPubKeyMan and classes (Andrew Chow) 6702048 MOVEONLY: Move key handling code out of wallet to keyman file (Andrew Chow) ab053ec Move wallet enums to walletutil.h (Andrew Chow) Pull request description: Moves key management functions into a new class LegacyScriptPubKeyMan. First two commits are move-only commits which move stuff out of wallet.{h/cpp} and into newly created scriptpubkeyman.{h/cpp}. Third commit changes several things in CWallet to use LegacyScriptPubKeyMan. First step in the wallet boxes refactor. Note that LegacyScriptPubKeyMan and ScriptPubKeyMan cannot be used standalone yet and are still very much tied into CWallet with both accessing functions within each other. This PR is to help reduce review burden. ACKs for top commit: Sjors: Code review ACK f201ba5. promag: Code review ACK f201ba5. ryanofsky: Code review ACK f201ba5 MarcoFalke: ACK f201ba5 Tree-SHA512: bdc0d8595a06233fe003afcf968a38e0e8cc584a6a89c5bcd05309ac29dca852391802d46763ef81a108d146d0f40c79ea5438e87234ed12b4b8360c9aec94c0
2 parents 4c1090c + f201ba5 commit 6a97e8a

24 files changed

+2152
-1763
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ BITCOIN_CORE_H = \
236236
wallet/load.h \
237237
wallet/psbtwallet.h \
238238
wallet/rpcwallet.h \
239+
wallet/scriptpubkeyman.h \
239240
wallet/wallet.h \
240241
wallet/walletdb.h \
241242
wallet/wallettool.h \
@@ -339,11 +340,11 @@ libbitcoin_wallet_a_SOURCES = \
339340
wallet/db.cpp \
340341
wallet/feebumper.cpp \
341342
wallet/fees.cpp \
342-
wallet/ismine.cpp \
343343
wallet/load.cpp \
344344
wallet/psbtwallet.cpp \
345345
wallet/rpcdump.cpp \
346346
wallet/rpcwallet.cpp \
347+
wallet/scriptpubkeyman.cpp \
347348
wallet/wallet.cpp \
348349
wallet/walletdb.cpp \
349350
wallet/walletutil.cpp \

src/interfaces/wallet.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
4646
result.txout_is_mine.emplace_back(wallet.IsMine(txout));
4747
result.txout_address.emplace_back();
4848
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
49-
IsMine(wallet, result.txout_address.back()) :
49+
wallet.IsMine(result.txout_address.back()) :
5050
ISMINE_NO);
5151
}
5252
result.credit = wtx.GetCredit(locked_chain, ISMINE_ALL);
@@ -117,10 +117,17 @@ class WalletImpl : public Wallet
117117
std::string error;
118118
return m_wallet->GetNewDestination(type, label, dest, error);
119119
}
120-
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet->GetPubKey(address, pub_key); }
121-
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet->GetKey(address, key); }
122-
bool isSpendable(const CTxDestination& dest) override { return IsMine(*m_wallet, dest) & ISMINE_SPENDABLE; }
123-
bool haveWatchOnly() override { return m_wallet->HaveWatchOnly(); };
120+
bool getPubKey(const CKeyID& address, CPubKey& pub_key) override { return m_wallet->GetLegacyScriptPubKeyMan()->GetPubKey(address, pub_key); }
121+
bool getPrivKey(const CKeyID& address, CKey& key) override { return m_wallet->GetLegacyScriptPubKeyMan()->GetKey(address, key); }
122+
bool isSpendable(const CTxDestination& dest) override { return m_wallet->IsMine(dest) & ISMINE_SPENDABLE; }
123+
bool haveWatchOnly() override
124+
{
125+
auto spk_man = m_wallet->GetLegacyScriptPubKeyMan();
126+
if (spk_man) {
127+
return spk_man->HaveWatchOnly();
128+
}
129+
return false;
130+
};
124131
bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) override
125132
{
126133
return m_wallet->SetAddressBook(dest, name, purpose);
@@ -143,7 +150,7 @@ class WalletImpl : public Wallet
143150
*name = it->second.name;
144151
}
145152
if (is_mine) {
146-
*is_mine = IsMine(*m_wallet, dest);
153+
*is_mine = m_wallet->IsMine(dest);
147154
}
148155
if (purpose) {
149156
*purpose = it->second.purpose;
@@ -155,11 +162,11 @@ class WalletImpl : public Wallet
155162
LOCK(m_wallet->cs_wallet);
156163
std::vector<WalletAddress> result;
157164
for (const auto& item : m_wallet->mapAddressBook) {
158-
result.emplace_back(item.first, IsMine(*m_wallet, item.first), item.second.name, item.second.purpose);
165+
result.emplace_back(item.first, m_wallet->IsMine(item.first), item.second.name, item.second.purpose);
159166
}
160167
return result;
161168
}
162-
void learnRelatedScripts(const CPubKey& key, OutputType type) override { m_wallet->LearnRelatedScripts(key, type); }
169+
void learnRelatedScripts(const CPubKey& key, OutputType type) override { m_wallet->GetLegacyScriptPubKeyMan()->LearnRelatedScripts(key, type); }
163170
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
164171
{
165172
LOCK(m_wallet->cs_wallet);
@@ -342,7 +349,7 @@ class WalletImpl : public Wallet
342349
result.balance = bal.m_mine_trusted;
343350
result.unconfirmed_balance = bal.m_mine_untrusted_pending;
344351
result.immature_balance = bal.m_mine_immature;
345-
result.have_watch_only = m_wallet->HaveWatchOnly();
352+
result.have_watch_only = haveWatchOnly();
346353
if (result.have_watch_only) {
347354
result.watch_only_balance = bal.m_watchonly_trusted;
348355
result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;

src/qt/test/wallettests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ void TestGUI()
138138
bool firstRun;
139139
wallet->LoadWallet(firstRun);
140140
{
141+
auto spk_man = wallet->GetLegacyScriptPubKeyMan();
141142
LOCK(wallet->cs_wallet);
143+
AssertLockHeld(spk_man->cs_wallet);
142144
wallet->SetAddressBook(GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type), "", "receive");
143-
wallet->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
145+
spk_man->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
144146
}
145147
{
146148
auto locked_chain = wallet->chain().lock();

src/script/signingprovider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvide
6363
class FillableSigningProvider : public SigningProvider
6464
{
6565
protected:
66-
mutable CCriticalSection cs_KeyStore;
67-
6866
using KeyMap = std::map<CKeyID, CKey>;
6967
using ScriptMap = std::map<CScriptID, CScript>;
7068

@@ -74,6 +72,8 @@ class FillableSigningProvider : public SigningProvider
7472
void ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
7573

7674
public:
75+
mutable CCriticalSection cs_KeyStore;
76+
7777
virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
7878
virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
7979
virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;

src/test/util.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ std::string getnewaddress(CWallet& w)
3232

3333
void importaddress(CWallet& wallet, const std::string& address)
3434
{
35+
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
3536
LOCK(wallet.cs_wallet);
37+
AssertLockHeld(spk_man->cs_wallet);
3638
const auto dest = DecodeDestination(address);
3739
assert(IsValidDestination(dest));
3840
const auto script = GetScriptForDestination(dest);
3941
wallet.MarkDirty();
40-
assert(!wallet.HaveWatchOnly(script));
41-
if (!wallet.AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
42+
assert(!spk_man->HaveWatchOnly(script));
43+
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
4244
wallet.SetAddressBook(dest, /* label */ "", "receive");
4345
}
4446
#endif // ENABLE_WALLET

src/util/translation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define BITCOIN_UTIL_TRANSLATION_H
77

88
#include <tinyformat.h>
9-
9+
#include <functional>
1010

1111
/**
1212
* Bilingual messages:

src/wallet/ismine.cpp

Lines changed: 0 additions & 192 deletions
This file was deleted.

src/wallet/ismine.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ enum isminetype : unsigned int
2828
/** used for bitflags of isminetype */
2929
typedef uint8_t isminefilter;
3030

31-
isminetype IsMine(const CWallet& wallet, const CScript& scriptPubKey);
32-
isminetype IsMine(const CWallet& wallet, const CTxDestination& dest);
33-
3431
/**
3532
* Cachable amount subdivided into watchonly and spendable parts.
3633
*/

src/wallet/psbtwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
3939
return TransactionError::SIGHASH_MISMATCH;
4040
}
4141

42-
complete &= SignPSBTInput(HidingSigningProvider(pwallet, !sign, !bip32derivs), psbtx, i, sighash_type);
42+
complete &= SignPSBTInput(HidingSigningProvider(pwallet->GetSigningProvider(), !sign, !bip32derivs), psbtx, i, sighash_type);
4343
}
4444

4545
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
4646
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
47-
UpdatePSBTOutput(HidingSigningProvider(pwallet, true, !bip32derivs), psbtx, i);
47+
UpdatePSBTOutput(HidingSigningProvider(pwallet->GetSigningProvider(), true, !bip32derivs), psbtx, i);
4848
}
4949

5050
return TransactionError::OK;

0 commit comments

Comments
 (0)