Skip to content

Commit 230a2f4

Browse files
ryanofskyw0xlt
authored andcommitted
wallet test: Add unit test for wallet scan save_progress option
1 parent a89ddfb commit 230a2f4

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,36 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
123123
// Verify ScanForWalletTransactions picks up transactions in both the old
124124
// and new block files.
125125
{
126-
CWallet wallet(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
126+
CWallet wallet(m_node.chain.get(), "", m_args, CreateMockWalletDatabase());
127127
{
128128
LOCK(wallet.cs_wallet);
129129
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
130130
wallet.SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
131131
}
132132
AddKey(wallet, coinbaseKey);
133133
WalletRescanReserver reserver(wallet);
134+
std::chrono::steady_clock::time_point fake_time;
135+
reserver.setNow([&] { fake_time += 60s; return fake_time; });
134136
reserver.reserve();
135-
CWallet::ScanResult result = wallet.ScanForWalletTransactions(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
137+
138+
{
139+
CBlockLocator locator;
140+
BOOST_CHECK(!WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
141+
BOOST_CHECK(locator.IsNull());
142+
}
143+
144+
CWallet::ScanResult result = wallet.ScanForWalletTransactions(/*start_block=*/oldTip->GetBlockHash(), /*start_height=*/oldTip->nHeight, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/true);
136145
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS);
137146
BOOST_CHECK(result.last_failed_block.IsNull());
138147
BOOST_CHECK_EQUAL(result.last_scanned_block, newTip->GetBlockHash());
139148
BOOST_CHECK_EQUAL(*result.last_scanned_height, newTip->nHeight);
140149
BOOST_CHECK_EQUAL(GetBalance(wallet).m_mine_immature, 100 * COIN);
150+
151+
{
152+
CBlockLocator locator;
153+
BOOST_CHECK(WalletBatch{wallet.GetDatabase()}.ReadBestBlock(locator));
154+
BOOST_CHECK(!locator.IsNull());
155+
}
141156
}
142157

143158
// Prune the older block file.

src/wallet/wallet.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,10 +1707,9 @@ int64_t CWallet::RescanFromTime(int64_t startTime, const WalletRescanReserver& r
17071707
*/
17081708
CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate, const bool save_progress)
17091709
{
1710-
using Clock = std::chrono::steady_clock;
17111710
constexpr auto INTERVAL_TIME{60s};
1712-
auto current_time{Clock::now()};
1713-
auto start_time{Clock::now()};
1711+
auto current_time{reserver.now()};
1712+
auto start_time{reserver.now()};
17141713

17151714
assert(reserver.isReserved());
17161715

@@ -1738,9 +1737,9 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
17381737
ShowProgress(strprintf("%s " + _("Rescanning…").translated, GetDisplayName()), std::max(1, std::min(99, (int)(m_scanning_progress * 100))));
17391738
}
17401739

1741-
bool next_interval = Clock::now() >= current_time + INTERVAL_TIME;
1740+
bool next_interval = reserver.now() >= current_time + INTERVAL_TIME;
17421741
if (next_interval) {
1743-
current_time = Clock::now();
1742+
current_time = reserver.now();
17441743
WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n", block_height, progress_current);
17451744
}
17461745

@@ -1817,7 +1816,7 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
18171816
WalletLogPrintf("Rescan interrupted by shutdown request at block %d. Progress=%f\n", block_height, progress_current);
18181817
result.status = ScanResult::USER_ABORT;
18191818
} else {
1820-
auto duration_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start_time);
1819+
auto duration_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(reserver.now() - start_time);
18211820
WalletLogPrintf("Rescan completed in %15dms\n", duration_milliseconds.count());
18221821
}
18231822
return result;

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,11 @@ void MaybeResendWalletTxs(WalletContext& context);
896896
class WalletRescanReserver
897897
{
898898
private:
899+
using Clock = std::chrono::steady_clock;
900+
using NowFn = std::function<Clock::time_point()>;
899901
CWallet& m_wallet;
900902
bool m_could_reserve;
903+
NowFn m_now;
901904
public:
902905
explicit WalletRescanReserver(CWallet& w) : m_wallet(w), m_could_reserve(false) {}
903906

@@ -918,6 +921,10 @@ class WalletRescanReserver
918921
return (m_could_reserve && m_wallet.fScanningWallet);
919922
}
920923

924+
Clock::time_point now() const { return m_now ? m_now() : Clock::now(); };
925+
926+
void setNow(NowFn now) { m_now = std::move(now); }
927+
921928
~WalletRescanReserver()
922929
{
923930
if (m_could_reserve) {

0 commit comments

Comments
 (0)