Skip to content

Commit 7127c31

Browse files
committed
Merge #17237: wallet: LearnRelatedScripts only if KeepDestination
3958295 wallet: LearnRelatedScripts only if KeepDestination (João Barbosa) 55295fb wallet: Lock address type in ReserveDestination (João Barbosa) Pull request description: Only mutates the wallet if the reserved key is kept. First commit is a refactor that makes the address type a class member. The second commit moves `LearnRelatedScripts` from `GetReservedDestination` to `KeepDestination` to avoid an unnecessary call to `AddCScript` - which in turn prevents multiple entries of the same script in the wallet DB. ACKs for top commit: achow101: Re-ACK 3958295 Sjors: ACK 3958295 ryanofsky: Code review ACK 3958295. I like this change. The new behavior makes more sense, and the change makes the code clearer, since the current LearnRelatedScripts call is hard to understand and explain. (Personally, I'd like it if this PR were merged before #17373 or that PR was rebased on top of this one so it would be less confusing.) meshcollider: utACK 3958295 Tree-SHA512: 49a5f4b022b28042ad37ea309b28378a3983cb904e234a25795b5a360356652e0f8e60f15e3e64d85094ea63af9be01812d90ccfc08ca4f1dd927fdd8566e33f
2 parents 0aa7206 + 3958295 commit 7127c31

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/wallet/wallet.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,8 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
25442544
int& nChangePosInOut, std::string& strFailReason, const CCoinControl& coin_control, bool sign)
25452545
{
25462546
CAmount nValue = 0;
2547-
ReserveDestination reservedest(this);
2547+
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
2548+
ReserveDestination reservedest(this, change_type);
25482549
int nChangePosRequest = nChangePosInOut;
25492550
unsigned int nSubtractFeeFromAmount = 0;
25502551
for (const auto& recipient : vecSend)
@@ -2603,8 +2604,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
26032604
return false;
26042605
}
26052606
CTxDestination dest;
2606-
const OutputType change_type = TransactionChangeType(coin_control.m_change_type ? *coin_control.m_change_type : m_default_change_type, vecSend);
2607-
bool ret = reservedest.GetReservedDestination(change_type, dest, true);
2607+
bool ret = reservedest.GetReservedDestination(dest, true);
26082608
if (!ret)
26092609
{
26102610
strFailReason = "Keypool ran out, please call keypoolrefill first";
@@ -3128,8 +3128,8 @@ bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& des
31283128

31293129
m_spk_man->TopUp();
31303130

3131-
ReserveDestination reservedest(this);
3132-
if (!reservedest.GetReservedDestination(type, dest, true)) {
3131+
ReserveDestination reservedest(this, type);
3132+
if (!reservedest.GetReservedDestination(dest, true)) {
31333133
error = "Error: Keypool ran out, please call keypoolrefill first";
31343134
return false;
31353135
}
@@ -3295,7 +3295,7 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
32953295
return result;
32963296
}
32973297

3298-
bool ReserveDestination::GetReservedDestination(const OutputType type, CTxDestination& dest, bool internal)
3298+
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal)
32993299
{
33003300
m_spk_man = pwallet->GetLegacyScriptPubKeyMan();
33013301
if (!m_spk_man) {
@@ -3316,16 +3316,17 @@ bool ReserveDestination::GetReservedDestination(const OutputType type, CTxDestin
33163316
fInternal = keypool.fInternal;
33173317
}
33183318
assert(vchPubKey.IsValid());
3319-
m_spk_man->LearnRelatedScripts(vchPubKey, type);
33203319
address = GetDestinationForKey(vchPubKey, type);
33213320
dest = address;
33223321
return true;
33233322
}
33243323

33253324
void ReserveDestination::KeepDestination()
33263325
{
3327-
if (nIndex != -1)
3326+
if (nIndex != -1) {
33283327
m_spk_man->KeepDestination(nIndex);
3328+
m_spk_man->LearnRelatedScripts(vchPubKey, type);
3329+
}
33293330
nIndex = -1;
33303331
vchPubKey = CPubKey();
33313332
address = CNoDestination();

src/wallet/wallet.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ class ReserveDestination
140140
{
141141
protected:
142142
//! The wallet to reserve from
143-
CWallet* pwallet;
143+
CWallet* const pwallet;
144144
LegacyScriptPubKeyMan* m_spk_man{nullptr};
145+
OutputType const type;
145146
//! The index of the address's key in the keypool
146147
int64_t nIndex{-1};
147148
//! The public key for the address
@@ -153,10 +154,9 @@ class ReserveDestination
153154

154155
public:
155156
//! Construct a ReserveDestination object. This does NOT reserve an address yet
156-
explicit ReserveDestination(CWallet* pwalletIn)
157-
{
158-
pwallet = pwalletIn;
159-
}
157+
explicit ReserveDestination(CWallet* pwallet, OutputType type)
158+
: pwallet(pwallet)
159+
, type(type) { }
160160

161161
ReserveDestination(const ReserveDestination&) = delete;
162162
ReserveDestination& operator=(const ReserveDestination&) = delete;
@@ -168,7 +168,7 @@ class ReserveDestination
168168
}
169169

170170
//! Reserve an address
171-
bool GetReservedDestination(const OutputType type, CTxDestination& pubkey, bool internal);
171+
bool GetReservedDestination(CTxDestination& pubkey, bool internal);
172172
//! Return reserved address
173173
void ReturnDestination();
174174
//! Keep the address. Do not return it's key to the keypool when this object goes out of scope

0 commit comments

Comments
 (0)