Skip to content

Commit 2565478

Browse files
committed
wallet test refactor: add CreateSyncedWallet function
No change in behavior. This just moves some code from the ListCoins test setup to a reusable util function, so it can be reused in a new test in the next commit.
1 parent b0e5fbf commit 2565478

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

build_msvc/test_bitcoin/test_bitcoin.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<ClCompile Include="..\..\src\test\util\*.cpp" />
1717
<ClCompile Include="..\..\src\wallet\test\*_fixture.cpp" />
1818
<ClCompile Include="..\..\src\wallet\test\*_tests.cpp" />
19+
<ClCompile Include="..\..\src\wallet\test\util.cpp" />
1920
</ItemGroup>
2021
<ItemGroup>
2122
<ProjectReference Include="..\libbitcoinconsensus\libbitcoinconsensus.vcxproj">

src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ endif
170170

171171

172172
BITCOIN_TEST_SUITE += \
173+
wallet/test/util.cpp \
174+
wallet/test/util.h \
173175
wallet/test/wallet_test_fixture.cpp \
174176
wallet/test/wallet_test_fixture.h \
175177
wallet/test/init_test_fixture.cpp \

src/wallet/test/util.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <wallet/test/util.h>
6+
7+
#include <chain.h>
8+
#include <key.h>
9+
#include <test/util/setup_common.h>
10+
#include <wallet/wallet.h>
11+
#include <wallet/walletdb.h>
12+
13+
#include <boost/test/unit_test.hpp>
14+
15+
#include <memory>
16+
17+
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
18+
{
19+
auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockWalletDatabase());
20+
{
21+
LOCK2(wallet->cs_wallet, ::cs_main);
22+
wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
23+
}
24+
wallet->LoadWallet();
25+
{
26+
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
27+
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore);
28+
spk_man->AddKeyPubKey(key, key.GetPubKey());
29+
}
30+
WalletRescanReserver reserver(*wallet);
31+
reserver.reserve();
32+
CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
33+
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
34+
BOOST_CHECK_EQUAL(result.last_scanned_block, cchain.Tip()->GetBlockHash());
35+
BOOST_CHECK_EQUAL(*result.last_scanned_height, cchain.Height());
36+
BOOST_CHECK(result.last_failed_block.IsNull());
37+
return wallet;
38+
}

src/wallet/test/util.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_WALLET_TEST_UTIL_H
6+
#define BITCOIN_WALLET_TEST_UTIL_H
7+
8+
#include <memory>
9+
10+
class CChain;
11+
class CKey;
12+
class CWallet;
13+
namespace interfaces {
14+
class Chain;
15+
} // namespace interfaces
16+
17+
std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
18+
19+
#endif // BITCOIN_WALLET_TEST_UTIL_H

src/wallet/test/wallet_tests.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <util/translation.h>
2121
#include <validation.h>
2222
#include <wallet/coincontrol.h>
23+
#include <wallet/test/util.h>
2324
#include <wallet/test/wallet_test_fixture.h>
2425

2526
#include <boost/test/unit_test.hpp>
@@ -480,20 +481,7 @@ class ListCoinsTestingSetup : public TestChain100Setup
480481
ListCoinsTestingSetup()
481482
{
482483
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
483-
wallet = std::make_unique<CWallet>(m_node.chain.get(), "", CreateMockWalletDatabase());
484-
{
485-
LOCK2(wallet->cs_wallet, ::cs_main);
486-
wallet->SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
487-
}
488-
wallet->LoadWallet();
489-
AddKey(*wallet, coinbaseKey);
490-
WalletRescanReserver reserver(*wallet);
491-
reserver.reserve();
492-
CWallet::ScanResult result = wallet->ScanForWalletTransactions(m_node.chainman->ActiveChain().Genesis()->GetBlockHash(), 0 /* start_height */, {} /* max_height */, reserver, false /* update */);
493-
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
494-
BOOST_CHECK_EQUAL(result.last_scanned_block, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
495-
BOOST_CHECK_EQUAL(*result.last_scanned_height, m_node.chainman->ActiveChain().Height());
496-
BOOST_CHECK(result.last_failed_block.IsNull());
484+
wallet = CreateSyncedWallet(*m_node.chain, m_node.chainman->ActiveChain(), coinbaseKey);
497485
}
498486

499487
~ListCoinsTestingSetup()

0 commit comments

Comments
 (0)