From 18c37d2eb7b5a0102f36237b67e43f41700bddca Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 28 Jun 2019 15:45:20 -0700 Subject: [PATCH] rpc: skip rescan if client has no addresses or UTXOs In this commit, we implement an optimization that will speed up clients that attempt to perform a rescan in the past with no addresses. If the client doesn't have any thing to search for, then we simply exit early and send them a rescan finished notification with the final block in our chain. --- rpcwebsocket.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 85e1eaa3..b4bdadbc 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -2588,6 +2588,11 @@ func handleRescan(wsc *wsClient, icmd interface{}) (interface{}, error) { } } + var ( + lastBlock *btcutil.Block + lastBlockHash *chainhash.Hash + ) + if len(lookups.addrs) != 0 || len(lookups.unspent) != 0 { // With all the arguments parsed, we'll execute our chunked rescan // which will notify the clients of any address deposits or output // spends. @@ -2597,6 +2602,22 @@ func handleRescan(wsc *wsClient, icmd interface{}) (interface{}, error) { if err != nil { return nil, err } + } else { + rpcsLog.Infof("Skipping rescan as client has no addrs/utxos") + + // If we didn't actually do a rescan, then we'll give the + // client our best known block within the final rescan finished + // notification. + chainTip := chain.BestSnapshot() + lastBlockHash = &chainTip.Hash + lastBlock, err = chain.BlockByHash(lastBlockHash) + if err != nil { + return nil, &btcjson.RPCError{ + Code: btcjson.ErrRPCBlockNotFound, + Message: "Error getting block: " + err.Error(), + } + } + } // Notify websocket client of the finished rescan. Due to how btcd // asynchronously queues notifications to not block calling code,