github.com/palcoin-project/palcd@v1.0.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/palcoin-project/palcd/btcjson"
    14  	"github.com/palcoin-project/palcd/chaincfg/chainhash"
    15  	"github.com/palcoin-project/palcd/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  // FutureGetMempoolEntryResult is a future promise to deliver the result of a
   689  // GetMempoolEntryAsync RPC invocation (or an applicable error).
   690  type FutureGetMempoolEntryResult chan *response
   691  
   692  // Receive waits for the response promised by the future and returns a data
   693  // structure with information about the transaction in the memory pool given
   694  // its hash.
   695  func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
   696  	res, err := receiveFuture(r)
   697  	if err != nil {
   698  		return nil, err
   699  	}
   700  
   701  	// Unmarshal the result as an array of strings.
   702  	var mempoolEntryResult btcjson.GetMempoolEntryResult
   703  	err = json.Unmarshal(res, &mempoolEntryResult)
   704  	if err != nil {
   705  		return nil, err
   706  	}
   707  
   708  	return &mempoolEntryResult, nil
   709  }
   710  
   711  // GetMempoolEntryAsync returns an instance of a type that can be used to get the
   712  // result of the RPC at some future time by invoking the Receive function on the
   713  // returned instance.
   714  //
   715  // See GetMempoolEntry for the blocking version and more details.
   716  func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult {
   717  	cmd := btcjson.NewGetMempoolEntryCmd(txHash)
   718  	return c.sendCmd(cmd)
   719  }
   720  
   721  // GetMempoolEntry returns a data structure with information about the
   722  // transaction in the memory pool given its hash.
   723  func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) {
   724  	return c.GetMempoolEntryAsync(txHash).Receive()
   725  }
   726  
   727  // FutureGetRawMempoolResult is a future promise to deliver the result of a
   728  // GetRawMempoolAsync RPC invocation (or an applicable error).
   729  type FutureGetRawMempoolResult chan *response
   730  
   731  // Receive waits for the response promised by the future and returns the hashes
   732  // of all transactions in the memory pool.
   733  func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, 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 txHashStrs []string
   741  	err = json.Unmarshal(res, &txHashStrs)
   742  	if err != nil {
   743  		return nil, err
   744  	}
   745  
   746  	// Create a slice of ShaHash arrays from the string slice.
   747  	txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
   748  	for _, hashStr := range txHashStrs {
   749  		txHash, err := chainhash.NewHashFromStr(hashStr)
   750  		if err != nil {
   751  			return nil, err
   752  		}
   753  		txHashes = append(txHashes, txHash)
   754  	}
   755  
   756  	return txHashes, nil
   757  }
   758  
   759  // GetRawMempoolAsync returns an instance of a type that can be used to get the
   760  // result of the RPC at some future time by invoking the Receive function on the
   761  // returned instance.
   762  //
   763  // See GetRawMempool for the blocking version and more details.
   764  func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
   765  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
   766  	return c.sendCmd(cmd)
   767  }
   768  
   769  // GetRawMempool returns the hashes of all transactions in the memory pool.
   770  //
   771  // See GetRawMempoolVerbose to retrieve data structures with information about
   772  // the transactions instead.
   773  func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) {
   774  	return c.GetRawMempoolAsync().Receive()
   775  }
   776  
   777  // FutureGetRawMempoolVerboseResult is a future promise to deliver the result of
   778  // a GetRawMempoolVerboseAsync RPC invocation (or an applicable error).
   779  type FutureGetRawMempoolVerboseResult chan *response
   780  
   781  // Receive waits for the response promised by the future and returns a map of
   782  // transaction hashes to an associated data structure with information about the
   783  // transaction for all transactions in the memory pool.
   784  func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
   785  	res, err := receiveFuture(r)
   786  	if err != nil {
   787  		return nil, err
   788  	}
   789  
   790  	// Unmarshal the result as a map of strings (tx shas) to their detailed
   791  	// results.
   792  	var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
   793  	err = json.Unmarshal(res, &mempoolItems)
   794  	if err != nil {
   795  		return nil, err
   796  	}
   797  	return mempoolItems, nil
   798  }
   799  
   800  // GetRawMempoolVerboseAsync returns an instance of a type that can be used to
   801  // get the result of the RPC at some future time by invoking the Receive
   802  // function on the returned instance.
   803  //
   804  // See GetRawMempoolVerbose for the blocking version and more details.
   805  func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
   806  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
   807  	return c.sendCmd(cmd)
   808  }
   809  
   810  // GetRawMempoolVerbose returns a map of transaction hashes to an associated
   811  // data structure with information about the transaction for all transactions in
   812  // the memory pool.
   813  //
   814  // See GetRawMempool to retrieve only the transaction hashes instead.
   815  func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
   816  	return c.GetRawMempoolVerboseAsync().Receive()
   817  }
   818  
   819  // FutureEstimateFeeResult is a future promise to deliver the result of a
   820  // EstimateFeeAsync RPC invocation (or an applicable error).
   821  type FutureEstimateFeeResult chan *response
   822  
   823  // Receive waits for the response promised by the future and returns the info
   824  // provided by the server.
   825  func (r FutureEstimateFeeResult) Receive() (float64, error) {
   826  	res, err := receiveFuture(r)
   827  	if err != nil {
   828  		return -1, err
   829  	}
   830  
   831  	// Unmarshal result as a getinfo result object.
   832  	var fee float64
   833  	err = json.Unmarshal(res, &fee)
   834  	if err != nil {
   835  		return -1, err
   836  	}
   837  
   838  	return fee, nil
   839  }
   840  
   841  // EstimateFeeAsync returns an instance of a type that can be used to get the result
   842  // of the RPC at some future time by invoking the Receive function on the
   843  // returned instance.
   844  //
   845  // See EstimateFee for the blocking version and more details.
   846  func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult {
   847  	cmd := btcjson.NewEstimateFeeCmd(numBlocks)
   848  	return c.sendCmd(cmd)
   849  }
   850  
   851  // EstimateFee provides an estimated fee  in bitcoins per kilobyte.
   852  func (c *Client) EstimateFee(numBlocks int64) (float64, error) {
   853  	return c.EstimateFeeAsync(numBlocks).Receive()
   854  }
   855  
   856  // FutureEstimateFeeResult is a future promise to deliver the result of a
   857  // EstimateSmartFeeAsync RPC invocation (or an applicable error).
   858  type FutureEstimateSmartFeeResult chan *response
   859  
   860  // Receive waits for the response promised by the future and returns the
   861  // estimated fee.
   862  func (r FutureEstimateSmartFeeResult) Receive() (*btcjson.EstimateSmartFeeResult, error) {
   863  	res, err := receiveFuture(r)
   864  	if err != nil {
   865  		return nil, err
   866  	}
   867  
   868  	var verified btcjson.EstimateSmartFeeResult
   869  	err = json.Unmarshal(res, &verified)
   870  	if err != nil {
   871  		return nil, err
   872  	}
   873  	return &verified, nil
   874  }
   875  
   876  // EstimateSmartFeeAsync returns an instance of a type that can be used to get the
   877  // result of the RPC at some future time by invoking the Receive function on the
   878  // returned instance.
   879  //
   880  // See EstimateSmartFee for the blocking version and more details.
   881  func (c *Client) EstimateSmartFeeAsync(confTarget int64, mode *btcjson.EstimateSmartFeeMode) FutureEstimateSmartFeeResult {
   882  	cmd := btcjson.NewEstimateSmartFeeCmd(confTarget, mode)
   883  	return c.sendCmd(cmd)
   884  }
   885  
   886  // EstimateSmartFee requests the server to estimate a fee level based on the given parameters.
   887  func (c *Client) EstimateSmartFee(confTarget int64, mode *btcjson.EstimateSmartFeeMode) (*btcjson.EstimateSmartFeeResult, error) {
   888  	return c.EstimateSmartFeeAsync(confTarget, mode).Receive()
   889  }
   890  
   891  // FutureVerifyChainResult is a future promise to deliver the result of a
   892  // VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync
   893  // invocation (or an applicable error).
   894  type FutureVerifyChainResult chan *response
   895  
   896  // Receive waits for the response promised by the future and returns whether
   897  // or not the chain verified based on the check level and number of blocks
   898  // to verify specified in the original call.
   899  func (r FutureVerifyChainResult) Receive() (bool, error) {
   900  	res, err := receiveFuture(r)
   901  	if err != nil {
   902  		return false, err
   903  	}
   904  
   905  	// Unmarshal the result as a boolean.
   906  	var verified bool
   907  	err = json.Unmarshal(res, &verified)
   908  	if err != nil {
   909  		return false, err
   910  	}
   911  	return verified, nil
   912  }
   913  
   914  // VerifyChainAsync 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 VerifyChain for the blocking version and more details.
   919  func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
   920  	cmd := btcjson.NewVerifyChainCmd(nil, nil)
   921  	return c.sendCmd(cmd)
   922  }
   923  
   924  // VerifyChain requests the server to verify the block chain database using
   925  // the default check level and number of blocks to verify.
   926  //
   927  // See VerifyChainLevel and VerifyChainBlocks to override the defaults.
   928  func (c *Client) VerifyChain() (bool, error) {
   929  	return c.VerifyChainAsync().Receive()
   930  }
   931  
   932  // VerifyChainLevelAsync returns an instance of a type that can be used to get
   933  // the result of the RPC at some future time by invoking the Receive function on
   934  // the returned instance.
   935  //
   936  // See VerifyChainLevel for the blocking version and more details.
   937  func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
   938  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
   939  	return c.sendCmd(cmd)
   940  }
   941  
   942  // VerifyChainLevel requests the server to verify the block chain database using
   943  // the passed check level and default number of blocks to verify.
   944  //
   945  // The check level controls how thorough the verification is with higher numbers
   946  // increasing the amount of checks done as consequently how long the
   947  // verification takes.
   948  //
   949  // See VerifyChain to use the default check level and VerifyChainBlocks to
   950  // override the number of blocks to verify.
   951  func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
   952  	return c.VerifyChainLevelAsync(checkLevel).Receive()
   953  }
   954  
   955  // VerifyChainBlocksAsync returns an instance of a type that can be used to get
   956  // the result of the RPC at some future time by invoking the Receive function on
   957  // the returned instance.
   958  //
   959  // See VerifyChainBlocks for the blocking version and more details.
   960  func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
   961  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
   962  	return c.sendCmd(cmd)
   963  }
   964  
   965  // VerifyChainBlocks requests the server to verify the block chain database
   966  // using the passed check level and number of blocks to verify.
   967  //
   968  // The check level controls how thorough the verification is with higher numbers
   969  // increasing the amount of checks done as consequently how long the
   970  // verification takes.
   971  //
   972  // The number of blocks refers to the number of blocks from the end of the
   973  // current longest chain.
   974  //
   975  // See VerifyChain and VerifyChainLevel to use defaults.
   976  func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
   977  	return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
   978  }
   979  
   980  // FutureGetTxOutResult is a future promise to deliver the result of a
   981  // GetTxOutAsync RPC invocation (or an applicable error).
   982  type FutureGetTxOutResult chan *response
   983  
   984  // Receive waits for the response promised by the future and returns a
   985  // transaction given its hash.
   986  func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
   987  	res, err := receiveFuture(r)
   988  	if err != nil {
   989  		return nil, err
   990  	}
   991  
   992  	// take care of the special case where the output has been spent already
   993  	// it should return the string "null"
   994  	if string(res) == "null" {
   995  		return nil, nil
   996  	}
   997  
   998  	// Unmarshal result as an gettxout result object.
   999  	var txOutInfo *btcjson.GetTxOutResult
  1000  	err = json.Unmarshal(res, &txOutInfo)
  1001  	if err != nil {
  1002  		return nil, err
  1003  	}
  1004  
  1005  	return txOutInfo, nil
  1006  }
  1007  
  1008  // GetTxOutAsync returns an instance of a type that can be used to get
  1009  // the result of the RPC at some future time by invoking the Receive function on
  1010  // the returned instance.
  1011  //
  1012  // See GetTxOut for the blocking version and more details.
  1013  func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult {
  1014  	hash := ""
  1015  	if txHash != nil {
  1016  		hash = txHash.String()
  1017  	}
  1018  
  1019  	cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
  1020  	return c.sendCmd(cmd)
  1021  }
  1022  
  1023  // GetTxOut returns the transaction output info if it's unspent and
  1024  // nil, otherwise.
  1025  func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
  1026  	return c.GetTxOutAsync(txHash, index, mempool).Receive()
  1027  }
  1028  
  1029  // FutureGetTxOutSetInfoResult is a future promise to deliver the result of a
  1030  // GetTxOutSetInfoAsync RPC invocation (or an applicable error).
  1031  type FutureGetTxOutSetInfoResult chan *response
  1032  
  1033  // Receive waits for the response promised by the future and returns the
  1034  // results of GetTxOutSetInfoAsync RPC invocation.
  1035  func (r FutureGetTxOutSetInfoResult) Receive() (*btcjson.GetTxOutSetInfoResult, error) {
  1036  	res, err := receiveFuture(r)
  1037  	if err != nil {
  1038  		return nil, err
  1039  	}
  1040  
  1041  	// Unmarshal result as an gettxoutsetinfo result object.
  1042  	var txOutSetInfo *btcjson.GetTxOutSetInfoResult
  1043  	err = json.Unmarshal(res, &txOutSetInfo)
  1044  	if err != nil {
  1045  		return nil, err
  1046  	}
  1047  
  1048  	return txOutSetInfo, nil
  1049  }
  1050  
  1051  // GetTxOutSetInfoAsync returns an instance of a type that can be used to get
  1052  // the result of the RPC at some future time by invoking the Receive function on
  1053  // the returned instance.
  1054  //
  1055  // See GetTxOutSetInfo for the blocking version and more details.
  1056  func (c *Client) GetTxOutSetInfoAsync() FutureGetTxOutSetInfoResult {
  1057  	cmd := btcjson.NewGetTxOutSetInfoCmd()
  1058  	return c.sendCmd(cmd)
  1059  }
  1060  
  1061  // GetTxOutSetInfo returns the statistics about the unspent transaction output
  1062  // set.
  1063  func (c *Client) GetTxOutSetInfo() (*btcjson.GetTxOutSetInfoResult, error) {
  1064  	return c.GetTxOutSetInfoAsync().Receive()
  1065  }
  1066  
  1067  // FutureRescanBlocksResult is a future promise to deliver the result of a
  1068  // RescanBlocksAsync RPC invocation (or an applicable error).
  1069  //
  1070  // NOTE: This is a btcsuite extension ported from
  1071  // github.com/decred/dcrrpcclient.
  1072  type FutureRescanBlocksResult chan *response
  1073  
  1074  // Receive waits for the response promised by the future and returns the
  1075  // discovered rescanblocks data.
  1076  //
  1077  // NOTE: This is a btcsuite extension ported from
  1078  // github.com/decred/dcrrpcclient.
  1079  func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
  1080  	res, err := receiveFuture(r)
  1081  	if err != nil {
  1082  		return nil, err
  1083  	}
  1084  
  1085  	var rescanBlocksResult []btcjson.RescannedBlock
  1086  	err = json.Unmarshal(res, &rescanBlocksResult)
  1087  	if err != nil {
  1088  		return nil, err
  1089  	}
  1090  
  1091  	return rescanBlocksResult, nil
  1092  }
  1093  
  1094  // RescanBlocksAsync returns an instance of a type that can be used to get the
  1095  // result of the RPC at some future time by invoking the Receive function on the
  1096  // returned instance.
  1097  //
  1098  // See RescanBlocks for the blocking version and more details.
  1099  //
  1100  // NOTE: This is a btcsuite extension ported from
  1101  // github.com/decred/dcrrpcclient.
  1102  func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult {
  1103  	strBlockHashes := make([]string, len(blockHashes))
  1104  	for i := range blockHashes {
  1105  		strBlockHashes[i] = blockHashes[i].String()
  1106  	}
  1107  
  1108  	cmd := btcjson.NewRescanBlocksCmd(strBlockHashes)
  1109  	return c.sendCmd(cmd)
  1110  }
  1111  
  1112  // RescanBlocks rescans the blocks identified by blockHashes, in order, using
  1113  // the client's loaded transaction filter.  The blocks do not need to be on the
  1114  // main chain, but they do need to be adjacent to each other.
  1115  //
  1116  // NOTE: This is a btcsuite extension ported from
  1117  // github.com/decred/dcrrpcclient.
  1118  func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) {
  1119  	return c.RescanBlocksAsync(blockHashes).Receive()
  1120  }
  1121  
  1122  // FutureInvalidateBlockResult is a future promise to deliver the result of a
  1123  // InvalidateBlockAsync RPC invocation (or an applicable error).
  1124  type FutureInvalidateBlockResult chan *response
  1125  
  1126  // Receive waits for the response promised by the future and returns the raw
  1127  // block requested from the server given its hash.
  1128  func (r FutureInvalidateBlockResult) Receive() error {
  1129  	_, err := receiveFuture(r)
  1130  
  1131  	return err
  1132  }
  1133  
  1134  // InvalidateBlockAsync returns an instance of a type that can be used to get the
  1135  // result of the RPC at some future time by invoking the Receive function on the
  1136  // returned instance.
  1137  //
  1138  // See InvalidateBlock for the blocking version and more details.
  1139  func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult {
  1140  	hash := ""
  1141  	if blockHash != nil {
  1142  		hash = blockHash.String()
  1143  	}
  1144  
  1145  	cmd := btcjson.NewInvalidateBlockCmd(hash)
  1146  	return c.sendCmd(cmd)
  1147  }
  1148  
  1149  // InvalidateBlock invalidates a specific block.
  1150  func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) error {
  1151  	return c.InvalidateBlockAsync(blockHash).Receive()
  1152  }
  1153  
  1154  // FutureGetCFilterResult is a future promise to deliver the result of a
  1155  // GetCFilterAsync RPC invocation (or an applicable error).
  1156  type FutureGetCFilterResult chan *response
  1157  
  1158  // Receive waits for the response promised by the future and returns the raw
  1159  // filter requested from the server given its block hash.
  1160  func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) {
  1161  	res, err := receiveFuture(r)
  1162  	if err != nil {
  1163  		return nil, err
  1164  	}
  1165  
  1166  	// Unmarshal result as a string.
  1167  	var filterHex string
  1168  	err = json.Unmarshal(res, &filterHex)
  1169  	if err != nil {
  1170  		return nil, err
  1171  	}
  1172  
  1173  	// Decode the serialized cf hex to raw bytes.
  1174  	serializedFilter, err := hex.DecodeString(filterHex)
  1175  	if err != nil {
  1176  		return nil, err
  1177  	}
  1178  
  1179  	// Assign the filter bytes to the correct field of the wire message.
  1180  	// We aren't going to set the block hash or extended flag, since we
  1181  	// don't actually get that back in the RPC response.
  1182  	var msgCFilter wire.MsgCFilter
  1183  	msgCFilter.Data = serializedFilter
  1184  	return &msgCFilter, nil
  1185  }
  1186  
  1187  // GetCFilterAsync returns an instance of a type that can be used to get the
  1188  // result of the RPC at some future time by invoking the Receive function on the
  1189  // returned instance.
  1190  //
  1191  // See GetCFilter for the blocking version and more details.
  1192  func (c *Client) GetCFilterAsync(blockHash *chainhash.Hash,
  1193  	filterType wire.FilterType) FutureGetCFilterResult {
  1194  	hash := ""
  1195  	if blockHash != nil {
  1196  		hash = blockHash.String()
  1197  	}
  1198  
  1199  	cmd := btcjson.NewGetCFilterCmd(hash, filterType)
  1200  	return c.sendCmd(cmd)
  1201  }
  1202  
  1203  // GetCFilter returns a raw filter from the server given its block hash.
  1204  func (c *Client) GetCFilter(blockHash *chainhash.Hash,
  1205  	filterType wire.FilterType) (*wire.MsgCFilter, error) {
  1206  	return c.GetCFilterAsync(blockHash, filterType).Receive()
  1207  }
  1208  
  1209  // FutureGetCFilterHeaderResult is a future promise to deliver the result of a
  1210  // GetCFilterHeaderAsync RPC invocation (or an applicable error).
  1211  type FutureGetCFilterHeaderResult chan *response
  1212  
  1213  // Receive waits for the response promised by the future and returns the raw
  1214  // filter header requested from the server given its block hash.
  1215  func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) {
  1216  	res, err := receiveFuture(r)
  1217  	if err != nil {
  1218  		return nil, err
  1219  	}
  1220  
  1221  	// Unmarshal result as a string.
  1222  	var headerHex string
  1223  	err = json.Unmarshal(res, &headerHex)
  1224  	if err != nil {
  1225  		return nil, err
  1226  	}
  1227  
  1228  	// Assign the decoded header into a hash
  1229  	headerHash, err := chainhash.NewHashFromStr(headerHex)
  1230  	if err != nil {
  1231  		return nil, err
  1232  	}
  1233  
  1234  	// Assign the hash to a headers message and return it.
  1235  	msgCFHeaders := wire.MsgCFHeaders{PrevFilterHeader: *headerHash}
  1236  	return &msgCFHeaders, nil
  1237  
  1238  }
  1239  
  1240  // GetCFilterHeaderAsync returns an instance of a type that can be used to get
  1241  // the result of the RPC at some future time by invoking the Receive function
  1242  // on the returned instance.
  1243  //
  1244  // See GetCFilterHeader for the blocking version and more details.
  1245  func (c *Client) GetCFilterHeaderAsync(blockHash *chainhash.Hash,
  1246  	filterType wire.FilterType) FutureGetCFilterHeaderResult {
  1247  	hash := ""
  1248  	if blockHash != nil {
  1249  		hash = blockHash.String()
  1250  	}
  1251  
  1252  	cmd := btcjson.NewGetCFilterHeaderCmd(hash, filterType)
  1253  	return c.sendCmd(cmd)
  1254  }
  1255  
  1256  // GetCFilterHeader returns a raw filter header from the server given its block
  1257  // hash.
  1258  func (c *Client) GetCFilterHeader(blockHash *chainhash.Hash,
  1259  	filterType wire.FilterType) (*wire.MsgCFHeaders, error) {
  1260  	return c.GetCFilterHeaderAsync(blockHash, filterType).Receive()
  1261  }
  1262  
  1263  // FutureGetBlockStatsResult is a future promise to deliver the result of a
  1264  // GetBlockStatsAsync RPC invocation (or an applicable error).
  1265  type FutureGetBlockStatsResult chan *response
  1266  
  1267  // Receive waits for the response promised by the future and returns statistics
  1268  // of a block at a certain height.
  1269  func (r FutureGetBlockStatsResult) Receive() (*btcjson.GetBlockStatsResult, error) {
  1270  	res, err := receiveFuture(r)
  1271  	if err != nil {
  1272  		return nil, err
  1273  	}
  1274  
  1275  	var blockStats btcjson.GetBlockStatsResult
  1276  	err = json.Unmarshal(res, &blockStats)
  1277  	if err != nil {
  1278  		return nil, err
  1279  	}
  1280  
  1281  	return &blockStats, nil
  1282  }
  1283  
  1284  // GetBlockStatsAsync returns an instance of a type that can be used to get
  1285  // the result of the RPC at some future time by invoking the Receive function on
  1286  // the returned instance.
  1287  //
  1288  // See GetBlockStats or the blocking version and more details.
  1289  func (c *Client) GetBlockStatsAsync(hashOrHeight interface{}, stats *[]string) FutureGetBlockStatsResult {
  1290  	if hash, ok := hashOrHeight.(*chainhash.Hash); ok {
  1291  		hashOrHeight = hash.String()
  1292  	}
  1293  
  1294  	cmd := btcjson.NewGetBlockStatsCmd(btcjson.HashOrHeight{Value: hashOrHeight}, stats)
  1295  	return c.sendCmd(cmd)
  1296  }
  1297  
  1298  // GetBlockStats returns block statistics. First argument specifies height or hash of the target block.
  1299  // Second argument allows to select certain stats to return.
  1300  func (c *Client) GetBlockStats(hashOrHeight interface{}, stats *[]string) (*btcjson.GetBlockStatsResult, error) {
  1301  	return c.GetBlockStatsAsync(hashOrHeight, stats).Receive()
  1302  }
  1303  
  1304  // FutureDeriveAddressesResult is a future promise to deliver the result of an
  1305  // DeriveAddressesAsync RPC invocation (or an applicable error).
  1306  type FutureDeriveAddressesResult chan *response
  1307  
  1308  // Receive waits for the response promised by the future and derives one or more addresses
  1309  // corresponding to the given output descriptor.
  1310  func (r FutureDeriveAddressesResult) Receive() (*btcjson.DeriveAddressesResult, error) {
  1311  	res, err := receiveFuture(r)
  1312  	if err != nil {
  1313  		return nil, err
  1314  	}
  1315  
  1316  	var deriveAddressesResult btcjson.DeriveAddressesResult
  1317  
  1318  	err = json.Unmarshal(res, &deriveAddressesResult)
  1319  	if err != nil {
  1320  		return nil, err
  1321  	}
  1322  
  1323  	return &deriveAddressesResult, nil
  1324  }
  1325  
  1326  // DeriveAddressesAsync returns an instance of a type that can be used to get the result
  1327  // of the RPC at some future time by invoking the Receive function on the
  1328  // returned instance.
  1329  //
  1330  // See DeriveAddresses for the blocking version and more details.
  1331  func (c *Client) DeriveAddressesAsync(descriptor string, descriptorRange *btcjson.DescriptorRange) FutureDeriveAddressesResult {
  1332  	cmd := btcjson.NewDeriveAddressesCmd(descriptor, descriptorRange)
  1333  	return c.sendCmd(cmd)
  1334  }
  1335  
  1336  // DeriveAddresses derives one or more addresses corresponding to an output
  1337  // descriptor. If a ranged descriptor is used, the end or the range
  1338  // (in [begin,end] notation) to derive must be specified.
  1339  func (c *Client) DeriveAddresses(descriptor string, descriptorRange *btcjson.DescriptorRange) (*btcjson.DeriveAddressesResult, error) {
  1340  	return c.DeriveAddressesAsync(descriptor, descriptorRange).Receive()
  1341  }
  1342  
  1343  // FutureGetDescriptorInfoResult is a future promise to deliver the result of a
  1344  // GetDescriptorInfoAsync RPC invocation (or an applicable error).
  1345  type FutureGetDescriptorInfoResult chan *response
  1346  
  1347  // Receive waits for the response promised by the future and returns the analysed
  1348  // info of the descriptor.
  1349  func (r FutureGetDescriptorInfoResult) Receive() (*btcjson.GetDescriptorInfoResult, error) {
  1350  	res, err := receiveFuture(r)
  1351  	if err != nil {
  1352  		return nil, err
  1353  	}
  1354  
  1355  	var descriptorInfo btcjson.GetDescriptorInfoResult
  1356  
  1357  	err = json.Unmarshal(res, &descriptorInfo)
  1358  	if err != nil {
  1359  		return nil, err
  1360  	}
  1361  
  1362  	return &descriptorInfo, nil
  1363  }
  1364  
  1365  // GetDescriptorInfoAsync returns an instance of a type that can be used to get
  1366  // the result of the RPC at some future time by invoking the Receive function on
  1367  // the returned instance.
  1368  //
  1369  // See GetDescriptorInfo for the blocking version and more details.
  1370  func (c *Client) GetDescriptorInfoAsync(descriptor string) FutureGetDescriptorInfoResult {
  1371  	cmd := btcjson.NewGetDescriptorInfoCmd(descriptor)
  1372  	return c.sendCmd(cmd)
  1373  }
  1374  
  1375  // GetDescriptorInfo returns the analysed info of a descriptor string, by invoking the
  1376  // getdescriptorinfo RPC.
  1377  //
  1378  // Use this function to analyse a descriptor string, or compute the checksum
  1379  // for a descriptor without one.
  1380  //
  1381  // See btcjson.GetDescriptorInfoResult for details about the result.
  1382  func (c *Client) GetDescriptorInfo(descriptor string) (*btcjson.GetDescriptorInfoResult, error) {
  1383  	return c.GetDescriptorInfoAsync(descriptor).Receive()
  1384  }