In 82a222b00d6497dc22e85205dae577a1adf15989 “rpc: extend importdescriptors with UTXO check and incremental rescan”
The lack of nesting in the if blocks makes the code harder to read.
0diff --git a/src/wallet/rpc/backup.cpp b/src/wallet/rpc/backup.cpp
1index 4146a65876..cf8269b2ef 100644
2--- a/src/wallet/rpc/backup.cpp
3+++ b/src/wallet/rpc/backup.cpp
4@@ -507,7 +507,6 @@ RPCHelpMan importdescriptors()
5 bool have_prune_boundary = false;
6 int64_t min_trial_start_time = 0;
7
8-
9 UniValue utxo_diff_obj;
10
11 if (do_scan_utxoset && rescan) {
12@@ -516,53 +515,53 @@ RPCHelpMan importdescriptors()
13
14 CAmount utxo_scanned_balance = GetWalletUTXOSetBalance(wallet);
15
16- if (utxo_scanned_balance != bal.m_mine_trusted) {
17- // Incremental-rescan chunking parameters
18- const int chunk_blocks = 1000;
19- const int64_t avg_block_time = 600; // seconds per block (approx)
20+ if (utxo_scanned_balance != bal.m_mine_trusted) {
21+ // Incremental-rescan chunking parameters
22+ const int chunk_blocks = 1000;
23+ const int64_t avg_block_time = 600; // seconds per block (approx)
24
25- // Get tip time and height
26- int64_t tip_time = 0;
27- int tip_height = 0;
28- {
29- LOCK(pwallet->cs_wallet);
30- CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(tip_time).height(tip_height)));
31- }
32+ // Get tip time and height
33+ int64_t tip_time = 0;
34+ int tip_height = 0;
35+ {
36+ LOCK(pwallet->cs_wallet);
37+ CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(tip_time).height(tip_height)));
38+ }
39
40- // If pruned, compute an approximate earliest start time based on prune height
41- bool is_pruned = pwallet->chain().havePruned();
42- std::optional<int> prune_height_opt = pwallet->chain().getPruneHeight();
43- have_prune_boundary = false;
44- min_trial_start_time = 0;
45- if (is_pruned && prune_height_opt.has_value()) {
46- const int prune_height = prune_height_opt.value();
47- int64_t blocks_diff = tip_height - prune_height;
48- if (blocks_diff < 0) blocks_diff = 0;
49- int64_t prune_time_est = tip_time - blocks_diff * avg_block_time;
50- if (prune_time_est < 0) prune_time_est = 0;
51- min_trial_start_time = prune_time_est;
52- have_prune_boundary = true;
53- }
54+ // If pruned, compute an approximate earliest start time based on prune height
55+ bool is_pruned = pwallet->chain().havePruned();
56+ std::optional<int> prune_height_opt = pwallet->chain().getPruneHeight();
57+ have_prune_boundary = false;
58+ min_trial_start_time = 0;
59+ if (is_pruned && prune_height_opt.has_value()) {
60+ const int prune_height = prune_height_opt.value();
61+ int64_t blocks_diff = tip_height - prune_height;
62+ if (blocks_diff < 0) blocks_diff = 0;
63+ int64_t prune_time_est = tip_time - blocks_diff * avg_block_time;
64+ if (prune_time_est < 0) prune_time_est = 0;
65+ min_trial_start_time = prune_time_est;
66+ have_prune_boundary = true;
67+ }
68
69- // Attempt incremental rescans using the helper defined above.
70- int out_chunks_tried = 0;
71- int64_t out_lowest_ts = 0;
72- UniValue early = IncrementalRescansNonOverlap(wallet, tip_time, tip_height, chunk_blocks, avg_block_time, have_prune_boundary,
73- min_trial_start_time, utxo_scanned_balance, reserver, response, out_chunks_tried, out_lowest_ts);
74+ // Attempt incremental rescans using the helper defined above.
75+ int out_chunks_tried = 0;
76+ int64_t out_lowest_ts = 0;
77+ UniValue early = IncrementalRescansNonOverlap(wallet, tip_time, tip_height, chunk_blocks, avg_block_time, have_prune_boundary,
78+ min_trial_start_time, utxo_scanned_balance, reserver, response, out_chunks_tried, out_lowest_ts);
79
80- if (!early.isNull()) {
81- // Matched and response already annotated by helper.
82- return early;
83- }
84+ if (!early.isNull()) {
85+ // Matched and response already annotated by helper.
86+ return early;
87+ }
88
89- if (have_prune_boundary) {
90- // Set the fallback rescan start to the prune boundary (instead of 0)
91- lowest_timestamp = min_trial_start_time;
92- } else {
93- // Non-pruned node: incremental attempts scanned back to timestamp 0 (genesis)
94- lowest_timestamp = 0;
95+ if (have_prune_boundary) {
96+ // Set the fallback rescan start to the prune boundary (instead of 0)
97+ lowest_timestamp = min_trial_start_time;
98+ } else {
99+ // Non-pruned node: incremental attempts scanned back to timestamp 0 (genesis)
100+ lowest_timestamp = 0;
101+ }
102 }
103- }
104
105 }
106