Skip to content

Commit

Permalink
rpc: implement rescanblockchain rpcclient
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Sep 1, 2022
1 parent ce37025 commit 2d04d31
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions btcjson/walletsvrcmds.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions btcjson/walletsvrresults.go
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions rpcclient/wallet.go
Expand Up @@ -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
// ************************
Expand Down
1 change: 1 addition & 0 deletions rpcserver.go
Expand Up @@ -228,6 +228,7 @@ var rpcAskWallet = map[string]struct{}{
"listtransactions": {},
"listunspent": {},
"lockunspent": {},
"rescanblockchain": {},
"sendfrom": {},
"sendmany": {},
"sendtoaddress": {},
Expand Down

0 comments on commit 2d04d31

Please sign in to comment.