Skip to content

Commit 1dec90d

Browse files
committed
Merge bitcoin/bitcoin#25526: wallet: avoid double keypool TopUp() call on descriptor wallets
bfb9b94 wallet: remove duplicate descriptor type check in GetNewDestination (furszy) 76b982a wallet: remove unused `nAccountingEntryNumber` field (furszy) 599ff5a wallet: avoid double TopUp() calls on descriptor wallets (furszy) Pull request description: Found it while was digging over a `getnewaddress` timeout on the functional test suite. ### Context: We are calling `TopUp()` twice in the following flows for descriptor wallets: A) `CWallet::GetNewDestination`: 1) Calls spk_man->TopUp() 2) Calls spk_man->GetNewDestination() --> which, after the basic script checks, calls TopUp() again. B) `CWallet::GetReservedDestination`: 1) Calls spk_man->TopUp() 2) Calls spk_man->GetReservedDestination() --> which calls to GetNewDestination (which calls to TopUp again). ### Changes: Move `TopUp()` responsibility from the wallet class to each scriptpubkeyman. So each spkm can decide to call it or not after perform the basic checks for the new destination request. Aside from that, remove the unused `nAccountingEntryNumber` wallet field. And a duplicated descriptor type check in `GetNewDestination` ACKs for top commit: aureleoules: re-ACK bfb9b94. achow101: ACK bfb9b94 theStack: Code-review ACK bfb9b94 Tree-SHA512: 3ab73f37729e50d6c6a4434f676855bc1fb404619d63c03e5b06ce61c292c09c59d64cb1aa3bd9277b06f26988956991d62c90f9d835884f41ed500b43a12058
2 parents cb9764b + bfb9b94 commit 1dec90d

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ util::Result<CTxDestination> LegacyScriptPubKeyMan::GetNewDestination(const Outp
2828
}
2929
assert(type != OutputType::BECH32M);
3030

31+
// Fill-up keypool if needed
32+
TopUp();
33+
3134
LOCK(cs_KeyStore);
3235

3336
// Generate a new key that is added to wallet
@@ -304,6 +307,9 @@ util::Result<CTxDestination> LegacyScriptPubKeyMan::GetReservedDestination(const
304307
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
305308
}
306309

310+
// Fill-up keypool if needed
311+
TopUp();
312+
307313
if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
308314
return util::Error{_("Error: Keypool ran out, please call keypoolrefill first")};
309315
}
@@ -1983,7 +1989,7 @@ util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const
19831989
std::optional<OutputType> desc_addr_type = m_wallet_descriptor.descriptor->GetOutputType();
19841990
assert(desc_addr_type);
19851991
if (type != *desc_addr_type) {
1986-
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent");
1992+
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
19871993
}
19881994

19891995
TopUp();
@@ -2001,11 +2007,8 @@ util::Result<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const
20012007
}
20022008

20032009
CTxDestination dest;
2004-
std::optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
2005-
if (out_script_type && out_script_type == type) {
2006-
ExtractDestination(scripts_temp[0], dest);
2007-
} else {
2008-
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
2010+
if (!ExtractDestination(scripts_temp[0], dest)) {
2011+
return util::Error{_("Error: Cannot extract destination from the generated scriptpubkey")}; // shouldn't happen
20092012
}
20102013
m_wallet_descriptor.next_index++;
20112014
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);

src/wallet/wallet.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,6 @@ util::Result<CTxDestination> CWallet::GetNewDestination(const OutputType type, c
23862386
return util::Error{strprintf(_("Error: No %s addresses available."), FormatOutputType(type))};
23872387
}
23882388

2389-
spk_man->TopUp();
23902389
auto op_dest = spk_man->GetNewDestination(type);
23912390
if (op_dest) {
23922391
SetAddressBook(*op_dest, label, "receive");
@@ -2480,10 +2479,7 @@ util::Result<CTxDestination> ReserveDestination::GetReservedDestination(bool int
24802479
return util::Error{strprintf(_("Error: No %s addresses available."), FormatOutputType(type))};
24812480
}
24822481

2483-
if (nIndex == -1)
2484-
{
2485-
m_spk_man->TopUp();
2486-
2482+
if (nIndex == -1) {
24872483
CKeyPool keypool;
24882484
auto op_address = m_spk_man->GetReservedDestination(type, internal, nIndex, keypool);
24892485
if (!op_address) return op_address;

src/wallet/wallet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
401401
TxItems wtxOrdered;
402402

403403
int64_t nOrderPosNext GUARDED_BY(cs_wallet) = 0;
404-
uint64_t nAccountingEntryNumber = 0;
405404

406405
std::map<CTxDestination, CAddressBookData> m_address_book GUARDED_BY(cs_wallet);
407406
const CAddressBookData* FindAddressBookEntry(const CTxDestination&, bool allow_change = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)