2017-07-22 04:53:54 +02:00
|
|
|
// Copyright (c) 2014-2017 The btcsuite developers
|
2014-05-07 18:14:39 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-07-22 04:53:54 +02:00
|
|
|
package rpcclient
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2014-06-10 02:49:41 +02:00
|
|
|
"encoding/json"
|
2014-06-27 03:24:40 +02:00
|
|
|
"errors"
|
2014-07-03 02:33:54 +02:00
|
|
|
|
2018-05-15 05:44:11 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2014-05-07 18:14:39 +02:00
|
|
|
)
|
|
|
|
|
2015-04-28 23:22:36 +02:00
|
|
|
// FutureGenerateResult is a future promise to deliver the result of a
|
|
|
|
// GenerateAsync RPC invocation (or an applicable error).
|
|
|
|
type FutureGenerateResult chan *response
|
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns a list of
|
|
|
|
// block hashes generated by the call.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (r FutureGenerateResult) Receive() ([]*chainhash.Hash, error) {
|
2015-04-28 23:22:36 +02:00
|
|
|
res, err := receiveFuture(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a list of strings.
|
|
|
|
var result []string
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:16:04 +02:00
|
|
|
// Convert each block hash to a chainhash.Hash and store a pointer to
|
|
|
|
// each.
|
|
|
|
convertedResult := make([]*chainhash.Hash, len(result))
|
2015-04-28 23:22:36 +02:00
|
|
|
for i, hashString := range result {
|
2016-08-08 21:16:04 +02:00
|
|
|
convertedResult[i], err = chainhash.NewHashFromStr(hashString)
|
2015-04-28 23:22:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertedResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateAsync 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 Generate for the blocking version and more details.
|
|
|
|
func (c *Client) GenerateAsync(numBlocks uint32) FutureGenerateResult {
|
|
|
|
cmd := btcjson.NewGenerateCmd(numBlocks)
|
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate generates numBlocks blocks and returns their hashes.
|
2016-08-08 21:16:04 +02:00
|
|
|
func (c *Client) Generate(numBlocks uint32) ([]*chainhash.Hash, error) {
|
2015-04-28 23:22:36 +02:00
|
|
|
return c.GenerateAsync(numBlocks).Receive()
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:33:48 +01:00
|
|
|
// FutureGenerateToAddressResult is a future promise to deliver the result of a
|
|
|
|
// GenerateToAddressResult RPC invocation (or an applicable error).
|
|
|
|
type FutureGenerateToAddressResult chan *response
|
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the hashes of
|
|
|
|
// of the generated blocks.
|
|
|
|
func (f FutureGenerateToAddressResult) Receive() ([]*chainhash.Hash, error) {
|
|
|
|
res, err := receiveFuture(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a list of strings.
|
|
|
|
var result []string
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert each block hash to a chainhash.Hash and store a pointer to
|
|
|
|
// each.
|
|
|
|
convertedResult := make([]*chainhash.Hash, len(result))
|
|
|
|
for i, hashString := range result {
|
|
|
|
convertedResult[i], err = chainhash.NewHashFromStr(hashString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertedResult, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateToAddressAsync 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 GenerateToAddress for the blocking version and more details.
|
|
|
|
func (c *Client) GenerateToAddressAsync(numBlocks int64, address btcutil.Address, maxTries *int64) FutureGenerateToAddressResult {
|
|
|
|
cmd := btcjson.NewGenerateToAddressCmd(numBlocks, address.EncodeAddress(), maxTries)
|
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateToAddress generates numBlocks blocks to the given address and returns their hashes.
|
|
|
|
func (c *Client) GenerateToAddress(numBlocks int64, address btcutil.Address, maxTries *int64) ([]*chainhash.Hash, error) {
|
|
|
|
return c.GenerateToAddressAsync(numBlocks, address, maxTries).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureGetGenerateResult is a future promise to deliver the result of a
|
|
|
|
// GetGenerateAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetGenerateResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns true if the
|
|
|
|
// server is set to mine, otherwise false.
|
|
|
|
func (r FutureGetGenerateResult) 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 result as a boolean.
|
|
|
|
var result bool
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGenerateAsync 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 GetGenerate for the blocking version and more details.
|
|
|
|
func (c *Client) GetGenerateAsync() FutureGetGenerateResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetGenerateCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGenerate returns true if the server is set to mine, otherwise false.
|
|
|
|
func (c *Client) GetGenerate() (bool, error) {
|
|
|
|
return c.GetGenerateAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureSetGenerateResult is a future promise to deliver the result of a
|
|
|
|
// SetGenerateAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureSetGenerateResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error if
|
|
|
|
// any occurred when setting the server to generate coins (mine) or not.
|
|
|
|
func (r FutureSetGenerateResult) Receive() error {
|
|
|
|
_, err := receiveFuture(r)
|
2016-11-16 21:52:02 +01:00
|
|
|
return err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGenerateAsync 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 SetGenerate for the blocking version and more details.
|
|
|
|
func (c *Client) SetGenerateAsync(enable bool, numCPUs int) FutureSetGenerateResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewSetGenerateCmd(enable, &numCPUs)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGenerate sets the server to generate coins (mine) or not.
|
|
|
|
func (c *Client) SetGenerate(enable bool, numCPUs int) error {
|
|
|
|
return c.SetGenerateAsync(enable, numCPUs).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetHashesPerSecResult is a future promise to deliver the result of a
|
|
|
|
// GetHashesPerSecAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetHashesPerSecResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns a recent
|
|
|
|
// hashes per second performance measurement while generating coins (mining).
|
|
|
|
// Zero is returned if the server is not mining.
|
|
|
|
func (r FutureGetHashesPerSecResult) 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 -1, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as an int64.
|
|
|
|
var result int64
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHashesPerSecAsync 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 GetHashesPerSec for the blocking version and more details.
|
|
|
|
func (c *Client) GetHashesPerSecAsync() FutureGetHashesPerSecResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetHashesPerSecCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHashesPerSec returns a recent hashes per second performance measurement
|
|
|
|
// while generating coins (mining). Zero is returned if the server is not
|
|
|
|
// mining.
|
|
|
|
func (c *Client) GetHashesPerSec() (int64, error) {
|
|
|
|
return c.GetHashesPerSecAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetMiningInfoResult is a future promise to deliver the result of a
|
|
|
|
// GetMiningInfoAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetMiningInfoResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the mining
|
|
|
|
// information.
|
|
|
|
func (r FutureGetMiningInfoResult) Receive() (*btcjson.GetMiningInfoResult, 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 getmininginfo result object.
|
|
|
|
var infoResult btcjson.GetMiningInfoResult
|
|
|
|
err = json.Unmarshal(res, &infoResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
return &infoResult, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMiningInfoAsync 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 GetMiningInfo for the blocking version and more details.
|
|
|
|
func (c *Client) GetMiningInfoAsync() FutureGetMiningInfoResult {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetMiningInfoCmd()
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMiningInfo returns mining information.
|
|
|
|
func (c *Client) GetMiningInfo() (*btcjson.GetMiningInfoResult, error) {
|
|
|
|
return c.GetMiningInfoAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetNetworkHashPS is a future promise to deliver the result of a
|
|
|
|
// GetNetworkHashPSAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetNetworkHashPS chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the
|
|
|
|
// estimated network hashes per second for the block heights provided by the
|
|
|
|
// parameters.
|
|
|
|
func (r FutureGetNetworkHashPS) 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 -1, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as an int64.
|
|
|
|
var result int64
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPSAsync 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 GetNetworkHashPS for the blocking version and more details.
|
|
|
|
func (c *Client) GetNetworkHashPSAsync() FutureGetNetworkHashPS {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetNetworkHashPSCmd(nil, nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPS returns the estimated network hashes per second using the
|
|
|
|
// default number of blocks and the most recent block height.
|
|
|
|
//
|
|
|
|
// See GetNetworkHashPS2 to override the number of blocks to use and
|
|
|
|
// GetNetworkHashPS3 to override the height at which to calculate the estimate.
|
|
|
|
func (c *Client) GetNetworkHashPS() (int64, error) {
|
|
|
|
return c.GetNetworkHashPSAsync().Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPS2Async 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 GetNetworkHashPS2 for the blocking version and more details.
|
|
|
|
func (c *Client) GetNetworkHashPS2Async(blocks int) FutureGetNetworkHashPS {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetNetworkHashPSCmd(&blocks, nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPS2 returns the estimated network hashes per second for the
|
|
|
|
// specified previous number of blocks working backwards from the most recent
|
|
|
|
// block height. The blocks parameter can also be -1 in which case the number
|
|
|
|
// of blocks since the last difficulty change will be used.
|
|
|
|
//
|
|
|
|
// See GetNetworkHashPS to use defaults and GetNetworkHashPS3 to override the
|
|
|
|
// height at which to calculate the estimate.
|
|
|
|
func (c *Client) GetNetworkHashPS2(blocks int) (int64, error) {
|
|
|
|
return c.GetNetworkHashPS2Async(blocks).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPS3Async 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 GetNetworkHashPS3 for the blocking version and more details.
|
|
|
|
func (c *Client) GetNetworkHashPS3Async(blocks, height int) FutureGetNetworkHashPS {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetNetworkHashPSCmd(&blocks, &height)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetworkHashPS3 returns the estimated network hashes per second for the
|
|
|
|
// specified previous number of blocks working backwards from the specified
|
|
|
|
// block height. The blocks parameter can also be -1 in which case the number
|
|
|
|
// of blocks since the last difficulty change will be used.
|
|
|
|
//
|
|
|
|
// See GetNetworkHashPS and GetNetworkHashPS2 to use defaults.
|
|
|
|
func (c *Client) GetNetworkHashPS3(blocks, height int) (int64, error) {
|
|
|
|
return c.GetNetworkHashPS3Async(blocks, height).Receive()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FutureGetWork is a future promise to deliver the result of a
|
|
|
|
// GetWorkAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetWork chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns the hash
|
|
|
|
// data to work on.
|
|
|
|
func (r FutureGetWork) Receive() (*btcjson.GetWorkResult, 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 getwork result object.
|
|
|
|
var result btcjson.GetWorkResult
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
return &result, nil
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetWorkAsync 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 GetWork for the blocking version and more details.
|
|
|
|
func (c *Client) GetWorkAsync() FutureGetWork {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetWorkCmd(nil)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWork returns hash data to work on.
|
2014-05-03 08:46:20 +02:00
|
|
|
//
|
|
|
|
// See GetWorkSubmit to submit the found solution.
|
2014-05-07 18:14:39 +02:00
|
|
|
func (c *Client) GetWork() (*btcjson.GetWorkResult, error) {
|
|
|
|
return c.GetWorkAsync().Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-03 08:46:20 +02:00
|
|
|
// FutureGetWorkSubmit is a future promise to deliver the result of a
|
|
|
|
// GetWorkSubmitAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureGetWorkSubmit chan *response
|
2014-05-03 08:46:20 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns whether
|
|
|
|
// or not the submitted block header was accepted.
|
|
|
|
func (r FutureGetWorkSubmit) Receive() (bool, error) {
|
2014-06-10 02:49:41 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-03 08:46:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2014-06-10 02:49:41 +02:00
|
|
|
// Unmarshal result as a boolean.
|
|
|
|
var accepted bool
|
|
|
|
err = json.Unmarshal(res, &accepted)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2014-05-03 08:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return accepted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWorkSubmitAsync 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 GetWorkSubmit for the blocking version and more details.
|
|
|
|
func (c *Client) GetWorkSubmitAsync(data string) FutureGetWorkSubmit {
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewGetWorkCmd(&data)
|
2014-05-03 08:46:20 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWorkSubmit submits a block header which is a solution to previously
|
|
|
|
// requested data and returns whether or not the solution was accepted.
|
|
|
|
//
|
|
|
|
// See GetWork to request data to work on.
|
|
|
|
func (c *Client) GetWorkSubmit(data string) (bool, error) {
|
|
|
|
return c.GetWorkSubmitAsync(data).Receive()
|
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
// FutureSubmitBlockResult is a future promise to deliver the result of a
|
|
|
|
// SubmitBlockAsync RPC invocation (or an applicable error).
|
2014-06-10 02:49:41 +02:00
|
|
|
type FutureSubmitBlockResult chan *response
|
2014-05-07 18:14:39 +02:00
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error if
|
|
|
|
// any occurred when submitting the block.
|
|
|
|
func (r FutureSubmitBlockResult) Receive() error {
|
2014-06-27 01:04:01 +02:00
|
|
|
res, err := receiveFuture(r)
|
2014-05-07 18:14:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-27 04:47:29 +02:00
|
|
|
if string(res) != "null" {
|
|
|
|
var result string
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New(result)
|
2014-06-27 01:04:01 +02:00
|
|
|
}
|
|
|
|
|
2014-05-07 18:14:39 +02:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitBlockAsync 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 SubmitBlock for the blocking version and more details.
|
|
|
|
func (c *Client) SubmitBlockAsync(block *btcutil.Block, options *btcjson.SubmitBlockOptions) FutureSubmitBlockResult {
|
2014-05-22 02:53:46 +02:00
|
|
|
blockHex := ""
|
|
|
|
if block != nil {
|
|
|
|
blockBytes, err := block.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return newFutureError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockHex = hex.EncodeToString(blockBytes)
|
2014-05-07 18:14:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:34:07 +01:00
|
|
|
cmd := btcjson.NewSubmitBlockCmd(blockHex, options)
|
2014-05-07 18:14:39 +02:00
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitBlock attempts to submit a new block into the bitcoin network.
|
|
|
|
func (c *Client) SubmitBlock(block *btcutil.Block, options *btcjson.SubmitBlockOptions) error {
|
|
|
|
return c.SubmitBlockAsync(block, options).Receive()
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:42:35 +02:00
|
|
|
// FutureGetBlockTemplateResponse is a future promise to deliver the result of a
|
|
|
|
// GetBlockTemplateAsync RPC invocation (or an applicable error).
|
|
|
|
type FutureGetBlockTemplateResponse chan *response
|
|
|
|
|
|
|
|
// Receive waits for the response promised by the future and returns an error if
|
|
|
|
// any occurred when retrieving the block template.
|
|
|
|
func (r FutureGetBlockTemplateResponse) Receive() (*btcjson.GetBlockTemplateResult, error) {
|
|
|
|
res, err := receiveFuture(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal result as a getwork result object.
|
|
|
|
var result btcjson.GetBlockTemplateResult
|
|
|
|
err = json.Unmarshal(res, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTemplateAsync 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 GetBlockTemplate for the blocking version and more details.
|
|
|
|
func (c *Client) GetBlockTemplateAsync(req *btcjson.TemplateRequest) FutureGetBlockTemplateResponse {
|
|
|
|
cmd := btcjson.NewGetBlockTemplateCmd(req)
|
|
|
|
return c.sendCmd(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockTemplate returns a new block template for mining.
|
|
|
|
func (c *Client) GetBlockTemplate(req *btcjson.TemplateRequest) (*btcjson.GetBlockTemplateResult, error) {
|
|
|
|
return c.GetBlockTemplateAsync(req).Receive()
|
|
|
|
}
|