2017-01-25 03:44:20 +01:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2017 The Decred developers
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-07-22 04:53:54 +02:00
|
|
|
package rpcclient
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2014-06-10 02:49:41 +02:00
|
|
|
"encoding/json"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/btcjson"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FutureGetBestBlockHashResult is a future promise to deliver the result of a
|
|
|
|
// GetBestBlockAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBestBlockHashResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the hash of
|
2014-05-07 18:14:39 +02:00
|
|
|
// the best block in the longest block chain.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a string.
|
|
|
|
var txHashStr string
|
|
|
|
err = json.Unmarshal(res, &txHashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2016-08-08 21:16:04 +02:00
|
|
|
return chainhash.NewHashFromStr(txHashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlockHashAsync 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 GetBestBlockHash for the blocking version and more details.
|
|
|
|
func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBestBlockHashCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlockHash returns the hash of the best block in the longest block
|
|
|
|
// chain.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetBestBlockHash() (*chainhash.Hash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBestBlockHashAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:15:50 +02:00
|
|
|
// legacyGetBlockRequest constructs and sends a legacy getblock request which
|
|
|
|
// contains two separate bools to denote verbosity, in contract to a single int
|
|
|
|
// parameter.
|
|
|
|
func (c *Client) legacyGetBlockRequest(hash string, verbose,
|
|
|
|
verboseTx bool) ([]byte, error) {
|
|
|
|
|
|
|
|
hashJSON, err := json.Marshal(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
verboseJSON, err := json.Marshal(btcjson.Bool(verbose))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
verboseTxJSON, err := json.Marshal(btcjson.Bool(verboseTx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.RawRequest("getblock", []json.RawMessage{
|
|
|
|
hashJSON, verboseJSON, verboseTxJSON,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// waitForGetBlockRes waits for the Response of a getblock request. If the
|
|
|
|
// Response indicates an invalid parameter was provided, a legacy style of the
|
|
|
|
// request is resent and its Response is returned instead.
|
|
|
|
func (c *Client) waitForGetBlockRes(respChan chan *Response, hash string,
|
2020-05-14 23:15:50 +02:00
|
|
|
verbose, verboseTx bool) ([]byte, error) {
|
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(respChan)
|
2020-05-14 23:15:50 +02:00
|
|
|
|
|
|
|
// If we receive an invalid parameter error, then we may be
|
|
|
|
// communicating with a btcd node which only understands the legacy
|
|
|
|
// request, so we'll try that.
|
|
|
|
if err, ok := err.(*btcjson.RPCError); ok &&
|
|
|
|
err.Code == btcjson.ErrRPCInvalidParams.Code {
|
|
|
|
return c.legacyGetBlockRequest(hash, verbose, verboseTx)
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Otherwise, we can return the Response as is.
|
2020-05-14 23:15:50 +02:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureGetBlockResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockAsync RPC invocation (or an applicable error).
|
2020-05-14 23:15:50 +02:00
|
|
|
type FutureGetBlockResult struct {
|
|
|
|
client *Client
|
|
|
|
hash string
|
2021-09-02 08:39:55 +02:00
|
|
|
Response chan *Response
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the raw
|
2014-05-07 18:14:39 +02:00
|
|
|
// block requested from the server given its hash.
|
2016-09-15 21:06:36 +02:00
|
|
|
func (r FutureGetBlockResult) Receive() (*wire.MsgBlock, error) {
|
2020-05-14 23:15:50 +02:00
|
|
|
res, err := r.client.waitForGetBlockRes(r.Response, r.hash, false, false)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a string.
|
|
|
|
var blockHex string
|
|
|
|
err = json.Unmarshal(res, &blockHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the serialized block hex to raw bytes.
|
|
|
|
serializedBlock, err := hex.DecodeString(blockHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the block and return it.
|
2015-02-05 22:50:03 +01:00
|
|
|
var msgBlock wire.MsgBlock
|
2014-12-13 16:02:59 +01:00
|
|
|
err = msgBlock.Deserialize(bytes.NewReader(serializedBlock))
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-15 21:06:36 +02:00
|
|
|
return &msgBlock, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockAsync 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 GetBlock for the blocking version and more details.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult {
|
2014-05-22 02:53:46 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
2020-05-15 02:27:59 +02:00
|
|
|
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(0))
|
2020-05-14 23:15:50 +02:00
|
|
|
return FutureGetBlockResult{
|
|
|
|
client: c,
|
|
|
|
hash: hash,
|
2021-09-02 08:39:55 +02:00
|
|
|
Response: c.SendCmd(cmd),
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock returns a raw block from the server given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlockVerbose to retrieve a data structure with information about the
|
|
|
|
// block instead.
|
2016-09-15 21:06:36 +02:00
|
|
|
func (c *Client) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBlockAsync(blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockVerboseResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockVerboseAsync RPC invocation (or an applicable error).
|
2020-05-14 23:15:50 +02:00
|
|
|
type FutureGetBlockVerboseResult struct {
|
|
|
|
client *Client
|
|
|
|
hash string
|
2021-09-02 08:39:55 +02:00
|
|
|
Response chan *Response
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the data
|
2014-05-07 18:14:39 +02:00
|
|
|
// structure from the server with information about the requested block.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) {
|
2020-05-14 23:15:50 +02:00
|
|
|
res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, false)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the raw result into a BlockResult.
|
2015-01-01 23:34:07 +01:00
|
|
|
var blockResult btcjson.GetBlockVerboseResult
|
2014-06-10 02:49:41 +02:00
|
|
|
err = json.Unmarshal(res, &blockResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
return &blockResult, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockVerboseAsync 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 GetBlockVerbose for the blocking version and more details.
|
2016-09-15 22:01:54 +02:00
|
|
|
func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
|
2014-05-22 02:53:46 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
2020-01-31 18:31:47 +01:00
|
|
|
// From the bitcoin-cli getblock documentation:
|
|
|
|
// "If verbosity is 1, returns an Object with information about block ."
|
|
|
|
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(1))
|
2020-05-14 23:15:50 +02:00
|
|
|
return FutureGetBlockVerboseResult{
|
|
|
|
client: c,
|
|
|
|
hash: hash,
|
2021-09-02 08:39:55 +02:00
|
|
|
Response: c.SendCmd(cmd),
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockVerbose returns a data structure from the server with information
|
|
|
|
// about a block given its hash.
|
|
|
|
//
|
2016-09-15 22:01:54 +02:00
|
|
|
// See GetBlockVerboseTx to retrieve transaction data structures as well.
|
2014-05-07 18:14:39 +02:00
|
|
|
// See GetBlock to retrieve a raw block instead.
|
2016-09-15 22:01:54 +02:00
|
|
|
func (c *Client) GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
|
|
|
|
return c.GetBlockVerboseAsync(blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:15:50 +02:00
|
|
|
// FutureGetBlockVerboseTxResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockVerboseTxResult RPC invocation (or an applicable error).
|
|
|
|
type FutureGetBlockVerboseTxResult struct {
|
|
|
|
client *Client
|
|
|
|
hash string
|
2021-09-02 08:39:55 +02:00
|
|
|
Response chan *Response
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2020-01-31 18:31:47 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns a verbose
|
2020-05-14 23:15:50 +02:00
|
|
|
// version of the block including detailed information about its transactions.
|
2020-01-31 18:31:47 +01:00
|
|
|
func (r FutureGetBlockVerboseTxResult) Receive() (*btcjson.GetBlockVerboseTxResult, error) {
|
2020-05-14 23:15:50 +02:00
|
|
|
res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, true)
|
2020-01-31 18:31:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockResult btcjson.GetBlockVerboseTxResult
|
|
|
|
err = json.Unmarshal(res, &blockResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blockResult, nil
|
|
|
|
}
|
|
|
|
|
2016-09-15 22:01:54 +02:00
|
|
|
// GetBlockVerboseTxAsync 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 GetBlockVerboseTx or the blocking version and more details.
|
2020-01-31 18:31:47 +01:00
|
|
|
func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseTxResult {
|
2016-09-15 22:01:54 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
2020-05-14 23:15:50 +02:00
|
|
|
|
2020-01-31 18:31:47 +01:00
|
|
|
// From the bitcoin-cli getblock documentation:
|
2020-05-14 23:15:50 +02:00
|
|
|
//
|
|
|
|
// If verbosity is 2, returns an Object with information about block
|
|
|
|
// and information about each transaction.
|
2020-01-31 18:31:47 +01:00
|
|
|
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(2))
|
2020-05-14 23:15:50 +02:00
|
|
|
return FutureGetBlockVerboseTxResult{
|
|
|
|
client: c,
|
|
|
|
hash: hash,
|
2021-09-02 08:39:55 +02:00
|
|
|
Response: c.SendCmd(cmd),
|
2020-05-14 23:15:50 +02:00
|
|
|
}
|
2016-09-15 22:01:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockVerboseTx returns a data structure from the server with information
|
|
|
|
// about a block and its transactions given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlockVerbose if only transaction hashes are preferred.
|
|
|
|
// See GetBlock to retrieve a raw block instead.
|
2020-01-31 18:31:47 +01:00
|
|
|
func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseTxResult, error) {
|
2016-09-15 22:01:54 +02:00
|
|
|
return c.GetBlockVerboseTxAsync(blockHash).Receive()
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockCountResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockCountAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockCountResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the number
|
2014-05-07 18:14:39 +02:00
|
|
|
// of blocks in the longest block chain.
|
|
|
|
func (r FutureGetBlockCountResult) Receive() (int64, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as an int64.
|
|
|
|
var count int64
|
|
|
|
err = json.Unmarshal(res, &count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
return count, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockCountAsync 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 GetBlockCount for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockCountCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockCount returns the number of blocks in the longest block chain.
|
|
|
|
func (c *Client) GetBlockCount() (int64, error) {
|
|
|
|
return c.GetBlockCountAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:12:44 +02:00
|
|
|
// FutureGetChainTxStatsResult is a future promise to deliver the result of a
|
|
|
|
// GetChainTxStatsAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetChainTxStatsResult chan *Response
|
2020-05-11 20:12:44 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns transaction statistics
|
2020-05-11 20:12:44 +02:00
|
|
|
func (r FutureGetChainTxStatsResult) Receive() (*btcjson.GetChainTxStatsResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-05-11 20:12:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainTxStats btcjson.GetChainTxStatsResult
|
|
|
|
err = json.Unmarshal(res, &chainTxStats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &chainTxStats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStatsAsync 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 GetChainTxStats for the blocking version and more details.
|
|
|
|
func (c *Client) GetChainTxStatsAsync() FutureGetChainTxStatsResult {
|
|
|
|
cmd := btcjson.NewGetChainTxStatsCmd(nil, nil)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-05-11 20:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStatsNBlocksAsync 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 GetChainTxStatsNBlocks for the blocking version and more details.
|
|
|
|
func (c *Client) GetChainTxStatsNBlocksAsync(nBlocks int32) FutureGetChainTxStatsResult {
|
|
|
|
cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, nil)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-05-11 20:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStatsNBlocksBlockHashAsync 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 GetChainTxStatsNBlocksBlockHash for the blocking version and more details.
|
|
|
|
func (c *Client) GetChainTxStatsNBlocksBlockHashAsync(nBlocks int32, blockHash chainhash.Hash) FutureGetChainTxStatsResult {
|
|
|
|
hash := blockHash.String()
|
|
|
|
cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, &hash)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-05-11 20:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStats returns statistics about the total number and rate of transactions in the chain.
|
|
|
|
//
|
|
|
|
// Size of the window is one month and it ends at chain tip.
|
|
|
|
func (c *Client) GetChainTxStats() (*btcjson.GetChainTxStatsResult, error) {
|
|
|
|
return c.GetChainTxStatsAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStatsNBlocks returns statistics about the total number and rate of transactions in the chain.
|
|
|
|
//
|
|
|
|
// The argument specifies size of the window in number of blocks. The window ends at chain tip.
|
|
|
|
func (c *Client) GetChainTxStatsNBlocks(nBlocks int32) (*btcjson.GetChainTxStatsResult, error) {
|
|
|
|
return c.GetChainTxStatsNBlocksAsync(nBlocks).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChainTxStatsNBlocksBlockHash returns statistics about the total number and rate of transactions in the chain.
|
|
|
|
//
|
|
|
|
// First argument specifies size of the window in number of blocks.
|
|
|
|
// Second argument is the hash of the block that ends the window.
|
|
|
|
func (c *Client) GetChainTxStatsNBlocksBlockHash(nBlocks int32, blockHash chainhash.Hash) (*btcjson.GetChainTxStatsResult, error) {
|
|
|
|
return c.GetChainTxStatsNBlocksBlockHashAsync(nBlocks, blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureGetDifficultyResult is a future promise to deliver the result of a
|
|
|
|
// GetDifficultyAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetDifficultyResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2014-05-07 18:14:39 +02:00
|
|
|
// proof-of-work difficulty as a multiple of the minimum difficulty.
|
|
|
|
func (r FutureGetDifficultyResult) Receive() (float64, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a float64.
|
|
|
|
var difficulty float64
|
|
|
|
err = json.Unmarshal(res, &difficulty)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return difficulty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifficultyAsync 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 GetDifficulty for the blocking version and more details.
|
|
|
|
func (c *Client) GetDifficultyAsync() FutureGetDifficultyResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetDifficultyCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifficulty returns the proof-of-work difficulty as a multiple of the
|
|
|
|
// minimum difficulty.
|
|
|
|
func (c *Client) GetDifficulty() (float64, error) {
|
|
|
|
return c.GetDifficultyAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2016-11-09 20:13:10 +01:00
|
|
|
// FutureGetBlockChainInfoResult is a promise to deliver the result of a
|
2016-11-07 01:03:08 +01:00
|
|
|
// GetBlockChainInfoAsync RPC invocation (or an applicable error).
|
2019-10-30 02:50:46 +01:00
|
|
|
type FutureGetBlockChainInfoResult struct {
|
|
|
|
client *Client
|
2021-09-02 08:39:55 +02:00
|
|
|
Response chan *Response
|
2019-10-30 02:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalPartialGetBlockChainInfoResult unmarshals the response into an
|
|
|
|
// instance of GetBlockChainInfoResult without populating the SoftForks and
|
|
|
|
// UnifiedSoftForks fields.
|
|
|
|
func unmarshalPartialGetBlockChainInfoResult(res []byte) (*btcjson.GetBlockChainInfoResult, error) {
|
|
|
|
var chainInfo btcjson.GetBlockChainInfoResult
|
|
|
|
if err := json.Unmarshal(res, &chainInfo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &chainInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// unmarshalGetBlockChainInfoResultSoftForks properly unmarshals the softforks
|
|
|
|
// related fields into the GetBlockChainInfoResult instance.
|
|
|
|
func unmarshalGetBlockChainInfoResultSoftForks(chainInfo *btcjson.GetBlockChainInfoResult,
|
|
|
|
version BackendVersion, res []byte) error {
|
|
|
|
|
|
|
|
switch version {
|
|
|
|
// Versions of bitcoind on or after v0.19.0 use the unified format.
|
|
|
|
case BitcoindPost19:
|
|
|
|
var softForks btcjson.UnifiedSoftForks
|
|
|
|
if err := json.Unmarshal(res, &softForks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chainInfo.UnifiedSoftForks = &softForks
|
|
|
|
|
|
|
|
// All other versions use the original format.
|
|
|
|
default:
|
|
|
|
var softForks btcjson.SoftForks
|
|
|
|
if err := json.Unmarshal(res, &softForks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chainInfo.SoftForks = &softForks
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-07 01:03:08 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns chain info
|
2016-11-07 01:03:08 +01:00
|
|
|
// result provided by the server.
|
|
|
|
func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r.Response)
|
2019-10-30 02:50:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chainInfo, err := unmarshalPartialGetBlockChainInfoResult(res)
|
2016-11-07 01:03:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-30 02:50:46 +01:00
|
|
|
// Inspect the version to determine how we'll need to parse the
|
|
|
|
// softforks from the response.
|
|
|
|
version, err := r.client.BackendVersion()
|
|
|
|
if err != nil {
|
2016-11-07 01:03:08 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-30 02:50:46 +01:00
|
|
|
|
|
|
|
err = unmarshalGetBlockChainInfoResultSoftForks(chainInfo, version, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chainInfo, nil
|
2016-11-07 01:03:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockChainInfoAsync 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 GetBlockChainInfo for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult {
|
|
|
|
cmd := btcjson.NewGetBlockChainInfoCmd()
|
2019-10-30 02:50:46 +01:00
|
|
|
return FutureGetBlockChainInfoResult{
|
|
|
|
client: c,
|
2021-09-02 08:39:55 +02:00
|
|
|
Response: c.SendCmd(cmd),
|
2019-10-30 02:50:46 +01:00
|
|
|
}
|
2016-11-07 01:03:08 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 20:13:10 +01:00
|
|
|
// GetBlockChainInfo returns information related to the processing state of
|
2016-11-07 01:03:08 +01:00
|
|
|
// various chain-specific details such as the current difficulty from the tip
|
2016-11-09 20:13:10 +01:00
|
|
|
// of the main chain.
|
2016-11-07 01:03:08 +01:00
|
|
|
func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
|
|
|
|
return c.GetBlockChainInfoAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2020-05-16 12:12:28 +02:00
|
|
|
// FutureGetBlockFilterResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockFilterAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockFilterResult chan *Response
|
2020-05-16 12:12:28 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns block filter
|
2020-05-16 12:12:28 +02:00
|
|
|
// result provided by the server.
|
|
|
|
func (r FutureGetBlockFilterResult) Receive() (*btcjson.GetBlockFilterResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-05-16 12:12:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockFilter btcjson.GetBlockFilterResult
|
|
|
|
err = json.Unmarshal(res, &blockFilter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blockFilter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockFilterAsync 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 GetBlockFilter for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockFilterAsync(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) FutureGetBlockFilterResult {
|
|
|
|
hash := blockHash.String()
|
|
|
|
|
|
|
|
cmd := btcjson.NewGetBlockFilterCmd(hash, filterType)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-05-16 12:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockFilter retrieves a BIP0157 content filter for a particular block.
|
|
|
|
func (c *Client) GetBlockFilter(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) (*btcjson.GetBlockFilterResult, error) {
|
|
|
|
return c.GetBlockFilterAsync(blockHash, filterType).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureGetBlockHashResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockHashAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockHashResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the hash of
|
2014-05-07 18:14:39 +02:00
|
|
|
// the block in the best block chain at the given height.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a string-encoded sha.
|
|
|
|
var txHashStr string
|
|
|
|
err = json.Unmarshal(res, &txHashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2016-08-08 21:16:04 +02:00
|
|
|
return chainhash.NewHashFromStr(txHashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHashAsync 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 GetBlockHash for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockHashCmd(blockHeight)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHash returns the hash of the block in the best block chain at the
|
|
|
|
// given height.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBlockHashAsync(blockHeight).Receive()
|
|
|
|
}
|
|
|
|
|
2016-09-15 18:38:52 +02:00
|
|
|
// FutureGetBlockHeaderResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockHeaderAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockHeaderResult chan *Response
|
2016-09-15 18:38:52 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2016-09-15 18:38:52 +02:00
|
|
|
// blockheader requested from the server given its hash.
|
|
|
|
func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2016-09-15 18:38:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a string.
|
|
|
|
var bhHex string
|
|
|
|
err = json.Unmarshal(res, &bhHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedBH, err := hex.DecodeString(bhHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the blockheader and return it.
|
|
|
|
var bh wire.BlockHeader
|
|
|
|
err = bh.Deserialize(bytes.NewReader(serializedBH))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bh, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHeaderAsync 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 GetBlockHeader for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult {
|
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false))
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2016-09-15 18:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHeader returns the blockheader from the server given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlockHeaderVerbose to retrieve a data structure with information about the
|
|
|
|
// block instead.
|
|
|
|
func (c *Client) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
|
|
|
|
return c.GetBlockHeaderAsync(blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockHeaderVerboseResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockHeaderVerboseResult chan *Response
|
2016-09-15 18:38:52 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2016-09-15 18:38:52 +02:00
|
|
|
// data structure of the blockheader requested from the server given its hash.
|
|
|
|
func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2016-09-15 18:38:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a string.
|
|
|
|
var bh btcjson.GetBlockHeaderVerboseResult
|
|
|
|
err = json.Unmarshal(res, &bh)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHeaderVerboseAsync 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 GetBlockHeader for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockHeaderVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderVerboseResult {
|
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(true))
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2016-09-15 18:38:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHeaderVerbose returns a data structure with information about the
|
|
|
|
// blockheader from the server given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlockHeader to retrieve a blockheader instead.
|
|
|
|
func (c *Client) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {
|
|
|
|
return c.GetBlockHeaderVerboseAsync(blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:27:05 +01:00
|
|
|
// FutureGetMempoolEntryResult is a future promise to deliver the result of a
|
|
|
|
// GetMempoolEntryAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetMempoolEntryResult chan *Response
|
2017-02-03 20:27:05 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns a data
|
2017-02-03 20:27:05 +01:00
|
|
|
// structure with information about the transaction in the memory pool given
|
|
|
|
// its hash.
|
|
|
|
func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2017-02-03 20:27:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal the result as an array of strings.
|
|
|
|
var mempoolEntryResult btcjson.GetMempoolEntryResult
|
|
|
|
err = json.Unmarshal(res, &mempoolEntryResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &mempoolEntryResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMempoolEntryAsync 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 GetMempoolEntry for the blocking version and more details.
|
|
|
|
func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult {
|
|
|
|
cmd := btcjson.NewGetMempoolEntryCmd(txHash)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-02-03 20:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMempoolEntry returns a data structure with information about the
|
|
|
|
// transaction in the memory pool given its hash.
|
|
|
|
func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) {
|
|
|
|
return c.GetMempoolEntryAsync(txHash).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureGetRawMempoolResult is a future promise to deliver the result of a
|
|
|
|
// GetRawMempoolAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetRawMempoolResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the hashes
|
2014-05-07 18:14:39 +02:00
|
|
|
// of all transactions in the memory pool.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as an array of strings.
|
|
|
|
var txHashStrs []string
|
|
|
|
err = json.Unmarshal(res, &txHashStrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Create a slice of ShaHash arrays from the string slice.
|
2016-08-08 21:16:04 +02:00
|
|
|
txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
|
2014-05-07 18:14:39 +02:00
|
|
|
for _, hashStr := range txHashStrs {
|
2016-08-08 21:16:04 +02:00
|
|
|
txHash, err := chainhash.NewHashFromStr(hashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txHashes = append(txHashes, txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHashes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolAsync 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 GetRawMempool for the blocking version and more details.
|
|
|
|
func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempool returns the hashes of all transactions in the memory pool.
|
|
|
|
//
|
|
|
|
// See GetRawMempoolVerbose to retrieve data structures with information about
|
|
|
|
// the transactions instead.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetRawMempoolAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetRawMempoolVerboseResult is a future promise to deliver the result of
|
|
|
|
// a GetRawMempoolVerboseAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetRawMempoolVerboseResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns a map of
|
2014-05-07 18:14:39 +02:00
|
|
|
// transaction hashes to an associated data structure with information about the
|
|
|
|
// transaction for all transactions in the memory pool.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a map of strings (tx shas) to their detailed
|
|
|
|
// results.
|
2015-01-01 23:34:07 +01:00
|
|
|
var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
|
2014-06-10 02:49:41 +02:00
|
|
|
err = json.Unmarshal(res, &mempoolItems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return mempoolItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolVerboseAsync 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 GetRawMempoolVerbose for the blocking version and more details.
|
|
|
|
func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolVerbose returns a map of transaction hashes to an associated
|
|
|
|
// data structure with information about the transaction for all transactions in
|
|
|
|
// the memory pool.
|
|
|
|
//
|
|
|
|
// See GetRawMempool to retrieve only the transaction hashes instead.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetRawMempoolVerboseAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2017-11-13 23:39:16 +01:00
|
|
|
// FutureEstimateFeeResult is a future promise to deliver the result of a
|
|
|
|
// EstimateFeeAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureEstimateFeeResult chan *Response
|
2017-11-13 23:39:16 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the info
|
2017-11-13 23:39:16 +01:00
|
|
|
// provided by the server.
|
|
|
|
func (r FutureEstimateFeeResult) Receive() (float64, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2017-11-13 23:39:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a getinfo result object.
|
|
|
|
var fee float64
|
|
|
|
err = json.Unmarshal(res, &fee)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fee, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateFeeAsync 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 EstimateFee for the blocking version and more details.
|
|
|
|
func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult {
|
|
|
|
cmd := btcjson.NewEstimateFeeCmd(numBlocks)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-11-13 23:39:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateFee provides an estimated fee in bitcoins per kilobyte.
|
|
|
|
func (c *Client) EstimateFee(numBlocks int64) (float64, error) {
|
|
|
|
return c.EstimateFeeAsync(numBlocks).Receive()
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:33:48 +01:00
|
|
|
// FutureEstimateFeeResult is a future promise to deliver the result of a
|
|
|
|
// EstimateSmartFeeAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureEstimateSmartFeeResult chan *Response
|
2019-11-26 14:33:48 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2019-11-26 14:33:48 +01:00
|
|
|
// estimated fee.
|
|
|
|
func (r FutureEstimateSmartFeeResult) Receive() (*btcjson.EstimateSmartFeeResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2019-11-26 14:33:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var verified btcjson.EstimateSmartFeeResult
|
|
|
|
err = json.Unmarshal(res, &verified)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &verified, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateSmartFeeAsync 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 EstimateSmartFee for the blocking version and more details.
|
|
|
|
func (c *Client) EstimateSmartFeeAsync(confTarget int64, mode *btcjson.EstimateSmartFeeMode) FutureEstimateSmartFeeResult {
|
|
|
|
cmd := btcjson.NewEstimateSmartFeeCmd(confTarget, mode)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2019-11-26 14:33:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EstimateSmartFee requests the server to estimate a fee level based on the given parameters.
|
|
|
|
func (c *Client) EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) {
|
|
|
|
return c.EstimateSmartFeeAsync(confTarget, mode).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureVerifyChainResult is a future promise to deliver the result of a
|
|
|
|
// VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync
|
|
|
|
// invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureVerifyChainResult chan *Response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns whether
|
2014-05-07 18:14:39 +02:00
|
|
|
// or not the chain verified based on the check level and number of blocks
|
|
|
|
// to verify specified in the original call.
|
|
|
|
func (r FutureVerifyChainResult) Receive() (bool, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a boolean.
|
|
|
|
var verified bool
|
|
|
|
err = json.Unmarshal(res, &verified)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return verified, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainAsync 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 VerifyChain for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(nil, nil)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChain requests the server to verify the block chain database using
|
|
|
|
// the default check level and number of blocks to verify.
|
|
|
|
//
|
|
|
|
// See VerifyChainLevel and VerifyChainBlocks to override the defaults.
|
|
|
|
func (c *Client) VerifyChain() (bool, error) {
|
|
|
|
return c.VerifyChainAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainLevelAsync 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 VerifyChainLevel for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainLevel requests the server to verify the block chain database using
|
|
|
|
// the passed check level and default number of blocks to verify.
|
|
|
|
//
|
|
|
|
// The check level controls how thorough the verification is with higher numbers
|
|
|
|
// increasing the amount of checks done as consequently how long the
|
|
|
|
// verification takes.
|
|
|
|
//
|
|
|
|
// See VerifyChain to use the default check level and VerifyChainBlocks to
|
|
|
|
// override the number of blocks to verify.
|
|
|
|
func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
|
|
|
|
return c.VerifyChainLevelAsync(checkLevel).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainBlocksAsync 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 VerifyChainBlocks for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainBlocks requests the server to verify the block chain database
|
|
|
|
// using the passed check level and number of blocks to verify.
|
|
|
|
//
|
|
|
|
// The check level controls how thorough the verification is with higher numbers
|
|
|
|
// increasing the amount of checks done as consequently how long the
|
|
|
|
// verification takes.
|
|
|
|
//
|
|
|
|
// The number of blocks refers to the number of blocks from the end of the
|
|
|
|
// current longest chain.
|
|
|
|
//
|
|
|
|
// See VerifyChain and VerifyChainLevel to use defaults.
|
|
|
|
func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
|
|
|
|
return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
|
|
|
|
}
|
2014-10-17 22:39:45 +02:00
|
|
|
|
|
|
|
// FutureGetTxOutResult is a future promise to deliver the result of a
|
|
|
|
// GetTxOutAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetTxOutResult chan *Response
|
2014-10-17 22:39:45 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns a
|
2014-10-17 22:39:45 +02:00
|
|
|
// transaction given its hash.
|
|
|
|
func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2014-10-17 22:39:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// take care of the special case where the output has been spent already
|
|
|
|
// it should return the string "null"
|
|
|
|
if string(res) == "null" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as an gettxout result object.
|
|
|
|
var txOutInfo *btcjson.GetTxOutResult
|
|
|
|
err = json.Unmarshal(res, &txOutInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txOutInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOutAsync 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 GetTxOut for the blocking version and more details.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult {
|
2014-10-17 22:39:45 +02:00
|
|
|
hash := ""
|
|
|
|
if txHash != nil {
|
|
|
|
hash = txHash.String()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2014-10-17 22:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOut returns the transaction output info if it's unspent and
|
|
|
|
// nil, otherwise.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
|
2014-10-17 22:39:45 +02:00
|
|
|
return c.GetTxOutAsync(txHash, index, mempool).Receive()
|
|
|
|
}
|
2017-01-25 03:44:20 +01:00
|
|
|
|
2020-09-28 14:42:18 +02:00
|
|
|
// FutureGetTxOutSetInfoResult is a future promise to deliver the result of a
|
|
|
|
// GetTxOutSetInfoAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetTxOutSetInfoResult chan *Response
|
2020-09-28 14:42:18 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2020-09-28 14:42:18 +02:00
|
|
|
// results of GetTxOutSetInfoAsync RPC invocation.
|
|
|
|
func (r FutureGetTxOutSetInfoResult) Receive() (*btcjson.GetTxOutSetInfoResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-09-28 14:42:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as an gettxoutsetinfo result object.
|
|
|
|
var txOutSetInfo *btcjson.GetTxOutSetInfoResult
|
|
|
|
err = json.Unmarshal(res, &txOutSetInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txOutSetInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOutSetInfoAsync 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 GetTxOutSetInfo for the blocking version and more details.
|
|
|
|
func (c *Client) GetTxOutSetInfoAsync() FutureGetTxOutSetInfoResult {
|
|
|
|
cmd := btcjson.NewGetTxOutSetInfoCmd()
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-09-28 14:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOutSetInfo returns the statistics about the unspent transaction output
|
|
|
|
// set.
|
|
|
|
func (c *Client) GetTxOutSetInfo() (*btcjson.GetTxOutSetInfoResult, error) {
|
|
|
|
return c.GetTxOutSetInfoAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:44:20 +01:00
|
|
|
// FutureRescanBlocksResult is a future promise to deliver the result of a
|
|
|
|
// RescanBlocksAsync RPC invocation (or an applicable error).
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureRescanBlocksResult chan *Response
|
2017-01-25 03:44:20 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the
|
2017-01-25 03:44:20 +01:00
|
|
|
// discovered rescanblocks data.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2017-01-25 03:44:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rescanBlocksResult []btcjson.RescannedBlock
|
|
|
|
err = json.Unmarshal(res, &rescanBlocksResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rescanBlocksResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RescanBlocksAsync 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 RescanBlocks for the blocking version and more details.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult {
|
|
|
|
strBlockHashes := make([]string, len(blockHashes))
|
|
|
|
for i := range blockHashes {
|
|
|
|
strBlockHashes[i] = blockHashes[i].String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewRescanBlocksCmd(strBlockHashes)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-01-25 03:44:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RescanBlocks rescans the blocks identified by blockHashes, in order, using
|
|
|
|
// the client's loaded transaction filter. The blocks do not need to be on the
|
|
|
|
// main chain, but they do need to be adjacent to each other.
|
|
|
|
//
|
|
|
|
// NOTE: This is a btcsuite extension ported from
|
|
|
|
// github.com/decred/dcrrpcclient.
|
|
|
|
func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) {
|
|
|
|
return c.RescanBlocksAsync(blockHashes).Receive()
|
|
|
|
}
|
2017-06-29 16:02:09 +02:00
|
|
|
|
|
|
|
// FutureInvalidateBlockResult is a future promise to deliver the result of a
|
|
|
|
// InvalidateBlockAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureInvalidateBlockResult chan *Response
|
2017-06-29 16:02:09 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the raw
|
2017-06-29 16:02:09 +02:00
|
|
|
// block requested from the server given its hash.
|
|
|
|
func (r FutureInvalidateBlockResult) Receive() error {
|
2021-09-02 08:39:55 +02:00
|
|
|
_, err := ReceiveFuture(r)
|
2017-06-29 16:02:09 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidateBlockAsync 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 InvalidateBlock for the blocking version and more details.
|
|
|
|
func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult {
|
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewInvalidateBlockCmd(hash)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-06-29 16:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidateBlock invalidates a specific block.
|
|
|
|
func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) error {
|
|
|
|
return c.InvalidateBlockAsync(blockHash).Receive()
|
|
|
|
}
|
2017-08-25 01:30:18 +02:00
|
|
|
|
|
|
|
// FutureGetCFilterResult is a future promise to deliver the result of a
|
|
|
|
// GetCFilterAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetCFilterResult chan *Response
|
2017-08-25 01:30:18 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the raw
|
2017-08-25 01:30:18 +02:00
|
|
|
// filter requested from the server given its block hash.
|
|
|
|
func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2017-08-25 01:30:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a string.
|
|
|
|
var filterHex string
|
|
|
|
err = json.Unmarshal(res, &filterHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the serialized cf hex to raw bytes.
|
|
|
|
serializedFilter, err := hex.DecodeString(filterHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the filter bytes to the correct field of the wire message.
|
|
|
|
// We aren't going to set the block hash or extended flag, since we
|
|
|
|
// don't actually get that back in the RPC response.
|
|
|
|
var msgCFilter wire.MsgCFilter
|
|
|
|
msgCFilter.Data = serializedFilter
|
|
|
|
return &msgCFilter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCFilterAsync 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 GetCFilter for the blocking version and more details.
|
2017-10-11 04:52:39 +02:00
|
|
|
func (c *Client) GetCFilterAsync(blockHash *chainhash.Hash,
|
2017-10-31 07:24:57 +01:00
|
|
|
filterType wire.FilterType) FutureGetCFilterResult {
|
2017-08-25 01:30:18 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
2017-10-11 04:52:39 +02:00
|
|
|
cmd := btcjson.NewGetCFilterCmd(hash, filterType)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-08-25 01:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCFilter returns a raw filter from the server given its block hash.
|
2017-10-11 04:52:39 +02:00
|
|
|
func (c *Client) GetCFilter(blockHash *chainhash.Hash,
|
2017-10-31 07:24:57 +01:00
|
|
|
filterType wire.FilterType) (*wire.MsgCFilter, error) {
|
2017-10-11 04:52:39 +02:00
|
|
|
return c.GetCFilterAsync(blockHash, filterType).Receive()
|
2017-08-25 01:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetCFilterHeaderResult is a future promise to deliver the result of a
|
|
|
|
// GetCFilterHeaderAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetCFilterHeaderResult chan *Response
|
2017-08-25 01:30:18 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the raw
|
2017-08-25 01:30:18 +02:00
|
|
|
// filter header requested from the server given its block hash.
|
|
|
|
func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2017-08-25 01:30:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a string.
|
|
|
|
var headerHex string
|
|
|
|
err = json.Unmarshal(res, &headerHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the decoded header into a hash
|
|
|
|
headerHash, err := chainhash.NewHashFromStr(headerHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the hash to a headers message and return it.
|
2018-02-01 06:47:03 +01:00
|
|
|
msgCFHeaders := wire.MsgCFHeaders{PrevFilterHeader: *headerHash}
|
2017-08-25 01:30:18 +02:00
|
|
|
return &msgCFHeaders, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCFilterHeaderAsync 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 GetCFilterHeader for the blocking version and more details.
|
2017-10-11 04:52:39 +02:00
|
|
|
func (c *Client) GetCFilterHeaderAsync(blockHash *chainhash.Hash,
|
2017-10-31 07:24:57 +01:00
|
|
|
filterType wire.FilterType) FutureGetCFilterHeaderResult {
|
2017-08-25 01:30:18 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
2017-10-11 04:52:39 +02:00
|
|
|
cmd := btcjson.NewGetCFilterHeaderCmd(hash, filterType)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2017-08-25 01:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCFilterHeader returns a raw filter header from the server given its block
|
|
|
|
// hash.
|
2017-10-11 04:52:39 +02:00
|
|
|
func (c *Client) GetCFilterHeader(blockHash *chainhash.Hash,
|
2017-10-31 07:24:57 +01:00
|
|
|
filterType wire.FilterType) (*wire.MsgCFHeaders, error) {
|
2017-10-11 04:52:39 +02:00
|
|
|
return c.GetCFilterHeaderAsync(blockHash, filterType).Receive()
|
2017-08-25 01:30:18 +02:00
|
|
|
}
|
2019-11-27 07:49:59 +01:00
|
|
|
|
|
|
|
// FutureGetBlockStatsResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockStatsAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetBlockStatsResult chan *Response
|
2019-11-27 07:49:59 +01:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns statistics
|
2019-11-27 07:49:59 +01:00
|
|
|
// of a block at a certain height.
|
|
|
|
func (r FutureGetBlockStatsResult) Receive() (*btcjson.GetBlockStatsResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2019-11-27 07:49:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockStats btcjson.GetBlockStatsResult
|
|
|
|
err = json.Unmarshal(res, &blockStats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blockStats, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockStatsAsync 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 GetBlockStats or the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockStatsAsync(hashOrHeight interface{}, stats *[]string) FutureGetBlockStatsResult {
|
|
|
|
if hash, ok := hashOrHeight.(*chainhash.Hash); ok {
|
|
|
|
hashOrHeight = hash.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := btcjson.NewGetBlockStatsCmd(btcjson.HashOrHeight{Value: hashOrHeight}, stats)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2019-11-27 07:49:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockStats returns block statistics. First argument specifies height or hash of the target block.
|
|
|
|
// Second argument allows to select certain stats to return.
|
|
|
|
func (c *Client) GetBlockStats(hashOrHeight interface{}, stats *[]string) (*btcjson.GetBlockStatsResult, error) {
|
|
|
|
return c.GetBlockStatsAsync(hashOrHeight, stats).Receive()
|
|
|
|
}
|
2020-08-24 20:53:04 +02:00
|
|
|
|
2020-09-13 09:47:39 +02:00
|
|
|
// FutureDeriveAddressesResult is a future promise to deliver the result of an
|
|
|
|
// DeriveAddressesAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureDeriveAddressesResult chan *Response
|
2020-09-13 09:47:39 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and derives one or more addresses
|
2020-09-13 09:47:39 +02:00
|
|
|
// corresponding to the given output descriptor.
|
|
|
|
func (r FutureDeriveAddressesResult) Receive() (*btcjson.DeriveAddressesResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-09-13 09:47:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var deriveAddressesResult btcjson.DeriveAddressesResult
|
|
|
|
|
|
|
|
err = json.Unmarshal(res, &deriveAddressesResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &deriveAddressesResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeriveAddressesAsync 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 DeriveAddresses for the blocking version and more details.
|
|
|
|
func (c *Client) DeriveAddressesAsync(descriptor string, descriptorRange *btcjson.DescriptorRange) FutureDeriveAddressesResult {
|
|
|
|
cmd := btcjson.NewDeriveAddressesCmd(descriptor, descriptorRange)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-09-13 09:47:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeriveAddresses derives one or more addresses corresponding to an output
|
|
|
|
// descriptor. If a ranged descriptor is used, the end or the range
|
|
|
|
// (in [begin,end] notation) to derive must be specified.
|
|
|
|
func (c *Client) DeriveAddresses(descriptor string, descriptorRange *btcjson.DescriptorRange) (*btcjson.DeriveAddressesResult, error) {
|
|
|
|
return c.DeriveAddressesAsync(descriptor, descriptorRange).Receive()
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:53:04 +02:00
|
|
|
// FutureGetDescriptorInfoResult is a future promise to deliver the result of a
|
|
|
|
// GetDescriptorInfoAsync RPC invocation (or an applicable error).
|
2021-09-02 08:39:55 +02:00
|
|
|
type FutureGetDescriptorInfoResult chan *Response
|
2020-08-24 20:53:04 +02:00
|
|
|
|
2021-09-02 08:39:55 +02:00
|
|
|
// Receive waits for the Response promised by the future and returns the analysed
|
2020-08-24 20:53:04 +02:00
|
|
|
// info of the descriptor.
|
|
|
|
func (r FutureGetDescriptorInfoResult) Receive() (*btcjson.GetDescriptorInfoResult, error) {
|
2021-09-02 08:39:55 +02:00
|
|
|
res, err := ReceiveFuture(r)
|
2020-08-24 20:53:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var descriptorInfo btcjson.GetDescriptorInfoResult
|
2020-09-13 09:47:39 +02:00
|
|
|
|
2020-08-24 20:53:04 +02:00
|
|
|
err = json.Unmarshal(res, &descriptorInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-13 09:47:39 +02:00
|
|
|
|
2020-08-24 20:53:04 +02:00
|
|
|
return &descriptorInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDescriptorInfoAsync 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 GetDescriptorInfo for the blocking version and more details.
|
|
|
|
func (c *Client) GetDescriptorInfoAsync(descriptor string) FutureGetDescriptorInfoResult {
|
|
|
|
cmd := btcjson.NewGetDescriptorInfoCmd(descriptor)
|
2021-09-02 08:39:55 +02:00
|
|
|
return c.SendCmd(cmd)
|
2020-08-24 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDescriptorInfo returns the analysed info of a descriptor string, by invoking the
|
|
|
|
// getdescriptorinfo RPC.
|
|
|
|
//
|
|
|
|
// Use this function to analyse a descriptor string, or compute the checksum
|
|
|
|
// for a descriptor without one.
|
|
|
|
//
|
|
|
|
// See btcjson.GetDescriptorInfoResult for details about the result.
|
|
|
|
func (c *Client) GetDescriptorInfo(descriptor string) (*btcjson.GetDescriptorInfoResult, error) {
|
|
|
|
return c.GetDescriptorInfoAsync(descriptor).Receive()
|
|
|
|
}
|