Skip to content

Commit 3477a28

Browse files
committed
wallet: set keypool_size instead of access global args manager
1 parent 2d5acc9 commit 3477a28

File tree

7 files changed

+38
-26
lines changed

7 files changed

+38
-26
lines changed

src/wallet/external_signer_scriptpubkeyman.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace wallet {
1313
class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
1414
{
1515
public:
16-
ExternalSignerScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor)
17-
: DescriptorScriptPubKeyMan(storage, descriptor)
16+
ExternalSignerScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
17+
: DescriptorScriptPubKeyMan(storage, descriptor, keypool_size)
1818
{}
19-
ExternalSignerScriptPubKeyMan(WalletStorage& storage)
20-
: DescriptorScriptPubKeyMan(storage)
19+
ExternalSignerScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
20+
: DescriptorScriptPubKeyMan(storage, keypool_size)
2121
{}
2222

2323
/** Provide a descriptor at setup time

src/wallet/rpc/backup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <sync.h>
1818
#include <uint256.h>
1919
#include <util/bip32.h>
20-
#include <util/system.h>
2120
#include <util/time.h>
2221
#include <util/translation.h>
2322
#include <wallet/rpc/util.h>
@@ -1478,7 +1477,7 @@ static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, c
14781477
} else {
14791478
warnings.push_back("Range not given, using default keypool range");
14801479
range_start = 0;
1481-
range_end = gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE);
1480+
range_end = wallet.m_keypool_size;
14821481
}
14831482
next_index = range_start;
14841483

src/wallet/scriptpubkeyman.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <util/bip32.h>
1212
#include <util/strencodings.h>
1313
#include <util/string.h>
14-
#include <util/system.h>
1514
#include <util/time.h>
1615
#include <util/translation.h>
1716
#include <wallet/scriptpubkeyman.h>
@@ -1294,7 +1293,7 @@ bool LegacyScriptPubKeyMan::TopUpChain(CHDChain& chain, unsigned int kpSize)
12941293
if (kpSize > 0) {
12951294
nTargetSize = kpSize;
12961295
} else {
1297-
nTargetSize = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), int64_t{0});
1296+
nTargetSize = m_keypool_size;
12981297
}
12991298
int64_t target = std::max((int64_t) nTargetSize, int64_t{1});
13001299

@@ -1784,7 +1783,7 @@ std::optional<MigrationData> LegacyScriptPubKeyMan::MigrateToDescriptor()
17841783
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
17851784

17861785
// Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
1787-
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc));
1786+
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc, m_keypool_size));
17881787
desc_spk_man->AddDescriptorKey(key, key.GetPubKey());
17891788
desc_spk_man->TopUp();
17901789
auto desc_spks = desc_spk_man->GetScriptPubKeys();
@@ -1829,7 +1828,7 @@ std::optional<MigrationData> LegacyScriptPubKeyMan::MigrateToDescriptor()
18291828
WalletDescriptor w_desc(std::move(desc), 0, 0, chain_counter, 0);
18301829

18311830
// Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
1832-
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc));
1831+
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc, m_keypool_size));
18331832
desc_spk_man->AddDescriptorKey(master_key.key, master_key.key.GetPubKey());
18341833
desc_spk_man->TopUp();
18351834
auto desc_spks = desc_spk_man->GetScriptPubKeys();
@@ -1891,7 +1890,7 @@ std::optional<MigrationData> LegacyScriptPubKeyMan::MigrateToDescriptor()
18911890
} else {
18921891
// Make the DescriptorScriptPubKeyMan and get the scriptPubKeys
18931892
WalletDescriptor w_desc(std::move(desc), creation_time, 0, 0, 0);
1894-
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc));
1893+
auto desc_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(m_storage, w_desc, m_keypool_size));
18951894
for (const auto& keyid : privkeyids) {
18961895
CKey key;
18971896
if (!GetKey(keyid, key)) {
@@ -2122,7 +2121,7 @@ bool DescriptorScriptPubKeyMan::TopUp(unsigned int size)
21222121
if (size > 0) {
21232122
target_size = size;
21242123
} else {
2125-
target_size = std::max(gArgs.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), int64_t{1});
2124+
target_size = m_keypool_size;
21262125
}
21272126

21282127
// Calculate the new range_end

src/wallet/scriptpubkeyman.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
286286

287287
int64_t nTimeFirstKey GUARDED_BY(cs_KeyStore) = 0;
288288

289+
//! Number of pre-generated keys/scripts (part of the look-ahead process, used to detect payments)
290+
int64_t m_keypool_size GUARDED_BY(cs_KeyStore){DEFAULT_KEYPOOL_SIZE};
291+
289292
bool AddKeyPubKeyInner(const CKey& key, const CPubKey &pubkey);
290293
bool AddCryptedKeyInner(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
291294

@@ -363,7 +366,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
363366

364367
bool TopUpChain(CHDChain& chain, unsigned int size);
365368
public:
366-
using ScriptPubKeyMan::ScriptPubKeyMan;
369+
LegacyScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size) : ScriptPubKeyMan(storage), m_keypool_size(keypool_size) {}
367370

368371
util::Result<CTxDestination> GetNewDestination(const OutputType type) override;
369372
isminetype IsMine(const CScript& script) const override;
@@ -555,6 +558,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
555558
//! keeps track of whether Unlock has run a thorough check before
556559
bool m_decryption_thoroughly_checked = false;
557560

561+
//! Number of pre-generated keys/scripts (part of the look-ahead process, used to detect payments)
562+
int64_t m_keypool_size GUARDED_BY(cs_desc_man){DEFAULT_KEYPOOL_SIZE};
563+
558564
bool AddDescriptorKeyWithDB(WalletBatch& batch, const CKey& key, const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
559565

560566
KeyMap GetKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
@@ -572,12 +578,14 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
572578
WalletDescriptor m_wallet_descriptor GUARDED_BY(cs_desc_man);
573579

574580
public:
575-
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor)
581+
DescriptorScriptPubKeyMan(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size)
576582
: ScriptPubKeyMan(storage),
583+
m_keypool_size(keypool_size),
577584
m_wallet_descriptor(descriptor)
578585
{}
579-
DescriptorScriptPubKeyMan(WalletStorage& storage)
580-
: ScriptPubKeyMan(storage)
586+
DescriptorScriptPubKeyMan(WalletStorage& storage, int64_t keypool_size)
587+
: ScriptPubKeyMan(storage),
588+
m_keypool_size(keypool_size)
581589
{}
582590

583591
mutable RecursiveMutex cs_desc_man;

src/wallet/test/coinselector_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ BOOST_AUTO_TEST_CASE(effective_value_test)
932932
BOOST_CHECK_EQUAL(output5.GetEffectiveValue(), nValue); // The effective value should be equal to the absolute value if input_bytes is -1
933933
}
934934

935-
static util::Result<SelectionResult> select_coins(const CAmount& target, const CoinSelectionParams& cs_params, const CCoinControl& cc, std::function<CoinsResult(CWallet&)> coin_setup, interfaces::Chain* chain, const ArgsManager& args)
935+
static util::Result<SelectionResult> select_coins(const CAmount& target, const CoinSelectionParams& cs_params, const CCoinControl& cc, std::function<CoinsResult(CWallet&)> coin_setup, interfaces::Chain* chain)
936936
{
937937
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(chain, "", args, CreateMockWalletDatabase());
938938
wallet->LoadWallet();
@@ -995,7 +995,7 @@ BOOST_AUTO_TEST_CASE(check_max_weight)
995995
add_coin(available_coins, wallet, CAmount(50 * COIN), CFeeRate(0), 144, false, 0, true);
996996
return available_coins;
997997
},
998-
chain, m_args);
998+
chain);
999999

10001000
BOOST_CHECK(result);
10011001
BOOST_CHECK(has_coin(result->GetInputSet(), CAmount(50 * COIN)));
@@ -1020,7 +1020,7 @@ BOOST_AUTO_TEST_CASE(check_max_weight)
10201020
}
10211021
return available_coins;
10221022
},
1023-
chain, m_args);
1023+
chain);
10241024

10251025
BOOST_CHECK(has_coin(result->GetInputSet(), CAmount(0.0625 * COIN)));
10261026
BOOST_CHECK(has_coin(result->GetInputSet(), CAmount(0.025 * COIN)));
@@ -1041,7 +1041,7 @@ BOOST_AUTO_TEST_CASE(check_max_weight)
10411041
}
10421042
return available_coins;
10431043
},
1044-
chain, m_args);
1044+
chain);
10451045

10461046
// No results
10471047
// 1515 inputs * 68 bytes = 103,020 bytes

src/wallet/wallet.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,9 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
29142914
// TODO: Can't use std::make_shared because we need a custom deleter but
29152915
// should be possible to use std::allocate_shared.
29162916
std::shared_ptr<CWallet> walletInstance(new CWallet(chain, name, args, std::move(database)), ReleaseWallet);
2917+
walletInstance->m_keypool_size = std::max(args.GetIntArg("-keypool", DEFAULT_KEYPOOL_SIZE), int64_t{1});
2918+
2919+
// Load wallet
29172920
bool rescan_required = false;
29182921
DBErrors nLoadWalletRet = walletInstance->LoadWallet();
29192922
if (nLoadWalletRet != DBErrors::LOAD_OK) {
@@ -3534,7 +3537,7 @@ void CWallet::SetupLegacyScriptPubKeyMan()
35343537
return;
35353538
}
35363539

3537-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new LegacyScriptPubKeyMan(*this));
3540+
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new LegacyScriptPubKeyMan(*this, m_keypool_size));
35383541
for (const auto& type : LEGACY_OUTPUT_TYPES) {
35393542
m_internal_spk_managers[type] = spk_manager.get();
35403543
m_external_spk_managers[type] = spk_manager.get();
@@ -3563,10 +3566,10 @@ void CWallet::ConnectScriptPubKeyManNotifiers()
35633566
void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
35643567
{
35653568
if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
3566-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc));
3569+
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc, m_keypool_size));
35673570
m_spk_managers[id] = std::move(spk_manager);
35683571
} else {
3569-
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc));
3572+
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
35703573
m_spk_managers[id] = std::move(spk_manager);
35713574
}
35723575
}
@@ -3577,7 +3580,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans(const CExtKey& master_key)
35773580

35783581
for (bool internal : {false, true}) {
35793582
for (OutputType t : OUTPUT_TYPES) {
3580-
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this));
3583+
auto spk_manager = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, m_keypool_size));
35813584
if (IsCrypted()) {
35823585
if (IsLocked()) {
35833586
throw std::runtime_error(std::string(__func__) + ": Wallet is locked, cannot setup new descriptors");
@@ -3633,7 +3636,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
36333636
continue;
36343637
}
36353638
OutputType t = *desc->GetOutputType();
3636-
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this));
3639+
auto spk_manager = std::unique_ptr<ExternalSignerScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, m_keypool_size));
36373640
spk_manager->SetupDescriptor(std::move(desc));
36383641
uint256 id = spk_manager->GetID();
36393642
m_spk_managers[id] = std::move(spk_manager);
@@ -3749,7 +3752,7 @@ ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const Flat
37493752
WalletLogPrintf("Update existing descriptor: %s\n", desc.descriptor->ToString());
37503753
spk_man->UpdateWalletDescriptor(desc);
37513754
} else {
3752-
auto new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc));
3755+
auto new_spk_man = std::unique_ptr<DescriptorScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc, m_keypool_size));
37533756
spk_man = new_spk_man.get();
37543757

37553758
// Save the descriptor to memory

src/wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
642642
/** Absolute maximum transaction fee (in satoshis) used by default for the wallet */
643643
CAmount m_default_max_tx_fee{DEFAULT_TRANSACTION_MAXFEE};
644644

645+
/** Number of pre-generated keys/scripts by each spkm (part of the look-ahead process, used to detect payments) */
646+
int64_t m_keypool_size{DEFAULT_KEYPOOL_SIZE};
647+
645648
size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
646649
bool TopUpKeyPool(unsigned int kpSize = 0);
647650

0 commit comments

Comments
 (0)