chain.go raw

   1  package rpcclient
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/hex"
   6  	js "encoding/json"
   7  	
   8  	"github.com/p9c/p9/pkg/btcjson"
   9  	"github.com/p9c/p9/pkg/chainhash"
  10  	"github.com/p9c/p9/pkg/wire"
  11  )
  12  
  13  // FutureGetBestBlockHashResult is a future promise to deliver the result of a GetBestBlockAsync RPC invocation (or an
  14  // applicable error).
  15  type FutureGetBestBlockHashResult chan *response
  16  
  17  // Receive waits for the response promised by the future and returns the hash of the best block in the longest block
  18  // chain.
  19  func (r FutureGetBestBlockHashResult) Receive() (*chainhash.Hash, error) {
  20  	res, e := receiveFuture(r)
  21  	if e != nil {
  22  		return nil, e
  23  	}
  24  	// Unmarshal result as a string.
  25  	var txHashStr string
  26  	e = js.Unmarshal(res, &txHashStr)
  27  	if e != nil {
  28  		return nil, e
  29  	}
  30  	return chainhash.NewHashFromStr(txHashStr)
  31  }
  32  
  33  // GetBestBlockHashAsync returns an instance of a type that can be used to get the result of the RPC at some future time
  34  // by invoking the Receive function on the returned instance. See GetBestBlockHash for the blocking version and more
  35  // details.
  36  func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult {
  37  	cmd := btcjson.NewGetBestBlockHashCmd()
  38  	return c.sendCmd(cmd)
  39  }
  40  
  41  // GetBestBlockHash returns the hash of the best block in the longest block chain.
  42  func (c *Client) GetBestBlockHash() (*chainhash.Hash, error) {
  43  	return c.GetBestBlockHashAsync().Receive()
  44  }
  45  
  46  // FutureGetBlockResult is a future promise to deliver the result of a GetBlockAsync RPC invocation (or an applicable
  47  // error).
  48  type FutureGetBlockResult chan *response
  49  
  50  // Receive waits for the response promised by the future and returns the raw block requested from the server given its
  51  // hash.
  52  func (r FutureGetBlockResult) Receive() (*wire.Block, error) {
  53  	res, e := receiveFuture(r)
  54  	if e != nil {
  55  		return nil, e
  56  	}
  57  	// Unmarshal result as a string.
  58  	var blockHex string
  59  	e = js.Unmarshal(res, &blockHex)
  60  	if e != nil {
  61  		return nil, e
  62  	}
  63  	// Decode the serialized block hex to raw bytes.
  64  	serializedBlock, e := hex.DecodeString(blockHex)
  65  	if e != nil {
  66  		return nil, e
  67  	}
  68  	// Deserialize the block and return it.
  69  	var msgBlock wire.Block
  70  	e = msgBlock.Deserialize(bytes.NewReader(serializedBlock))
  71  	if e != nil {
  72  		return nil, e
  73  	}
  74  	return &msgBlock, nil
  75  }
  76  
  77  // GetBlockAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
  78  // invoking the Receive function on the returned instance. See GetBlock for the blocking version and more details.
  79  func (c *Client) GetBlockAsync(blockHash *chainhash.Hash) FutureGetBlockResult {
  80  	hash := ""
  81  	if blockHash != nil {
  82  		hash = blockHash.String()
  83  	}
  84  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(false), nil)
  85  	return c.sendCmd(cmd)
  86  }
  87  
  88  // GetBlock returns a raw block from the server given its hash. GetBlockVerbose to retrieve a data structure with
  89  // information about the block instead.
  90  func (c *Client) GetBlock(blockHash *chainhash.Hash) (*wire.Block, error) {
  91  	return c.GetBlockAsync(blockHash).Receive()
  92  }
  93  
  94  // FutureGetBlockVerboseResult is a future promise to deliver the result of a GetBlockVerboseAsync RPC invocation (or an
  95  // applicable error).
  96  type FutureGetBlockVerboseResult chan *response
  97  
  98  // Receive waits for the response promised by the future and returns the data structure from the server with information about the requested block.
  99  func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) {
 100  	res, e := receiveFuture(r)
 101  	if e != nil {
 102  		return nil, e
 103  	}
 104  	// Unmarshal the raw result into a BlockResult.
 105  	var blockResult btcjson.GetBlockVerboseResult
 106  	e = js.Unmarshal(res, &blockResult)
 107  	if e != nil {
 108  		return nil, e
 109  	}
 110  	return &blockResult, nil
 111  }
 112  
 113  // GetBlockVerboseAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 114  // by invoking the Receive function on the returned instance. See GetBlockVerbose for the blocking version and more
 115  // details.
 116  func (c *Client) GetBlockVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
 117  	hash := ""
 118  	if blockHash != nil {
 119  		hash = blockHash.String()
 120  	}
 121  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), nil)
 122  	return c.sendCmd(cmd)
 123  }
 124  
 125  // GetBlockVerbose returns a data structure from the server with information about a block given its hash. See
 126  // GetBlockVerboseTx to retrieve transaction data structures as well. See GetBlock to retrieve a raw block instead.
 127  func (c *Client) GetBlockVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
 128  	return c.GetBlockVerboseAsync(blockHash).Receive()
 129  }
 130  
 131  // GetBlockVerboseTxAsync returns an instance of a type that can be used to get the result of the RPC at some future
 132  // time by invoking the Receive function on the returned instance. See GetBlockVerboseTx or the blocking version and
 133  // more details.
 134  func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
 135  	hash := ""
 136  	if blockHash != nil {
 137  		hash = blockHash.String()
 138  	}
 139  	cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), btcjson.Bool(true))
 140  	return c.sendCmd(cmd)
 141  }
 142  
 143  // GetBlockVerboseTx returns a data structure from the server with information about a block and its transactions given
 144  // its hash. See GetBlockVerbose if only transaction hashes are preferred. See GetBlock to retrieve a raw block instead.
 145  func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
 146  	return c.GetBlockVerboseTxAsync(blockHash).Receive()
 147  }
 148  
 149  // FutureGetBlockCountResult is a future promise to deliver the result of a GetBlockCountAsync RPC invocation (or an
 150  // applicable error).
 151  type FutureGetBlockCountResult chan *response
 152  
 153  // Receive waits for the response promised by the future and returns the number of blocks in the longest block chain.
 154  func (r FutureGetBlockCountResult) Receive() (int64, error) {
 155  	res, e := receiveFuture(r)
 156  	if e != nil {
 157  		return 0, e
 158  	}
 159  	// Unmarshal the result as an int64.
 160  	var count int64
 161  	e = js.Unmarshal(res, &count)
 162  	if e != nil {
 163  		return 0, e
 164  	}
 165  	return count, nil
 166  }
 167  
 168  // GetBlockCountAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 169  // invoking the Receive function on the returned instance. See GetBlockCount for the blocking version and more details.
 170  func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult {
 171  	cmd := btcjson.NewGetBlockCountCmd()
 172  	return c.sendCmd(cmd)
 173  }
 174  
 175  // GetBlockCount returns the number of blocks in the longest block chain.
 176  func (c *Client) GetBlockCount() (int64, error) {
 177  	return c.GetBlockCountAsync().Receive()
 178  }
 179  
 180  // FutureGetDifficultyResult is a future promise to deliver the result of a GetDifficultyAsync RPC invocation (or an
 181  // applicable error).
 182  type FutureGetDifficultyResult chan *response
 183  
 184  // Receive waits for the response promised by the future and returns the proof-of-work difficulty as a multiple of the
 185  // minimum difficulty.
 186  func (r FutureGetDifficultyResult) Receive() (float64, error) {
 187  	res, e := receiveFuture(r)
 188  	if e != nil {
 189  		return 0, e
 190  	}
 191  	// Unmarshal the result as a float64.
 192  	var difficulty float64
 193  	e = js.Unmarshal(res, &difficulty)
 194  	if e != nil {
 195  		return 0, e
 196  	}
 197  	return difficulty, nil
 198  }
 199  
 200  // GetDifficultyAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 201  // invoking the Receive function on the returned instance. See GetDifficulty for the blocking version and more details.
 202  func (c *Client) GetDifficultyAsync(algo string) FutureGetDifficultyResult {
 203  	cmd := btcjson.NewGetDifficultyCmd(algo)
 204  	return c.sendCmd(cmd)
 205  }
 206  
 207  // GetDifficulty returns the proof-of-work difficulty as a multiple of the minimum difficulty.
 208  func (c *Client) GetDifficulty(algo string) (float64, error) {
 209  	return c.GetDifficultyAsync(algo).Receive()
 210  }
 211  
 212  // FutureGetBlockChainInfoResult is a promise to deliver the result of a GetBlockChainInfoAsync RPC invocation (or an
 213  // applicable error).
 214  type FutureGetBlockChainInfoResult chan *response
 215  
 216  // Receive waits for the response promised by the future and returns chain info result provided by the server.
 217  func (r FutureGetBlockChainInfoResult) Receive() (*btcjson.GetBlockChainInfoResult, error) {
 218  	res, e := receiveFuture(r)
 219  	if e != nil {
 220  		return nil, e
 221  	}
 222  	var chainInfo btcjson.GetBlockChainInfoResult
 223  	if e := js.Unmarshal(res, &chainInfo); E.Chk(e) {
 224  		return nil, e
 225  	}
 226  	return &chainInfo, nil
 227  }
 228  
 229  // GetBlockChainInfoAsync returns an instance of a type that can be used to get the result of the RPC at some future
 230  // time by invoking the Receive function on the returned instance. GetBlockChainInfo for the blocking version and more
 231  // details.
 232  func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult {
 233  	cmd := btcjson.NewGetBlockChainInfoCmd()
 234  	return c.sendCmd(cmd)
 235  }
 236  
 237  // GetBlockChainInfo returns information related to the processing state of various chain-specific details such as the
 238  // current difficulty from the tip of the main chain.
 239  func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
 240  	return c.GetBlockChainInfoAsync().Receive()
 241  }
 242  
 243  // FutureGetBlockHashResult is a future promise to deliver the result of a GetBlockHashAsync RPC invocation (or an
 244  // applicable error).
 245  type FutureGetBlockHashResult chan *response
 246  
 247  // Receive waits for the response promised by the future and returns the hash of the block in the best block chain at
 248  // the given height.
 249  func (r FutureGetBlockHashResult) Receive() (*chainhash.Hash, error) {
 250  	res, e := receiveFuture(r)
 251  	if e != nil {
 252  		return nil, e
 253  	}
 254  	// Unmarshal the result as a string-encoded sha.
 255  	var txHashStr string
 256  	e = js.Unmarshal(res, &txHashStr)
 257  	if e != nil {
 258  		return nil, e
 259  	}
 260  	return chainhash.NewHashFromStr(txHashStr)
 261  }
 262  
 263  // GetBlockHashAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 264  // invoking the Receive function on the returned instance. See GetBlockHash for the blocking version and more details.
 265  func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
 266  	cmd := btcjson.NewGetBlockHashCmd(blockHeight)
 267  	return c.sendCmd(cmd)
 268  }
 269  
 270  // GetBlockHash returns the hash of the block in the best block chain at the given height.
 271  func (c *Client) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
 272  	return c.GetBlockHashAsync(blockHeight).Receive()
 273  }
 274  
 275  // FutureGetBlockHeaderResult is a future promise to deliver the result of a GetBlockHeaderAsync RPC invocation (or an
 276  // applicable error).
 277  type FutureGetBlockHeaderResult chan *response
 278  
 279  // Receive waits for the response promised by the future and returns the blockheader requested from the server given its
 280  // hash.
 281  func (r FutureGetBlockHeaderResult) Receive() (*wire.BlockHeader, error) {
 282  	res, e := receiveFuture(r)
 283  	if e != nil {
 284  		return nil, e
 285  	}
 286  	// Unmarshal result as a string.
 287  	var bhHex string
 288  	e = js.Unmarshal(res, &bhHex)
 289  	if e != nil {
 290  		return nil, e
 291  	}
 292  	serializedBH, e := hex.DecodeString(bhHex)
 293  	if e != nil {
 294  		return nil, e
 295  	}
 296  	// Deserialize the blockheader and return it.
 297  	var bh wire.BlockHeader
 298  	e = bh.Deserialize(bytes.NewReader(serializedBH))
 299  	if e != nil {
 300  		return nil, e
 301  	}
 302  	return &bh, e
 303  }
 304  
 305  // GetBlockHeaderAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 306  // by invoking the Receive function on the returned instance. See GetBlockHeader for the blocking version and more
 307  // details.
 308  func (c *Client) GetBlockHeaderAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderResult {
 309  	hash := ""
 310  	if blockHash != nil {
 311  		hash = blockHash.String()
 312  	}
 313  	cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(false))
 314  	return c.sendCmd(cmd)
 315  }
 316  
 317  // GetBlockHeader returns the blockheader from the server given its hash. See GetBlockHeaderVerbose to retrieve a data
 318  // structure with information about the block instead.
 319  func (c *Client) GetBlockHeader(blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
 320  	return c.GetBlockHeaderAsync(blockHash).Receive()
 321  }
 322  
 323  // FutureGetBlockHeaderVerboseResult is a future promise to deliver the result of a GetBlockAsync RPC invocation (or an
 324  // applicable error).
 325  type FutureGetBlockHeaderVerboseResult chan *response
 326  
 327  // Receive waits for the response promised by the future and returns the data structure of the blockheader requested
 328  // from the server given its hash.
 329  func (r FutureGetBlockHeaderVerboseResult) Receive() (*btcjson.GetBlockHeaderVerboseResult, error) {
 330  	res, e := receiveFuture(r)
 331  	if e != nil {
 332  		return nil, e
 333  	}
 334  	// Unmarshal result as a string.
 335  	var bh btcjson.GetBlockHeaderVerboseResult
 336  	e = js.Unmarshal(res, &bh)
 337  	if e != nil {
 338  		return nil, e
 339  	}
 340  	return &bh, nil
 341  }
 342  
 343  // GetBlockHeaderVerboseAsync returns an instance of a type that can be used to get the result of the RPC at some future
 344  // time by invoking the Receive function on the returned instance. See GetBlockHeader for the blocking version and more
 345  // details.
 346  func (c *Client) GetBlockHeaderVerboseAsync(blockHash *chainhash.Hash) FutureGetBlockHeaderVerboseResult {
 347  	hash := ""
 348  	if blockHash != nil {
 349  		hash = blockHash.String()
 350  	}
 351  	cmd := btcjson.NewGetBlockHeaderCmd(hash, btcjson.Bool(true))
 352  	return c.sendCmd(cmd)
 353  }
 354  
 355  // GetBlockHeaderVerbose returns a data structure with information about the blockheader from the server given its hash.
 356  // See GetBlockHeader to retrieve a blockheader instead.
 357  func (c *Client) GetBlockHeaderVerbose(blockHash *chainhash.Hash) (*btcjson.GetBlockHeaderVerboseResult, error) {
 358  	return c.GetBlockHeaderVerboseAsync(blockHash).Receive()
 359  }
 360  
 361  // FutureGetMempoolEntryResult is a future promise to deliver the result of a GetMempoolEntryAsync RPC invocation (or an
 362  // applicable error).
 363  type FutureGetMempoolEntryResult chan *response
 364  
 365  // Receive waits for the response promised by the future and returns a data structure with information about the
 366  // transaction in the memory pool given its hash.
 367  func (r FutureGetMempoolEntryResult) Receive() (*btcjson.GetMempoolEntryResult, error) {
 368  	res, e := receiveFuture(r)
 369  	if e != nil {
 370  		return nil, e
 371  	}
 372  	// Unmarshal the result as an array of strings.
 373  	var mempoolEntryResult btcjson.GetMempoolEntryResult
 374  	e = js.Unmarshal(res, &mempoolEntryResult)
 375  	if e != nil {
 376  		return nil, e
 377  	}
 378  	return &mempoolEntryResult, nil
 379  }
 380  
 381  // GetMempoolEntryAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 382  // by invoking the Receive function on the returned instance. See GetMempoolEntry for the blocking version and more
 383  // details.
 384  func (c *Client) GetMempoolEntryAsync(txHash string) FutureGetMempoolEntryResult {
 385  	cmd := btcjson.NewGetMempoolEntryCmd(txHash)
 386  	return c.sendCmd(cmd)
 387  }
 388  
 389  // GetMempoolEntry returns a data structure with information about the transaction in the memory pool given its hash.
 390  func (c *Client) GetMempoolEntry(txHash string) (*btcjson.GetMempoolEntryResult, error) {
 391  	return c.GetMempoolEntryAsync(txHash).Receive()
 392  }
 393  
 394  // FutureGetRawMempoolResult is a future promise to deliver the result of a GetRawMempoolAsync RPC invocation (or an
 395  // applicable error).
 396  type FutureGetRawMempoolResult chan *response
 397  
 398  // Receive waits for the response promised by the future and returns the hashes of all transactions in the memory pool.
 399  func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
 400  	res, e := receiveFuture(r)
 401  	if e != nil {
 402  		return nil, e
 403  	}
 404  	// Unmarshal the result as an array of strings.
 405  	var txHashStrs []string
 406  	e = js.Unmarshal(res, &txHashStrs)
 407  	if e != nil {
 408  		return nil, e
 409  	}
 410  	// Create a slice of ShaHash arrays from the string slice.
 411  	txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
 412  	for _, hashStr := range txHashStrs {
 413  		txHash, e := chainhash.NewHashFromStr(hashStr)
 414  		if e != nil {
 415  			return nil, e
 416  		}
 417  		txHashes = append(txHashes, txHash)
 418  	}
 419  	return txHashes, nil
 420  }
 421  
 422  // GetRawMempoolAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 423  // invoking the Receive function on the returned instance. See GetRawMempool for the blocking version and more details.
 424  func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
 425  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
 426  	return c.sendCmd(cmd)
 427  }
 428  
 429  // GetRawMempool returns the hashes of all transactions in the memory pool. See GetRawMempoolVerbose to retrieve data
 430  // structures with information about the transactions instead.
 431  func (c *Client) GetRawMempool() ([]*chainhash.Hash, error) {
 432  	return c.GetRawMempoolAsync().Receive()
 433  }
 434  
 435  // FutureGetRawMempoolVerboseResult is a future promise to deliver the result of a GetRawMempoolVerboseAsync RPC
 436  // invocation (or an applicable error).
 437  type FutureGetRawMempoolVerboseResult chan *response
 438  
 439  // Receive waits for the response promised by the future and returns a map of transaction hashes to an associated data
 440  // structure with information about the transaction for all transactions in the memory pool.
 441  func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
 442  	res, e := receiveFuture(r)
 443  	if e != nil {
 444  		return nil, e
 445  	}
 446  	// Unmarshal the result as a map of strings (tx shas) to their detailed results.
 447  	var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
 448  	e = js.Unmarshal(res, &mempoolItems)
 449  	if e != nil {
 450  		return nil, e
 451  	}
 452  	return mempoolItems, nil
 453  }
 454  
 455  // GetRawMempoolVerboseAsync returns an instance of a type that can be used to get the result of the RPC at some future
 456  // time by invoking the Receive function on the returned instance. See GetRawMempoolVerbose for the blocking version and
 457  // more details.
 458  func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
 459  	cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
 460  	return c.sendCmd(cmd)
 461  }
 462  
 463  // GetRawMempoolVerbose returns a map of transaction hashes to an associated data structure with information about the
 464  // transaction for all transactions in the memory pool. See GetRawMempool to retrieve only the transaction hashes
 465  // instead.
 466  func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
 467  	return c.GetRawMempoolVerboseAsync().Receive()
 468  }
 469  
 470  // FutureEstimateFeeResult is a future promise to deliver the result of a EstimateFeeAsync RPC invocation (or an
 471  // applicable error).
 472  type FutureEstimateFeeResult chan *response
 473  
 474  // Receive waits for the response promised by the future and returns the info provided by the server.
 475  func (r FutureEstimateFeeResult) Receive() (float64, error) {
 476  	res, e := receiveFuture(r)
 477  	if e != nil {
 478  		return -1, e
 479  	}
 480  	// Unmarshal result as a getinfo result object.
 481  	var fee float64
 482  	e = js.Unmarshal(res, &fee)
 483  	if e != nil {
 484  		return -1, e
 485  	}
 486  	return fee, nil
 487  }
 488  
 489  // EstimateFeeAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 490  // invoking the Receive function on the returned instance. See EstimateFee for the blocking version and more details.
 491  func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult {
 492  	cmd := btcjson.NewEstimateFeeCmd(numBlocks)
 493  	return c.sendCmd(cmd)
 494  }
 495  
 496  // EstimateFee provides an estimated fee in bitcoins per kilobyte.
 497  func (c *Client) EstimateFee(numBlocks int64) (float64, error) {
 498  	return c.EstimateFeeAsync(numBlocks).Receive()
 499  }
 500  
 501  // FutureVerifyChainResult is a future promise to deliver the result of a VerifyChainAsync, VerifyChainLevelAsyncRPC, or
 502  // VerifyChainBlocksAsync invocation (or an applicable error).
 503  type FutureVerifyChainResult chan *response
 504  
 505  // Receive waits for the response promised by the future and returns whether or not the chain verified based on the
 506  // check level and number of blocks to verify specified in the original call.
 507  func (r FutureVerifyChainResult) Receive() (bool, error) {
 508  	res, e := receiveFuture(r)
 509  	if e != nil {
 510  		return false, e
 511  	}
 512  	// Unmarshal the result as a boolean.
 513  	var verified bool
 514  	e = js.Unmarshal(res, &verified)
 515  	if e != nil {
 516  		return false, e
 517  	}
 518  	return verified, nil
 519  }
 520  
 521  // VerifyChainAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 522  // invoking the Receive function on the returned instance. See VerifyChain for the blocking version and more details.
 523  func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
 524  	cmd := btcjson.NewVerifyChainCmd(nil, nil)
 525  	return c.sendCmd(cmd)
 526  }
 527  
 528  // VerifyChain requests the server to verify the block chain database using the default check level and number of blocks
 529  // to verify. See VerifyChainLevel and VerifyChainBlocks to override the defaults.
 530  func (c *Client) VerifyChain() (bool, error) {
 531  	return c.VerifyChainAsync().Receive()
 532  }
 533  
 534  // VerifyChainLevelAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 535  // by invoking the Receive function on the returned instance. See VerifyChainLevel for the blocking version and more
 536  // details.
 537  func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
 538  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
 539  	return c.sendCmd(cmd)
 540  }
 541  
 542  // VerifyChainLevel requests the server to verify the block chain database using the passed check level and default
 543  // number of blocks to verify. The check level controls how thorough the verification is with higher numbers increasing
 544  // the amount of checks done as consequently how long the verification takes. See VerifyChain to use the default check
 545  // level and VerifyChainBlocks to override the number of blocks to verify.
 546  func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
 547  	return c.VerifyChainLevelAsync(checkLevel).Receive()
 548  }
 549  
 550  // VerifyChainBlocksAsync returns an instance of a type that can be used to get the result of the RPC at some future
 551  // time by invoking the Receive function on the returned instance. See VerifyChainBlocks for the blocking version and
 552  // more details.
 553  func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
 554  	cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
 555  	return c.sendCmd(cmd)
 556  }
 557  
 558  // VerifyChainBlocks requests the server to verify the block chain database using the passed check level and number of
 559  // blocks to verify. The check level controls how thorough the verification is with higher numbers increasing the amount
 560  // of checks done as consequently how long the verification takes. The number of blocks refers to the number of blocks
 561  // from the end of the current longest chain. See VerifyChain and VerifyChainLevel to use defaults.
 562  func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
 563  	return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
 564  }
 565  
 566  // FutureGetTxOutResult is a future promise to deliver the result of a GetTxOutAsync RPC invocation (or an applicable
 567  // error).
 568  type FutureGetTxOutResult chan *response
 569  
 570  // Receive waits for the response promised by the future and returns a transaction given its hash.
 571  func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
 572  	res, e := receiveFuture(r)
 573  	if e != nil {
 574  		return nil, e
 575  	}
 576  	// take care of the special case where the output has been spent already it should return the string "null"
 577  	if string(res) == "null" {
 578  		return nil, nil
 579  	}
 580  	// Unmarshal result as an gettxout result object.
 581  	var txOutInfo *btcjson.GetTxOutResult
 582  	e = js.Unmarshal(res, &txOutInfo)
 583  	if e != nil {
 584  		return nil, e
 585  	}
 586  	return txOutInfo, nil
 587  }
 588  
 589  // GetTxOutAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 590  // invoking the Receive function on the returned instance. See GetTxOut for the blocking version and more details.
 591  func (c *Client) GetTxOutAsync(txHash *chainhash.Hash, index uint32, mempool bool) FutureGetTxOutResult {
 592  	hash := ""
 593  	if txHash != nil {
 594  		hash = txHash.String()
 595  	}
 596  	cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
 597  	return c.sendCmd(cmd)
 598  }
 599  
 600  // GetTxOut returns the transaction output info if it's unspent and nil, otherwise.
 601  func (c *Client) GetTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
 602  	return c.GetTxOutAsync(txHash, index, mempool).Receive()
 603  }
 604  
 605  // FutureRescanBlocksResult is a future promise to deliver the result of a RescanBlocksAsync RPC invocation (or an
 606  // applicable error).
 607  //
 608  // NOTE: This is a btcsuite extension ported from github.com/decred/dcrrpcclient.
 609  type FutureRescanBlocksResult chan *response
 610  
 611  // Receive waits for the response promised by the future and returns the discovered rescanblocks data. NOTE: This is a
 612  // btcsuite extension ported from github.com/decred/dcrrpcclient.
 613  func (r FutureRescanBlocksResult) Receive() ([]btcjson.RescannedBlock, error) {
 614  	res, e := receiveFuture(r)
 615  	if e != nil {
 616  		return nil, e
 617  	}
 618  	var rescanBlocksResult []btcjson.RescannedBlock
 619  	e = js.Unmarshal(res, &rescanBlocksResult)
 620  	if e != nil {
 621  		return nil, e
 622  	}
 623  	return rescanBlocksResult, nil
 624  }
 625  
 626  // RescanBlocksAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 627  // invoking the Receive function on the returned instance. See RescanBlocks for the blocking version and more details.
 628  //
 629  // NOTE: This is a btcsuite extension ported from github.com/decred/dcrrpcclient.
 630  func (c *Client) RescanBlocksAsync(blockHashes []chainhash.Hash) FutureRescanBlocksResult {
 631  	strBlockHashes := make([]string, len(blockHashes))
 632  	for i := range blockHashes {
 633  		strBlockHashes[i] = blockHashes[i].String()
 634  	}
 635  	cmd := btcjson.NewRescanBlocksCmd(strBlockHashes)
 636  	return c.sendCmd(cmd)
 637  }
 638  
 639  // RescanBlocks rescans the blocks identified by blockHashes, in order, using the client's loaded transaction filter.
 640  // The blocks do not need to be on the main chain, but they do need to be adjacent to each other.
 641  //
 642  // NOTE: This is a btcsuite extension ported from github.com/decred/dcrrpcclient.
 643  func (c *Client) RescanBlocks(blockHashes []chainhash.Hash) ([]btcjson.RescannedBlock, error) {
 644  	return c.RescanBlocksAsync(blockHashes).Receive()
 645  }
 646  
 647  // FutureInvalidateBlockResult is a future promise to deliver the result of a InvalidateBlockAsync RPC invocation (or an
 648  // applicable error).
 649  type FutureInvalidateBlockResult chan *response
 650  
 651  // Receive waits for the response promised by the future and returns the raw block requested from the server given its
 652  // hash.
 653  func (r FutureInvalidateBlockResult) Receive() (e error) {
 654  	_, e = receiveFuture(r)
 655  	return e
 656  }
 657  
 658  // InvalidateBlockAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 659  // by invoking the Receive function on the returned instance. See InvalidateBlock for the blocking version and more
 660  // details.
 661  func (c *Client) InvalidateBlockAsync(blockHash *chainhash.Hash) FutureInvalidateBlockResult {
 662  	hash := ""
 663  	if blockHash != nil {
 664  		hash = blockHash.String()
 665  	}
 666  	cmd := btcjson.NewInvalidateBlockCmd(hash)
 667  	return c.sendCmd(cmd)
 668  }
 669  
 670  // InvalidateBlock invalidates a specific block.
 671  func (c *Client) InvalidateBlock(blockHash *chainhash.Hash) (e error) {
 672  	return c.InvalidateBlockAsync(blockHash).Receive()
 673  }
 674  
 675  // FutureGetCFilterResult is a future promise to deliver the result of a GetCFilterAsync RPC invocation (or an
 676  // applicable error).
 677  type FutureGetCFilterResult chan *response
 678  
 679  // Receive waits for the response promised by the future and returns the raw filter requested from the server given its
 680  // block hash.
 681  func (r FutureGetCFilterResult) Receive() (*wire.MsgCFilter, error) {
 682  	res, e := receiveFuture(r)
 683  	if e != nil {
 684  		return nil, e
 685  	}
 686  	// Unmarshal result as a string.
 687  	var filterHex string
 688  	e = js.Unmarshal(res, &filterHex)
 689  	if e != nil {
 690  		return nil, e
 691  	}
 692  	// Decode the serialized cf hex to raw bytes.
 693  	serializedFilter, e := hex.DecodeString(filterHex)
 694  	if e != nil {
 695  		return nil, e
 696  	}
 697  	// Assign the filter bytes to the correct field of the wire message. We aren't going to set the block hash or
 698  	// extended flag, since we don't actually get that back in the RPC response.
 699  	var msgCFilter wire.MsgCFilter
 700  	msgCFilter.Data = serializedFilter
 701  	return &msgCFilter, nil
 702  }
 703  
 704  // GetCFilterAsync returns an instance of a type that can be used to get the result of the RPC at some future time by
 705  // invoking the Receive function on the returned instance. See GetCFilter for the blocking version and more details.
 706  func (c *Client) GetCFilterAsync(
 707  	blockHash *chainhash.Hash,
 708  	filterType wire.FilterType,
 709  ) FutureGetCFilterResult {
 710  	hash := ""
 711  	if blockHash != nil {
 712  		hash = blockHash.String()
 713  	}
 714  	cmd := btcjson.NewGetCFilterCmd(hash, filterType)
 715  	return c.sendCmd(cmd)
 716  }
 717  
 718  // GetCFilter returns a raw filter from the server given its block hash.
 719  func (c *Client) GetCFilter(
 720  	blockHash *chainhash.Hash,
 721  	filterType wire.FilterType,
 722  ) (*wire.MsgCFilter, error) {
 723  	return c.GetCFilterAsync(blockHash, filterType).Receive()
 724  }
 725  
 726  // FutureGetCFilterHeaderResult is a future promise to deliver the result of a GetCFilterHeaderAsync RPC invocation (or
 727  // an applicable error).
 728  type FutureGetCFilterHeaderResult chan *response
 729  
 730  // Receive waits for the response promised by the future and returns the raw filter header requested from the server
 731  // given its block hash.
 732  func (r FutureGetCFilterHeaderResult) Receive() (*wire.MsgCFHeaders, error) {
 733  	res, e := receiveFuture(r)
 734  	if e != nil {
 735  		return nil, e
 736  	}
 737  	// Unmarshal result as a string.
 738  	var headerHex string
 739  	e = js.Unmarshal(res, &headerHex)
 740  	if e != nil {
 741  		return nil, e
 742  	}
 743  	// Assign the decoded header into a hash
 744  	headerHash, e := chainhash.NewHashFromStr(headerHex)
 745  	if e != nil {
 746  		return nil, e
 747  	}
 748  	// Assign the hash to a headers message and return it.
 749  	msgCFHeaders := wire.MsgCFHeaders{PrevFilterHeader: *headerHash}
 750  	return &msgCFHeaders, nil
 751  }
 752  
 753  // GetCFilterHeaderAsync returns an instance of a type that can be used to get the result of the RPC at some future time
 754  // by invoking the Receive function on the returned instance. See GetCFilterHeader for the blocking version and more
 755  // details.
 756  func (c *Client) GetCFilterHeaderAsync(
 757  	blockHash *chainhash.Hash,
 758  	filterType wire.FilterType,
 759  ) FutureGetCFilterHeaderResult {
 760  	hash := ""
 761  	if blockHash != nil {
 762  		hash = blockHash.String()
 763  	}
 764  	cmd := btcjson.NewGetCFilterHeaderCmd(hash, filterType)
 765  	return c.sendCmd(cmd)
 766  }
 767  
 768  // GetCFilterHeader returns a raw filter header from the server given its block hash.
 769  func (c *Client) GetCFilterHeader(
 770  	blockHash *chainhash.Hash,
 771  	filterType wire.FilterType,
 772  ) (*wire.MsgCFHeaders, error) {
 773  	return c.GetCFilterHeaderAsync(blockHash, filterType).Receive()
 774  }
 775