Skip to content

Commit 12eda27

Browse files
committed
Merge bitcoin/bitcoin#23288: tests: remove usage of LegacyScriptPubKeyMan from some wallet tests
2d2edc1 tests: Use Descriptor wallets for generic wallet tests (Andrew Chow) 9951628 tests: Use legacy change type in subtract fee from outputs test (Andrew Chow) dcd6eeb tests: Use descriptors in psbt_wallet_tests (Andrew Chow) 4b1588c tests: Use DescriptorScriptPubKeyMan in coinselector_tests (Andrew Chow) 811319f tests, gui: Use DescriptorScriptPubKeyMan in GUI tests (Andrew Chow) 9bf0243 bench: Use DescriptorScriptPubKeyMan for wallet things (Andrew Chow) 5e54aa9 bench: remove global testWallet from CoinSelection benchmark (Andrew Chow) a5595b1 tests: Remove global vCoins and testWallet from coinselector_tests (Andrew Chow) Pull request description: Currently, various tests use `LegacyScriptPubKeyMan` because it was convenient for the refactor that introduced the `ScriptPubKeyMan` interface. However, with the legacy wallet slated to be removed, these tests should not continue to use `LegacyScriptPubKeyMan` as they are not testing any specific legacy wallet behavior. These tests are changed to use `DescriptorScriptPubKeyMan`s. Some of the coin selection tests and benchmarks had a global `testWallet`, but this seemed to cause some issues with ensuring that descriptors were set up in that wallet for each test. Those have been restructured to not have any global variables that may be modified between tests. The tests which test specific legacy wallet behavior remain unchanged. ACKs for top commit: laanwj: Code review ACK 2d2edc1 brunoerg: tACK 2d2edc1 Tree-SHA512: 6d60e5978e822d48e46cfc0dae4635fcb1939f21ea9d84eb72e36112e925554b7ee8f932c7ed0c4881b6566c6c19260bec346abdff1956ca9f300b30fb4e2dd1
2 parents 5bb03fb + 2d2edc1 commit 12eda27

File tree

10 files changed

+300
-277
lines changed

10 files changed

+300
-277
lines changed

src/bench/coin_selection.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ static void CoinSelection(benchmark::Bench& bench)
3333
NodeContext node;
3434
auto chain = interfaces::MakeChain(node);
3535
CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
36-
wallet.SetupLegacyScriptPubKeyMan();
3736
std::vector<std::unique_ptr<CWalletTx>> wtxs;
3837
LOCK(wallet.cs_wallet);
3938

@@ -65,21 +64,16 @@ static void CoinSelection(benchmark::Bench& bench)
6564
}
6665

6766
typedef std::set<CInputCoin> CoinSet;
68-
static NodeContext testNode;
69-
static auto testChain = interfaces::MakeChain(testNode);
70-
static CWallet testWallet(testChain.get(), "", CreateDummyWalletDatabase());
71-
std::vector<std::unique_ptr<CWalletTx>> wtxn;
7267

7368
// Copied from src/wallet/test/coinselector_tests.cpp
7469
static void add_coin(const CAmount& nValue, int nInput, std::vector<OutputGroup>& set)
7570
{
7671
CMutableTransaction tx;
7772
tx.vout.resize(nInput + 1);
7873
tx.vout[nInput].nValue = nValue;
79-
std::unique_ptr<CWalletTx> wtx = std::make_unique<CWalletTx>(MakeTransactionRef(std::move(tx)));
74+
CInputCoin coin(MakeTransactionRef(tx), nInput);
8075
set.emplace_back();
81-
set.back().Insert(COutput(testWallet, *wtx, nInput, 0, true, true, true).GetInputCoin(), 0, true, 0, 0, false);
82-
wtxn.emplace_back(std::move(wtx));
76+
set.back().Insert(coin, 0, true, 0, 0, false);
8377
}
8478
// Copied from src/wallet/test/coinselector_tests.cpp
8579
static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
@@ -97,7 +91,6 @@ static CAmount make_hard_case(int utxos, std::vector<OutputGroup>& utxo_pool)
9791
static void BnBExhaustion(benchmark::Bench& bench)
9892
{
9993
// Setup
100-
testWallet.SetupLegacyScriptPubKeyMan();
10194
std::vector<OutputGroup> utxo_pool;
10295
CoinSet selection;
10396
CAmount value_ret = 0;

src/bench/wallet_balance.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@
1414

1515
#include <optional>
1616

17-
static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
17+
static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_mine)
1818
{
1919
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
2020

2121
const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
2222

2323
CWallet wallet{test_setup->m_node.chain.get(), "", CreateMockWalletDatabase()};
2424
{
25-
wallet.SetupLegacyScriptPubKeyMan();
25+
LOCK(wallet.cs_wallet);
26+
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
27+
wallet.SetupDescriptorScriptPubKeyMans();
2628
if (wallet.LoadWallet() != DBErrors::LOAD_OK) assert(false);
2729
}
2830
auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
2931

3032
const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
31-
if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
3233

3334
for (int i = 0; i < 100; ++i) {
3435
generatetoaddress(test_setup->m_node, address_mine.value_or(ADDRESS_WATCHONLY));
@@ -42,14 +43,13 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
4243
if (set_dirty) wallet.MarkDirty();
4344
bal = GetBalance(wallet);
4445
if (add_mine) assert(bal.m_mine_trusted > 0);
45-
if (add_watchonly) assert(bal.m_watchonly_trusted > 0);
4646
});
4747
}
4848

