github.com/bcskill/bcschain/v3@v3.4.9-beta2/goclient/goclient.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package goclient provides a client for the Ethereum RPC API.
    18  package goclient
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"errors"
    24  	"fmt"
    25  	"math/big"
    26  
    27  	"github.com/bcskill/bcschain/v3"
    28  	"github.com/bcskill/bcschain/v3/common"
    29  	"github.com/bcskill/bcschain/v3/common/hexutil"
    30  	"github.com/bcskill/bcschain/v3/consensus/clique"
    31  	"github.com/bcskill/bcschain/v3/core/types"
    32  	"github.com/bcskill/bcschain/v3/rlp"
    33  	"github.com/bcskill/bcschain/v3/rpc"
    34  )
    35  
    36  // Client defines typed wrappers for the Ethereum RPC API.
    37  type Client struct {
    38  	c *rpc.Client
    39  }
    40  
    41  // Dial connects a client to the given URL.
    42  func Dial(rawurl string) (*Client, error) {
    43  	c, err := rpc.Dial(rawurl)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return NewClient(c), nil
    48  }
    49  
    50  // NewClient creates a client that uses the given RPC client.
    51  func NewClient(c *rpc.Client) *Client {
    52  	return &Client{c}
    53  }
    54  
    55  // Blockchain Access
    56  
    57  // BlockByHash returns the given full block.
    58  //
    59  // Note that loading full blocks requires two requests. Use HeaderByHash
    60  // if you don't need all transactions or uncle headers.
    61  func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
    62  	return ec.getBlock(ctx, "eth_getBlockByHash", hash, true)
    63  }
    64  
    65  // BlockByNumber returns a block from the current canonical chain. If number is nil, the
    66  // latest known block is returned.
    67  //
    68  // Note that loading full blocks requires two requests. Use HeaderByNumber
    69  // if you don't need all transactions or uncle headers.
    70  func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
    71  	return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
    72  }
    73  
    74  // LatestBlockNumber gets latest block number
    75  func (ec *Client) LatestBlockNumber(ctx context.Context) (*big.Int, error) {
    76  	var result hexutil.Big
    77  	err := ec.c.CallContext(ctx, &result, "eth_blockNumber")
    78  	return (*big.Int)(&result), err
    79  }
    80  
    81  type rpcBlock struct {
    82  	Hash         common.Hash      `json:"hash"`
    83  	Transactions []rpcTransaction `json:"transactions"`
    84  	UncleHashes  []common.Hash    `json:"uncles"`
    85  }
    86  
    87  func (ec *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
    88  	var raw json.RawMessage
    89  	err := ec.c.CallContext(ctx, &raw, method, args...)
    90  	if err != nil {
    91  		return nil, err
    92  	} else if len(raw) == 0 {
    93  		return nil, gochain.NotFound
    94  	}
    95  	// Decode header and transactions.
    96  	var head *types.Header
    97  	var body rpcBlock
    98  	if err := json.Unmarshal(raw, &head); err != nil {
    99  		return nil, err
   100  	}
   101  	if err := json.Unmarshal(raw, &body); err != nil {
   102  		return nil, err
   103  	}
   104  	// Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
   105  	if head.UncleHash == types.EmptyUncleHash && len(body.UncleHashes) > 0 {
   106  		return nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
   107  	}
   108  	if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
   109  		return nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
   110  	}
   111  	if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
   112  		return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
   113  	}
   114  	if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
   115  		return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
   116  	}
   117  	// Load uncles because they are not included in the block response.
   118  	var uncles []*types.Header
   119  	if len(body.UncleHashes) > 0 {
   120  		uncles = make([]*types.Header, len(body.UncleHashes))
   121  		reqs := make([]rpc.BatchElem, len(body.UncleHashes))
   122  		for i := range reqs {
   123  			reqs[i] = rpc.BatchElem{
   124  				Method: "eth_getUncleByBlockHashAndIndex",
   125  				Args:   []interface{}{body.Hash, hexutil.EncodeUint64(uint64(i))},
   126  				Result: &uncles[i],
   127  			}
   128  		}
   129  		if err := ec.c.BatchCallContext(ctx, reqs); err != nil {
   130  			return nil, err
   131  		}
   132  		for i := range reqs {
   133  			if reqs[i].Error != nil {
   134  				return nil, reqs[i].Error
   135  			}
   136  			if uncles[i] == nil {
   137  				return nil, fmt.Errorf("got null header for uncle %d of block %x", i, body.Hash[:])
   138  			}
   139  		}
   140  	}
   141  	// Fill the sender cache of transactions in the block.
   142  	txs := make([]*types.Transaction, len(body.Transactions))
   143  	for i, tx := range body.Transactions {
   144  		if tx.From != nil {
   145  			setSenderFromServer(ctx, tx.tx, *tx.From, body.Hash)
   146  		}
   147  		txs[i] = tx.tx
   148  	}
   149  	return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil
   150  }
   151  
   152  // HeaderByHash returns the block header with the given hash.
   153  func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
   154  	var head *types.Header
   155  	err := ec.c.CallContext(ctx, &head, "eth_getBlockByHash", hash, false)
   156  	if err == nil && head == nil {
   157  		err = gochain.NotFound
   158  	}
   159  	return head, err
   160  }
   161  
   162  // HeaderByNumber returns a block header from the current canonical chain. If number is
   163  // nil, the latest known header is returned.
   164  func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
   165  	var head *types.Header
   166  	err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)
   167  	if err == nil && head == nil {
   168  		err = gochain.NotFound
   169  	}
   170  	return head, err
   171  }
   172  
   173  type rpcTransaction struct {
   174  	tx *types.Transaction
   175  	txExtraInfo
   176  }
   177  
   178  type txExtraInfo struct {
   179  	BlockNumber *string         `json:"blockNumber,omitempty"`
   180  	BlockHash   *common.Hash    `json:"blockHash,omitempty"`
   181  	From        *common.Address `json:"from,omitempty"`
   182  }
   183  
   184  func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
   185  	if err := json.Unmarshal(msg, &tx.tx); err != nil {
   186  		return err
   187  	}
   188  	return json.Unmarshal(msg, &tx.txExtraInfo)
   189  }
   190  
   191  // TransactionByHash returns the transaction with the given hash.
   192  func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
   193  	var json *rpcTransaction
   194  	err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
   195  	if err != nil {
   196  		return nil, false, err
   197  	} else if json == nil {
   198  		return nil, false, gochain.NotFound
   199  	} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
   200  		return nil, false, fmt.Errorf("server returned transaction without signature")
   201  	}
   202  	if json.From != nil && json.BlockHash != nil {
   203  		setSenderFromServer(ctx, json.tx, *json.From, *json.BlockHash)
   204  	}
   205  	return json.tx, json.BlockNumber == nil, nil
   206  }
   207  
   208  type TransactionFull struct {
   209  	types.Transaction
   210  	txExtraInfo
   211  }
   212  
   213  // TransactionByHashFull returns the transaction with the given hash with extra data included such as From
   214  func (ec *Client) TransactionByHashFull(ctx context.Context, hash common.Hash) (tx *TransactionFull, isPending bool, err error) {
   215  	var json *rpcTransaction
   216  	err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
   217  	if err != nil {
   218  		return nil, false, err
   219  	} else if json == nil {
   220  		return nil, false, gochain.NotFound
   221  	} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
   222  		return nil, false, fmt.Errorf("server returned transaction without signature")
   223  	}
   224  	if json.From != nil && json.BlockHash != nil {
   225  		setSenderFromServer(ctx, json.tx, *json.From, *json.BlockHash)
   226  	}
   227  
   228  	return &TransactionFull{Transaction: *json.tx, txExtraInfo: txExtraInfo{From: json.From, BlockHash: json.BlockHash, BlockNumber: json.BlockNumber}}, json.BlockNumber == nil, nil
   229  }
   230  
   231  // TransactionSender returns the sender address of the given transaction. The transaction
   232  // must be known to the remote node and included in the blockchain at the given block and
   233  // index. The sender is the one derived by the protocol at the time of inclusion.
   234  //
   235  // There is a fast-path for transactions retrieved by TransactionByHash and
   236  // TransactionInBlock. Getting their sender address can be done without an RPC interaction.
   237  func (ec *Client) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
   238  	// Try to load the address from the cache.
   239  	sender, err := types.Sender(&senderFromServer{blockhash: block}, tx)
   240  	if err == nil {
   241  		return sender, nil
   242  	}
   243  	var meta struct {
   244  		Hash common.Hash
   245  		From common.Address
   246  	}
   247  	if err = ec.c.CallContext(ctx, &meta, "eth_getTransactionByBlockHashAndIndex", block, hexutil.Uint64(index)); err != nil {
   248  		return common.Address{}, err
   249  	}
   250  	if meta.Hash == (common.Hash{}) || meta.Hash != tx.Hash() {
   251  		return common.Address{}, errors.New("wrong inclusion block/index")
   252  	}
   253  	return meta.From, nil
   254  }
   255  
   256  // TransactionCount returns the total number of transactions in the given block.
   257  func (ec *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
   258  	var num hexutil.Uint
   259  	err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByHash", blockHash)
   260  	return uint(num), err
   261  }
   262  
   263  // TransactionInBlock returns a single transaction at index in the given block.
   264  func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
   265  	var json *rpcTransaction
   266  	err := ec.c.CallContext(ctx, &json, "eth_getTransactionByBlockHashAndIndex", blockHash, hexutil.Uint64(index))
   267  	if err == nil {
   268  		if json == nil {
   269  			return nil, gochain.NotFound
   270  		} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
   271  			return nil, fmt.Errorf("server returned transaction without signature")
   272  		}
   273  	}
   274  	if json.From != nil && json.BlockHash != nil {
   275  		setSenderFromServer(ctx, json.tx, *json.From, *json.BlockHash)
   276  	}
   277  	return json.tx, err
   278  }
   279  
   280  // TransactionReceipt returns the receipt of a transaction by transaction hash.
   281  // Note that the receipt is not available for pending transactions.
   282  func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
   283  	var r *types.Receipt
   284  	err := ec.c.CallContext(ctx, &r, "eth_getTransactionReceipt", txHash)
   285  	if err == nil {
   286  		if r == nil {
   287  			return nil, gochain.NotFound
   288  		}
   289  	}
   290  	return r, err
   291  }
   292  
   293  func toBlockNumArg(number *big.Int) string {
   294  	if number == nil {
   295  		return "latest"
   296  	}
   297  	return hexutil.EncodeBig(number)
   298  }
   299  
   300  type rpcProgress struct {
   301  	StartingBlock hexutil.Uint64
   302  	CurrentBlock  hexutil.Uint64
   303  	HighestBlock  hexutil.Uint64
   304  	PulledStates  hexutil.Uint64
   305  	KnownStates   hexutil.Uint64
   306  }
   307  
   308  // SyncProgress retrieves the current progress of the sync algorithm. If there's
   309  // no sync currently running, it returns nil.
   310  func (ec *Client) SyncProgress(ctx context.Context) (*gochain.SyncProgress, error) {
   311  	var raw json.RawMessage
   312  	if err := ec.c.CallContext(ctx, &raw, "eth_syncing"); err != nil {
   313  		return nil, err
   314  	}
   315  	// Handle the possible response types
   316  	var syncing bool
   317  	if err := json.Unmarshal(raw, &syncing); err == nil {
   318  		return nil, nil // Not syncing (always false)
   319  	}
   320  	var progress *rpcProgress
   321  	if err := json.Unmarshal(raw, &progress); err != nil {
   322  		return nil, err
   323  	}
   324  	return &gochain.SyncProgress{
   325  		StartingBlock: uint64(progress.StartingBlock),
   326  		CurrentBlock:  uint64(progress.CurrentBlock),
   327  		HighestBlock:  uint64(progress.HighestBlock),
   328  		PulledStates:  uint64(progress.PulledStates),
   329  		KnownStates:   uint64(progress.KnownStates),
   330  	}, nil
   331  }
   332  
   333  // SubscribeNewHead subscribes to notifications about the current blockchain head
   334  // on the given channel.
   335  func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (gochain.Subscription, error) {
   336  	return ec.c.EthSubscribe(ctx, ch, "newHeads", map[string]struct{}{})
   337  }
   338  
   339  // State Access
   340  
   341  // NetworkID returns the network ID for this chain.
   342  func (ec *Client) NetworkID(ctx context.Context) (*big.Int, error) {
   343  	version := new(big.Int)
   344  	var ver string
   345  	if err := ec.c.CallContext(ctx, &ver, "net_version"); err != nil {
   346  		return nil, err
   347  	}
   348  	if _, ok := version.SetString(ver, 10); !ok {
   349  		return nil, fmt.Errorf("invalid net_version result %q", ver)
   350  	}
   351  	return version, nil
   352  }
   353  
   354  // ChainID returns the chain ID for this chain.
   355  func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) {
   356  	var result hexutil.Big
   357  	err := ec.c.CallContext(ctx, &result, "eth_chainId")
   358  	return (*big.Int)(&result), err
   359  }
   360  
   361  // BalanceAt returns the wei balance of the given account.
   362  // The block number can be nil, in which case the balance is taken from the latest known block.
   363  func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
   364  	var result hexutil.Big
   365  	err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, toBlockNumArg(blockNumber))
   366  	return (*big.Int)(&result), err
   367  }
   368  
   369  // StorageAt returns the value of key in the contract storage of the given account.
   370  // The block number can be nil, in which case the value is taken from the latest known block.
   371  func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
   372  	var result hexutil.Bytes
   373  	err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, toBlockNumArg(blockNumber))
   374  	return result, err
   375  }
   376  
   377  // CodeAt returns the contract code of the given account.
   378  // The block number can be nil, in which case the code is taken from the latest known block.
   379  func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
   380  	var result hexutil.Bytes
   381  	err := ec.c.CallContext(ctx, &result, "eth_getCode", account, toBlockNumArg(blockNumber))
   382  	return result, err
   383  }
   384  
   385  // NonceAt returns the account nonce of the given account.
   386  // The block number can be nil, in which case the nonce is taken from the latest known block.
   387  func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
   388  	var result hexutil.Uint64
   389  	err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, toBlockNumArg(blockNumber))
   390  	return uint64(result), err
   391  }
   392  
   393  // Filters
   394  
   395  // FilterLogs executes a filter query.
   396  func (ec *Client) FilterLogs(ctx context.Context, q gochain.FilterQuery) ([]types.Log, error) {
   397  	var result []types.Log
   398  	arg, err := toFilterArg(q)
   399  	if err != nil {
   400  		return nil, err
   401  	}
   402  	err = ec.c.CallContext(ctx, &result, "eth_getLogs", arg)
   403  	return result, err
   404  }
   405  
   406  // SubscribeFilterLogs subscribes to the results of a streaming filter query.
   407  func (ec *Client) SubscribeFilterLogs(ctx context.Context, q gochain.FilterQuery, ch chan<- types.Log) (gochain.Subscription, error) {
   408  	arg, err := toFilterArg(q)
   409  	if err != nil {
   410  		return nil, err
   411  	}
   412  	return ec.c.EthSubscribe(ctx, ch, "logs", arg)
   413  }
   414  
   415  func toFilterArg(q gochain.FilterQuery) (interface{}, error) {
   416  	arg := map[string]interface{}{
   417  		"address": q.Addresses,
   418  		"topics":  q.Topics,
   419  	}
   420  	if q.BlockHash != nil {
   421  		arg["blockHash"] = *q.BlockHash
   422  		if q.FromBlock != nil || q.ToBlock != nil {
   423  			return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
   424  		}
   425  	} else {
   426  		if q.FromBlock == nil {
   427  			arg["fromBlock"] = "0x0"
   428  		} else {
   429  			arg["fromBlock"] = toBlockNumArg(q.FromBlock)
   430  		}
   431  		arg["toBlock"] = toBlockNumArg(q.ToBlock)
   432  	}
   433  	return arg, nil
   434  }
   435  
   436  // Pending State
   437  
   438  // PendingBalanceAt returns the wei balance of the given account in the pending state.
   439  func (ec *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
   440  	var result hexutil.Big
   441  	err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, "pending")
   442  	return (*big.Int)(&result), err
   443  }
   444  
   445  // PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
   446  func (ec *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) {
   447  	var result hexutil.Bytes
   448  	err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, "pending")
   449  	return result, err
   450  }
   451  
   452  // PendingCodeAt returns the contract code of the given account in the pending state.
   453  func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
   454  	var result hexutil.Bytes
   455  	err := ec.c.CallContext(ctx, &result, "eth_getCode", account, "pending")
   456  	return result, err
   457  }
   458  
   459  // PendingNonceAt returns the account nonce of the given account in the pending state.
   460  // This is the nonce that should be used for the next transaction.
   461  func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
   462  	var result hexutil.Uint64
   463  	err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending")
   464  	return uint64(result), err
   465  }
   466  
   467  // PendingTransactionCount returns the total number of transactions in the pending state.
   468  func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
   469  	var num hexutil.Uint
   470  	err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByNumber", "pending")
   471  	return uint(num), err
   472  }
   473  
   474  // TODO: SubscribePendingTransactions (needs server side)
   475  
   476  // Contract Calling
   477  
   478  // CallContract executes a message call transaction, which is directly executed in the VM
   479  // of the node, but never mined into the blockchain.
   480  //
   481  // blockNumber selects the block height at which the call runs. It can be nil, in which
   482  // case the code is taken from the latest known block. Note that state from very old
   483  // blocks might not be available.
   484  func (ec *Client) CallContract(ctx context.Context, msg gochain.CallMsg, blockNumber *big.Int) ([]byte, error) {
   485  	var hex hexutil.Bytes
   486  	err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), toBlockNumArg(blockNumber))
   487  	if err != nil {
   488  		return nil, err
   489  	}
   490  	return hex, nil
   491  }
   492  
   493  // PendingCallContract executes a message call transaction using the EVM.
   494  // The state seen by the contract call is the pending state.
   495  func (ec *Client) PendingCallContract(ctx context.Context, msg gochain.CallMsg) ([]byte, error) {
   496  	var hex hexutil.Bytes
   497  	err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), "pending")
   498  	if err != nil {
   499  		return nil, err
   500  	}
   501  	return hex, nil
   502  }
   503  
   504  // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
   505  // execution of a transaction.
   506  func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
   507  	var hex hexutil.Big
   508  	if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
   509  		return nil, err
   510  	}
   511  	return (*big.Int)(&hex), nil
   512  }
   513  
   514  // EstimateGas tries to estimate the gas needed to execute a specific transaction based on
   515  // the current pending state of the backend blockchain. There is no guarantee that this is
   516  // the true gas limit requirement as other transactions may be added or removed by miners,
   517  // but it should provide a basis for setting a reasonable default.
   518  func (ec *Client) EstimateGas(ctx context.Context, msg gochain.CallMsg) (uint64, error) {
   519  	var hex hexutil.Uint64
   520  	err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
   521  	if err != nil {
   522  		return 0, err
   523  	}
   524  	return uint64(hex), nil
   525  }
   526  
   527  // SendTransaction injects a signed transaction into the pending pool for execution.
   528  //
   529  // If the transaction was a contract creation use the TransactionReceipt method to get the
   530  // contract address after the transaction has been mined.
   531  func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) error {
   532  	data, err := rlp.EncodeToBytes(tx)
   533  	if err != nil {
   534  		return err
   535  	}
   536  	return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(data))
   537  }
   538  
   539  // SignersAt returns the set of clique signers at the given block.
   540  func (ec *Client) SignersAt(ctx context.Context, blockNumber *big.Int) ([]common.Address, error) {
   541  	var signers []common.Address
   542  	err := ec.c.CallContext(ctx, &signers, "clique_getSigners", toBlockNumArg(blockNumber))
   543  	if err != nil {
   544  		return nil, err
   545  	}
   546  	return signers, nil
   547  }
   548  
   549  // SignersAtHash returns the set of clique signers at the given block.
   550  func (ec *Client) SignersAtHash(ctx context.Context, blockHash common.Hash) ([]common.Address, error) {
   551  	var signers []common.Address
   552  	err := ec.c.CallContext(ctx, &signers, "clique_getSignersAtHash", blockHash)
   553  	if err != nil {
   554  		return nil, err
   555  	}
   556  	return signers, nil
   557  }
   558  
   559  // VotersAt returns the set of clique voters at the given block.
   560  func (ec *Client) VotersAt(ctx context.Context, blockNumber *big.Int) ([]common.Address, error) {
   561  	var voters []common.Address
   562  	err := ec.c.CallContext(ctx, &voters, "clique_getVoters", toBlockNumArg(blockNumber))
   563  	if err != nil {
   564  		return nil, err
   565  	}
   566  	return voters, nil
   567  }
   568  
   569  // VotersAtHash returns the set of clique voters at the given block.
   570  func (ec *Client) VotersAtHash(ctx context.Context, blockHash common.Hash) ([]common.Address, error) {
   571  	var voters []common.Address
   572  	err := ec.c.CallContext(ctx, &voters, "clique_getVotersAtHash", blockHash)
   573  	if err != nil {
   574  		return nil, err
   575  	}
   576  	return voters, nil
   577  }
   578  
   579  // SnapshotAt returns the clique snapshot at the given block.
   580  func (ec *Client) SnapshotAt(ctx context.Context, blockNumber *big.Int) (*clique.Snapshot, error) {
   581  	var s clique.Snapshot
   582  	err := ec.c.CallContext(ctx, &s, "clique_getSnapshot", toBlockNumArg(blockNumber))
   583  	if err != nil {
   584  		return nil, err
   585  	}
   586  	return &s, nil
   587  }
   588  
   589  // SnapshotAt returns the clique snapshot at the given block.
   590  func (ec *Client) SnapshotAtHash(ctx context.Context, blockHash common.Hash) (*clique.Snapshot, error) {
   591  	var s clique.Snapshot
   592  	err := ec.c.CallContext(ctx, &s, "clique_getSnapshotAtHash", blockHash)
   593  	if err != nil {
   594  		return nil, err
   595  	}
   596  	return &s, nil
   597  }
   598  
   599  func toCallArg(msg gochain.CallMsg) interface{} {
   600  	arg := map[string]interface{}{
   601  		"from": msg.From,
   602  		"to":   msg.To,
   603  	}
   604  	if len(msg.Data) > 0 {
   605  		arg["data"] = hexutil.Bytes(msg.Data)
   606  	}
   607  	if msg.Value != nil {
   608  		arg["value"] = (*hexutil.Big)(msg.Value)
   609  	}
   610  	if msg.Gas != 0 {
   611  		arg["gas"] = hexutil.Uint64(msg.Gas)
   612  	}
   613  	if msg.GasPrice != nil {
   614  		arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
   615  	}
   616  	return arg
   617  }