2022-07-21 19:39:53 +00:00
|
|
|
package rpcclient
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2018-12-21 09:32:18 +00:00
|
|
|
import (
|
2021-11-17 21:08:10 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2022-06-15 18:23:29 +00:00
|
|
|
"encoding/json"
|
2020-08-06 14:44:08 +00:00
|
|
|
"errors"
|
2020-07-21 07:31:45 +00:00
|
|
|
"fmt"
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2022-06-15 18:23:29 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-10-14 15:13:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 17:26:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-09-28 14:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/fee"
|
2021-02-08 10:43:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-03-23 10:49:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativeprices"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-09-21 12:34:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-08 10:43:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-03-03 17:26:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2021-02-08 10:43:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2022-08-09 12:18:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-11-10 11:30:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-02-08 10:43:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2022-06-15 18:23:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2018-12-21 09:32:18 +00:00
|
|
|
)
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2020-10-14 15:13:20 +00:00
|
|
|
var errNetworkNotInitialized = errors.New("RPC client network is not initialized")
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CalculateNetworkFee calculates network fee for the transaction. The transaction may
|
2021-03-24 17:32:48 +00:00
|
|
|
// have empty witnesses for contract signers and may have only verification scripts
|
|
|
|
// filled for standard sig/multisig signers.
|
|
|
|
func (c *Client) CalculateNetworkFee(tx *transaction.Transaction) (int64, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{tx.Bytes()}
|
2021-06-04 17:44:58 +00:00
|
|
|
resp = new(result.NetworkFee)
|
2021-03-24 17:32:48 +00:00
|
|
|
)
|
2021-06-04 17:44:58 +00:00
|
|
|
if err := c.performRequest("calculatenetworkfee", params, resp); err != nil {
|
2021-03-24 17:32:48 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-04 17:44:58 +00:00
|
|
|
return resp.Value, nil
|
2021-03-24 17:32:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetApplicationLog returns a contract log based on the specified txid.
|
2020-11-10 11:30:00 +00:00
|
|
|
func (c *Client) GetApplicationLog(hash util.Uint256, trig *trigger.Type) (*result.ApplicationLog, error) {
|
2020-03-11 14:14:05 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE()}
|
2020-11-11 15:43:28 +00:00
|
|
|
resp = new(result.ApplicationLog)
|
2020-03-11 14:14:05 +00:00
|
|
|
)
|
2020-11-10 11:30:00 +00:00
|
|
|
if trig != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, trig.String())
|
2020-11-10 11:30:00 +00:00
|
|
|
}
|
2020-03-11 14:14:05 +00:00
|
|
|
if err := c.performRequest("getapplicationlog", params, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBestBlockHash returns the hash of the tallest block in the blockchain.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetBestBlockHash() (util.Uint256, error) {
|
|
|
|
var resp = util.Uint256{}
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getbestblockhash", nil, &resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockCount returns the number of blocks in the blockchain.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetBlockCount() (uint32, error) {
|
|
|
|
var resp uint32
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getblockcount", nil, &resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:09:53 +00:00
|
|
|
// GetBlockByIndex returns a block by its height. In-header stateroot option
|
|
|
|
// must be initialized with Init before calling this method.
|
2020-03-03 17:26:56 +00:00
|
|
|
func (c *Client) GetBlockByIndex(index uint32) (*block.Block, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getBlock(index)
|
2020-03-03 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 16:09:53 +00:00
|
|
|
// GetBlockByHash returns a block by its hash. In-header stateroot option
|
|
|
|
// must be initialized with Init before calling this method.
|
2020-03-03 17:26:56 +00:00
|
|
|
func (c *Client) GetBlockByHash(hash util.Uint256) (*block.Block, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getBlock(hash.StringLE())
|
2020-03-03 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) getBlock(param interface{}) (*block.Block, error) {
|
2020-03-03 17:26:56 +00:00
|
|
|
var (
|
2020-11-06 14:37:58 +00:00
|
|
|
resp []byte
|
2020-03-03 17:26:56 +00:00
|
|
|
err error
|
|
|
|
b *block.Block
|
|
|
|
)
|
2022-07-07 17:03:10 +00:00
|
|
|
if err = c.performRequest("getblock", []interface{}{param}, &resp); err != nil {
|
2020-03-03 17:26:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-06 14:37:58 +00:00
|
|
|
r := io.NewBinReaderFromBuf(resp)
|
2022-02-21 10:41:14 +00:00
|
|
|
sr, err := c.StateRootInHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b = block.New(sr)
|
2020-03-03 17:26:56 +00:00
|
|
|
b.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByIndexVerbose returns a block wrapper with additional metadata by
|
2022-09-07 16:09:53 +00:00
|
|
|
// its height. In-header stateroot option must be initialized with Init before
|
|
|
|
// calling this method.
|
2020-03-23 14:31:28 +00:00
|
|
|
// NOTE: to get transaction.ID and transaction.Size, use t.Hash() and io.GetVarSize(t) respectively.
|
2020-03-03 17:26:56 +00:00
|
|
|
func (c *Client) GetBlockByIndexVerbose(index uint32) (*result.Block, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getBlockVerbose(index)
|
2020-03-03 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHashVerbose returns a block wrapper with additional metadata by
|
2022-09-07 16:09:53 +00:00
|
|
|
// its hash. In-header stateroot option must be initialized with Init before
|
|
|
|
// calling this method.
|
2020-03-03 17:26:56 +00:00
|
|
|
func (c *Client) GetBlockByHashVerbose(hash util.Uint256) (*result.Block, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getBlockVerbose(hash.StringLE())
|
2020-03-03 17:26:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) getBlockVerbose(param interface{}) (*result.Block, error) {
|
2020-03-03 17:26:56 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{param, 1} // 1 for verbose.
|
|
|
|
resp = &result.Block{}
|
|
|
|
err error
|
2020-03-03 17:26:56 +00:00
|
|
|
)
|
2022-02-21 10:41:14 +00:00
|
|
|
sr, err := c.StateRootInHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-14 15:13:20 +00:00
|
|
|
}
|
2022-02-21 10:41:14 +00:00
|
|
|
resp.Header.StateRootEnabled = sr
|
2020-03-24 17:11:49 +00:00
|
|
|
if err = c.performRequest("getblock", params, resp); err != nil {
|
2020-03-03 17:26:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockHash returns the hash value of the corresponding block based on the specified index.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetBlockHash(index uint32) (util.Uint256, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{index}
|
2020-03-11 14:14:05 +00:00
|
|
|
resp = util.Uint256{}
|
|
|
|
)
|
|
|
|
if err := c.performRequest("getblockhash", params, &resp); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockHeader returns the corresponding block header information from a serialized hex string
|
2022-09-07 16:09:53 +00:00
|
|
|
// according to the specified script hash. In-header stateroot option must be
|
|
|
|
// initialized with Init before calling this method.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetBlockHeader(hash util.Uint256) (*block.Header, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE()}
|
2020-11-06 14:37:58 +00:00
|
|
|
resp []byte
|
2020-03-11 14:14:05 +00:00
|
|
|
h *block.Header
|
|
|
|
)
|
|
|
|
if err := c.performRequest("getblockheader", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-21 10:41:14 +00:00
|
|
|
sr, err := c.StateRootInHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-06 14:37:58 +00:00
|
|
|
r := io.NewBinReaderFromBuf(resp)
|
2020-03-11 14:14:05 +00:00
|
|
|
h = new(block.Header)
|
2022-02-21 10:41:14 +00:00
|
|
|
h.StateRootEnabled = sr
|
2020-03-11 14:14:05 +00:00
|
|
|
h.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2021-02-07 19:01:22 +00:00
|
|
|
// GetBlockHeaderCount returns the number of headers in the main chain.
|
|
|
|
func (c *Client) GetBlockHeaderCount() (uint32, error) {
|
|
|
|
var resp uint32
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getblockheadercount", nil, &resp); err != nil {
|
2021-02-07 19:01:22 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockHeaderVerbose returns the corresponding block header information from a Json format string
|
2022-09-07 16:09:53 +00:00
|
|
|
// according to the specified script hash. In-header stateroot option must be
|
|
|
|
// initialized with Init before calling this method.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetBlockHeaderVerbose(hash util.Uint256) (*result.Header, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE(), 1}
|
2020-03-11 14:14:05 +00:00
|
|
|
resp = &result.Header{}
|
|
|
|
)
|
2020-03-24 17:11:49 +00:00
|
|
|
if err := c.performRequest("getblockheader", params, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetBlockSysFee returns the system fees of the block based on the specified index.
|
2020-12-01 08:40:58 +00:00
|
|
|
func (c *Client) GetBlockSysFee(index uint32) (fixedn.Fixed8, error) {
|
2020-03-11 14:14:05 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{index}
|
2020-12-01 08:40:58 +00:00
|
|
|
resp fixedn.Fixed8
|
2020-03-11 14:14:05 +00:00
|
|
|
)
|
|
|
|
if err := c.performRequest("getblocksysfee", params, &resp); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetConnectionCount returns the current number of the connections for the node.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetConnectionCount() (int, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp int
|
|
|
|
|
|
|
|
if err := c.performRequest("getconnectioncount", nil, &resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetCommittee returns the current public keys of NEO nodes in the committee.
|
2020-09-21 12:34:04 +00:00
|
|
|
func (c *Client) GetCommittee() (keys.PublicKeys, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = new(keys.PublicKeys)
|
|
|
|
|
|
|
|
if err := c.performRequest("getcommittee", nil, resp); err != nil {
|
2020-09-21 12:34:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return *resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetContractStateByHash queries contract information according to the contract script hash.
|
2020-09-25 16:32:53 +00:00
|
|
|
func (c *Client) GetContractStateByHash(hash util.Uint160) (*state.Contract, error) {
|
|
|
|
return c.getContractState(hash.StringLE())
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetContractStateByAddressOrName queries contract information according to the contract address or name.
|
2020-09-25 16:32:53 +00:00
|
|
|
func (c *Client) GetContractStateByAddressOrName(addressOrName string) (*state.Contract, error) {
|
|
|
|
return c.getContractState(addressOrName)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetContractStateByID queries contract information according to the contract ID.
|
2020-09-25 16:32:53 +00:00
|
|
|
func (c *Client) GetContractStateByID(id int32) (*state.Contract, error) {
|
|
|
|
return c.getContractState(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getContractState is an internal representation of GetContractStateBy* methods.
|
|
|
|
func (c *Client) getContractState(param interface{}) (*state.Contract, error) {
|
2020-03-11 14:14:05 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{param}
|
2020-06-09 09:12:56 +00:00
|
|
|
resp = &state.Contract{}
|
2020-03-11 14:14:05 +00:00
|
|
|
)
|
|
|
|
if err := c.performRequest("getcontractstate", params, resp); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 09:37:03 +00:00
|
|
|
// GetNativeContracts queries information about native contracts.
|
|
|
|
func (c *Client) GetNativeContracts() ([]state.NativeContract, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp []state.NativeContract
|
|
|
|
if err := c.performRequest("getnativecontracts", nil, &resp); err != nil {
|
2021-02-11 09:37:03 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
2022-02-22 12:39:42 +00:00
|
|
|
|
|
|
|
// Update native contract hashes.
|
|
|
|
c.cacheLock.Lock()
|
|
|
|
for _, cs := range resp {
|
|
|
|
c.cache.nativeHashes[cs.Manifest.Name] = cs.Hash
|
|
|
|
}
|
|
|
|
c.cacheLock.Unlock()
|
|
|
|
|
2021-02-11 09:37:03 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:08:10 +00:00
|
|
|
// GetNEP11Balances is a wrapper for getnep11balances RPC.
|
|
|
|
func (c *Client) GetNEP11Balances(address util.Uint160) (*result.NEP11Balances, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{address.StringLE()}
|
2021-11-17 21:08:10 +00:00
|
|
|
resp := new(result.NEP11Balances)
|
|
|
|
if err := c.performRequest("getnep11balances", params, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
// GetNEP17Balances is a wrapper for getnep17balances RPC.
|
|
|
|
func (c *Client) GetNEP17Balances(address util.Uint160) (*result.NEP17Balances, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{address.StringLE()}
|
2020-11-24 08:14:25 +00:00
|
|
|
resp := new(result.NEP17Balances)
|
|
|
|
if err := c.performRequest("getnep17balances", params, resp); err != nil {
|
2020-03-05 11:50:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 21:08:10 +00:00
|
|
|
// GetNEP11Properties is a wrapper for getnep11properties RPC. We recommend using
|
2022-08-19 07:37:22 +00:00
|
|
|
// nep11 package and Properties method there to receive proper VM types and work with them.
|
2022-04-20 18:30:09 +00:00
|
|
|
// This method is provided mostly for the sake of completeness. For well-known
|
2021-11-17 21:08:10 +00:00
|
|
|
// attributes like "description", "image", "name" and "tokenURI" it returns strings,
|
2022-04-20 18:30:09 +00:00
|
|
|
// while for all others []byte (which can be nil).
|
2021-11-17 21:08:10 +00:00
|
|
|
func (c *Client) GetNEP11Properties(asset util.Uint160, token []byte) (map[string]interface{}, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{asset.StringLE(), hex.EncodeToString(token)}
|
2021-11-17 21:08:10 +00:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
if err := c.performRequest("getnep11properties", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range resp {
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
str, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("value is not a string")
|
|
|
|
}
|
|
|
|
if result.KnownNEP11Properties[k] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val, err := base64.StdEncoding.DecodeString(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp[k] = val
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNEP11Transfers is a wrapper for getnep11transfers RPC. Address parameter
|
2022-04-20 18:30:09 +00:00
|
|
|
// is mandatory, while all others are optional. Limit and page parameters are
|
2021-11-17 21:08:10 +00:00
|
|
|
// only supported by NeoGo servers and can only be specified with start and stop.
|
2021-11-18 14:00:48 +00:00
|
|
|
func (c *Client) GetNEP11Transfers(address util.Uint160, start, stop *uint64, limit, page *int) (*result.NEP11Transfers, error) {
|
2021-11-17 21:08:10 +00:00
|
|
|
params, err := packTransfersParams(address, start, stop, limit, page)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp := new(result.NEP11Transfers)
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getnep11transfers", params, resp); err != nil {
|
2021-11-17 21:08:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func packTransfersParams(address util.Uint160, start, stop *uint64, limit, page *int) ([]interface{}, error) {
|
|
|
|
params := []interface{}{address.StringLE()}
|
2020-09-14 19:12:49 +00:00
|
|
|
if start != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, *start)
|
2020-09-14 19:12:49 +00:00
|
|
|
if stop != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, *stop)
|
2020-09-14 19:12:49 +00:00
|
|
|
if limit != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, *limit)
|
2020-09-14 19:12:49 +00:00
|
|
|
if page != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, *page)
|
2020-09-14 19:12:49 +00:00
|
|
|
}
|
|
|
|
} else if page != nil {
|
|
|
|
return nil, errors.New("bad parameters")
|
|
|
|
}
|
|
|
|
} else if limit != nil || page != nil {
|
|
|
|
return nil, errors.New("bad parameters")
|
|
|
|
}
|
|
|
|
} else if stop != nil || limit != nil || page != nil {
|
|
|
|
return nil, errors.New("bad parameters")
|
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
return params, nil
|
2021-11-17 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNEP17Transfers is a wrapper for getnep17transfers RPC. Address parameter
|
2022-04-20 18:30:09 +00:00
|
|
|
// is mandatory while all the others are optional. Start and stop parameters
|
2021-11-17 21:08:10 +00:00
|
|
|
// are supported since neo-go 0.77.0 and limit and page since neo-go 0.78.0.
|
2022-04-20 18:30:09 +00:00
|
|
|
// These parameters are positional in the JSON-RPC call. For example, you can't specify the limit
|
|
|
|
// without specifying start/stop first.
|
2021-11-18 14:00:48 +00:00
|
|
|
func (c *Client) GetNEP17Transfers(address util.Uint160, start, stop *uint64, limit, page *int) (*result.NEP17Transfers, error) {
|
2021-11-17 21:08:10 +00:00
|
|
|
params, err := packTransfersParams(address, start, stop, limit, page)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-24 08:14:25 +00:00
|
|
|
resp := new(result.NEP17Transfers)
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getnep17transfers", params, resp); err != nil {
|
2020-03-05 12:16:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetPeers returns a list of the nodes that the node is currently connected to/disconnected from.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetPeers() (*result.GetPeers, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = &result.GetPeers{}
|
|
|
|
|
|
|
|
if err := c.performRequest("getpeers", nil, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetRawMemPool returns a list of unconfirmed transactions in the memory.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetRawMemPool() ([]util.Uint256, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = new([]util.Uint256)
|
|
|
|
|
|
|
|
if err := c.performRequest("getrawmempool", nil, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return *resp, err
|
|
|
|
}
|
|
|
|
return *resp, nil
|
|
|
|
}
|
|
|
|
|
2022-02-21 10:41:14 +00:00
|
|
|
// GetRawTransaction returns a transaction by hash.
|
2020-03-03 17:43:57 +00:00
|
|
|
func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE()}
|
2020-11-06 14:37:58 +00:00
|
|
|
resp []byte
|
2020-03-03 17:43:57 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = c.performRequest("getrawtransaction", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
tx, err := transaction.NewTransactionFromBytes(resp)
|
2020-04-13 09:02:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-03 17:43:57 +00:00
|
|
|
}
|
|
|
|
return tx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawTransactionVerbose returns a transaction wrapper with additional
|
2022-02-21 10:41:14 +00:00
|
|
|
// metadata by transaction's hash.
|
2020-03-23 14:31:28 +00:00
|
|
|
// NOTE: to get transaction.ID and transaction.Size, use t.Hash() and io.GetVarSize(t) respectively.
|
2020-03-03 17:43:57 +00:00
|
|
|
func (c *Client) GetRawTransactionVerbose(hash util.Uint256) (*result.TransactionOutputRaw, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE(), 1} // 1 for verbose.
|
2020-03-03 17:43:57 +00:00
|
|
|
resp = &result.TransactionOutputRaw{}
|
|
|
|
err error
|
|
|
|
)
|
2020-03-24 17:11:49 +00:00
|
|
|
if err = c.performRequest("getrawtransaction", params, resp); err != nil {
|
2020-03-03 17:43:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:03:37 +00:00
|
|
|
// GetState returns historical contract storage item state by the given stateroot,
|
|
|
|
// historical contract hash and historical item key.
|
|
|
|
func (c *Client) GetState(stateroot util.Uint256, historicalContractHash util.Uint160, historicalKey []byte) ([]byte, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{stateroot.StringLE(), historicalContractHash.StringLE(), historicalKey}
|
2021-10-07 09:03:37 +00:00
|
|
|
resp []byte
|
|
|
|
)
|
|
|
|
if err := c.performRequest("getstate", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 13:56:27 +00:00
|
|
|
// FindStates returns historical contract storage item states by the given stateroot,
|
2022-04-20 18:30:09 +00:00
|
|
|
// historical contract hash and historical prefix. If `start` path is specified, items
|
2021-10-07 13:56:27 +00:00
|
|
|
// starting from `start` path are being returned (excluding item located at the start path).
|
2022-04-20 18:30:09 +00:00
|
|
|
// If `maxCount` specified, the maximum number of items to be returned equals to `maxCount`.
|
2021-10-07 13:56:27 +00:00
|
|
|
func (c *Client) FindStates(stateroot util.Uint256, historicalContractHash util.Uint160, historicalPrefix []byte,
|
|
|
|
start []byte, maxCount *int) (result.FindStates, error) {
|
2022-01-18 11:56:17 +00:00
|
|
|
if historicalPrefix == nil {
|
|
|
|
historicalPrefix = []byte{}
|
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{stateroot.StringLE(), historicalContractHash.StringLE(), historicalPrefix}
|
2021-10-07 13:56:27 +00:00
|
|
|
resp result.FindStates
|
|
|
|
)
|
2022-01-18 11:56:17 +00:00
|
|
|
if start == nil && maxCount != nil {
|
|
|
|
start = []byte{}
|
|
|
|
}
|
|
|
|
if start != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, start)
|
2022-01-18 11:56:17 +00:00
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
if maxCount != nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
params = append(params, *maxCount)
|
2021-10-07 13:56:27 +00:00
|
|
|
}
|
|
|
|
if err := c.performRequest("findstates", params, &resp); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetStateRootByHeight returns the state root for the specified height.
|
2022-01-17 08:59:13 +00:00
|
|
|
func (c *Client) GetStateRootByHeight(height uint32) (*state.MPTRoot, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getStateRoot(height)
|
2022-01-17 08:59:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetStateRootByBlockHash returns the state root for the block with the specified hash.
|
2022-01-17 08:59:13 +00:00
|
|
|
func (c *Client) GetStateRootByBlockHash(hash util.Uint256) (*state.MPTRoot, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getStateRoot(hash)
|
2022-01-17 08:59:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) getStateRoot(param interface{}) (*state.MPTRoot, error) {
|
2022-01-17 08:59:13 +00:00
|
|
|
var resp = new(state.MPTRoot)
|
2022-07-07 17:03:10 +00:00
|
|
|
if err := c.performRequest("getstateroot", []interface{}{param}, resp); err != nil {
|
2022-01-17 08:59:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetStateHeight returns the current validated and local node state height.
|
2021-07-22 17:02:59 +00:00
|
|
|
func (c *Client) GetStateHeight() (*result.StateHeight, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = new(result.StateHeight)
|
|
|
|
|
|
|
|
if err := c.performRequest("getstateheight", nil, resp); err != nil {
|
2021-07-22 17:02:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetStorageByID returns the stored value according to the contract ID and the stored key.
|
2020-06-23 14:27:23 +00:00
|
|
|
func (c *Client) GetStorageByID(id int32, key []byte) ([]byte, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getStorage([]interface{}{id, key})
|
2020-06-23 14:27:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetStorageByHash returns the stored value according to the contract script hash and the stored key.
|
2020-06-23 14:27:23 +00:00
|
|
|
func (c *Client) GetStorageByHash(hash util.Uint160, key []byte) ([]byte, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
return c.getStorage([]interface{}{hash.StringLE(), key})
|
2020-06-23 14:27:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) getStorage(params []interface{}) ([]byte, error) {
|
2021-02-07 15:27:19 +00:00
|
|
|
var resp []byte
|
2020-03-11 14:14:05 +00:00
|
|
|
if err := c.performRequest("getstorage", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-07 15:27:19 +00:00
|
|
|
return resp, nil
|
2020-03-11 14:14:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetTransactionHeight returns the block index where the transaction is found.
|
2020-03-11 14:14:05 +00:00
|
|
|
func (c *Client) GetTransactionHeight(hash util.Uint256) (uint32, error) {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{hash.StringLE()}
|
2020-03-11 14:14:05 +00:00
|
|
|
resp uint32
|
|
|
|
)
|
|
|
|
if err := c.performRequest("gettransactionheight", params, &resp); err != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetUnclaimedGas returns the unclaimed GAS amount for the specified address.
|
2020-07-09 14:25:26 +00:00
|
|
|
func (c *Client) GetUnclaimedGas(address string) (result.UnclaimedGas, error) {
|
2020-03-11 14:14:05 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{address}
|
2020-07-09 14:25:26 +00:00
|
|
|
resp result.UnclaimedGas
|
2020-03-11 14:14:05 +00:00
|
|
|
)
|
2020-06-01 20:27:03 +00:00
|
|
|
if err := c.performRequest("getunclaimedgas", params, &resp); err != nil {
|
2020-07-09 14:25:26 +00:00
|
|
|
return resp, err
|
2020-06-01 20:27:03 +00:00
|
|
|
}
|
2020-07-09 14:25:26 +00:00
|
|
|
return resp, nil
|
2020-03-11 14:14:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 13:02:03 +00:00
|
|
|
// GetCandidates returns the current list of NEO candidate node with voting data and
|
|
|
|
// validator status.
|
|
|
|
func (c *Client) GetCandidates() ([]result.Candidate, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = new([]result.Candidate)
|
|
|
|
|
|
|
|
if err := c.performRequest("getcandidates", nil, resp); err != nil {
|
2022-07-01 13:02:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return *resp, nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:34:43 +00:00
|
|
|
// GetNextBlockValidators returns the current NEO consensus nodes information and voting data.
|
2020-10-01 12:26:54 +00:00
|
|
|
func (c *Client) GetNextBlockValidators() ([]result.Validator, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = new([]result.Validator)
|
|
|
|
|
|
|
|
if err := c.performRequest("getnextblockvalidators", nil, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return *resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion returns the version information about the queried node.
|
|
|
|
func (c *Client) GetVersion() (*result.Version, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var resp = &result.Version{}
|
|
|
|
|
|
|
|
if err := c.performRequest("getversion", nil, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:01:10 +00:00
|
|
|
// InvokeScript returns the result of the given script after running it true the VM.
|
|
|
|
// NOTE: This is a test invoke and will not affect the blockchain.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *Client) InvokeScript(script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{script}
|
2020-07-29 16:57:38 +00:00
|
|
|
return c.invokeSomething("invokescript", p, signers)
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 15:13:08 +00:00
|
|
|
// InvokeScriptAtHeight returns the result of the given script after running it
|
|
|
|
// true the VM using the provided chain state retrieved from the specified chain
|
|
|
|
// height.
|
|
|
|
// NOTE: This is a test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeScriptAtHeight(height uint32, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{height, script}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokescripthistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeScriptAtBlock returns the result of the given script after running it
|
|
|
|
// true the VM using the provided chain state retrieved from the specified block
|
|
|
|
// hash.
|
|
|
|
// NOTE: This is a test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeScriptAtBlock(blockHash util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{blockHash.StringLE(), script}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokescripthistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeScriptWithState returns the result of the given script after running it
|
|
|
|
// true the VM using the provided chain state retrieved from the specified
|
|
|
|
// stateroot hash.
|
|
|
|
// NOTE: This is a test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeScriptWithState(stateroot util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{stateroot.StringLE(), script}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokescripthistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// InvokeFunction returns the results after calling the smart contract scripthash
|
2018-03-25 16:21:00 +00:00
|
|
|
// with the given operation and parameters.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
2020-07-29 16:57:38 +00:00
|
|
|
func (c *Client) InvokeFunction(contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{contract.StringLE(), operation, params}
|
2020-07-29 16:57:38 +00:00
|
|
|
return c.invokeSomething("invokefunction", p, signers)
|
2020-07-02 12:53:39 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 15:13:08 +00:00
|
|
|
// InvokeFunctionAtHeight returns the results after calling the smart contract
|
|
|
|
// with the given operation and parameters at the given blockchain state
|
|
|
|
// specified by the blockchain height.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeFunctionAtHeight(height uint32, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{height, contract.StringLE(), operation, params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokefunctionhistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeFunctionAtBlock returns the results after calling the smart contract
|
|
|
|
// with the given operation and parameters at given the blockchain state
|
|
|
|
// specified by the block hash.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeFunctionAtBlock(blockHash util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{blockHash.StringLE(), contract.StringLE(), operation, params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokefunctionhistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeFunctionWithState returns the results after calling the smart contract
|
|
|
|
// with the given operation and parameters at the given blockchain state defined
|
|
|
|
// by the specified stateroot hash.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeFunctionWithState(stateroot util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{stateroot.StringLE(), contract.StringLE(), operation, params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokefunctionhistoric", p, signers)
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:23:39 +00:00
|
|
|
// InvokeContractVerify returns the results after calling `verify` method of the smart contract
|
|
|
|
// with the given parameters under verification trigger type.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeContractVerify(contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{contract.StringLE(), params}
|
2020-12-14 12:23:39 +00:00
|
|
|
return c.invokeSomething("invokecontractverify", p, signers, witnesses...)
|
|
|
|
}
|
|
|
|
|
2022-04-07 15:13:08 +00:00
|
|
|
// InvokeContractVerifyAtHeight returns the results after calling `verify` method
|
|
|
|
// of the smart contract with the given parameters under verification trigger type
|
|
|
|
// at the blockchain state specified by the blockchain height.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeContractVerifyAtHeight(height uint32, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{height, contract.StringLE(), params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeContractVerifyAtBlock returns the results after calling `verify` method
|
|
|
|
// of the smart contract with the given parameters under verification trigger type
|
|
|
|
// at the blockchain state specified by the block hash.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeContractVerifyAtBlock(blockHash util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{blockHash.StringLE(), contract.StringLE(), params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeContractVerifyWithState returns the results after calling `verify` method
|
|
|
|
// of the smart contract with the given parameters under verification trigger type
|
|
|
|
// at the blockchain state specified by the stateroot hash.
|
|
|
|
// NOTE: this is test invoke and will not affect the blockchain.
|
|
|
|
func (c *Client) InvokeContractVerifyWithState(stateroot util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
2022-07-07 17:03:10 +00:00
|
|
|
var p = []interface{}{stateroot.StringLE(), contract.StringLE(), params}
|
2022-04-07 15:13:08 +00:00
|
|
|
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// invokeSomething is an inner wrapper for Invoke* functions.
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) invokeSomething(method string, p []interface{}, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
|
2020-07-02 12:53:39 +00:00
|
|
|
var resp = new(result.Invoke)
|
2020-07-29 16:57:38 +00:00
|
|
|
if signers != nil {
|
2020-12-14 12:23:39 +00:00
|
|
|
if witnesses == nil {
|
2022-07-07 17:03:10 +00:00
|
|
|
p = append(p, signers)
|
2020-12-14 12:23:39 +00:00
|
|
|
} else {
|
|
|
|
if len(witnesses) != len(signers) {
|
|
|
|
return nil, fmt.Errorf("number of witnesses should match number of signers, got %d vs %d", len(witnesses), len(signers))
|
|
|
|
}
|
2022-07-22 16:09:29 +00:00
|
|
|
signersWithWitnesses := make([]neorpc.SignerWithWitness, len(signers))
|
2020-12-14 12:23:39 +00:00
|
|
|
for i := range signersWithWitnesses {
|
2022-07-22 16:09:29 +00:00
|
|
|
signersWithWitnesses[i] = neorpc.SignerWithWitness{
|
2020-12-14 12:23:39 +00:00
|
|
|
Signer: signers[i],
|
|
|
|
Witness: witnesses[i],
|
|
|
|
}
|
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
p = append(p, signersWithWitnesses)
|
2020-12-14 12:23:39 +00:00
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2020-07-02 12:53:39 +00:00
|
|
|
if err := c.performRequest(method, p, resp); err != nil {
|
2018-11-26 15:57:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 07:58:46 +00:00
|
|
|
// SendRawTransaction broadcasts a transaction over the NEO network.
|
2018-03-05 08:53:09 +00:00
|
|
|
// The given hex string needs to be signed with a keypair.
|
|
|
|
// When the result of the response object is true, the TX has successfully
|
|
|
|
// been broadcasted to the network.
|
2020-07-21 07:31:45 +00:00
|
|
|
func (c *Client) SendRawTransaction(rawTX *transaction.Transaction) (util.Uint256, error) {
|
2018-03-05 08:53:09 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{rawTX.Bytes()}
|
2020-07-21 07:31:45 +00:00
|
|
|
resp = new(result.RelayResult)
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
2020-07-21 07:31:45 +00:00
|
|
|
if err := c.performRequest("sendrawtransaction", params, resp); err != nil {
|
|
|
|
return util.Uint256{}, err
|
2020-02-20 18:08:22 +00:00
|
|
|
}
|
2020-07-21 07:31:45 +00:00
|
|
|
return resp.Hash, nil
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2020-03-11 14:14:05 +00:00
|
|
|
// SubmitBlock broadcasts a raw block over the NEO network.
|
2020-07-21 07:41:18 +00:00
|
|
|
func (c *Client) SubmitBlock(b block.Block) (util.Uint256, error) {
|
2020-03-11 14:14:05 +00:00
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params []interface{}
|
2020-07-21 07:41:18 +00:00
|
|
|
resp = new(result.RelayResult)
|
2020-03-11 14:14:05 +00:00
|
|
|
)
|
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
b.EncodeBinary(buf.BinWriter)
|
|
|
|
if err := buf.Err; err != nil {
|
2020-07-21 07:41:18 +00:00
|
|
|
return util.Uint256{}, err
|
2020-03-11 14:14:05 +00:00
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{buf.Bytes()}
|
2020-03-11 14:14:05 +00:00
|
|
|
|
2020-07-21 07:41:18 +00:00
|
|
|
if err := c.performRequest("submitblock", params, resp); err != nil {
|
|
|
|
return util.Uint256{}, err
|
2020-03-11 14:14:05 +00:00
|
|
|
}
|
2020-07-21 07:41:18 +00:00
|
|
|
return resp.Hash, nil
|
2020-03-11 14:14:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SubmitRawOracleResponse submits a raw oracle response to the oracle node.
|
2020-09-28 11:58:04 +00:00
|
|
|
// Raw params are used to avoid excessive marshalling.
|
2022-07-07 17:03:10 +00:00
|
|
|
func (c *Client) SubmitRawOracleResponse(ps []interface{}) error {
|
2020-09-28 11:58:04 +00:00
|
|
|
return c.performRequest("submitoracleresponse", ps, new(result.RelayResult))
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignAndPushInvocationTx signs and pushes the given script as an invocation
|
|
|
|
// transaction using the given wif to sign it and the given cosigners to cosign it if
|
2021-04-20 09:04:25 +00:00
|
|
|
// possible. It spends the amount of gas specified. It returns a hash of the
|
|
|
|
// invocation transaction and an error. If one of the cosigners accounts is
|
2022-04-20 18:30:09 +00:00
|
|
|
// neither contract-based nor unlocked, an error is returned.
|
2022-08-07 19:21:03 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use actor.Actor API, this method will be removed in future
|
|
|
|
// versions.
|
2021-03-02 12:43:09 +00:00
|
|
|
func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sysfee int64, netfee fixedn.Fixed8, cosigners []SignerAccount) (util.Uint256, error) {
|
2021-03-02 10:58:53 +00:00
|
|
|
tx, err := c.CreateTxFromScript(script, acc, sysfee, int64(netfee), cosigners)
|
rpc/client: handle tx creation error
Fix
neo-go contract invokefunction -w wallets/neofs1.json -a NXnzw3J9VvKXjM1BPAJK4QUpTtEQu4TpU6 -r http://main_chain.neofs.devenv:30333 af5dc5f7e6a6efc64d679098f328027591a2e518 deposit 12b97a2206ae4b10c7e0194b7b655c32cc912057 int:50 bytes: -- 12b97a2206ae4b10c7e0194b7b655c32cc912057
Enter account NXnzw3J9VvKXjM1BPAJK4QUpTtEQu4TpU6 password >
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x80 pc=0x934bbc]
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/core/transaction.(*Transaction).GetSignedPart(0x0, 0x1028200, 0xc00013c000, 0xc0001a2680)
github.com/nspcc-dev/neo-go/pkg/core/transaction/transaction.go:208 +0x13c
github.com/nspcc-dev/neo-go/pkg/wallet.(*Account).SignTx(0xc0000ef880, 0x0, 0x40, 0x80)
github.com/nspcc-dev/neo-go/pkg/wallet/account.go:105 +0x61
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).SignAndPushInvocationTx(0xc0001a0180, 0xc00008e280, 0x40, 0x80, 0xc0000ef880, 0xc140c6, 0x0, 0xc000218b90, 0x1, 0x1, ...)
github.com/nspcc-dev/neo-go/pkg/rpc/client/rpc.go:449 +0xfe
github.com/nspcc-dev/neo-go/cli/smartcontract.invokeInternal(0xc0000acb00, 0x1, 0x0, 0x0)
github.com/nspcc-dev/neo-go/cli/smartcontract/smart_contract.go:493 +0xa4d
github.com/nspcc-dev/neo-go/cli/smartcontract.invokeFunction(0xc0000acb00, 0x10100, 0xc0000acb00)
github.com/nspcc-dev/neo-go/cli/smartcontract/smart_contract.go:418 +0x30
github.com/urfave/cli.HandleAction(0xc76cc0, 0xf175d0, 0xc0000acb00, 0xc00007ef00, 0x0)
github.com/urfave/cli@v1.20.0/app.go:490 +0xc8
github.com/urfave/cli.Command.Run(0xdbcc3c, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd9659, 0x2a, 0xde821b, ...)
github.com/urfave/cli@v1.20.0/command.go:210 +0x9e8
github.com/urfave/cli.(*App).RunAsSubcommand(0xc00021e000, 0xc0000ac840, 0x0, 0x0)
github.com/urfave/cli@v1.20.0/app.go:379 +0x88b
github.com/urfave/cli.Command.startApp(0xdb803f, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd7472, 0x28, 0x0, ...)
github.com/urfave/cli@v1.20.0/command.go:298 +0x81a
github.com/urfave/cli.Command.Run(0xdb803f, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd7472, 0x28, 0x0, ...)
github.com/urfave/cli@v1.20.0/command.go:98 +0x1219
github.com/urfave/cli.(*App).Run(0xc00009bd40, 0xc000030100, 0x10, 0x10, 0x0, 0x0)
github.com/urfave/cli@v1.20.0/app.go:255 +0x741
main.main()
command-line-arguments/main.go:18 +0x4b
2020-09-28 13:53:35 +00:00
|
|
|
if err != nil {
|
2021-04-21 10:44:37 +00:00
|
|
|
return util.Uint256{}, fmt.Errorf("failed to create tx: %w", err)
|
rpc/client: handle tx creation error
Fix
neo-go contract invokefunction -w wallets/neofs1.json -a NXnzw3J9VvKXjM1BPAJK4QUpTtEQu4TpU6 -r http://main_chain.neofs.devenv:30333 af5dc5f7e6a6efc64d679098f328027591a2e518 deposit 12b97a2206ae4b10c7e0194b7b655c32cc912057 int:50 bytes: -- 12b97a2206ae4b10c7e0194b7b655c32cc912057
Enter account NXnzw3J9VvKXjM1BPAJK4QUpTtEQu4TpU6 password >
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x80 pc=0x934bbc]
goroutine 1 [running]:
github.com/nspcc-dev/neo-go/pkg/core/transaction.(*Transaction).GetSignedPart(0x0, 0x1028200, 0xc00013c000, 0xc0001a2680)
github.com/nspcc-dev/neo-go/pkg/core/transaction/transaction.go:208 +0x13c
github.com/nspcc-dev/neo-go/pkg/wallet.(*Account).SignTx(0xc0000ef880, 0x0, 0x40, 0x80)
github.com/nspcc-dev/neo-go/pkg/wallet/account.go:105 +0x61
github.com/nspcc-dev/neo-go/pkg/rpc/client.(*Client).SignAndPushInvocationTx(0xc0001a0180, 0xc00008e280, 0x40, 0x80, 0xc0000ef880, 0xc140c6, 0x0, 0xc000218b90, 0x1, 0x1, ...)
github.com/nspcc-dev/neo-go/pkg/rpc/client/rpc.go:449 +0xfe
github.com/nspcc-dev/neo-go/cli/smartcontract.invokeInternal(0xc0000acb00, 0x1, 0x0, 0x0)
github.com/nspcc-dev/neo-go/cli/smartcontract/smart_contract.go:493 +0xa4d
github.com/nspcc-dev/neo-go/cli/smartcontract.invokeFunction(0xc0000acb00, 0x10100, 0xc0000acb00)
github.com/nspcc-dev/neo-go/cli/smartcontract/smart_contract.go:418 +0x30
github.com/urfave/cli.HandleAction(0xc76cc0, 0xf175d0, 0xc0000acb00, 0xc00007ef00, 0x0)
github.com/urfave/cli@v1.20.0/app.go:490 +0xc8
github.com/urfave/cli.Command.Run(0xdbcc3c, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd9659, 0x2a, 0xde821b, ...)
github.com/urfave/cli@v1.20.0/command.go:210 +0x9e8
github.com/urfave/cli.(*App).RunAsSubcommand(0xc00021e000, 0xc0000ac840, 0x0, 0x0)
github.com/urfave/cli@v1.20.0/app.go:379 +0x88b
github.com/urfave/cli.Command.startApp(0xdb803f, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd7472, 0x28, 0x0, ...)
github.com/urfave/cli@v1.20.0/command.go:298 +0x81a
github.com/urfave/cli.Command.Run(0xdb803f, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd7472, 0x28, 0x0, ...)
github.com/urfave/cli@v1.20.0/command.go:98 +0x1219
github.com/urfave/cli.(*App).Run(0xc00009bd40, 0xc000030100, 0x10, 0x10, 0x0, 0x0)
github.com/urfave/cli@v1.20.0/app.go:255 +0x741
main.main()
command-line-arguments/main.go:18 +0x4b
2020-09-28 13:53:35 +00:00
|
|
|
}
|
2021-04-21 10:44:37 +00:00
|
|
|
return c.SignAndPushTx(tx, acc, cosigners)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignAndPushTx signs the given transaction using the given wif and cosigners and pushes
|
2021-04-21 10:44:37 +00:00
|
|
|
// it to the chain. It returns a hash of the transaction and an error. If one of
|
2022-04-20 18:30:09 +00:00
|
|
|
// the cosigners accounts is neither contract-based nor unlocked, an error is
|
2021-04-21 10:44:37 +00:00
|
|
|
// returned.
|
2022-08-07 19:21:03 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use actor.Actor API, this method will be removed in future
|
|
|
|
// versions.
|
2021-04-21 10:44:37 +00:00
|
|
|
func (c *Client) SignAndPushTx(tx *transaction.Transaction, acc *wallet.Account, cosigners []SignerAccount) (util.Uint256, error) {
|
|
|
|
var (
|
|
|
|
txHash util.Uint256
|
|
|
|
err error
|
|
|
|
)
|
2022-02-21 10:41:14 +00:00
|
|
|
m, err := c.GetNetwork()
|
|
|
|
if err != nil {
|
|
|
|
return txHash, fmt.Errorf("failed to sign tx: %w", err)
|
|
|
|
}
|
|
|
|
if err = acc.SignTx(m, tx); err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return txHash, fmt.Errorf("failed to sign tx: %w", err)
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2021-04-20 09:04:25 +00:00
|
|
|
// try to add witnesses for the rest of the signers
|
|
|
|
for i, signer := range tx.Signers[1:] {
|
|
|
|
var isOk bool
|
|
|
|
for _, cosigner := range cosigners {
|
|
|
|
if signer.Account == cosigner.Signer.Account {
|
2022-02-21 10:41:14 +00:00
|
|
|
err = cosigner.Account.SignTx(m, tx)
|
2021-04-20 09:04:25 +00:00
|
|
|
if err != nil { // then account is non-contract-based and locked, but let's provide more detailed error
|
|
|
|
if paramNum := len(cosigner.Account.Contract.Parameters); paramNum != 0 && cosigner.Account.Contract.Deployed {
|
|
|
|
return txHash, fmt.Errorf("failed to add contract-based witness for signer #%d (%s): "+
|
|
|
|
"%d parameters must be provided to construct invocation script", i, address.Uint160ToString(signer.Account), paramNum)
|
|
|
|
}
|
|
|
|
return txHash, fmt.Errorf("failed to add witness for signer #%d (%s): account should be unlocked to add the signature. "+
|
|
|
|
"Store partially-signed transaction and then use 'wallet sign' command to cosign it", i, address.Uint160ToString(signer.Account))
|
|
|
|
}
|
|
|
|
isOk = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isOk {
|
|
|
|
return txHash, fmt.Errorf("failed to add witness for signer #%d (%s): account wasn't provided", i, address.Uint160ToString(signer.Account))
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 13:07:43 +00:00
|
|
|
txHash = tx.Hash()
|
2020-07-21 07:31:45 +00:00
|
|
|
actualHash, err := c.SendRawTransaction(tx)
|
2019-11-20 13:07:43 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return txHash, fmt.Errorf("failed to send tx: %w", err)
|
2019-11-20 13:07:43 +00:00
|
|
|
}
|
2020-07-21 07:31:45 +00:00
|
|
|
if !actualHash.Equals(txHash) {
|
|
|
|
return actualHash, fmt.Errorf("sent and actual tx hashes mismatch:\n\tsent: %v\n\tactual: %v", txHash.StringLE(), actualHash.StringLE())
|
|
|
|
}
|
2019-11-20 13:07:43 +00:00
|
|
|
return txHash, nil
|
|
|
|
}
|
2020-03-11 14:14:05 +00:00
|
|
|
|
2021-03-02 12:43:09 +00:00
|
|
|
// getSigners returns an array of transaction signers and corresponding accounts from
|
|
|
|
// given sender and cosigners. If cosigners list already contains sender, the sender
|
|
|
|
// will be placed at the start of the list.
|
|
|
|
func getSigners(sender *wallet.Account, cosigners []SignerAccount) ([]transaction.Signer, []*wallet.Account, error) {
|
|
|
|
var (
|
|
|
|
signers []transaction.Signer
|
|
|
|
accounts []*wallet.Account
|
|
|
|
)
|
2022-09-01 16:04:47 +00:00
|
|
|
from := sender.ScriptHash()
|
2020-07-29 16:57:38 +00:00
|
|
|
s := transaction.Signer{
|
2021-03-02 12:43:09 +00:00
|
|
|
Account: from,
|
2020-10-01 12:26:51 +00:00
|
|
|
Scopes: transaction.None,
|
2020-07-29 16:57:38 +00:00
|
|
|
}
|
2021-03-02 12:43:09 +00:00
|
|
|
for _, c := range cosigners {
|
|
|
|
if c.Signer.Account == from {
|
2021-11-22 09:44:42 +00:00
|
|
|
s = c.Signer
|
2021-03-02 12:43:09 +00:00
|
|
|
continue
|
2020-07-29 16:57:38 +00:00
|
|
|
}
|
2021-03-02 12:43:09 +00:00
|
|
|
signers = append(signers, c.Signer)
|
|
|
|
accounts = append(accounts, c.Account)
|
2020-07-29 16:57:38 +00:00
|
|
|
}
|
2021-03-02 12:43:09 +00:00
|
|
|
signers = append([]transaction.Signer{s}, signers...)
|
|
|
|
accounts = append([]*wallet.Account{sender}, accounts...)
|
|
|
|
return signers, accounts, nil
|
2020-07-29 16:57:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SignAndPushP2PNotaryRequest creates and pushes a P2PNotary request constructed from the main
|
|
|
|
// and fallback transactions using the given wif to sign it. It returns the request and an error.
|
2021-02-08 10:43:11 +00:00
|
|
|
// Fallback transaction is constructed from the given script using the amount of gas specified.
|
|
|
|
// For successful fallback transaction validation at least 2*transaction.NotaryServiceFeePerKey
|
2022-04-20 18:30:09 +00:00
|
|
|
// GAS should be deposited to the Notary contract.
|
|
|
|
// Main transaction should be constructed by the user. Several rules should be met for
|
2021-02-08 10:43:11 +00:00
|
|
|
// successful main transaction acceptance:
|
2022-08-08 10:23:21 +00:00
|
|
|
// 1. Native Notary contract should be a signer of the main transaction.
|
|
|
|
// 2. Notary signer should have None scope.
|
|
|
|
// 3. Main transaction should have dummy contract witness for Notary signer.
|
|
|
|
// 4. Main transaction should have NotaryAssisted attribute with NKeys specified.
|
|
|
|
// 5. NotaryAssisted attribute and dummy Notary witness (as long as the other incomplete witnesses)
|
|
|
|
// should be paid for. Use CalculateNotaryWitness to calculate the amount of network fee to pay
|
|
|
|
// for the attribute and Notary witness.
|
|
|
|
// 6. Main transaction either shouldn't have all witnesses attached (in this case none of them
|
|
|
|
// can be multisignature), or it only should have a partial multisignature.
|
|
|
|
//
|
2021-02-08 10:43:11 +00:00
|
|
|
// Note: client should be initialized before SignAndPushP2PNotaryRequest call.
|
2022-08-26 15:10:58 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use Actor from the notary subpackage. This method will be
|
|
|
|
// deleted in future versions.
|
2021-02-08 10:43:11 +00:00
|
|
|
func (c *Client) SignAndPushP2PNotaryRequest(mainTx *transaction.Transaction, fallbackScript []byte, fallbackSysFee int64, fallbackNetFee int64, fallbackValidFor uint32, acc *wallet.Account) (*payload.P2PNotaryRequest, error) {
|
|
|
|
var err error
|
|
|
|
notaryHash, err := c.GetNativeContractHash(nativenames.Notary)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get native Notary hash: %w", err)
|
|
|
|
}
|
2022-09-01 16:04:47 +00:00
|
|
|
from := acc.ScriptHash()
|
2021-02-08 10:43:11 +00:00
|
|
|
signers := []transaction.Signer{{Account: notaryHash}, {Account: from}}
|
|
|
|
if fallbackSysFee < 0 {
|
|
|
|
result, err := c.InvokeScript(fallbackScript, signers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't add system fee to fallback transaction: %w", err)
|
|
|
|
}
|
|
|
|
if result.State != "HALT" {
|
|
|
|
return nil, fmt.Errorf("can't add system fee to fallback transaction: bad vm state %s due to an error: %s", result.State, result.FaultException)
|
|
|
|
}
|
|
|
|
fallbackSysFee = result.GasConsumed
|
|
|
|
}
|
|
|
|
|
|
|
|
maxNVBDelta, err := c.GetMaxNotValidBeforeDelta()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get MaxNotValidBeforeDelta")
|
|
|
|
}
|
|
|
|
if int64(fallbackValidFor) > maxNVBDelta {
|
|
|
|
return nil, fmt.Errorf("fallback transaction should be valid for not more than %d blocks", maxNVBDelta)
|
|
|
|
}
|
2021-03-25 16:18:01 +00:00
|
|
|
fallbackTx := transaction.New(fallbackScript, fallbackSysFee)
|
2021-02-08 10:43:11 +00:00
|
|
|
fallbackTx.Signers = signers
|
|
|
|
fallbackTx.ValidUntilBlock = mainTx.ValidUntilBlock
|
|
|
|
fallbackTx.Attributes = []transaction.Attribute{
|
|
|
|
{
|
|
|
|
Type: transaction.NotaryAssistedT,
|
|
|
|
Value: &transaction.NotaryAssisted{NKeys: 0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: transaction.NotValidBeforeT,
|
|
|
|
Value: &transaction.NotValidBefore{Height: fallbackTx.ValidUntilBlock - fallbackValidFor + 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: transaction.ConflictsT,
|
|
|
|
Value: &transaction.Conflicts{Hash: mainTx.Hash()},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fallbackTx.Scripts = []transaction.Witness{
|
|
|
|
{
|
|
|
|
InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, make([]byte, 64)...),
|
|
|
|
VerificationScript: []byte{},
|
|
|
|
},
|
2022-08-22 11:52:04 +00:00
|
|
|
{
|
|
|
|
InvocationScript: []byte{},
|
|
|
|
VerificationScript: acc.GetVerificationScript(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fallbackTx.NetworkFee, err = c.CalculateNetworkFee(fallbackTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to add network fee: %w", err)
|
2021-02-08 10:43:11 +00:00
|
|
|
}
|
2022-08-22 11:52:04 +00:00
|
|
|
fallbackTx.NetworkFee += fallbackNetFee
|
2022-02-21 10:41:14 +00:00
|
|
|
m, err := c.GetNetwork()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to sign fallback tx: %w", err)
|
|
|
|
}
|
|
|
|
if err = acc.SignTx(m, fallbackTx); err != nil {
|
2021-02-08 10:43:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to sign fallback tx: %w", err)
|
|
|
|
}
|
|
|
|
fallbackHash := fallbackTx.Hash()
|
|
|
|
req := &payload.P2PNotaryRequest{
|
|
|
|
MainTransaction: mainTx,
|
|
|
|
FallbackTransaction: fallbackTx,
|
|
|
|
}
|
|
|
|
req.Witness = transaction.Witness{
|
2022-09-01 17:42:42 +00:00
|
|
|
InvocationScript: append([]byte{byte(opcode.PUSHDATA1), 64}, acc.SignHashable(m, req)...),
|
2021-02-08 10:43:11 +00:00
|
|
|
VerificationScript: acc.GetVerificationScript(),
|
|
|
|
}
|
|
|
|
actualHash, err := c.SubmitP2PNotaryRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return req, fmt.Errorf("failed to submit notary request: %w", err)
|
|
|
|
}
|
|
|
|
if !actualHash.Equals(fallbackHash) {
|
|
|
|
return req, fmt.Errorf("sent and actual fallback tx hashes mismatch:\n\tsent: %v\n\tactual: %v", fallbackHash.StringLE(), actualHash.StringLE())
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateNotaryFee calculates network fee for one dummy Notary witness and NotaryAssisted attribute with NKeys specified.
|
|
|
|
// The result should be added to the transaction's net fee for successful verification.
|
2022-08-22 13:51:58 +00:00
|
|
|
//
|
|
|
|
// Deprecated: NeoGo calculatenetworkfee method handles notary fees as well since 0.99.3, so
|
|
|
|
// this method is just no longer needed and will be removed in future versions.
|
2021-02-08 10:43:11 +00:00
|
|
|
func (c *Client) CalculateNotaryFee(nKeys uint8) (int64, error) {
|
|
|
|
baseExecFee, err := c.GetExecFeeFactor()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get BaseExecFeeFactor: %w", err)
|
|
|
|
}
|
|
|
|
feePerByte, err := c.GetFeePerByte()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get FeePerByte: %w", err)
|
|
|
|
}
|
2022-03-01 10:10:54 +00:00
|
|
|
feePerKey, err := c.GetNotaryServiceFeePerKey()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to get NotaryServiceFeePerKey: %w", err)
|
|
|
|
}
|
|
|
|
return int64((nKeys+1))*feePerKey + // fee for NotaryAssisted attribute
|
2021-02-08 10:43:11 +00:00
|
|
|
fee.Opcode(baseExecFee, // Notary node witness
|
|
|
|
opcode.PUSHDATA1, opcode.RET, // invocation script
|
2021-02-15 13:40:44 +00:00
|
|
|
opcode.PUSH0, opcode.SYSCALL, opcode.RET) + // System.Contract.CallNative
|
2021-03-23 10:49:27 +00:00
|
|
|
nativeprices.NotaryVerificationPrice*baseExecFee + // Notary witness verification price
|
2021-02-08 10:43:11 +00:00
|
|
|
feePerByte*int64(io.GetVarSize(make([]byte, 66))) + // invocation script per-byte fee
|
|
|
|
feePerByte*int64(io.GetVarSize([]byte{})), // verification script per-byte fee
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitP2PNotaryRequest submits given P2PNotaryRequest payload to the RPC node.
|
|
|
|
func (c *Client) SubmitP2PNotaryRequest(req *payload.P2PNotaryRequest) (util.Uint256, error) {
|
|
|
|
var resp = new(result.RelayResult)
|
|
|
|
bytes, err := req.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("failed to encode request: %w", err)
|
|
|
|
}
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{bytes}
|
2021-02-08 10:43:11 +00:00
|
|
|
if err := c.performRequest("submitnotaryrequest", params, resp); err != nil {
|
|
|
|
return util.Uint256{}, err
|
|
|
|
}
|
|
|
|
return resp.Hash, nil
|
|
|
|
}
|
|
|
|
|
2020-03-11 14:14:05 +00:00
|
|
|
// ValidateAddress verifies that the address is a correct NEO address.
|
|
|
|
func (c *Client) ValidateAddress(address string) error {
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{address}
|
2020-03-11 14:14:05 +00:00
|
|
|
resp = &result.ValidateAddress{}
|
|
|
|
)
|
|
|
|
|
2020-03-24 17:11:49 +00:00
|
|
|
if err := c.performRequest("validateaddress", params, resp); err != nil {
|
2020-03-11 14:14:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp.IsValid {
|
|
|
|
return errors.New("validateaddress returned false")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-15 06:50:13 +00:00
|
|
|
|
|
|
|
// CalculateValidUntilBlock calculates ValidUntilBlock field for tx as
|
|
|
|
// current blockchain height + number of validators. Number of validators
|
2020-10-01 12:26:54 +00:00
|
|
|
// is the length of blockchain validators list got from GetNextBlockValidators()
|
2020-04-15 06:50:13 +00:00
|
|
|
// method. Validators count is being cached and updated every 100 blocks.
|
2022-08-02 09:40:12 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use (*Actor).CalculateValidUntilBlock. This method will be
|
|
|
|
// removed in future versions.
|
2020-04-15 06:50:13 +00:00
|
|
|
func (c *Client) CalculateValidUntilBlock() (uint32, error) {
|
|
|
|
var (
|
|
|
|
result uint32
|
|
|
|
validatorsCount uint32
|
|
|
|
)
|
|
|
|
blockCount, err := c.GetBlockCount()
|
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return result, fmt.Errorf("can't get block count: %w", err)
|
2020-04-15 06:50:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RLock()
|
2020-04-15 06:50:13 +00:00
|
|
|
if c.cache.calculateValidUntilBlock.expiresAt > blockCount {
|
|
|
|
validatorsCount = c.cache.calculateValidUntilBlock.validatorsCount
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RUnlock()
|
2020-04-15 06:50:13 +00:00
|
|
|
} else {
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RUnlock()
|
2020-10-01 12:26:54 +00:00
|
|
|
validators, err := c.GetNextBlockValidators()
|
2020-04-15 06:50:13 +00:00
|
|
|
if err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return result, fmt.Errorf("can't get validators: %w", err)
|
2020-04-15 06:50:13 +00:00
|
|
|
}
|
|
|
|
validatorsCount = uint32(len(validators))
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.Lock()
|
2020-04-15 06:50:13 +00:00
|
|
|
c.cache.calculateValidUntilBlock = calculateValidUntilBlockCache{
|
|
|
|
validatorsCount: validatorsCount,
|
|
|
|
expiresAt: blockCount + cacheTimeout,
|
|
|
|
}
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.Unlock()
|
2020-04-15 06:50:13 +00:00
|
|
|
}
|
2020-09-04 13:03:31 +00:00
|
|
|
return blockCount + validatorsCount + 1, nil
|
2020-04-15 06:50:13 +00:00
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
|
2020-07-06 07:49:24 +00:00
|
|
|
// AddNetworkFee adds network fee for each witness script and optional extra
|
2020-08-17 08:12:21 +00:00
|
|
|
// network fee to transaction. `accs` is an array signer's accounts.
|
2022-08-07 19:21:03 +00:00
|
|
|
//
|
|
|
|
// Deprecated: please use CalculateNetworkFee or actor.Actor. This method will
|
|
|
|
// be removed in future versions.
|
2020-08-17 08:12:21 +00:00
|
|
|
func (c *Client) AddNetworkFee(tx *transaction.Transaction, extraFee int64, accs ...*wallet.Account) error {
|
|
|
|
if len(tx.Signers) != len(accs) {
|
|
|
|
return errors.New("number of signers must match number of scripts")
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
2020-08-17 08:12:21 +00:00
|
|
|
size := io.GetVarSize(tx)
|
2020-12-14 09:18:59 +00:00
|
|
|
var ef int64
|
2020-08-17 08:12:21 +00:00
|
|
|
for i, cosigner := range tx.Signers {
|
2020-08-24 11:00:05 +00:00
|
|
|
if accs[i].Contract.Deployed {
|
2022-07-27 16:00:36 +00:00
|
|
|
res, err := c.InvokeContractVerify(cosigner.Account, []smartcontract.Parameter{}, tx.Signers)
|
2020-10-05 13:33:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to invoke verify: %w", err)
|
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
r, err := unwrap.Bool(res, err)
|
2021-03-22 09:21:48 +00:00
|
|
|
if err != nil {
|
2022-08-09 12:18:16 +00:00
|
|
|
return fmt.Errorf("signer #%d: %w", i, err)
|
2021-03-22 09:21:48 +00:00
|
|
|
}
|
2022-08-09 12:18:16 +00:00
|
|
|
if !r {
|
2021-03-22 09:21:48 +00:00
|
|
|
return fmt.Errorf("signer #%d: `verify` returned `false`", i)
|
2020-06-25 07:55:45 +00:00
|
|
|
}
|
2020-08-24 11:00:05 +00:00
|
|
|
tx.NetworkFee += res.GasConsumed
|
|
|
|
size += io.GetVarSize([]byte{}) * 2 // both scripts are empty
|
|
|
|
continue
|
2020-05-08 17:54:24 +00:00
|
|
|
}
|
2020-12-14 09:18:59 +00:00
|
|
|
|
|
|
|
if ef == 0 {
|
|
|
|
var err error
|
|
|
|
ef, err = c.GetExecFeeFactor()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get `ExecFeeFactor`: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
netFee, sizeDelta := fee.Calculate(ef, accs[i].Contract.Script)
|
2020-05-08 17:54:24 +00:00
|
|
|
tx.NetworkFee += netFee
|
|
|
|
size += sizeDelta
|
|
|
|
}
|
2020-06-16 12:46:28 +00:00
|
|
|
fee, err := c.GetFeePerByte()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-06 07:49:24 +00:00
|
|
|
tx.NetworkFee += int64(size)*fee + extraFee
|
2020-05-08 17:54:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-14 15:13:20 +00:00
|
|
|
|
2022-09-07 16:09:53 +00:00
|
|
|
// GetNetwork returns the network magic of the RPC node the client connected to. It
|
|
|
|
// requires Init to be done first, otherwise an error is returned.
|
2022-02-21 10:41:14 +00:00
|
|
|
func (c *Client) GetNetwork() (netmode.Magic, error) {
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RLock()
|
|
|
|
defer c.cacheLock.RUnlock()
|
|
|
|
|
2022-02-21 10:41:14 +00:00
|
|
|
if !c.cache.initDone {
|
|
|
|
return 0, errNetworkNotInitialized
|
|
|
|
}
|
|
|
|
return c.cache.network, nil
|
2020-10-14 15:13:20 +00:00
|
|
|
}
|
2020-09-25 16:32:53 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// StateRootInHeader returns true if the state root is contained in the block header.
|
2022-02-21 10:41:14 +00:00
|
|
|
// You should initialize Client cache with Init() before calling StateRootInHeader.
|
|
|
|
func (c *Client) StateRootInHeader() (bool, error) {
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RLock()
|
|
|
|
defer c.cacheLock.RUnlock()
|
|
|
|
|
2022-02-21 10:41:14 +00:00
|
|
|
if !c.cache.initDone {
|
|
|
|
return false, errNetworkNotInitialized
|
|
|
|
}
|
|
|
|
return c.cache.stateRootInHeader, nil
|
2020-11-17 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:25:04 +00:00
|
|
|
// GetNativeContractHash returns native contract hash by its name.
|
2020-09-25 16:32:53 +00:00
|
|
|
func (c *Client) GetNativeContractHash(name string) (util.Uint160, error) {
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RLock()
|
2020-12-13 18:25:04 +00:00
|
|
|
hash, ok := c.cache.nativeHashes[name]
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.RUnlock()
|
2020-09-25 16:32:53 +00:00
|
|
|
if ok {
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
cs, err := c.GetContractStateByAddressOrName(name)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.Lock()
|
2020-12-13 18:25:04 +00:00
|
|
|
c.cache.nativeHashes[name] = cs.Hash
|
2022-02-21 13:06:43 +00:00
|
|
|
c.cacheLock.Unlock()
|
2020-11-18 20:10:48 +00:00
|
|
|
return cs.Hash, nil
|
2020-09-25 16:32:53 +00:00
|
|
|
}
|
2022-06-15 18:23:29 +00:00
|
|
|
|
|
|
|
// TraverseIterator returns a set of iterator values (maxItemsCount at max) for
|
|
|
|
// the specified iterator and session. If result contains no elements, then either
|
|
|
|
// Iterator has no elements or session was expired and terminated by the server.
|
2022-07-06 14:08:53 +00:00
|
|
|
// If maxItemsCount is non-positive, then config.DefaultMaxIteratorResultItems
|
|
|
|
// iterator values will be returned using single `traverseiterator` call.
|
|
|
|
// Note that iterator session lifetime is restricted by the RPC-server
|
|
|
|
// configuration and is being reset each time iterator is accessed. If session
|
|
|
|
// won't be accessed within session expiration time, then it will be terminated
|
|
|
|
// by the RPC-server automatically.
|
2022-06-15 18:23:29 +00:00
|
|
|
func (c *Client) TraverseIterator(sessionID, iteratorID uuid.UUID, maxItemsCount int) ([]stackitem.Item, error) {
|
|
|
|
if maxItemsCount <= 0 {
|
|
|
|
maxItemsCount = config.DefaultMaxIteratorResultItems
|
|
|
|
}
|
|
|
|
var (
|
2022-07-07 17:03:10 +00:00
|
|
|
params = []interface{}{sessionID.String(), iteratorID.String(), maxItemsCount}
|
2022-07-06 14:08:53 +00:00
|
|
|
resp []json.RawMessage
|
2022-06-15 18:23:29 +00:00
|
|
|
)
|
2022-07-06 14:08:53 +00:00
|
|
|
if err := c.performRequest("traverseiterator", params, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := make([]stackitem.Item, len(resp))
|
|
|
|
for i, iBytes := range resp {
|
|
|
|
itm, err := stackitem.FromJSONWithTypes(iBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal %d-th iterator value: %w", i, err)
|
2022-06-15 18:23:29 +00:00
|
|
|
}
|
2022-07-06 14:08:53 +00:00
|
|
|
result[i] = itm
|
2022-06-15 18:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TerminateSession tries to terminate the specified session and returns `true` iff
|
|
|
|
// the specified session was found on server.
|
|
|
|
func (c *Client) TerminateSession(sessionID uuid.UUID) (bool, error) {
|
|
|
|
var resp bool
|
2022-07-07 17:03:10 +00:00
|
|
|
params := []interface{}{sessionID.String()}
|
2022-06-15 18:23:29 +00:00
|
|
|
if err := c.performRequest("terminatesession", params, &resp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|