2015-01-03 07:52:21 +01:00
|
|
|
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcrpcclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
2014-06-10 02:49:41 +02:00
|
|
|
"encoding/json"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
"github.com/btcsuite/btcd/btcjson/v2/btcjson"
|
2015-02-05 22:50:03 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:41:20 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FutureGetBestBlockHashResult is a future promise to deliver the result of a
|
|
|
|
// GetBestBlockAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetBestBlockHashResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the hash of
|
|
|
|
// the best block in the longest block chain.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (r FutureGetBestBlockHashResult) Receive() (*wire.ShaHash, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a string.
|
|
|
|
var txHashStr string
|
|
|
|
err = json.Unmarshal(res, &txHashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2015-02-05 22:50:03 +01:00
|
|
|
return wire.NewShaHashFromStr(txHashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlockHashAsync returns an instance of a type that can be used to get
|
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See GetBestBlockHash for the blocking version and more details.
|
|
|
|
func (c *Client) GetBestBlockHashAsync() FutureGetBestBlockHashResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBestBlockHashCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBestBlockHash returns the hash of the best block in the longest block
|
|
|
|
// chain.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetBestBlockHash() (*wire.ShaHash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBestBlockHashAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetBlockResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the raw
|
|
|
|
// block requested from the server given its hash.
|
|
|
|
func (r FutureGetBlockResult) Receive() (*btcutil.Block, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a string.
|
|
|
|
var blockHex string
|
|
|
|
err = json.Unmarshal(res, &blockHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the serialized block hex to raw bytes.
|
|
|
|
serializedBlock, err := hex.DecodeString(blockHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize the block and return it.
|
2015-02-05 22:50:03 +01:00
|
|
|
var msgBlock wire.MsgBlock
|
2014-12-13 16:02:59 +01:00
|
|
|
err = msgBlock.Deserialize(bytes.NewReader(serializedBlock))
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return btcutil.NewBlock(&msgBlock), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetBlock for the blocking version and more details.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetBlockAsync(blockHash *wire.ShaHash) FutureGetBlockResult {
|
2014-05-22 02:53:46 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(false), nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlock returns a raw block from the server given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlockVerbose to retrieve a data structure with information about the
|
|
|
|
// block instead.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetBlock(blockHash *wire.ShaHash) (*btcutil.Block, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBlockAsync(blockHash).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockVerboseResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockVerboseAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetBlockVerboseResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the data
|
|
|
|
// structure from the server with information about the requested block.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (r FutureGetBlockVerboseResult) Receive() (*btcjson.GetBlockVerboseResult, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the raw result into a BlockResult.
|
2015-01-01 23:34:07 +01:00
|
|
|
var blockResult btcjson.GetBlockVerboseResult
|
2014-06-10 02:49:41 +02:00
|
|
|
err = json.Unmarshal(res, &blockResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
return &blockResult, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockVerboseAsync returns an instance of a type that can be used to get
|
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See GetBlockVerbose for the blocking version and more details.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetBlockVerboseAsync(blockHash *wire.ShaHash, verboseTx bool) FutureGetBlockVerboseResult {
|
2014-05-22 02:53:46 +02:00
|
|
|
hash := ""
|
|
|
|
if blockHash != nil {
|
|
|
|
hash = blockHash.String()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), &verboseTx)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockVerbose returns a data structure from the server with information
|
|
|
|
// about a block given its hash.
|
|
|
|
//
|
|
|
|
// See GetBlock to retrieve a raw block instead.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) GetBlockVerbose(blockHash *wire.ShaHash, verboseTx bool) (*btcjson.GetBlockVerboseResult, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBlockVerboseAsync(blockHash, verboseTx).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockCountResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockCountAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetBlockCountResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the number
|
|
|
|
// of blocks in the longest block chain.
|
|
|
|
func (r FutureGetBlockCountResult) Receive() (int64, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as an int64.
|
|
|
|
var count int64
|
|
|
|
err = json.Unmarshal(res, &count)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2014-06-10 02:49:41 +02:00
|
|
|
return count, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockCountAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetBlockCount for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockCountAsync() FutureGetBlockCountResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockCountCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockCount returns the number of blocks in the longest block chain.
|
|
|
|
func (c *Client) GetBlockCount() (int64, error) {
|
|
|
|
return c.GetBlockCountAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetDifficultyResult is a future promise to deliver the result of a
|
|
|
|
// GetDifficultyAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetDifficultyResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the
|
|
|
|
// proof-of-work difficulty as a multiple of the minimum difficulty.
|
|
|
|
func (r FutureGetDifficultyResult) Receive() (float64, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a float64.
|
|
|
|
var difficulty float64
|
|
|
|
err = json.Unmarshal(res, &difficulty)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return difficulty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifficultyAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetDifficulty for the blocking version and more details.
|
|
|
|
func (c *Client) GetDifficultyAsync() FutureGetDifficultyResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetDifficultyCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifficulty returns the proof-of-work difficulty as a multiple of the
|
|
|
|
// minimum difficulty.
|
|
|
|
func (c *Client) GetDifficulty() (float64, error) {
|
|
|
|
return c.GetDifficultyAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetBlockHashResult is a future promise to deliver the result of a
|
|
|
|
// GetBlockHashAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetBlockHashResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the hash of
|
|
|
|
// the block in the best block chain at the given height.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (r FutureGetBlockHashResult) Receive() (*wire.ShaHash, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a string-encoded sha.
|
|
|
|
var txHashStr string
|
|
|
|
err = json.Unmarshal(res, &txHashStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
2015-02-05 22:50:03 +01:00
|
|
|
return wire.NewShaHashFromStr(txHashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHashAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetBlockHash for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockHashAsync(blockHeight int64) FutureGetBlockHashResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetBlockHashCmd(blockHeight)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockHash returns the hash of the block in the best block chain at the
|
|
|
|
// given height.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetBlockHash(blockHeight int64) (*wire.ShaHash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetBlockHashAsync(blockHeight).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetRawMempoolResult is a future promise to deliver the result of a
|
|
|
|
// GetRawMempoolAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetRawMempoolResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the hashes
|
|
|
|
// of all transactions in the memory pool.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (r FutureGetRawMempoolResult) Receive() ([]*wire.ShaHash, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as an array of strings.
|
|
|
|
var txHashStrs []string
|
|
|
|
err = json.Unmarshal(res, &txHashStrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Create a slice of ShaHash arrays from the string slice.
|
2015-02-05 22:50:03 +01:00
|
|
|
txHashes := make([]*wire.ShaHash, 0, len(txHashStrs))
|
2014-05-07 18:14:39 +02:00
|
|
|
for _, hashStr := range txHashStrs {
|
2015-02-05 22:50:03 +01:00
|
|
|
txHash, err := wire.NewShaHashFromStr(hashStr)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txHashes = append(txHashes, txHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txHashes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See GetRawMempool for the blocking version and more details.
|
|
|
|
func (c *Client) GetRawMempoolAsync() FutureGetRawMempoolResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(false))
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempool returns the hashes of all transactions in the memory pool.
|
|
|
|
//
|
|
|
|
// See GetRawMempoolVerbose to retrieve data structures with information about
|
|
|
|
// the transactions instead.
|
2015-02-05 22:50:03 +01:00
|
|
|
func (c *Client) GetRawMempool() ([]*wire.ShaHash, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetRawMempoolAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetRawMempoolVerboseResult is a future promise to deliver the result of
|
|
|
|
// a GetRawMempoolVerboseAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetRawMempoolVerboseResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns a map of
|
|
|
|
// transaction hashes to an associated data structure with information about the
|
|
|
|
// transaction for all transactions in the memory pool.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (r FutureGetRawMempoolVerboseResult) Receive() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a map of strings (tx shas) to their detailed
|
|
|
|
// results.
|
2015-01-01 23:34:07 +01:00
|
|
|
var mempoolItems map[string]btcjson.GetRawMempoolVerboseResult
|
2014-06-10 02:49:41 +02:00
|
|
|
err = json.Unmarshal(res, &mempoolItems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return mempoolItems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolVerboseAsync returns an instance of a type that can be used to
|
|
|
|
// get the result of the RPC at some future time by invoking the Receive
|
|
|
|
// function on the returned instance.
|
|
|
|
//
|
|
|
|
// See GetRawMempoolVerbose for the blocking version and more details.
|
|
|
|
func (c *Client) GetRawMempoolVerboseAsync() FutureGetRawMempoolVerboseResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetRawMempoolCmd(btcjson.Bool(true))
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawMempoolVerbose returns a map of transaction hashes to an associated
|
|
|
|
// data structure with information about the transaction for all transactions in
|
|
|
|
// the memory pool.
|
|
|
|
//
|
|
|
|
// See GetRawMempool to retrieve only the transaction hashes instead.
|
2015-01-01 23:34:07 +01:00
|
|
|
func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerboseResult, error) {
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.GetRawMempoolVerboseAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureVerifyChainResult is a future promise to deliver the result of a
|
|
|
|
// VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync
|
|
|
|
// invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureVerifyChainResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns whether
|
|
|
|
// or not the chain verified based on the check level and number of blocks
|
|
|
|
// to verify specified in the original call.
|
|
|
|
func (r FutureVerifyChainResult) Receive() (bool, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal the result as a boolean.
|
|
|
|
var verified bool
|
|
|
|
err = json.Unmarshal(res, &verified)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
return verified, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainAsync returns an instance of a type that can be used to get the
|
|
|
|
// result of the RPC at some future time by invoking the Receive function on the
|
|
|
|
// returned instance.
|
|
|
|
//
|
|
|
|
// See VerifyChain for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainAsync() FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(nil, nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChain requests the server to verify the block chain database using
|
|
|
|
// the default check level and number of blocks to verify.
|
|
|
|
//
|
|
|
|
// See VerifyChainLevel and VerifyChainBlocks to override the defaults.
|
|
|
|
func (c *Client) VerifyChain() (bool, error) {
|
|
|
|
return c.VerifyChainAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainLevelAsync returns an instance of a type that can be used to get
|
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See VerifyChainLevel for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainLevelAsync(checkLevel int32) FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(&checkLevel, nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainLevel requests the server to verify the block chain database using
|
|
|
|
// the passed check level and default number of blocks to verify.
|
|
|
|
//
|
|
|
|
// The check level controls how thorough the verification is with higher numbers
|
|
|
|
// increasing the amount of checks done as consequently how long the
|
|
|
|
// verification takes.
|
|
|
|
//
|
|
|
|
// See VerifyChain to use the default check level and VerifyChainBlocks to
|
|
|
|
// override the number of blocks to verify.
|
|
|
|
func (c *Client) VerifyChainLevel(checkLevel int32) (bool, error) {
|
|
|
|
return c.VerifyChainLevelAsync(checkLevel).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainBlocksAsync returns an instance of a type that can be used to get
|
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See VerifyChainBlocks for the blocking version and more details.
|
|
|
|
func (c *Client) VerifyChainBlocksAsync(checkLevel, numBlocks int32) FutureVerifyChainResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewVerifyChainCmd(&checkLevel, &numBlocks)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyChainBlocks requests the server to verify the block chain database
|
|
|
|
// using the passed check level and number of blocks to verify.
|
|
|
|
//
|
|
|
|
// The check level controls how thorough the verification is with higher numbers
|
|
|
|
// increasing the amount of checks done as consequently how long the
|
|
|
|
// verification takes.
|
|
|
|
//
|
|
|
|
// The number of blocks refers to the number of blocks from the end of the
|
|
|
|
// current longest chain.
|
|
|
|
//
|
|
|
|
// See VerifyChain and VerifyChainLevel to use defaults.
|
|
|
|
func (c *Client) VerifyChainBlocks(checkLevel, numBlocks int32) (bool, error) {
|
|
|
|
return c.VerifyChainBlocksAsync(checkLevel, numBlocks).Receive()
|
|
|
|
}
|
2014-10-17 22:39:45 +02:00
|
|
|
|
|
|
|
// FutureGetTxOutResult is a future promise to deliver the result of a
|
|
|
|
// GetTxOutAsync RPC invocation (or an applicable error).
|
|
|
|
type FutureGetTxOutResult chan *response
|
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns a
|
|
|
|
// transaction given its hash.
|
|
|
|
func (r FutureGetTxOutResult) Receive() (*btcjson.GetTxOutResult, error) {
|
|
|
|
res, err := receiveFuture(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// take care of the special case where the output has been spent already
|
|
|
|
// it should return the string "null"
|
|
|
|
if string(res) == "null" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as an gettxout result object.
|
|
|
|
var txOutInfo *btcjson.GetTxOutResult
|
|
|
|
err = json.Unmarshal(res, &txOutInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txOutInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOutAsync returns an instance of a type that can be used to get
|
|
|
|
// the result of the RPC at some future time by invoking the Receive function on
|
|
|
|
// the returned instance.
|
|
|
|
//
|
|
|
|
// See GetTxOut for the blocking version and more details.
|
2015-02-25 06:49:06 +01:00
|
|
|
func (c *Client) GetTxOutAsync(txHash *wire.ShaHash, index uint32, mempool bool) FutureGetTxOutResult {
|
2014-10-17 22:39:45 +02:00
|
|
|
hash := ""
|
|
|
|
if txHash != nil {
|
|
|
|
hash = txHash.String()
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetTxOutCmd(hash, index, &mempool)
|
2014-10-17 22:39:45 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTxOut returns the transaction output info if it's unspent and
|
|
|
|
// nil, otherwise.
|
2015-02-25 06:49:06 +01:00
|
|
|
func (c *Client) GetTxOut(txHash *wire.ShaHash, index uint32, mempool bool) (*btcjson.GetTxOutResult, error) {
|
2014-10-17 22:39:45 +02:00
|
|
|
return c.GetTxOutAsync(txHash, index, mempool).Receive()
|
|
|
|
}
|