github.com/btcsuite/btcd@v0.24.0/rpcclient/chain.go (about)

     1  // Copyright (c) 2014-2017 The btcsuite developers
     2  // Copyright (c) 2015-2017 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package rpcclient
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/hex"
    11  	"encoding/json"
    12  
    13  	"github.com/btcsuite/btcd/btcjson"
    14  	"github.com/btcsuite/btcd/chaincfg/chainhash"
    15  	"github.com/btcsuite/btcd/wire"
    16  )
    17  
    18  // FutureGetBestBlockHashResult is a future promise to deliver the result of a
    19  // GetBestBlockAsync RPC invocation (or an applicable error).
    20  type FutureGetBestBlockHashResult chan *Response
    21  
    22  // Receive waits for the Response promised by the future and returns the hash of
    23  // the best block in the longest block chain.
    24  func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) {
    25  	res, err := ReceiveFuture(r)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	// Unmarshal result as a string.
    31  	var txHashStr string
    32  	err = json.Unmarshal(res, &txHashStr)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return chainhash.NewHashFromStr(txHashStr)
    37  }
    38  
    39  // GetBestBlockHashAsync returns an instance of a type that can be used to get
    40  // the result of the RPC at some future time by invoking the Receive function on
    41  // the returned instance.
    42  //
    43  // See GetBestBlockHash for the blocking version and more details.
    44  func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult {
    45  	cmd := btcjson.NewGetBestBlockHashCmd()
    46  	return c.SendCmd(cmd)
    47  }
    48  
    49  // GetBestBlockHash returns the hash of the best block in the longest block
    50  // chain.
    51  func (c *Client) GetBestBlockHash() (*chainhash.Hash, error) {
    52  	return c.GetBestBlockHashAsync().Receive()
    53  }
    54  
    55  // legacyGetBlockRequest constructs and sends a legacy getblock request which
    56  // contains two separate bools to denote verbosity, in contract to a single int
    57  // parameter.
    58  func (c *Client) legacyGetBlockRequest(hash string, verbose,
    59  	verboseTx bool) ([]byte, error) {
    60  
    61  	hashJSON, err := json.Marshal(hash)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	verboseJSON, err := json.Marshal(btcjson.Bool(verbose))
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	verboseTxJSON, err := json.Marshal(btcjson.Bool(verboseTx))
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return c.RawRequest("getblock", []json.RawMessage{
    74  		hashJSON, verboseJSON, verboseTxJSON,
    75  	})
    76  }
    77  
    78  // waitForGetBlockRes waits for the Response of a getblock request. If the
    79  // Response indicates an invalid parameter was provided, a legacy style of the
    80  // request is resent and its Response is returned instead.
    81  func (c *Client) waitForGetBlockRes(respChan chan *Response, hash string,
    82  	verbose, verboseTx bool) ([]byte, error) {
    83  
    84  	res, err := ReceiveFuture(respChan)
    85  
    86  	// If we receive an invalid parameter error, then we may be
    87  	// communicating with a btcd node which only understands the legacy
    88  	// request, so we'll try that.
    89  	if err, ok := err.(*btcjson.RPCError); ok &&
    90  		err.Code == btcjson.ErrRPCInvalidParams.Code {
    91  		return c.legacyGetBlockRequest(hash, verbose, verboseTx)
    92  	}
    93  
    94  	// Otherwise, we can return the Response as is.
    95  	return res, err
    96  }
    97  
    98  // FutureGetBlockResult is a future promise to deliver the result of a
    99  // GetBlockAsync RPC invocation (or an applicable error).
   100  type FutureGetBlockResult struct {
   101  	client   *Client
   102  	hash     string
   103  	Response chan *Response
   104  }
   105  
   106  // Receive waits for the Response promised by the future and returns the raw
   107  // block requested from the server given its hash.
   108  func (r FutureGetBlockResult) Receive() (*wire.MsgBlock, error) {
   109  	res, err := r.client.waitForGetBlockRes(r.Response, r.hash, false, false)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	// Unmarshal result as a string.
   115  	var blockHex string
   116  	err = json.Unmarshal(res, &blockHex)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	// Decode the serialized block hex to raw bytes.
   122  	serializedBlock, err := hex.DecodeString(blockHex)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	// Deserialize the block and return it.
   128  	var msgBlock wire.MsgBlock
   129  	err = msgBlock.Deserialize(bytes.NewReader(serializedBlock))
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	return &msgBlock, nil
   134  }
   135  
   136  // GetBlockAsync returns an instance of a type that can be used to get the
   137  // result of the RPC at some future time by invoking the Receive function on the
   138  // returned instance.
   139  //
   140  // See GetBlock for the blocking version and more details.
   141  func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult {
   142  	hash := ""
   143  	if blockHash != nil {
   144  		hash = blockHash.String()
   145  	}
   146  
   147  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(0))
   148  	return FutureGetBlockResult{
   149  		client:   c,
   150  		hash:     hash,
   151  		Response: c.SendCmd(cmd),
   152  	}
   153  }
   154  
   155  // GetBlock returns a raw block from the server given its hash.
   156  //
   157  // See GetBlockVerbose to retrieve a data structure with information about the
   158  // block instead.
   159  func (c *Client) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock, error) {
   160  	return c.GetBlockAsync(blockHash).Receive()
   161  }
   162  
   163  // FutureGetBlockVerboseResult is a future promise to deliver the result of a
   164  // GetBlockVerboseAsync RPC invocation (or an applicable error).
   165  type FutureGetBlockVerboseResult struct {
   166  	client   *Client
   167  	hash     string
   168  	Response chan *Response
   169  }
   170  
   171  // Receive waits for the Response promised by the future and returns the data
   172  // structure from the server with information about the requested block.
   173  func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) {
   174  	res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, false)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	// Unmarshal the raw result into a BlockResult.
   180  	var blockResult btcjson.GetBlockVerboseResult
   181  	err = json.Unmarshal(res, &blockResult)
   182  	if err != nil {
   183  		return nil, err
   184  	}
   185  	return &blockResult, nil
   186  }
   187  
   188  // GetBlockVerboseAsync returns an instance of a type that can be used to get
   189  // the result of the RPC at some future time by invoking the Receive function on
   190  // the returned instance.
   191  //
   192  // See GetBlockVerbose for the blocking version and more details.
   193  func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
   194  	hash := ""
   195  	if blockHash != nil {
   196  		hash = blockHash.String()
   197  	}
   198  	// From the bitcoin-cli getblock documentation:
   199  	// "If verbosity is 1, returns an Object with information about block ."
   200  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(1))
   201  	return FutureGetBlockVerboseResult{
   202  		client:   c,
   203  		hash:     hash,
   204  		Response: c.SendCmd(cmd),
   205  	}
   206  }
   207  
   208  // GetBlockVerbose returns a data structure from the server with information
   209  // about a block given its hash.
   210  //
   211  // See GetBlockVerboseTx to retrieve transaction data structures as well.
   212  // See GetBlock to retrieve a raw block instead.
   213  func (c *Client) GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
   214  	return c.GetBlockVerboseAsync(blockHash).Receive()
   215  }
   216  
   217  // FutureGetBlockVerboseTxResult is a future promise to deliver the result of a
   218  // GetBlockVerboseTxResult RPC invocation (or an applicable error).
   219  type FutureGetBlockVerboseTxResult struct {
   220  	client   *Client
   221  	hash     string
   222  	Response chan *Response
   223  }
   224  
   225  // Receive waits for the Response promised by the future and returns a verbose
   226  // version of the block including detailed information about its transactions.
   227  func (r FutureGetBlockVerboseTxResult) Receive() (*btcjson.GetBlockVerboseTxResult, error) {
   228  	res, err := r.client.waitForGetBlockRes(r.Response, r.hash, true, true)
   229  	if err != nil {
   230  		return nil, err
   231  	}
   232  
   233  	var blockResult btcjson.GetBlockVerboseTxResult
   234  	err = json.Unmarshal(res, &blockResult)
   235  	if err != nil {
   236  		return nil, err
   237  	}
   238  
   239  	return &blockResult, nil
   240  }
   241  
   242  // GetBlockVerboseTxAsync returns an instance of a type that can be used to get
   243  // the result of the RPC at some future time by invoking the Receive function on
   244  // the returned instance.
   245  //
   246  // See GetBlockVerboseTx or the blocking version and more details.
   247  func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseTxResult {
   248  	hash := ""
   249  	if blockHash != nil {
   250  		hash = blockHash.String()
   251  	}
   252  
   253  	// From the bitcoin-cli getblock documentation:
   254  	//
   255  	// If verbosity is 2, returns an Object with information about block
   256  	// and information about each transaction.
   257  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Int(2))
   258  	return FutureGetBlockVerboseTxResult{
   259  		client:   c,
   260  		hash:     hash,
   261  		Response: c.SendCmd(cmd),
   262  	}
   263  }
   264  
   265  // GetBlockVerboseTx returns a data structure from the server with information
   266  // about a block and its transactions given its hash.
   267  //
   268  // See GetBlockVerbose if only transaction hashes are preferred.
   269  // See GetBlock to retrieve a raw block instead.
   270  func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseTxResult, error) {
   271  	return c.GetBlockVerboseTxAsync(blockHash).Receive()
   272  }
   273  
   274  // FutureGetBlockCountResult is a future promise to deliver the result of a
   275  // GetBlockCountAsync RPC invocation (or an applicable error).
   276  type FutureGetBlockCountResult chan *Response
   277  
   278  // Receive waits for the Response promised by the future and returns the number
   279  // of blocks in the longest block chain.
   280  func (r FutureGetBlockCountResult) Receive() (int64, error) {
   281  	res, err := ReceiveFuture(r)
   282  	if err != nil {
   283  		return 0, err
   284  	}
   285  
   286  	// Unmarshal the result as an int64.
   287  	var count int64
   288  	err = json.Unmarshal(res, &count)
   289  	if err != nil {
   290  		return 0, err
   291  	}
   292  	return count, nil
   293  }
   294  
   295  // GetBlockCountAsync returns an instance of a type that can be used to get the
   296  // result of the RPC at some future time by invoking the Receive function on the
   297  // returned instance.
   298  //
   299  // See GetBlockCount for the blocking version and more details.
   300  func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult {
   301  	cmd := btcjson.NewGetBlockCountCmd()
   302  	return c.SendCmd(cmd)
   303  }
   304  
   305  // GetBlockCount returns the number of blocks in the longest block chain.
   306  func (c *Client) GetBlockCount() (int64, error) {
   307  	return c.GetBlockCountAsync().Receive()
   308  }
   309  
   310  // FutureGetChainTxStatsResult is a future promise to deliver the result of a
   311  // GetChainTxStatsAsync RPC invocation (or an applicable error).
   312  type FutureGetChainTxStatsResult chan *Response
   313  
   314  // Receive waits for the Response promised by the future and returns transaction statistics
   315  func (r FutureGetChainTxStatsResult) Receive() (*btcjson.GetChainTxStatsResult, error) {
   316  	res, err := ReceiveFuture(r)
   317  	if err != nil {
   318  		return nil, err
   319  	}
   320  
   321  	var chainTxStats btcjson.GetChainTxStatsResult
   322  	err = json.Unmarshal(res, &chainTxStats)
   323  	if err != nil {
   324  		return nil, err
   325  	}
   326  
   327  	return &chainTxStats, nil
   328  }
   329  
   330  // GetChainTxStatsAsync returns an instance of a type that can be used to get
   331  // the result of the RPC at some future time by invoking the Receive function on
   332  // the returned instance.
   333  //
   334  // See GetChainTxStats for the blocking version and more details.
   335  func (c *Client) GetChainTxStatsAsync() FutureGetChainTxStatsResult {
   336  	cmd := btcjson.NewGetChainTxStatsCmd(nil, nil)
   337  	return c.SendCmd(cmd)
   338  }
   339  
   340  // GetChainTxStatsNBlocksAsync returns an instance of a type that can be used to get
   341  // the result of the RPC at some future time by invoking the Receive function on
   342  // the returned instance.
   343  //
   344  // See GetChainTxStatsNBlocks for the blocking version and more details.
   345  func (c *Client) GetChainTxStatsNBlocksAsync(nBlocks int32) FutureGetChainTxStatsResult {
   346  	cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, nil)
   347  	return c.SendCmd(cmd)
   348  }
   349  
   350  // GetChainTxStatsNBlocksBlockHashAsync returns an instance of a type that can be used to get
   351  // the result of the RPC at some future time by invoking the Receive function on
   352  // the returned instance.
   353  //
   354  // See GetChainTxStatsNBlocksBlockHash for the blocking version and more details.
   355  func (c *Client) GetChainTxStatsNBlocksBlockHashAsync(nBlocks int32, blockHash chainhash.Hash) FutureGetChainTxStatsResult {
   356  	hash := blockHash.String()
   357  	cmd := btcjson.NewGetChainTxStatsCmd(&nBlocks, &hash)
   358  	return c.SendCmd(cmd)
   359  }
   360  
   361  // GetChainTxStats returns statistics about the total number and rate of transactions in the chain.
   362  //
   363  // Size of the window is one month and it ends at chain tip.
   364  func (c *Client) GetChainTxStats() (*btcjson.GetChainTxStatsResult, error) {
   365  	return c.GetChainTxStatsAsync().Receive()
   366  }
   367  
   368  // GetChainTxStatsNBlocks returns statistics about the total number and rate of transactions in the chain.
   369  //
   370  // The argument specifies size of the window in number of blocks. The window ends at chain tip.
   371  func (c *Client) GetChainTxStatsNBlocks(nBlocks int32) (*btcjson.GetChainTxStatsResult, error) {
   372  	return c.GetChainTxStatsNBlocksAsync(nBlocks).Receive()
   373  }
   374  
   375  // GetChainTxStatsNBlocksBlockHash returns statistics about the total number and rate of transactions in the chain.
   376  //
   377  // First argument specifies size of the window in number of blocks.
   378  // Second argument is the hash of the block that ends the window.
   379  func (c *Client) GetChainTxStatsNBlocksBlockHash(nBlocks int32, blockHash chainhash.Hash) (*btcjson.GetChainTxStatsResult, error) {
   380  	return c.GetChainTxStatsNBlocksBlockHashAsync(nBlocks, blockHash).Receive()
   381  }
   382  
   383  // FutureGetDifficultyResult is a future promise to deliver the result of a
   384  // GetDifficultyAsync RPC invocation (or an applicable error).
   385  type FutureGetDifficultyResult chan *Response
   386  
   387  // Receive waits for the Response promised by the future and returns the
   388  // proof-of-work difficulty as a multiple of the minimum difficulty.
   389  func (r FutureGetDifficultyResult) Receive() (float64, error) {
   390  	res, err := ReceiveFuture(r)
   391  	if err != nil {
   392  		return 0, err
   393  	}
   394  
   395  	// Unmarshal the result as a float64.
   396  	var difficulty float64
   397  	err = json.Unmarshal(res, &difficulty)
   398  	if err != nil {
   399  		return 0, err
   400  	}
   401  	return difficulty, nil
   402  }
   403  
   404  // GetDifficultyAsync returns an instance of a type that can be used to get the
   405  // result of the RPC at some future time by invoking the Receive function on the
   406  // returned instance.
   407  //
   408  // See GetDifficulty for the blocking version and more details.
   409  func (c *Client) GetDifficultyAsync() FutureGetDifficultyResult {
   410  	cmd := btcjson.NewGetDifficultyCmd()
   411  	return c.SendCmd(cmd)
   412  }
   413  
   414  // GetDifficulty returns the proof-of-work difficulty as a multiple of the
   415  // minimum difficulty.
   416  func (c *Client) GetDifficulty() (float64, error) {
   417  	return c.GetDifficultyAsync().Receive()
   418  }
   419  
   420  // FutureGetBlockChainInfoResult is a promise to deliver the result of a
   421  // GetBlockChainInfoAsync RPC invocation (or an applicable error).
   422  type FutureGetBlockChainInfoResult struct {
   423  	client   *Client
   424  	Response chan *Response
   425  }
   426  
   427  // unmarshalPartialGetBlockChainInfoResult unmarshals the response into an
   428  // instance of GetBlockChainInfoResult without populating the SoftForks and
   429  // UnifiedSoftForks fields.
   430  func unmarshalPartialGetBlockChainInfoResult(res []byte) (*btcjson.GetBlockChainInfoResult, error) {
   431  	var chainInfo btcjson.GetBlockChainInfoResult
   432  	if err := json.Unmarshal(res, &chainInfo); err != nil {
   433  		return nil, err
   434  	}
   435  	return &chainInfo, nil
   436  }
   437  
   438  // unmarshalGetBlockChainInfoResultSoftForks properly unmarshals the softforks
   439  // related fields into the GetBlockChainInfoResult instance.
   440  func unmarshalGetBlockChainInfoResultSoftForks(chainInfo *btcjson.GetBlockChainInfoResult,
   441  	version BackendVersion, res []byte) error {
   442  
   443  	switch version {
   444  	// Versions of bitcoind on or after v0.19.0 use the unified format.
   445  	case BitcoindPost19:
   446  		var softForks btcjson.UnifiedSoftForks
   447  		if err := json.Unmarshal(res, &softForks); err != nil {
   448  			return err
   449  		}
   450  		chainInfo.UnifiedSoftForks = &softForks
   451  
   452  	// All other versions use the original format.
   453  	default:
   454  		var softForks btcjson.SoftForks
   455  		if err := json.Unmarshal(res, &softForks); err != nil {
   456  			return err
   457  		}
   458  		chainInfo.SoftForks = &softForks
   459  	}
   460  
   461  	return nil
   462  }
   463  
   464  // Receive waits for the Response promised by the future and returns chain info
   465  // result provided by the server.
   466  func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) {
   467  	res, err := ReceiveFuture(r.Response)
   468  	if err != nil {
   469  		return nil, err
   470  	}
   471  	chainInfo, err := unmarshalPartialGetBlockChainInfoResult(res)
   472  	if err != nil {
   473  		return nil, err
   474  	}
   475  
   476  	// Inspect the version to determine how we'll need to parse the
   477  	// softforks from the response.
   478  	version, err := r.client.BackendVersion()
   479  	if err != nil {
   480  		return nil, err
   481  	}
   482  
   483  	err = unmarshalGetBlockChainInfoResultSoftForks(chainInfo, version, res)
   484  	if err != nil {
   485  		return nil, err
   486  	}
   487  
   488  	return chainInfo, nil
   489  }
   490  
   491  // GetBlockChainInfoAsync returns an instance of a type that can be used to get
   492  // the result of the RPC at some future time by invoking the Receive function
   493  // on the returned instance.
   494  //
   495  // See GetBlockChainInfo for the blocking version and more details.
   496  func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult {
   497  	cmd := btcjson.NewGetBlockChainInfoCmd()
   498  	return FutureGetBlockChainInfoResult{
   499  		client:   c,
   500  		Response: c.SendCmd(cmd),
   501  	}
   502  }
   503  
   504  // GetBlockChainInfo returns information related to the processing state of
   505  // various chain-specific details such as the current difficulty from the tip
   506  // of the main chain.
   507  func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
   508  	return c.GetBlockChainInfoAsync().Receive()
   509  }
   510  
   511  // FutureGetBlockFilterResult is a future promise to deliver the result of a
   512  // GetBlockFilterAsync RPC invocation (or an applicable error).
   513  type FutureGetBlockFilterResult chan *Response
   514  
   515  // Receive waits for the Response promised by the future and returns block filter
   516  // result provided by the server.
   517  func (r FutureGetBlockFilterResult) Receive() (*btcjson.GetBlockFilterResult, error) {
   518  	res, err := ReceiveFuture(r)
   519  	if err != nil {
   520  		return nil, err
   521  	}
   522  
   523  	var blockFilter btcjson.GetBlockFilterResult
   524  	err = json.Unmarshal(res, &blockFilter)
   525  	if err != nil {
   526  		return nil, err
   527  	}
   528  
   529  	return &blockFilter, nil
   530  }
   531  
   532  // GetBlockFilterAsync returns an instance of a type that can be used to get the
   533  // result of the RPC at some future time by invoking the Receive function on the
   534  // returned instance.
   535  //
   536  // See GetBlockFilter for the blocking version and more details.
   537  func (c *Client) GetBlockFilterAsync(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) FutureGetBlockFilterResult {
   538  	hash := blockHash.String()
   539  
   540  	cmd := btcjson.NewGetBlockFilterCmd(hash, filterType)
   541  	return c.SendCmd(cmd)
   542  }
   543  
   544  // GetBlockFilter retrieves a BIP0157 content filter for a particular block.
   545  func (c *Client) GetBlockFilter(blockHash chainhash.Hash, filterType *btcjson.FilterTypeName) (*btcjson.GetBlockFilterResult, error) {
   546  	return c.GetBlockFilterAsync(blockHash, filterType).Receive()
   547  }
   548  
   549  // FutureGetBlockHashResult is a future promise to deliver the result of a
   550  // GetBlockHashAsync RPC invocation (or an applicable error).
   551  type FutureGetBlockHashResult chan *Response
   552  
   553  // Receive waits for the Response promised by the future and returns the hash of
   554  // the block in the best block chain at the given height.
   555  func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) {
   556  	res, err := ReceiveFuture(r)
   557  	if err != nil {
   558  		return nil, err
   559  	}
   560  
   561  	// Unmarshal the result as a string-encoded sha.
   562  	var txHashStr string
   563  	err = json.Unmarshal(res, &txHashStr)
   564  	if err != nil {
   565  		return nil, err
   566  	}
   567  	return chainhash.NewHashFromStr(txHashStr)
   568  }
   569  
   570  // GetBlockHashAsync returns an instance of a type that can be used to get the
   571  // result of the RPC at some future time by invoking the Receive function on the
   572  // returned instance.
   573  //
   574  // See GetBlockHash for the blocking version and more details.
   575  func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
   576  	cmd := btcjson.NewGetBlockHashCmd(blockHeight)
   577  	return c.SendCmd(cmd)
   578  }
   579  
   580  // GetBlockHash returns the hash of the block in the best block chain at the
   581  // given height.
   582  func (c *Client) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
   583  	return c.GetBlockHashAsync(blockHeight).Receive()
   584  }
   585  
   586  // FutureGetBlockHeaderResult is a future promise to deliver the result of a
   587  // GetBlockHeaderAsync RPC invocation (or an applicable error).
   588  type FutureGetBlockHeaderResult chan *Response
   589  
   590  // Receive waits for the Response promised by the future and returns the
   591  // blockheader requested from the server given its hash.
   592  func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) {
   593  	res, err := ReceiveFuture(r)
   594  	if err != nil {
   595  		return nil, err
   596  	}
   597  
   598  	// Unmarshal result as a string.
   599  	var bhHex string
   600  	err = json.Unmarshal(res, &bhHex)
   601  	if err != nil {
   602  		return nil, err
   603  	}
   604  
   605  	serializedBH, err := hex.DecodeString(bhHex)
   606  	if err != nil {
   607  		return nil, err
   608  	}
   609  
   610  	// Deserialize the blockheader and return it.
   611  	var bh wire.BlockHeader
   612  	err = bh.Deserialize(bytes.NewReader(serializedBH))
   613  	if err != nil {
   614  		return nil, err
   615  	}
   616  
   617  	return &bh, err
   618  }
   619  
   620  // GetBlockHeaderAsync returns an instance of a type that can be used to get the
   621  // result of the RPC at some future time by invoking the Receive function on the
   622  // returned instance.
   623  //
   624  // See GetBlockHeader for the blocking version and more details.
   625  func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult {
   626  	hash := ""
   627  	if blockHash != nil {
   628  		hash = blockHash.String()
   629  	}
   630  
   631  	cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false))
   632  	return c.SendCmd(cmd)
   633  }
   634  
   635  // GetBlockHeader returns the blockheader from the server given its hash.
   636  //
   637  // See GetBlockHeaderVerbose to retrieve a data structure with information about the
   638  // block instead.
   639  func (c *Client) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
   640  	return c.GetBlockHeaderAsync(blockHash).Receive()
   641  }
   642  
   643  // FutureGetBlockHeaderVerboseResult is a future promise to deliver the result of a
   644  // GetBlockAsync RPC invocation (or an applicable error).
   645  type FutureGetBlockHeaderVerboseResult chan *Response
   646  
   647  // Receive waits for the Response promised by the future and returns the
   648  // data structure of the blockheader requested from the server given its hash.
   649  func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
   650  	res, err := ReceiveFuture(r)
   651  	if err != nil {
   652  		return nil, err
   653  	}
   654  
   655  	// Unmarshal result as a string.
   656  	var bh btcjson.GetBlockHeaderVerboseResult
   657  	err = json.Unmarshal(res, &bh)
   658  	if err != nil {
   659  		return nil, err
   660  	}
   661  
   662  	return &bh, nil
   663  }
   664  
   665  // GetBlockHeaderVerboseAsync returns an instance of a type that can be used to get the
   666  // result of the RPC at some future time by invoking the Receive function on the
   667  // returned instance.
   668  //
   669  // See GetBlockHeader for the blocking version and more details.
   670  func (c *Client) GetBlockHeaderVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderVerboseResult {
   671  	hash := ""
   672  	if blockHash != nil {
   673  		hash = blockHash.String()
   674  	}
   675  
   676  	cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(true))
   677  	return c.SendCmd(cmd)
   678  }
   679  
   680  // GetBlockHeaderVerbose returns a data structure with information about the
   681  // blockheader from the server given its hash.
   682  //
   683  // See GetBlockHeader to retrieve a blockheader instead.
   684  func (c *Client) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {
   685  	return c.GetBlockHeaderVerboseAsync(blockHash).Receive()
   686  }
   687  
   688  // FutureGetChainTipsResult is a future promise to deliver the result of a
   689  // GetChainTips RPC invocation (or an applicable error).
   690  type FutureGetChainTipsResult chan *Response
   691  
   692  // Receive waits for the Response promised by the future and returns the
   693  // data structure of all the chain tips the node is aware of.
   694  func (r FutureGetChainTipsResult) Receive() ([]*btcjson.GetChainTipsResult, error) {
   695  	res, err := ReceiveFuture(r)
   696  	if err != nil {
   697  		return nil, err
   698  	}
   699  
   700  	// Unmarshal result as a string.
   701  	var chainTips []*btcjson.GetChainTipsResult
   702  	err = json.Unmarshal(res, &chainTips)
   703  	if err != nil {
   704  		return nil, err
   705  	}
   706  
   707  	return chainTips, nil
   708  }
   709  
   710  // GetChainTipsAsync returns an instance of a type that can be used to get the
   711  // result of the RPC at some future time by invoking the Receive function on the
   712  // returned instance.
   713  //
   714  // See GetChainTips for the blocking version and more details.
   715  func (c *Client) GetChainTipsAsync() FutureGetChainTipsResult {
   716  	cmd := btcjson.NewGetChainTipsCmd()
   717  	return c.SendCmd(cmd)
   718  }
   719  
   720  // GetChainTips returns a slice of data structure with information about all the
   721  // current chain tips that this node is aware of.
   722  func (c *Client) GetChainTips() ([]*btcjson.GetChainTipsResult, error) {
   723  	return c.GetChainTipsAsync().Receive()
   724  }
   725  
   726  // FutureGetMempoolEntryResult is a future promise to deliver the result of a
   727  // GetMempoolEntryAsync RPC invocation (or an applicable error).
   728  type FutureGetMempoolEntryResult chan *Response
   729  
   730  // Receive waits for the Response promised by the future and returns a data
   731  // structure with information about the transaction in the memory pool given
   732  // its hash.
   733  func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
   734  	res, err := ReceiveFuture(r)
   735  	if err != nil {
   736  		return nil, err
   737  	}
   738  
   739  	// Unmarshal the result as an array of strings.
   740  	var mempoolEntryResult btcjson.GetMempoolEntryResult
   741  	err = json.Unmarshal(res, &mempoolEntryResult)
   742  	if err != nil {
   743  		return nil, err
   744  	}
   745  
   746  	return &mempoolEntryResult, nil
   747  }
   748  
   749  // GetMempoolEntryAsync returns an instance of a type that can be used to get the
   750  // result of the RPC at some future time by invoking the Receive function on the
   751  // returned instance.
   752  //
   753  // See GetMempoolEntry for the blocking version and more details.
   754  func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult {
   755  	cmd := btcjson.NewGetMempoolEntryCmd(txHash)
   756  	return c.SendCmd(cmd)
   757  }
   758  
   759  // GetMempoolEntry returns a data structure with information about the
   760  // transaction in the memory pool given its hash.
   761  func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) {
   762  	return c.GetMempoolEntryAsync(txHash).Receive()
   763  }
   764  
   765  // FutureGetRawMempoolResult is a future promise to deliver the result of a
   766  // GetRawMempoolAsync RPC invocation (or an applicable error).
   767  type FutureGetRawMempoolResult chan *Response
   768  
   769  // Receive waits for the Response promised by the future and returns the hashes
   770  // of all transactions in the memory pool.
   771  func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
   772  	res, err := ReceiveFuture(r)
   773  	if err != nil {
   774  		return nil, err
   775  	}
   776  
   777  	// Unmarshal the result as an array of strings.
   778  	var txHashStrs []string
   779  	err = json.Unmarshal(res, &txHashStrs)
   780  	if err != nil {
   781  		return nil, err
   782  	}
   783  
   784  	// Create a slice of ShaHash arrays from the string slice.
   785  	txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
   786  	for _, hashStr := range txHashStrs {
   787  		txHash, err := chainhash.NewHashFromStr(hashStr)
   788  		if err != nil {
   789  			return nil, err
   790  		}
   791  		txHashes = append(txHashes, txHash)
   792  	}
   793  
   794  	return txHashes, nil
   795  }
   796  
   797  // GetRawMempoolAsync returns an instance of a type that can be used to get the
   798  // result of the RPC at some future time by invoking the Receive function on the
   799  // returned instance.
   800  //
   801  // See GetRawMempool for the blocking version and more details.
   802  func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
   803  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
   804  	return c.SendCmd(cmd)
   805  }
   806  
   807  // GetRawMempool returns the hashes of all transactions in the memory pool.
   808  //
   809  // See GetRawMempoolVerbose to retrieve data structures with information about
   810  // the transactions instead.
   811  func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) {
   812  	return c.GetRawMempoolAsync().Receive()
   813  }
   814  
   815  // FutureGetRawMempoolVerboseResult is a future promise to deliver the result of
   816  // a GetRawMempoolVerboseAsync RPC invocation (or an applicable error).
   817  type FutureGetRawMempoolVerboseResult chan *Response
   818  
   819  // Receive waits for the Response promised by the future and returns a map of
   820  // transaction hashes to an associated data structure with information about the
   821  // transaction for all transactions in the memory pool.
   822  func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
   823  	res, err := ReceiveFuture(r)
   824  	if err != nil {
   825  		return nil, err
   826  	}
   827  
   828  	// Unmarshal the result as a map of strings (tx shas) to their detailed
   829  	// results.
   830  	var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
   831  	err = json.Unmarshal(res, &mempoolItems)
   832  	if err != nil {
   833  		return nil, err
   834  	}
   835  	return mempoolItems, nil
   836  }
   837  
   838  // GetRawMempoolVerboseAsync returns an instance of a type that can be used to
   839  // get the result of the RPC at some future time by invoking the Receive
   840  // function on the returned instance.
   841  //
   842  // See GetRawMempoolVerbose for the blocking version and more details.
   843  func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
   844  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
   845  	return c.SendCmd(cmd)
   846  }
   847  
   848  // GetRawMempoolVerbose returns a map of transaction hashes to an associated
   849  // data structure with information about the transaction for all transactions in
   850  // the memory pool.
   851  //
   852  // See GetRawMempool to retrieve only the transaction hashes instead.
   853  func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
   854  	return c.GetRawMempoolVerboseAsync().Receive()
   855  }
   856  
   857  // FutureEstimateFeeResult is a future promise to deliver the result of a
   858  // EstimateFeeAsync RPC invocation (or an applicable error).
   859  type FutureEstimateFeeResult chan *Response
   860  
   861  // Receive waits for the Response promised by the future and returns the info
   862  // provided by the server.
   863  func (r FutureEstimateFeeResult) Receive() (float64, error) {
   864  	res, err := ReceiveFuture(r)
   865  	if err != nil {
   866  		return -1, err
   867  	}
   868  
   869  	// Unmarshal result as a getinfo result object.
   870  	var fee float64
   871  	err = json.Unmarshal(res, &fee)
   872  	if err != nil {
   873  		return -1, err
   874  	}
   875  
   876  	return fee, nil
   877  }
   878  
   879  // EstimateFeeAsync returns an instance of a type that can be used to get the result
   880  // of the RPC at some future time by invoking the Receive function on the
   881  // returned instance.
   882  //
   883  // See EstimateFee for the blocking version and more details.
   884  func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult {
   885  	cmd := btcjson.NewEstimateFeeCmd(numBlocks)
   886  	return c.SendCmd(cmd)
   887  }
   888  
   889  // EstimateFee provides an estimated fee  in bitcoins per kilobyte.
   890  func (c *Client) EstimateFee(numBlocks int64) (float64, error) {
   891  	return c.EstimateFeeAsync(numBlocks).Receive()
   892  }
   893  
   894  // FutureEstimateFeeResult is a future promise to deliver the result of a
   895  // EstimateSmartFeeAsync RPC invocation (or an applicable error).
   896  type FutureEstimateSmartFeeResult chan *Response
   897  
   898  // Receive waits for the Response promised by the future and returns the
   899  // estimated fee.
   900  func (r FutureEstimateSmartFeeResult) Receive() (*btcjson.EstimateSmartFeeResult, error) {
   901  	res, err := ReceiveFuture(r)
   902  	if err != nil {
   903  		return nil, err
   904  	}
   905  
   906  	var verified btcjson.EstimateSmartFeeResult
   907  	err = json.Unmarshal(res, &verified)
   908  	if err != nil {
   909  		return nil, err
   910  	}
   911  	return &verified, nil
   912  }
   913  
   914  // EstimateSmartFeeAsync returns an instance of a type that can be used to get the
   915  // result of the RPC at some future time by invoking the Receive function on the
   916  // returned instance.
   917  //
   918  // See EstimateSmartFee for the blocking version and more details.
   919  func (c *Client) EstimateSmartFeeAsync(confTarget int64, mode *btcjson.EstimateSmartFeeMode) FutureEstimateSmartFeeResult {
   920  	cmd := btcjson.NewEstimateSmartFeeCmd(confTarget, mode)
   921  	return c.SendCmd(cmd)
   922  }
   923  
   924  // EstimateSmartFee requests the server to estimate a fee level based on the given parameters.
   925  func (c *Client) EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) {
   926  	return c.EstimateSmartFeeAsync(confTarget, mode).Receive()
   927  }
   928  
   929  // FutureVerifyChainResult is a future promise to deliver the result of a
   930  // VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync
   931  // invocation (or an applicable error).
   932  type FutureVerifyChainResult chan *Response
   933  
   934  // Receive waits for the Response promised by the future and returns whether
   935  // or not the chain verified based on the check level and number of blocks
   936  // to verify specified in the original call.
   937  func (r FutureVerifyChainResult) Receive() (bool, error) {
   938  	res, err := ReceiveFuture(r)
   939  	if err != nil {
   940  		return false, err
   941  	}
   942  
   943  	// Unmarshal the result as a boolean.
   944  	var verified bool
   945  	err = json.Unmarshal(res, &verified)
   946  	if err != nil {
   947  		return false, err
   948  	}
   949  	return verified, nil
   950  }
   951  
   952  // VerifyChainAsync returns an instance of a type that can be used to get the
   953  // result of the RPC at some future time by invoking the Receive function on the
   954  // returned instance.
   955  //
   956  // See VerifyChain for the blocking version and more details.
   957  func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
   958  	cmd := btcjson.NewVerifyChainCmd(nil, nil)
   959  	return c.SendCmd(cmd)
   960  }
   961  
   962  // VerifyChain requests the server to verify the block chain database using
   963  // the default check level and number of blocks to verify.
   964  //
   965  // See VerifyChainLevel and VerifyChainBlocks to override the defaults.
   966  func (c *Client) VerifyChain() (bool, error) {
   967  	return c.VerifyChainAsync().Receive()
   968  }
   969  
   970  // VerifyChainLevelAsync returns an instance of a type that can be used to get
   971  // the result of the RPC at some future time by invoking the Receive function on
   972  // the returned instance.
   973  //
   974  // See VerifyChainLevel for the blocking version and more details.
   975  func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
   976  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
   977  	return c.SendCmd(cmd)
   978  }
   979  
   980  // VerifyChainLevel requests the server to verify the block chain database using
   981  // the passed check level and default number of blocks to verify.
   982  //
   983  // The check level controls how thorough the verification is with higher numbers
   984  // increasing the amount of checks done as consequently how long the
   985  // verification takes.
   986  //
   987  // See VerifyChain to use the default check level and VerifyChainBlocks to
   988  // override the number of blocks to verify.
   989  func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
   990  	return c.VerifyChainLevelAsync(checkLevel).Receive()
   991  }
   992  
   993  // VerifyChainBlocksAsync returns an instance of a type that can be used to get
   994  // the result of the RPC at some future time by invoking the Receive function on
   995  // the returned instance.
   996  //
   997  // See VerifyChainBlocks for the blocking version and more details.
   998  func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
   999  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
  1000  	return c.SendCmd(cmd)
  1001  }
  1002  
  1003  // VerifyChainBlocks requests the server to verify the block chain database
  1004  // using the passed check level and number of blocks to verify.
  1005  //
  1006  // The check level controls how thorough the verification is with higher numbers
  1007  // increasing the amount of checks done as consequently how long the
  1008  // verification takes.
  1009  //
  1010  // The number of blocks refers to the number of blocks from the end of the
  1011  // current longest chain.
  1012  //
  1013  // See VerifyChain and VerifyChainLevel to use defaults.
  1014  func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
  1015  	return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
  1016  }
  1017  
  1018  // FutureGetTxOutResult is a future promise to deliver the result of a
  1019  // GetTxOutAsync RPC invocation (or an applicable error).
  1020  type FutureGetTxOutResult chan *Response
  1021  
  1022  // Receive waits for the Response promised by the future and returns a
  1023  // transaction given its hash.
  1024  func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
  1025  	res, err := ReceiveFuture(r)
  1026  	if err != nil {
  1027  		return nil, err
  1028  	}
  1029  
  1030  	// take care of the special case where the output has been spent already
  1031  	// it should return the string "null"
  1032  	if string(res) == "null" {
  1033  		return nil, nil
  1034  	}
  1035  
  1036  	// Unmarshal result as an gettxout result object.
  1037  	var txOutInfo *btcjson.GetTxOutResult
  1038  	err = json.Unmarshal(res, &txOutInfo)
  1039  	if err != nil {
  1040  		return nil, err
  1041  	}
  1042  
  1043  	return txOutInfo, nil
  1044  }
  1045  
  1046  // GetTxOutAsync returns an instance of a type that can be used to get
  1047  // the result of the RPC at some future time by invoking the Receive function on
  1048  // the returned instance.
  1049  //
  1050  // See GetTxOut for the blocking version and more details.
  1051  func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult {
  1052  	hash := ""
  1053  	if txHash != nil {
  1054  		hash = txHash.String()
  1055  	}
  1056  
  1057  	cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
  1058  	return c.SendCmd(cmd)
  1059  }
  1060  
  1061  // GetTxOut returns the transaction output info if it's unspent and
  1062  // nil, otherwise.
  1063  func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
  1064  	return c.GetTxOutAsync(txHash, index, mempool).Receive()
  1065  }
  1066  
  1067  // FutureGetTxOutSetInfoResult is a future promise to deliver the result of a
  1068  // GetTxOutSetInfoAsync RPC invocation (or an applicable error).
  1069  type FutureGetTxOutSetInfoResult chan *Response
  1070  
  1071  // Receive waits for the Response promised by the future and returns the
  1072  // results of GetTxOutSetInfoAsync RPC invocation.
  1073  func (r FutureGetTxOutSetInfoResult) Receive() (*btcjson.GetTxOutSetInfoResult, error) {
  1074  	res, err := ReceiveFuture(r)
  1075  	if err != nil {
  1076  		return nil, err
  1077  	}
  1078  
  1079  	// Unmarshal result as an gettxoutsetinfo result object.
  1080  	var txOutSetInfo *btcjson.GetTxOutSetInfoResult
  1081  	err = json.Unmarshal(res, &txOutSetInfo)
  1082  	if err != nil {
  1083  		return nil, err
  1084  	}
  1085  
  1086  	return txOutSetInfo, nil
  1087  }
  1088  
  1089  // GetTxOutSetInfoAsync returns an instance of a type that can be used to get
  1090  // the result of the RPC at some future time by invoking the Receive function on
  1091  // the returned instance.
  1092  //
  1093  // See GetTxOutSetInfo for the blocking version and more details.
  1094  func (c *Client) GetTxOutSetInfoAsync() FutureGetTxOutSetInfoResult {
  1095  	cmd := btcjson.NewGetTxOutSetInfoCmd()
  1096  	return c.SendCmd(cmd)
  1097  }
  1098  
  1099  // GetTxOutSetInfo returns the statistics about the unspent transaction output
  1100  // set.
  1101  func (c *Client) GetTxOutSetInfo() (*btcjson.GetTxOutSetInfoResult, error) {
  1102  	return c.GetTxOutSetInfoAsync().Receive()
  1103  }
  1104  
  1105  // FutureRescanBlocksResult is a future promise to deliver the result of a
  1106  // RescanBlocksAsync RPC invocation (or an applicable error).
  1107  //
  1108  // NOTE: This is a btcsuite extension ported from
  1109  // github.com/decred/dcrrpcclient.
  1110  type FutureRescanBlocksResult chan *Response
  1111  
  1112  // Receive waits for the Response promised by the future and returns the
  1113  // discovered rescanblocks data.
  1114  //
  1115  // NOTE: This is a btcsuite extension ported from
  1116  // github.com/decred/dcrrpcclient.
  1117  func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
  1118  	res, err := ReceiveFuture(r)
  1119  	if err != nil {
  1120  		return nil, err
  1121  	}
  1122  
  1123  	var rescanBlocksResult []btcjson.RescannedBlock
  1124  	err = json.Unmarshal(res, &rescanBlocksResult)
  1125  	if err != nil {
  1126  		return nil, err
  1127  	}
  1128  
  1129  	return rescanBlocksResult, nil
  1130  }
  1131  
  1132  // RescanBlocksAsync returns an instance of a type that can be used to get the
  1133  // result of the RPC at some future time by invoking the Receive function on the
  1134  // returned instance.
  1135  //
  1136  // See RescanBlocks for the blocking version and more details.
  1137  //
  1138  // NOTE: This is a btcsuite extension ported from
  1139  // github.com/decred/dcrrpcclient.
  1140  func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult {
  1141  	strBlockHashes := make([]string, len(blockHashes))
  1142  	for i := range blockHashes {
  1143  		strBlockHashes[i] = blockHashes[i].String()
  1144  	}
  1145  
  1146  	cmd := btcjson.NewRescanBlocksCmd(strBlockHashes)
  1147  	return c.SendCmd(cmd)
  1148  }
  1149  
  1150  // RescanBlocks rescans the blocks identified by blockHashes, in order, using
  1151  // the client's loaded transaction filter.  The blocks do not need to be on the
  1152  // main chain, but they do need to be adjacent to each other.
  1153  //
  1154  // NOTE: This is a btcsuite extension ported from
  1155  // github.com/decred/dcrrpcclient.
  1156  func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) {
  1157  	return c.RescanBlocksAsync(blockHashes).Receive()
  1158  }
  1159  
  1160  // FutureInvalidateBlockResult is a future promise to deliver the result of a
  1161  // InvalidateBlockAsync RPC invocation (or an applicable error).
  1162  type FutureInvalidateBlockResult chan *Response
  1163  
  1164  // Receive waits for the Response promised by the future and returns the raw
  1165  // block requested from the server given its hash.
  1166  func (r FutureInvalidateBlockResult) Receive() error {
  1167  	_, err := ReceiveFuture(r)
  1168  
  1169  	return err
  1170  }
  1171  
  1172  // InvalidateBlockAsync returns an instance of a type that can be used to get the
  1173  // result of the RPC at some future time by invoking the Receive function on the
  1174  // returned instance.
  1175  //
  1176  // See InvalidateBlock for the blocking version and more details.
  1177  func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult {
  1178  	hash := ""
  1179  	if blockHash != nil {
  1180  		hash = blockHash.String()
  1181  	}
  1182  
  1183  	cmd := btcjson.NewInvalidateBlockCmd(hash)
  1184  	return c.SendCmd(cmd)
  1185  }
  1186  
  1187  // InvalidateBlock invalidates a specific block.
  1188  func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) error {
  1189  	return c.InvalidateBlockAsync(blockHash).Receive()
  1190  }
  1191  
  1192  // FutureGetCFilterResult is a future promise to deliver the result of a
  1193  // GetCFilterAsync RPC invocation (or an applicable error).
  1194  type FutureGetCFilterResult chan *Response
  1195  
  1196  // Receive waits for the Response promised by the future and returns the raw
  1197  // filter requested from the server given its block hash.
  1198  func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) {
  1199  	res, err := ReceiveFuture(r)
  1200  	if err != nil {
  1201  		return nil, err
  1202  	}
  1203  
  1204  	// Unmarshal result as a string.
  1205  	var filterHex string
  1206  	err = json.Unmarshal(res, &filterHex)
  1207  	if err != nil {
  1208  		return nil, err
  1209  	}
  1210  
  1211  	// Decode the serialized cf hex to raw bytes.
  1212  	serializedFilter, err := hex.DecodeString(filterHex)
  1213  	if err != nil {
  1214  		return nil, err
  1215  	}
  1216  
  1217  	// Assign the filter bytes to the correct field of the wire message.
  1218  	// We aren't going to set the block hash or extended flag, since we
  1219  	// don't actually get that back in the RPC response.
  1220  	var msgCFilter wire.MsgCFilter
  1221  	msgCFilter.Data = serializedFilter
  1222  	return &msgCFilter, nil
  1223  }
  1224  
  1225  // GetCFilterAsync returns an instance of a type that can be used to get the
  1226  // result of the RPC at some future time by invoking the Receive function on the
  1227  // returned instance.
  1228  //
  1229  // See GetCFilter for the blocking version and more details.
  1230  func (c *Client) GetCFilterAsync(blockHash *chainhash.Hash,
  1231  	filterType wire.FilterType) FutureGetCFilterResult {
  1232  	hash := ""
  1233  	if blockHash != nil {
  1234  		hash = blockHash.String()
  1235  	}
  1236  
  1237  	cmd := btcjson.NewGetCFilterCmd(hash, filterType)
  1238  	return c.SendCmd(cmd)
  1239  }
  1240  
  1241  // GetCFilter returns a raw filter from the server given its block hash.
  1242  func (c *Client) GetCFilter(blockHash *chainhash.Hash,
  1243  	filterType wire.FilterType) (*wire.MsgCFilter, error) {
  1244  	return c.GetCFilterAsync(blockHash, filterType).Receive()
  1245  }
  1246  
  1247  // FutureGetCFilterHeaderResult is a future promise to deliver the result of a
  1248  // GetCFilterHeaderAsync RPC invocation (or an applicable error).
  1249  type FutureGetCFilterHeaderResult chan *Response
  1250  
  1251  // Receive waits for the Response promised by the future and returns the raw
  1252  // filter header requested from the server given its block hash.
  1253  func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) {
  1254  	res, err := ReceiveFuture(r)
  1255  	if err != nil {
  1256  		return nil, err
  1257  	}
  1258  
  1259  	// Unmarshal result as a string.
  1260  	var headerHex string
  1261  	err = json.Unmarshal(res, &headerHex)
  1262  	if err != nil {
  1263  		return nil, err
  1264  	}
  1265  
  1266  	// Assign the decoded header into a hash
  1267  	headerHash, err := chainhash.NewHashFromStr(headerHex)
  1268  	if err != nil {
  1269  		return nil, err
  1270  	}
  1271  
  1272  	// Assign the hash to a headers message and return it.
  1273  	msgCFHeaders := wire.MsgCFHeaders{PrevFilterHeader: *headerHash}
  1274  	return &msgCFHeaders, nil
  1275  
  1276  }
  1277  
  1278  // GetCFilterHeaderAsync returns an instance of a type that can be used to get
  1279  // the result of the RPC at some future time by invoking the Receive function
  1280  // on the returned instance.
  1281  //
  1282  // See GetCFilterHeader for the blocking version and more details.
  1283  func (c *Client) GetCFilterHeaderAsync(blockHash *chainhash.Hash,
  1284  	filterType wire.FilterType) FutureGetCFilterHeaderResult {
  1285  	hash := ""
  1286  	if blockHash != nil {
  1287  		hash = blockHash.String()
  1288  	}
  1289  
  1290  	cmd := btcjson.NewGetCFilterHeaderCmd(hash, filterType)
  1291  	return c.SendCmd(cmd)
  1292  }
  1293  
  1294  // GetCFilterHeader returns a raw filter header from the server given its block
  1295  // hash.
  1296  func (c *Client) GetCFilterHeader(blockHash *chainhash.Hash,
  1297  	filterType wire.FilterType) (*wire.MsgCFHeaders, error) {
  1298  	return c.GetCFilterHeaderAsync(blockHash, filterType).Receive()
  1299  }
  1300  
  1301  // FutureGetBlockStatsResult is a future promise to deliver the result of a
  1302  // GetBlockStatsAsync RPC invocation (or an applicable error).
  1303  type FutureGetBlockStatsResult chan *Response
  1304  
  1305  // Receive waits for the Response promised by the future and returns statistics
  1306  // of a block at a certain height.
  1307  func (r FutureGetBlockStatsResult) Receive() (*btcjson.GetBlockStatsResult, error) {
  1308  	res, err := ReceiveFuture(r)
  1309  	if err != nil {
  1310  		return nil, err
  1311  	}
  1312  
  1313  	var blockStats btcjson.GetBlockStatsResult
  1314  	err = json.Unmarshal(res, &blockStats)
  1315  	if err != nil {
  1316  		return nil, err
  1317  	}
  1318  
  1319  	return &blockStats, nil
  1320  }
  1321  
  1322  // GetBlockStatsAsync returns an instance of a type that can be used to get
  1323  // the result of the RPC at some future time by invoking the Receive function on
  1324  // the returned instance.
  1325  //
  1326  // See GetBlockStats or the blocking version and more details.
  1327  func (c *Client) GetBlockStatsAsync(hashOrHeight interface{}, stats *[]string) FutureGetBlockStatsResult {
  1328  	if hash, ok := hashOrHeight.(*chainhash.Hash); ok {
  1329  		hashOrHeight = hash.String()
  1330  	}
  1331  
  1332  	cmd := btcjson.NewGetBlockStatsCmd(btcjson.HashOrHeight{Value: hashOrHeight}, stats)
  1333  	return c.SendCmd(cmd)
  1334  }
  1335  
  1336  // GetBlockStats returns block statistics. First argument specifies height or hash of the target block.
  1337  // Second argument allows to select certain stats to return.
  1338  func (c *Client) GetBlockStats(hashOrHeight interface{}, stats *[]string) (*btcjson.GetBlockStatsResult, error) {
  1339  	return c.GetBlockStatsAsync(hashOrHeight, stats).Receive()
  1340  }
  1341  
  1342  // FutureDeriveAddressesResult is a future promise to deliver the result of an
  1343  // DeriveAddressesAsync RPC invocation (or an applicable error).
  1344  type FutureDeriveAddressesResult chan *Response
  1345  
  1346  // Receive waits for the Response promised by the future and derives one or more addresses
  1347  // corresponding to the given output descriptor.
  1348  func (r FutureDeriveAddressesResult) Receive() (*btcjson.DeriveAddressesResult, error) {
  1349  	res, err := ReceiveFuture(r)
  1350  	if err != nil {
  1351  		return nil, err
  1352  	}
  1353  
  1354  	var deriveAddressesResult btcjson.DeriveAddressesResult
  1355  
  1356  	err = json.Unmarshal(res, &deriveAddressesResult)
  1357  	if err != nil {
  1358  		return nil, err
  1359  	}
  1360  
  1361  	return &deriveAddressesResult, nil
  1362  }
  1363  
  1364  // DeriveAddressesAsync returns an instance of a type that can be used to get the result
  1365  // of the RPC at some future time by invoking the Receive function on the
  1366  // returned instance.
  1367  //
  1368  // See DeriveAddresses for the blocking version and more details.
  1369  func (c *Client) DeriveAddressesAsync(descriptor string, descriptorRange *btcjson.DescriptorRange) FutureDeriveAddressesResult {
  1370  	cmd := btcjson.NewDeriveAddressesCmd(descriptor, descriptorRange)
  1371  	return c.SendCmd(cmd)
  1372  }
  1373  
  1374  // DeriveAddresses derives one or more addresses corresponding to an output
  1375  // descriptor. If a ranged descriptor is used, the end or the range
  1376  // (in [begin,end] notation) to derive must be specified.
  1377  func (c *Client) DeriveAddresses(descriptor string, descriptorRange *btcjson.DescriptorRange) (*btcjson.DeriveAddressesResult, error) {
  1378  	return c.DeriveAddressesAsync(descriptor, descriptorRange).Receive()
  1379  }
  1380  
  1381  // FutureGetDescriptorInfoResult is a future promise to deliver the result of a
  1382  // GetDescriptorInfoAsync RPC invocation (or an applicable error).
  1383  type FutureGetDescriptorInfoResult chan *Response
  1384  
  1385  // Receive waits for the Response promised by the future and returns the analysed
  1386  // info of the descriptor.
  1387  func (r FutureGetDescriptorInfoResult) Receive() (*btcjson.GetDescriptorInfoResult, error) {
  1388  	res, err := ReceiveFuture(r)
  1389  	if err != nil {
  1390  		return nil, err
  1391  	}
  1392  
  1393  	var descriptorInfo btcjson.GetDescriptorInfoResult
  1394  
  1395  	err = json.Unmarshal(res, &descriptorInfo)
  1396  	if err != nil {
  1397  		return nil, err
  1398  	}
  1399  
  1400  	return &descriptorInfo, nil
  1401  }
  1402  
  1403  // GetDescriptorInfoAsync returns an instance of a type that can be used to get
  1404  // the result of the RPC at some future time by invoking the Receive function on
  1405  // the returned instance.
  1406  //
  1407  // See GetDescriptorInfo for the blocking version and more details.
  1408  func (c *Client) GetDescriptorInfoAsync(descriptor string) FutureGetDescriptorInfoResult {
  1409  	cmd := btcjson.NewGetDescriptorInfoCmd(descriptor)
  1410  	return c.SendCmd(cmd)
  1411  }
  1412  
  1413  // GetDescriptorInfo returns the analysed info of a descriptor string, by invoking the
  1414  // getdescriptorinfo RPC.
  1415  //
  1416  // Use this function to analyse a descriptor string, or compute the checksum
  1417  // for a descriptor without one.
  1418  //
  1419  // See btcjson.GetDescriptorInfoResult for details about the result.
  1420  func (c *Client) GetDescriptorInfo(descriptor string) (*btcjson.GetDescriptorInfoResult, error) {
  1421  	return c.GetDescriptorInfoAsync(descriptor).Receive()
  1422  }