From 2d04d31894586d69524d43b78e605239499032b2 Mon Sep 17 00:00:00 2001 From: Roy Lee Date: Fri, 19 Aug 2022 11:57:29 -0700 Subject: [PATCH] rpc: implement rescanblockchain rpcclient --- btcjson/walletsvrcmds.go | 19 +++++++++++++++++++ btcjson/walletsvrresults.go | 6 ++++++ rpcclient/wallet.go | 38 +++++++++++++++++++++++++++++++++++++ rpcserver.go | 1 + 4 files changed, 64 insertions(+) diff --git a/btcjson/walletsvrcmds.go b/btcjson/walletsvrcmds.go index cf77877a..bc1515f9 100644 --- a/btcjson/walletsvrcmds.go +++ b/btcjson/walletsvrcmds.go @@ -960,6 +960,24 @@ func NewImportMultiCmd(requests []ImportMultiRequest, options *ImportMultiOption } } +// RescanBlockchainCmd defines the RescanBlockchain JSON-RPC command. +type RescanBlockchainCmd struct { + StartHeight *int64 `jsonrpcdefault:"0"` + StopHeight *int64 `jsonrpcdefault:"0"` +} + +// NewRescanBlockchainCmd returns a new instance which can be used to issue +// an RescanBlockchain JSON-RPC command. +// +// The parameters which are pointers indicate they are optional. Passing nil +// for optional parameters will use the default value. +func NewRescanBlockchainCmd(startHeight *int64, stopHeight *int64) *RescanBlockchainCmd { + return &RescanBlockchainCmd{ + StartHeight: startHeight, + StopHeight: stopHeight, + } +} + // PsbtInput represents an input to include in the PSBT created by the // WalletCreateFundedPsbtCmd command. type PsbtInput struct { @@ -1081,6 +1099,7 @@ func init() { MustRegisterCmd("listunspent", (*ListUnspentCmd)(nil), flags) MustRegisterCmd("loadwallet", (*LoadWalletCmd)(nil), flags) MustRegisterCmd("lockunspent", (*LockUnspentCmd)(nil), flags) + MustRegisterCmd("rescanblockchain", (*RescanBlockchainCmd)(nil), flags) MustRegisterCmd("sendfrom", (*SendFromCmd)(nil), flags) MustRegisterCmd("sendmany", (*SendManyCmd)(nil), flags) MustRegisterCmd("sendtoaddress", (*SendToAddressCmd)(nil), flags) diff --git a/btcjson/walletsvrresults.go b/btcjson/walletsvrresults.go index 42402496..0c07ac1c 100644 --- a/btcjson/walletsvrresults.go +++ b/btcjson/walletsvrresults.go @@ -317,6 +317,12 @@ type ListUnspentResult struct { IsStake bool `json:"isstake"` } +// RescanBlockchainResult models the data returned from the rescanblockchain command. +type RescanBlockchainResult struct { + StartHeight int64 `json:"start_height"` + StoptHeight int64 `json:"stop_height"` +} + // SignRawTransactionError models the data that contains script verification // errors from the signrawtransaction request. type SignRawTransactionError struct { diff --git a/rpcclient/wallet.go b/rpcclient/wallet.go index bc4dd917..54a5c56f 100644 --- a/rpcclient/wallet.go +++ b/rpcclient/wallet.go @@ -2027,6 +2027,44 @@ func (c *Client) ListReceivedByAddressIncludeEmpty(minConfirms int, includeEmpty includeEmpty).Receive() } +// FutureRescanBlockchainResult is a future promise to deliver the error result of a +// RescanBlockchainAsync RPC invocation. +type FutureRescanBlockchainResult chan *Response + +// Receive waits for the Response promised by the future and returns the result +// of locking or unlocking the unspent output(s). +func (r FutureRescanBlockchainResult) Receive() (*btcjson.RescanBlockchainResult, error) { + res, err := ReceiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarshal as an array of listreceivedbyaddress result objects. + var received btcjson.RescanBlockchainResult + err = json.Unmarshal(res, &received) + if err != nil { + return nil, err + } + + return &received, nil +} + +// RescanBlockchainAsync returns an instance of a type that can be used to get the +// result of the RPC at some future time by invoking the Receive function on the +// returned instance. +// +// See RescanBlockchain for the blocking version and more details. +func (c *Client) RescanBlockchainAsync(startHeight *int64, stopHeight *int64) FutureRescanBlockchainResult { + cmd := btcjson.NewRescanBlockchainCmd(startHeight, stopHeight) + return c.SendCmd(cmd) +} + +// RescanBlockchain rescans the local blockchain for wallet related +// transactions from the startHeight to the the inclusive stopHeight. +func (c *Client) RescanBlockchain(startHeight *int64, stopHeight *int64) (*btcjson.RescanBlockchainResult, error) { + return c.RescanBlockchainAsync(startHeight, stopHeight).Receive() +} + // ************************ // Wallet Locking Functions // ************************ diff --git a/rpcserver.go b/rpcserver.go index f02e347a..c9ca0eda 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -228,6 +228,7 @@ var rpcAskWallet = map[string]struct{}{ "listtransactions": {}, "listunspent": {}, "lockunspent": {}, + "rescanblockchain": {}, "sendfrom": {}, "sendmany": {}, "sendtoaddress": {},