github.com/ethereum/go-ethereum@v1.14.3/ethclient/gethclient/gethclient.go (about)

     1  // Copyright 2021 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 gethclient provides an RPC client for geth-specific APIs.
    18  package gethclient
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"fmt"
    24  	"math/big"
    25  	"runtime"
    26  	"runtime/debug"
    27  
    28  	"github.com/ethereum/go-ethereum"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/common/hexutil"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/p2p"
    33  	"github.com/ethereum/go-ethereum/rpc"
    34  )
    35  
    36  // Client is a wrapper around rpc.Client that implements geth-specific functionality.
    37  //
    38  // If you want to use the standardized Ethereum RPC functionality, use ethclient.Client instead.
    39  type Client struct {
    40  	c *rpc.Client
    41  }
    42  
    43  // New creates a client that uses the given RPC client.
    44  func New(c *rpc.Client) *Client {
    45  	return &Client{c}
    46  }
    47  
    48  // CreateAccessList tries to create an access list for a specific transaction based on the
    49  // current pending state of the blockchain.
    50  func (ec *Client) CreateAccessList(ctx context.Context, msg ethereum.CallMsg) (*types.AccessList, uint64, string, error) {
    51  	type accessListResult struct {
    52  		Accesslist *types.AccessList `json:"accessList"`
    53  		Error      string            `json:"error,omitempty"`
    54  		GasUsed    hexutil.Uint64    `json:"gasUsed"`
    55  	}
    56  	var result accessListResult
    57  	if err := ec.c.CallContext(ctx, &result, "eth_createAccessList", toCallArg(msg)); err != nil {
    58  		return nil, 0, "", err
    59  	}
    60  	return result.Accesslist, uint64(result.GasUsed), result.Error, nil
    61  }
    62  
    63  // AccountResult is the result of a GetProof operation.
    64  type AccountResult struct {
    65  	Address      common.Address  `json:"address"`
    66  	AccountProof []string        `json:"accountProof"`
    67  	Balance      *big.Int        `json:"balance"`
    68  	CodeHash     common.Hash     `json:"codeHash"`
    69  	Nonce        uint64          `json:"nonce"`
    70  	StorageHash  common.Hash     `json:"storageHash"`
    71  	StorageProof []StorageResult `json:"storageProof"`
    72  }
    73  
    74  // StorageResult provides a proof for a key-value pair.
    75  type StorageResult struct {
    76  	Key   string   `json:"key"`
    77  	Value *big.Int `json:"value"`
    78  	Proof []string `json:"proof"`
    79  }
    80  
    81  // GetProof returns the account and storage values of the specified account including the Merkle-proof.
    82  // The block number can be nil, in which case the value is taken from the latest known block.
    83  func (ec *Client) GetProof(ctx context.Context, account common.Address, keys []string, blockNumber *big.Int) (*AccountResult, error) {
    84  	type storageResult struct {
    85  		Key   string       `json:"key"`
    86  		Value *hexutil.Big `json:"value"`
    87  		Proof []string     `json:"proof"`
    88  	}
    89  
    90  	type accountResult struct {
    91  		Address      common.Address  `json:"address"`
    92  		AccountProof []string        `json:"accountProof"`
    93  		Balance      *hexutil.Big    `json:"balance"`
    94  		CodeHash     common.Hash     `json:"codeHash"`
    95  		Nonce        hexutil.Uint64  `json:"nonce"`
    96  		StorageHash  common.Hash     `json:"storageHash"`
    97  		StorageProof []storageResult `json:"storageProof"`
    98  	}
    99  
   100  	// Avoid keys being 'null'.
   101  	if keys == nil {
   102  		keys = []string{}
   103  	}
   104  
   105  	var res accountResult
   106  	err := ec.c.CallContext(ctx, &res, "eth_getProof", account, keys, toBlockNumArg(blockNumber))
   107  	// Turn hexutils back to normal datatypes
   108  	storageResults := make([]StorageResult, 0, len(res.StorageProof))
   109  	for _, st := range res.StorageProof {
   110  		storageResults = append(storageResults, StorageResult{
   111  			Key:   st.Key,
   112  			Value: st.Value.ToInt(),
   113  			Proof: st.Proof,
   114  		})
   115  	}
   116  	result := AccountResult{
   117  		Address:      res.Address,
   118  		AccountProof: res.AccountProof,
   119  		Balance:      res.Balance.ToInt(),
   120  		Nonce:        uint64(res.Nonce),
   121  		CodeHash:     res.CodeHash,
   122  		StorageHash:  res.StorageHash,
   123  		StorageProof: storageResults,
   124  	}
   125  	return &result, err
   126  }
   127  
   128  // CallContract executes a message call transaction, which is directly executed in the VM
   129  // of the node, but never mined into the blockchain.
   130  //
   131  // blockNumber selects the block height at which the call runs. It can be nil, in which
   132  // case the code is taken from the latest known block. Note that state from very old
   133  // blocks might not be available.
   134  //
   135  // overrides specifies a map of contract states that should be overwritten before executing
   136  // the message call.
   137  // Please use ethclient.CallContract instead if you don't need the override functionality.
   138  func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int, overrides *map[common.Address]OverrideAccount) ([]byte, error) {
   139  	var hex hexutil.Bytes
   140  	err := ec.c.CallContext(
   141  		ctx, &hex, "eth_call", toCallArg(msg),
   142  		toBlockNumArg(blockNumber), overrides,
   143  	)
   144  	return hex, err
   145  }
   146  
   147  // CallContractWithBlockOverrides executes a message call transaction, which is directly executed
   148  // in the VM  of the node, but never mined into the blockchain.
   149  //
   150  // blockNumber selects the block height at which the call runs. It can be nil, in which
   151  // case the code is taken from the latest known block. Note that state from very old
   152  // blocks might not be available.
   153  //
   154  // overrides specifies a map of contract states that should be overwritten before executing
   155  // the message call.
   156  //
   157  // blockOverrides specifies block fields exposed to the EVM that can be overridden for the call.
   158  //
   159  // Please use ethclient.CallContract instead if you don't need the override functionality.
   160  func (ec *Client) CallContractWithBlockOverrides(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int, overrides *map[common.Address]OverrideAccount, blockOverrides BlockOverrides) ([]byte, error) {
   161  	var hex hexutil.Bytes
   162  	err := ec.c.CallContext(
   163  		ctx, &hex, "eth_call", toCallArg(msg),
   164  		toBlockNumArg(blockNumber), overrides, blockOverrides,
   165  	)
   166  	return hex, err
   167  }
   168  
   169  // GCStats retrieves the current garbage collection stats from a geth node.
   170  func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) {
   171  	var result debug.GCStats
   172  	err := ec.c.CallContext(ctx, &result, "debug_gcStats")
   173  	return &result, err
   174  }
   175  
   176  // MemStats retrieves the current memory stats from a geth node.
   177  func (ec *Client) MemStats(ctx context.Context) (*runtime.MemStats, error) {
   178  	var result runtime.MemStats
   179  	err := ec.c.CallContext(ctx, &result, "debug_memStats")
   180  	return &result, err
   181  }
   182  
   183  // SetHead sets the current head of the local chain by block number.
   184  // Note, this is a destructive action and may severely damage your chain.
   185  // Use with extreme caution.
   186  func (ec *Client) SetHead(ctx context.Context, number *big.Int) error {
   187  	return ec.c.CallContext(ctx, nil, "debug_setHead", toBlockNumArg(number))
   188  }
   189  
   190  // GetNodeInfo retrieves the node info of a geth node.
   191  func (ec *Client) GetNodeInfo(ctx context.Context) (*p2p.NodeInfo, error) {
   192  	var result p2p.NodeInfo
   193  	err := ec.c.CallContext(ctx, &result, "admin_nodeInfo")
   194  	return &result, err
   195  }
   196  
   197  // SubscribeFullPendingTransactions subscribes to new pending transactions.
   198  func (ec *Client) SubscribeFullPendingTransactions(ctx context.Context, ch chan<- *types.Transaction) (*rpc.ClientSubscription, error) {
   199  	return ec.c.EthSubscribe(ctx, ch, "newPendingTransactions", true)
   200  }
   201  
   202  // SubscribePendingTransactions subscribes to new pending transaction hashes.
   203  func (ec *Client) SubscribePendingTransactions(ctx context.Context, ch chan<- common.Hash) (*rpc.ClientSubscription, error) {
   204  	return ec.c.EthSubscribe(ctx, ch, "newPendingTransactions")
   205  }
   206  
   207  func toBlockNumArg(number *big.Int) string {
   208  	if number == nil {
   209  		return "latest"
   210  	}
   211  	if number.Sign() >= 0 {
   212  		return hexutil.EncodeBig(number)
   213  	}
   214  	// It's negative.
   215  	if number.IsInt64() {
   216  		return rpc.BlockNumber(number.Int64()).String()
   217  	}
   218  	// It's negative and large, which is invalid.
   219  	return fmt.Sprintf("<invalid %d>", number)
   220  }
   221  
   222  func toCallArg(msg ethereum.CallMsg) interface{} {
   223  	arg := map[string]interface{}{
   224  		"from": msg.From,
   225  		"to":   msg.To,
   226  	}
   227  	if len(msg.Data) > 0 {
   228  		arg["input"] = hexutil.Bytes(msg.Data)
   229  	}
   230  	if msg.Value != nil {
   231  		arg["value"] = (*hexutil.Big)(msg.Value)
   232  	}
   233  	if msg.Gas != 0 {
   234  		arg["gas"] = hexutil.Uint64(msg.Gas)
   235  	}
   236  	if msg.GasPrice != nil {
   237  		arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
   238  	}
   239  	if msg.GasFeeCap != nil {
   240  		arg["maxFeePerGas"] = (*hexutil.Big)(msg.GasFeeCap)
   241  	}
   242  	if msg.GasTipCap != nil {
   243  		arg["maxPriorityFeePerGas"] = (*hexutil.Big)(msg.GasTipCap)
   244  	}
   245  	if msg.AccessList != nil {
   246  		arg["accessList"] = msg.AccessList
   247  	}
   248  	if msg.BlobGasFeeCap != nil {
   249  		arg["maxFeePerBlobGas"] = (*hexutil.Big)(msg.BlobGasFeeCap)
   250  	}
   251  	if msg.BlobHashes != nil {
   252  		arg["blobVersionedHashes"] = msg.BlobHashes
   253  	}
   254  	return arg
   255  }
   256  
   257  // OverrideAccount specifies the state of an account to be overridden.
   258  type OverrideAccount struct {
   259  	// Nonce sets nonce of the account. Note: the nonce override will only
   260  	// be applied when it is set to a non-zero value.
   261  	Nonce uint64
   262  
   263  	// Code sets the contract code. The override will be applied
   264  	// when the code is non-nil, i.e. setting empty code is possible
   265  	// using an empty slice.
   266  	Code []byte
   267  
   268  	// Balance sets the account balance.
   269  	Balance *big.Int
   270  
   271  	// State sets the complete storage. The override will be applied
   272  	// when the given map is non-nil. Using an empty map wipes the
   273  	// entire contract storage during the call.
   274  	State map[common.Hash]common.Hash
   275  
   276  	// StateDiff allows overriding individual storage slots.
   277  	StateDiff map[common.Hash]common.Hash
   278  }
   279  
   280  func (a OverrideAccount) MarshalJSON() ([]byte, error) {
   281  	type acc struct {
   282  		Nonce     hexutil.Uint64              `json:"nonce,omitempty"`
   283  		Code      string                      `json:"code,omitempty"`
   284  		Balance   *hexutil.Big                `json:"balance,omitempty"`
   285  		State     interface{}                 `json:"state,omitempty"`
   286  		StateDiff map[common.Hash]common.Hash `json:"stateDiff,omitempty"`
   287  	}
   288  
   289  	output := acc{
   290  		Nonce:     hexutil.Uint64(a.Nonce),
   291  		Balance:   (*hexutil.Big)(a.Balance),
   292  		StateDiff: a.StateDiff,
   293  	}
   294  	if a.Code != nil {
   295  		output.Code = hexutil.Encode(a.Code)
   296  	}
   297  	if a.State != nil {
   298  		output.State = a.State
   299  	}
   300  	return json.Marshal(output)
   301  }
   302  
   303  // BlockOverrides specifies the  set of header fields to override.
   304  type BlockOverrides struct {
   305  	// Number overrides the block number.
   306  	Number *big.Int
   307  	// Difficulty overrides the block difficulty.
   308  	Difficulty *big.Int
   309  	// Time overrides the block timestamp. Time is applied only when
   310  	// it is non-zero.
   311  	Time uint64
   312  	// GasLimit overrides the block gas limit. GasLimit is applied only when
   313  	// it is non-zero.
   314  	GasLimit uint64
   315  	// Coinbase overrides the block coinbase. Coinbase is applied only when
   316  	// it is different from the zero address.
   317  	Coinbase common.Address
   318  	// Random overrides the block extra data which feeds into the RANDOM opcode.
   319  	// Random is applied only when it is a non-zero hash.
   320  	Random common.Hash
   321  	// BaseFee overrides the block base fee.
   322  	BaseFee *big.Int
   323  }
   324  
   325  func (o BlockOverrides) MarshalJSON() ([]byte, error) {
   326  	type override struct {
   327  		Number     *hexutil.Big    `json:"number,omitempty"`
   328  		Difficulty *hexutil.Big    `json:"difficulty,omitempty"`
   329  		Time       hexutil.Uint64  `json:"time,omitempty"`
   330  		GasLimit   hexutil.Uint64  `json:"gasLimit,omitempty"`
   331  		Coinbase   *common.Address `json:"coinbase,omitempty"`
   332  		Random     *common.Hash    `json:"random,omitempty"`
   333  		BaseFee    *hexutil.Big    `json:"baseFee,omitempty"`
   334  	}
   335  
   336  	output := override{
   337  		Number:     (*hexutil.Big)(o.Number),
   338  		Difficulty: (*hexutil.Big)(o.Difficulty),
   339  		Time:       hexutil.Uint64(o.Time),
   340  		GasLimit:   hexutil.Uint64(o.GasLimit),
   341  		BaseFee:    (*hexutil.Big)(o.BaseFee),
   342  	}
   343  	if o.Coinbase != (common.Address{}) {
   344  		output.Coinbase = &o.Coinbase
   345  	}
   346  	if o.Random != (common.Hash{}) {
   347  		output.Random = &o.Random
   348  	}
   349  	return json.Marshal(output)
   350  }