github.com/ava-labs/subnet-evm@v0.6.4/ethclient/subnetevmclient/subnet_evm_client.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2021 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 // Package subnetevmclient provides an RPC client for subnet-evm-specific APIs. 28 package subnetevmclient 29 30 import ( 31 "context" 32 "math/big" 33 "runtime" 34 "runtime/debug" 35 36 "github.com/ava-labs/subnet-evm/core/types" 37 "github.com/ava-labs/subnet-evm/ethclient" 38 "github.com/ava-labs/subnet-evm/interfaces" 39 "github.com/ava-labs/subnet-evm/rpc" 40 "github.com/ethereum/go-ethereum/common" 41 "github.com/ethereum/go-ethereum/common/hexutil" 42 ) 43 44 // Client is a wrapper around rpc.Client that implements geth-specific functionality. 45 // 46 // If you want to use the standardized Ethereum RPC functionality, use ethclient.Client instead. 47 type Client struct { 48 c *rpc.Client 49 } 50 51 // New creates a client that uses the given RPC client. 52 func New(c *rpc.Client) *Client { 53 return &Client{c} 54 } 55 56 // CreateAccessList tries to create an access list for a specific transaction based on the 57 // current pending state of the blockchain. 58 func (ec *Client) CreateAccessList(ctx context.Context, msg interfaces.CallMsg) (*types.AccessList, uint64, string, error) { 59 type accessListResult struct { 60 Accesslist *types.AccessList `json:"accessList"` 61 Error string `json:"error,omitempty"` 62 GasUsed hexutil.Uint64 `json:"gasUsed"` 63 } 64 var result accessListResult 65 if err := ec.c.CallContext(ctx, &result, "eth_createAccessList", toCallArg(msg)); err != nil { 66 return nil, 0, "", err 67 } 68 return result.Accesslist, uint64(result.GasUsed), result.Error, nil 69 } 70 71 // AccountResult is the result of a GetProof operation. 72 type AccountResult struct { 73 Address common.Address `json:"address"` 74 AccountProof []string `json:"accountProof"` 75 Balance *big.Int `json:"balance"` 76 CodeHash common.Hash `json:"codeHash"` 77 Nonce uint64 `json:"nonce"` 78 StorageHash common.Hash `json:"storageHash"` 79 StorageProof []StorageResult `json:"storageProof"` 80 } 81 82 // StorageResult provides a proof for a key-value pair. 83 type StorageResult struct { 84 Key string `json:"key"` 85 Value *big.Int `json:"value"` 86 Proof []string `json:"proof"` 87 } 88 89 // GetProof returns the account and storage values of the specified account including the Merkle-proof. 90 // The block number can be nil, in which case the value is taken from the latest known block. 91 func (ec *Client) GetProof(ctx context.Context, account common.Address, keys []string, blockNumber *big.Int) (*AccountResult, error) { 92 type storageResult struct { 93 Key string `json:"key"` 94 Value *hexutil.Big `json:"value"` 95 Proof []string `json:"proof"` 96 } 97 98 type accountResult struct { 99 Address common.Address `json:"address"` 100 AccountProof []string `json:"accountProof"` 101 Balance *hexutil.Big `json:"balance"` 102 CodeHash common.Hash `json:"codeHash"` 103 Nonce hexutil.Uint64 `json:"nonce"` 104 StorageHash common.Hash `json:"storageHash"` 105 StorageProof []storageResult `json:"storageProof"` 106 } 107 108 // Avoid keys being 'null'. 109 if keys == nil { 110 keys = []string{} 111 } 112 113 var res accountResult 114 err := ec.c.CallContext(ctx, &res, "eth_getProof", account, keys, ethclient.ToBlockNumArg(blockNumber)) 115 // Turn hexutils back to normal datatypes 116 storageResults := make([]StorageResult, 0, len(res.StorageProof)) 117 for _, st := range res.StorageProof { 118 storageResults = append(storageResults, StorageResult{ 119 Key: st.Key, 120 Value: st.Value.ToInt(), 121 Proof: st.Proof, 122 }) 123 } 124 result := AccountResult{ 125 Address: res.Address, 126 AccountProof: res.AccountProof, 127 Balance: res.Balance.ToInt(), 128 Nonce: uint64(res.Nonce), 129 CodeHash: res.CodeHash, 130 StorageHash: res.StorageHash, 131 StorageProof: storageResults, 132 } 133 return &result, err 134 } 135 136 // OverrideAccount specifies the state of an account to be overridden. 137 type OverrideAccount struct { 138 Nonce uint64 `json:"nonce"` 139 Code []byte `json:"code"` 140 Balance *big.Int `json:"balance"` 141 State map[common.Hash]common.Hash `json:"state"` 142 StateDiff map[common.Hash]common.Hash `json:"stateDiff"` 143 } 144 145 // CallContract executes a message call transaction, which is directly executed in the VM 146 // of the node, but never mined into the blockchain. 147 // 148 // blockNumber selects the block height at which the call runs. It can be nil, in which 149 // case the code is taken from the latest known block. Note that state from very old 150 // blocks might not be available. 151 // 152 // overrides specifies a map of contract states that should be overwritten before executing 153 // the message call. 154 // Please use ethclient.CallContract instead if you don't need the override functionality. 155 func (ec *Client) CallContract(ctx context.Context, msg interfaces.CallMsg, blockNumber *big.Int, overrides *map[common.Address]OverrideAccount) ([]byte, error) { 156 var hex hexutil.Bytes 157 err := ec.c.CallContext( 158 ctx, &hex, "eth_call", toCallArg(msg), 159 ethclient.ToBlockNumArg(blockNumber), toOverrideMap(overrides), 160 ) 161 return hex, err 162 } 163 164 // GCStats retrieves the current garbage collection stats from a geth node. 165 func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) { 166 var result debug.GCStats 167 err := ec.c.CallContext(ctx, &result, "debug_gcStats") 168 return &result, err 169 } 170 171 // MemStats retrieves the current memory stats from a geth node. 172 func (ec *Client) MemStats(ctx context.Context) (*runtime.MemStats, error) { 173 var result runtime.MemStats 174 err := ec.c.CallContext(ctx, &result, "debug_memStats") 175 return &result, err 176 } 177 178 // SubscribePendingTransactions subscribes to new pending transactions. 179 func (ec *Client) SubscribePendingTransactions(ctx context.Context, ch chan<- common.Hash) (*rpc.ClientSubscription, error) { 180 return ec.c.EthSubscribe(ctx, ch, "newPendingTransactions") 181 } 182 183 func toCallArg(msg interfaces.CallMsg) interface{} { 184 arg := map[string]interface{}{ 185 "from": msg.From, 186 "to": msg.To, 187 } 188 if len(msg.Data) > 0 { 189 arg["input"] = hexutil.Bytes(msg.Data) 190 } 191 if msg.Value != nil { 192 arg["value"] = (*hexutil.Big)(msg.Value) 193 } 194 if msg.Gas != 0 { 195 arg["gas"] = hexutil.Uint64(msg.Gas) 196 } 197 if msg.GasPrice != nil { 198 arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) 199 } 200 return arg 201 } 202 203 func toOverrideMap(overrides *map[common.Address]OverrideAccount) interface{} { 204 if overrides == nil { 205 return nil 206 } 207 type overrideAccount struct { 208 Nonce hexutil.Uint64 `json:"nonce"` 209 Code hexutil.Bytes `json:"code"` 210 Balance *hexutil.Big `json:"balance"` 211 State map[common.Hash]common.Hash `json:"state"` 212 StateDiff map[common.Hash]common.Hash `json:"stateDiff"` 213 } 214 result := make(map[common.Address]overrideAccount) 215 for addr, override := range *overrides { 216 result[addr] = overrideAccount{ 217 Nonce: hexutil.Uint64(override.Nonce), 218 Code: override.Code, 219 Balance: (*hexutil.Big)(override.Balance), 220 State: override.State, 221 StateDiff: override.StateDiff, 222 } 223 } 224 return &result 225 }