Skip to content

Commit 8f5ef8d

Browse files
committed
wallet/scan: simplify ChainScanner::Scan main loop
Extract ProcessBlock method and simplify the main scanning loop. This final refactoring demonstrates the clean separation of concerns with each helper method handling a specific aspect of the scanning process.
1 parent f2c2949 commit 8f5ef8d

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/wallet/scan.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ void ChainScanner::UpdateTipIfChanged() {
166166
}
167167
}
168168

169+
void ChainScanner::ProcessBlock(const uint256& block_hash, int block_height, bool fetch_block, bool next_interval, ScanResult& result) {
170+
if (fetch_block) {
171+
if (ScanBlock(block_hash, block_height, m_save_progress && next_interval)) {
172+
result.last_scanned_block = block_hash;
173+
result.last_scanned_height = block_height;
174+
} else {
175+
result.last_failed_block = block_hash;
176+
result.status = ScanResult::FAILURE;
177+
}
178+
} else {
179+
result.last_scanned_block = block_hash;
180+
result.last_scanned_height = block_height;
181+
}
182+
}
183+
169184
bool ChainScanner::ScanBlock(const uint256& block_hash, int block_height, bool save_progress) {
170185
// Read block data and locator if needed (the locator is usually null unless we need to save progress)
171186
CBlock block;
@@ -238,28 +253,10 @@ ScanResult ChainScanner::Scan() {
238253
m_wallet.WalletLogPrintf("Still rescanning. At block %d. Progress=%f\n", block_height, m_progress_current);
239254
}
240255

241-
bool fetch_block{true};
242-
if (fast_rescan_filter) {
243-
fast_rescan_filter->UpdateIfNeeded();
244-
fetch_block = ShouldFetchBlock(fast_rescan_filter, block_hash, block_height);
245-
if (!fetch_block) {
246-
result.last_scanned_block = block_hash;
247-
result.last_scanned_height = block_height;
248-
}
249-
}
250-
251-
if (fetch_block) {
252-
if (ScanBlock(block_hash, block_height, m_save_progress && next_interval)) {
253-
// scan succeeded, record block as most recent successfully scanned
254-
result.last_scanned_block = block_hash;
255-
result.last_scanned_height = block_height;
256-
} else {
257-
// could not scan block, keep scanning but record this block as the most recent failure
258-
result.last_failed_block = block_hash;
259-
result.status = ScanResult::FAILURE;
260-
}
261-
}
256+
bool fetch_block = !fast_rescan_filter ||
257+
(fast_rescan_filter->UpdateIfNeeded(), ShouldFetchBlock(fast_rescan_filter, block_hash, block_height));
262258

259+
ProcessBlock(block_hash, block_height, fetch_block, next_interval, result);
263260
m_progress_current = chain.guessVerificationProgress(block_hash);
264261
if (!m_max_height) UpdateTipIfChanged();
265262
}

src/wallet/scan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ChainScanner {
4343
bool ScanBlock(const uint256& block_hash, int block_height, bool save_progress);
4444
void UpdateProgress(int block_height);
4545
void UpdateTipIfChanged();
46+
void ProcessBlock(const uint256& block_hash, int block_height, bool fetch_block, bool next_interval, ScanResult& result);
4647

4748
public:
4849
ChainScanner(CWallet& wallet, const WalletRescanReserver& reserver, const uint256& start_block, int start_height,

0 commit comments

Comments
 (0)