rpc/client: add a set of GetBlock methods

They differ in input and output types (and data), thus four methods are
added.
This commit is contained in:
Roman Khimov 2020-03-03 20:26:56 +03:00
parent 685e34d83c
commit f01766131b
2 changed files with 58 additions and 18 deletions

View file

@ -19,6 +19,7 @@ TODO:
Supported methods
getaccountstate
getblock
getclaimable
getunspents
invoke
@ -34,7 +35,6 @@ Unsupported methods
getassetstate
getbalance
getbestblockhash
getblock
getblockcount
getblockhash
getblockheader

View file

@ -4,8 +4,10 @@ import (
"encoding/hex"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -14,23 +16,6 @@ import (
"github.com/pkg/errors"
)
// getBlock returns a block by its hash or index/height. If verbose is true
// the response will contain a pretty Block object instead of the raw hex string.
// missing output wrapper at the moment, thus commented out
// func (c *Client) getBlock(indexOrHash interface{}, verbose bool) (*response, error) {
// var (
// params = request.NewRawParams(indexOrHash)
// resp = &response{}
// )
// if verbose {
// params = request.NewRawParams(indexOrHash, 1)
// }
// if err := c.performRequest("getblock", params, resp); err != nil {
// return nil, err
// }
// return resp, nil
// }
// GetAccountState returns detailed information about a NEO account.
func (c *Client) GetAccountState(address string) (*result.AccountState, error) {
var (
@ -43,6 +28,61 @@ func (c *Client) GetAccountState(address string) (*result.AccountState, error) {
return resp, nil
}
// GetBlockByIndex returns a block by its height.
func (c *Client) GetBlockByIndex(index uint32) (*block.Block, error) {
return c.getBlock(request.NewRawParams(index))
}
// GetBlockByHash returns a block by its hash.
func (c *Client) GetBlockByHash(hash util.Uint256) (*block.Block, error) {
return c.getBlock(request.NewRawParams(hash.StringLE()))
}
func (c *Client) getBlock(params request.RawParams) (*block.Block, error) {
var (
resp string
err error
b *block.Block
)
if err = c.performRequest("getblock", params, &resp); err != nil {
return nil, err
}
blockBytes, err := hex.DecodeString(resp)
if err != nil {
return nil, err
}
r := io.NewBinReaderFromBuf(blockBytes)
b = new(block.Block)
b.DecodeBinary(r)
if r.Err != nil {
return nil, r.Err
}
return b, nil
}
// GetBlockByIndexVerbose returns a block wrapper with additional metadata by
// its height.
func (c *Client) GetBlockByIndexVerbose(index uint32) (*result.Block, error) {
return c.getBlockVerbose(request.NewRawParams(index, 1))
}
// GetBlockByHashVerbose returns a block wrapper with additional metadata by
// its hash.
func (c *Client) GetBlockByHashVerbose(hash util.Uint256) (*result.Block, error) {
return c.getBlockVerbose(request.NewRawParams(hash.StringLE(), 1))
}
func (c *Client) getBlockVerbose(params request.RawParams) (*result.Block, error) {
var (
resp = &result.Block{}
err error
)
if err = c.performRequest("getblock", params, &resp); err != nil {
return nil, err
}
return resp, nil
}
// GetClaimable returns tx outputs which can be claimed.
func (c *Client) GetClaimable(address string) (*result.ClaimableInfo, error) {
params := request.NewRawParams(address)