rpc: implement rescanblockchain rpcclient

This commit is contained in:
Roy Lee 2022-08-19 11:57:29 -07:00
parent ce37025d5a
commit 2d04d31894
4 changed files with 64 additions and 0 deletions

View file

@ -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)

View file

@ -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 {

View file

@ -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
// ************************

View file

@ -228,6 +228,7 @@ var rpcAskWallet = map[string]struct{}{
"listtransactions": {},
"listunspent": {},
"lockunspent": {},
"rescanblockchain": {},
"sendfrom": {},
"sendmany": {},
"sendtoaddress": {},