rpcclient: support getblockchaininfo RPC

This adds support for the getblockchaininfo RPC to rpcclient,
adding asynchronous and blocking functions to rpcclient.Client.
The rpcclient module version is bumped to v1.1.0.
This commit is contained in:
Jonathan Chappelow 2018-12-07 10:19:32 -06:00
parent 300b6848c9
commit a0816cf430
2 changed files with 39 additions and 1 deletions

2
go.mod
View File

@ -28,7 +28,7 @@ require (
github.com/decred/dcrd/mempool v1.0.2
github.com/decred/dcrd/mining v1.0.1
github.com/decred/dcrd/peer v1.1.0
github.com/decred/dcrd/rpcclient v1.0.2
github.com/decred/dcrd/rpcclient v1.1.0
github.com/decred/dcrd/txscript v1.0.2
github.com/decred/dcrd/wire v1.2.0
github.com/decred/slog v1.0.0

View File

@ -230,6 +230,44 @@ func (c *Client) GetDifficulty() (float64, error) {
return c.GetDifficultyAsync().Receive()
}
// FutureGetBlockChainInfoResult is a future promise to deliver the result of a
// GetBlockChainInfoAsync RPC invocation (or an applicable error).
type FutureGetBlockChainInfoResult chan *response
// Receive waits for the response promised by the future and returns the info
// provided by the server.
func (r FutureGetBlockChainInfoResult) Receive() (*dcrjson.GetBlockChainInfoResult, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a getblockchaininfo result object.
var blockchainInfoRes dcrjson.GetBlockChainInfoResult
err = json.Unmarshal(res, &blockchainInfoRes)
if err != nil {
return nil, err
}
return &blockchainInfoRes, nil
}
// GetBlockChainInfoAsync 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 GetBlockChainInfo for the blocking version and more details.
func (c *Client) GetBlockChainInfoAsync() FutureGetBlockChainInfoResult {
cmd := dcrjson.NewGetBlockChainInfoCmd()
return c.sendCmd(cmd)
}
// GetBlockChainInfo returns information about the current state of the block
// chain.
func (c *Client) GetBlockChainInfo() (*dcrjson.GetBlockChainInfoResult, error) {
return c.GetBlockChainInfoAsync().Receive()
}
// FutureGetBlockHashResult is a future promise to deliver the result of a
// GetBlockHashAsync RPC invocation (or an applicable error).
type FutureGetBlockHashResult chan *response