I think handling a rescan of an empty chain by just returning success …
It would also report that the rescan started at block 0, which does not exist, since the chain was not initialized.
A user calling this function assumes that the chain was initialized and should be notified that it wasn’t instead of returning {start_height=0, stop_height=null}
, indicating success.
Also, we already throw an error if the chain wasn’t initialized and the user supplies at least one of the optional parameters.
I’d suggest the following code change:
0diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
1index c96a9b0aff..ba8d3750b5 100644
2--- a/src/wallet/rpcwallet.cpp
3+++ b/src/wallet/rpcwallet.cpp
4@@ -3432,22 +3432,27 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
5 uint256 start_block, stop_block;
6 {
7 auto locked_chain = pwallet->chain().lock();
8- Optional<int> tip_height = locked_chain->getHeight();
9+ int tip_height;
10+ if (Optional<int> tip_height_opt = locked_chain->getHeight()) {
11+ tip_height = *tip_height_opt;
12+ } else {
13+ throw JSONRPCError(RPC_MISC_ERROR, "Chain not initialized, nothing to rescan");
14+ }
15
16 if (!request.params[0].isNull()) {
17 start_height = request.params[0].get_int();
18- if (start_height < 0 || !tip_height || start_height > *tip_height) {
19- throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
20- }
21+ }
22+ if (start_height < 0 || start_height > tip_height) {
23+ throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
24 }
25
26 Optional<int> stop_height;
27 if (!request.params[1].isNull()) {
28 stop_height = request.params[1].get_int();
29- if (*stop_height < 0 || !tip_height || *stop_height > *tip_height) {
30+ if (*stop_height < 0 || *stop_height > tip_height) {
31 throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid stop_height");
32 }
33- else if (*stop_height < start_height) {
34+ if (*stop_height < start_height) {
35 throw JSONRPCError(RPC_INVALID_PARAMETER, "stop_height must be greater than start_height");
36 }
37 }
38@@ -3457,11 +3462,9 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
39 throw JSONRPCError(RPC_MISC_ERROR, "Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height.");
40 }
41
42- if (tip_height) {
43- start_block = locked_chain->getBlockHash(start_height);
44- if (stop_height) {
45- stop_block = locked_chain->getBlockHash(*stop_height);
46- }
47+ start_block = locked_chain->getBlockHash(start_height);
48+ if (stop_height) {
49+ stop_block = locked_chain->getBlockHash(*stop_height);
50 }
51 }
52