49-
static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ true, /* add_watchonly */ true, /* add_mine */ true); }
50-
static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ true); }
51-
static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ false, /* add_mine */ true); }
52-
static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_watchonly */ true, /* add_mine */ false); }
49+
static void WalletBalanceDirty(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ true, /* add_mine */ true); }
50+
static void WalletBalanceClean(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_mine */ true); }
51+
static void WalletBalanceMine(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_mine */ true); }
52+
static void WalletBalanceWatch(benchmark::Bench& bench) { WalletBalance(bench, /* set_dirty */ false, /* add_mine */ false); }
5353

5454
BENCHMARK(WalletBalanceDirty);
5555
BENCHMARK(WalletBalanceClean);

src/qt/test/addressbooktests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
6464
test.m_node.wallet_client = wallet_client.get();
6565
node.setContext(&test.m_node);
6666
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
67-
wallet->SetupLegacyScriptPubKeyMan();
6867
wallet->LoadWallet();
68+
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
69+
{
70+
LOCK(wallet->cs_wallet);
71+
wallet->SetupDescriptorScriptPubKeyMans();
72+
}
6973

7074
auto build_address = [&wallet]() {
7175
CKey key;

src/qt/test/wallettests.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,20 @@ void TestGUI(interfaces::Node& node)
143143
node.setContext(&test.m_node);
144144
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
145145
wallet->LoadWallet();
146+
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
146147
{
147-
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
148-
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
149-
wallet->SetAddressBook(GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type), "", "receive");
150-
spk_man->AddKeyPubKey(test.coinbaseKey, test.coinbaseKey.GetPubKey());
148+
LOCK(wallet->cs_wallet);
149+
wallet->SetupDescriptorScriptPubKeyMans();
150+
151+
// Add the coinbase key
152+
FlatSigningProvider provider;
153+
std::string error;
154+
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(test.coinbaseKey) + ")", provider, error, /* require_checksum=*/ false);
155+
assert(desc);
156+
WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
157+
if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
158+
CTxDestination dest = GetDestinationForKey(test.coinbaseKey.GetPubKey(), wallet->m_default_address_type);
159+
wallet->SetAddressBook(dest, "", "receive");
151160
wallet->SetLastBlockProcessed(105, node.context()->chainman->ActiveChain().Tip()->GetBlockHash());
152161
}
153162
{

src/test/util/wallet.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,4 @@ std::string getnewaddress(CWallet& w)
2525
return EncodeDestination(dest);
2626
}
2727

28-
void importaddress(CWallet& wallet, const std::string& address)
29-
{
30-
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
31-
LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
32-
const auto dest = DecodeDestination(address);
33-
assert(IsValidDestination(dest));
34-
const auto script = GetScriptForDestination(dest);
35-
wallet.MarkDirty();
36-
assert(!spk_man->HaveWatchOnly(script));
37-
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
38-
wallet.SetAddressBook(dest, /* label */ "", "receive");
39-
}
4028
#endif // ENABLE_WALLET

0 commit comments

Comments
 (0)