Bitcoin Core 32.99.0
P2P Digital Currency
scan.h
Go to the documentation of this file.
1// Copyright (c) 2026-present 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_SCAN_H
6#define BITCOIN_WALLET_SCAN_H
7
8#include <uint256.h>
9#include <util/time.h>
10
11#include <atomic>
12#include <functional>
13#include <optional>
14
15namespace wallet {
16class CWallet;
17
19struct ScanResult {
21
26 std::optional<int> last_scanned_height;
27
33};
34
37{
38private:
39 using Clock = std::chrono::steady_clock;
40 using NowFn = std::function<Clock::time_point()>;
42 bool m_could_reserve{false};
44public:
46
47 bool reserve(bool with_passphrase = false);
48 bool isReserved() const;
49
50 Clock::time_point now() const { return m_now ? m_now() : Clock::now(); };
51
52 void setNow(NowFn now) { m_now = std::move(now); }
53
55};
56
58private:
60
61 std::atomic<bool> m_abort{false};
62 std::atomic<bool> m_scanning{false};
63 std::atomic<bool> m_scanning_with_passphrase{false};
64 std::atomic<SteadyClock::time_point> m_scanning_start{SteadyClock::time_point{}};
65 std::atomic<double> m_scanning_progress{0};
66
71 struct LoopState {
72 double progress_begin{0};
73 double progress_end{0};
75 };
76
80 bool QueueNextBlock(const uint256& block_hash, int block_height, std::optional<std::pair<uint256, int>>& next_block, std::optional<int> max_height);
81 bool ScanBlock(const uint256& block_hash, int block_height, bool save_progress);
82 void UpdateProgress(const LoopState& state, double progress_current, int block_height);
83 void UpdateTipIfChanged(LoopState& state);
84
88 bool TryReserve(bool with_passphrase = false);
89 void Release();
90
91public:
93
94 void Abort() { m_abort = true; }
95 bool IsAborting() const { return m_abort; }
96 bool IsScanning() const { return m_scanning; }
98 SteadyClock::duration ScanningDuration() const { return m_scanning ? SteadyClock::now() - m_scanning_start.load() : SteadyClock::duration{}; }
99 double ScanningProgress() const { return m_scanning ? m_scanning_progress.load() : 0; }
100
106 int64_t ScanFromTime(int64_t startTime, const WalletRescanReserver& reserver);
107
129 ScanResult Scan(const uint256& start_block, int start_height, std::optional<int> max_height,
130 const WalletRescanReserver& reserver, bool save_progress);
131
132};
133
134} // namespace wallet
135
136#endif // BITCOIN_WALLET_SCAN_H
256-bit opaque blob.
Definition: uint256.h:196
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:313
std::atomic< SteadyClock::time_point > m_scanning_start
Definition: scan.h:64
void Abort()
Definition: scan.h:94
void UpdateProgress(const LoopState &state, double progress_current, int block_height)
Definition: scan.cpp:165
bool IsScanningWithPassphrase() const
Definition: scan.h:97
bool IsAborting() const
Definition: scan.h:95
CWallet & m_wallet
Definition: scan.h:59
bool IsScanning() const
Definition: scan.h:96
SteadyClock::duration ScanningDuration() const
Definition: scan.h:98
std::atomic< double > m_scanning_progress
Definition: scan.h:65
void UpdateTipIfChanged(LoopState &state)
Definition: scan.cpp:179
bool QueueNextBlock(const uint256 &block_hash, int block_height, std::optional< std::pair< uint256, int > > &next_block, std::optional< int > max_height)
Locate block_hash in the chain, queueing its active-chain successor into next_block if it exists and ...
Definition: scan.cpp:148
bool ScanBlock(const uint256 &block_hash, int block_height, bool save_progress)
Definition: scan.cpp:187
bool TryReserve(bool with_passphrase=false)
Definition: scan.cpp:59
int64_t ScanFromTime(int64_t startTime, const WalletRescanReserver &reserver)
Scan active chain for relevant transactions after importing keys.
Definition: scan.cpp:18
double ScanningProgress() const
Definition: scan.h:99
ScanResult Scan(const uint256 &start_block, int start_height, std::optional< int > max_height, const WalletRescanReserver &reserver, bool save_progress)
Scan the block chain (starting in start_block) for transactions from or to us.
Definition: scan.cpp:218
std::atomic< bool > m_scanning_with_passphrase
Definition: scan.h:63
std::atomic< bool > m_scanning
Definition: scan.h:62
std::atomic< bool > m_abort
Definition: scan.h:61
ChainScanner(CWallet &wallet)
Definition: scan.h:92
RAII object to check and reserve a wallet rescan.
Definition: scan.h:37
std::function< Clock::time_point()> NowFn
Definition: scan.h:40
bool isReserved() const
Definition: scan.cpp:49
std::chrono::steady_clock Clock
Definition: scan.h:39
WalletRescanReserver(CWallet &w)
Definition: scan.h:45
Clock::time_point now() const
Definition: scan.h:50
void setNow(NowFn now)
Definition: scan.h:52
bool reserve(bool with_passphrase=false)
Definition: scan.cpp:40
Progress window and tip tracked across Scan loop iterations.
Definition: scan.h:71
Result of a wallet scan.
Definition: scan.h:19
uint256 last_scanned_block
Hash and height of most recent block that was successfully scanned.
Definition: scan.h:25
enum wallet::ScanResult::@19 status
std::optional< int > last_scanned_height
Definition: scan.h:26
uint256 last_failed_block
Height of the most recent block that could not be scanned due to read errors or pruning.
Definition: scan.h:32