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:
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index c96a9b0aff..ba8d3750b5 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -3432,22 +3432,27 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
uint256 start_block, stop_block;
{
auto locked_chain = pwallet->chain().lock();
- Optional<int> tip_height = locked_chain->getHeight();
+ int tip_height;
+ if (Optional<int> tip_height_opt = locked_chain->getHeight()) {
+ tip_height = *tip_height_opt;
+ } else {
+ throw JSONRPCError(RPC_MISC_ERROR, "Chain not initialized, nothing to rescan");
+ }
if (!request.params[0].isNull()) {
start_height = request.params[0].get_int();
- if (start_height < 0 || !tip_height || start_height > *tip_height) {
- throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
- }
+ }
+ if (start_height < 0 || start_height > tip_height) {
+ throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid start_height");
}
Optional<int> stop_height;
if (!request.params[1].isNull()) {
stop_height = request.params[1].get_int();
- if (*stop_height < 0 || !tip_height || *stop_height > *tip_height) {
+ if (*stop_height < 0 || *stop_height > tip_height) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid stop_height");
}
- else if (*stop_height < start_height) {
+ if (*stop_height < start_height) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "stop_height must be greater than start_height");
}
}
@@ -3457,11 +3462,9 @@ UniValue rescanblockchain(const JSONRPCRequest& request)
throw JSONRPCError(RPC_MISC_ERROR, "Can't rescan beyond pruned data. Use RPC call getblockchaininfo to determine your pruned height.");
}
- if (tip_height) {
- start_block = locked_chain->getBlockHash(start_height);
- if (stop_height) {
- stop_block = locked_chain->getBlockHash(*stop_height);
- }
+ start_block = locked_chain->getBlockHash(start_height);
+ if (stop_height) {
+ stop_block = locked_chain->getBlockHash(*stop_height);
}
}