Skip to content

Commit

Permalink
wallet, tests: Have CreateSyncedWallet use CWallet::Create
Browse files Browse the repository at this point in the history
CWallet::Create will properly connect the wallet to the chain, so we
should be doing that rather than ad-hoc chain connection.
  • Loading branch information
achow101 committed Dec 11, 2023
1 parent c2182da commit 99d1c54
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
11 changes: 9 additions & 2 deletions src/wallet/test/spend_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <script/solver.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/context.h>
#include <wallet/spend.h>
#include <wallet/test/util.h>
#include <wallet/test/wallet_test_fixture.h>
Expand All @@ -19,7 +20,10 @@ BOOST_FIXTURE_TEST_SUITE(spend_tests, WalletTestingSetup)
BOOST_FIXTURE_TEST_CASE(SubtractFee, TestChain100Setup)
{
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
WalletContext context;
context.chain = m_node.chain.get();
context.args = m_node.args;
auto wallet = CreateSyncedWallet(context, coinbaseKey);

// Check that a subtract-from-recipient transaction slightly less than the
// coinbase input amount does not create a change output (because it would
Expand Down Expand Up @@ -67,7 +71,10 @@ BOOST_FIXTURE_TEST_CASE(wallet_duplicated_preset_inputs_test, TestChain100Setup)

// Add 4 spendable UTXO, 50 BTC each, to the wallet (total balance 200 BTC)
for (int i = 0; i < 4; i++) CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
auto wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
WalletContext context;
context.chain = m_node.chain.get();
context.args = m_node.args;
auto wallet = CreateSyncedWallet(context, coinbaseKey);

LOCK(wallet->cs_wallet);
auto available_coins = AvailableCoins(*wallet);
Expand Down
26 changes: 13 additions & 13 deletions src/wallet/test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@
#include <memory>

namespace wallet {
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
std::shared_ptr<CWallet> CreateSyncedWallet(WalletContext& context, const CKey& key)
{
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
{
LOCK2(wallet->cs_wallet, ::cs_main);
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
}
wallet->LoadWallet();
bilingual_str error;
std::vector<bilingual_str> warnings;
auto wallet = CWallet::Create(context, "", CreateMockableWalletDatabase(), WALLET_FLAG_DESCRIPTORS, error, warnings);

// Allow the fallback fee with it's default
wallet->m_allow_fallback_fee = true;

{
LOCK(wallet->cs_wallet);
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet->SetupDescriptorScriptPubKeyMans();

FlatSigningProvider provider;
std::string error;
std::unique_ptr<Descriptor> desc = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
Expand All @@ -39,11 +37,13 @@ std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cc
}
WalletRescanReserver reserver(*wallet);
reserver.reserve();
CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
CWallet::ScanResult result = wallet->ScanForWalletTransactions(context.chain->getBlockHash(0), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
assert(result.status == CWallet::ScanResult::SUCCESS);
assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
assert(*result.last_scanned_height == cchain.Height());
int tip_height = context.chain->getHeight().value();
assert(*result.last_scanned_height == tip_height);
assert(result.last_scanned_block == context.chain->getBlockHash(tip_height));
assert(result.last_failed_block.IsNull());

return wallet;
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static const DatabaseFormat DATABASE_FORMATS[] = {

const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";

std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
std::shared_ptr<CWallet> CreateSyncedWallet(WalletContext& chain, const CKey& key);

std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
Expand Down
15 changes: 9 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,10 @@ class ListCoinsTestingSetup : public TestChain100Setup
ListCoinsTestingSetup()
{
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
wallet = CreateSyncedWallet(*m_node.chain, WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain()), coinbaseKey);
WalletContext context;
context.chain = m_node.chain.get();
context.args = m_node.args;
wallet = CreateSyncedWallet(context, coinbaseKey);
}

~ListCoinsTestingSetup()
Expand All @@ -540,17 +543,16 @@ class ListCoinsTestingSetup : public TestChain100Setup
blocktx = CMutableTransaction(*wallet->mapWallet.at(tx->GetHash()).tx);
}
CreateAndProcessBlock({CMutableTransaction(blocktx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
SyncWithValidationInterfaceQueue();

LOCK(wallet->cs_wallet);
LOCK(Assert(m_node.chainman)->GetMutex());
wallet->SetLastBlockProcessed(wallet->GetLastBlockHeight() + 1, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
auto it = wallet->mapWallet.find(tx->GetHash());
BOOST_CHECK(it != wallet->mapWallet.end());
it->second.SetState(TxStateConfirmed{m_node.chainman->ActiveChain().Tip()->GetBlockHash(), m_node.chainman->ActiveChain().Height(), /*index=*/1});
BOOST_CHECK(it->second.state<TxStateConfirmed>());
return it->second;
}

std::unique_ptr<CWallet> wallet;
std::shared_ptr<CWallet> wallet;
};

BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
Expand Down Expand Up @@ -613,9 +615,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoinsTest, ListCoinsTestingSetup)
void TestCoinsResult(ListCoinsTest& context, OutputType out_type, CAmount amount,
std::map<OutputType, size_t>& expected_coins_sizes)
{
LOCK(context.wallet->cs_wallet);
util::Result<CTxDestination> dest = Assert(context.wallet->GetNewDestination(out_type, ""));
CWalletTx& wtx = context.AddTx(CRecipient{*dest, amount, /*fSubtractFeeFromAmount=*/true});

LOCK(context.wallet->cs_wallet);
CoinFilterParams filter;
filter.skip_locked = false;
CoinsResult available_coins = AvailableCoins(*context.wallet, nullptr, std::nullopt, filter);
Expand Down

0 comments on commit 99d1c54

Please sign in to comment.