github.com/MetalBlockchain/subnet-evm@v0.4.9/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/MetalBlockchain/subnet-evm/core/types" 37 "github.com/MetalBlockchain/subnet-evm/ethclient" 38 "github.com/MetalBlockchain/subnet-evm/interfaces" 39 "github.com/MetalBlockchain/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 var res accountResult 109 err := ec.c.CallContext(ctx, &res, "eth_getProof", account, keys, ethclient.ToBlockNumArg(blockNumber)) 110 // Turn hexutils back to normal datatypes 111 storageResults := make([]StorageResult, 0, len(res.StorageProof)) 112 for _, st := range res.StorageProof { 113 storageResults = append(storageResults, StorageResult{ 114 Key: st.Key, 115 Value: st.Value.ToInt(), 116 Proof: st.Proof, 117 }) 118 } 119 result := AccountResult{ 120 Address: res.Address, 121 AccountProof: res.AccountProof, 122 Balance: res.Balance.ToInt(), 123 Nonce: uint64(res.Nonce), 124 CodeHash: res.CodeHash, 125 StorageHash: res.StorageHash, 126 StorageProof: storageResults, 127 } 128 return &result, err 129 } 130 131 // OverrideAccount specifies the state of an account to be overridden. 132 type OverrideAccount struct { 133 Nonce uint64 `json:"nonce"` 134 Code []byte `json:"code"` 135 Balance *big.Int `json:"balance"` 136 State map[common.Hash]common.Hash `json:"state"` 137 StateDiff map[common.Hash]common.Hash `json:"stateDiff"` 138 } 139 140 // CallContract executes a message call transaction, which is directly executed in the VM 141 // of the node, but never mined into the blockchain. 142 // 143 // blockNumber selects the block height at which the call runs. It can be nil, in which 144 // case the code is taken from the latest known block. Note that state from very old 145 // blocks might not be available. 146 // 147 // overrides specifies a map of contract states that should be overwritten before executing 148 // the message call. 149 // Please use ethclient.CallContract instead if you don't need the override functionality. 150 func (ec *Client) CallContract(ctx context.Context, msg interfaces.CallMsg, blockNumber *big.Int, overrides *map[common.Address]OverrideAccount) ([]byte, error) { 151 var hex hexutil.Bytes 152 err := ec.c.CallContext( 153 ctx, &hex, "eth_call", toCallArg(msg), 154 ethclient.ToBlockNumArg(blockNumber), toOverrideMap(overrides), 155 ) 156 return hex, err 157 } 158 159 // GCStats retrieves the current garbage collection stats from a geth node. 160 func (ec *Client) GCStats(ctx context.Context) (*debug.GCStats, error) { 161 var result debug.GCStats 162 err := ec.c.CallContext(ctx, &result, "debug_gcStats") 163 return &result, err 164 } 165 166 // MemStats retrieves the current memory stats from a geth node. 167 func (ec *Client) MemStats(ctx context.Context) (*runtime.MemStats, error) { 168 var result runtime.MemStats 169 err := ec.c.CallContext(ctx, &result, "debug_memStats") 170 return &result, err 171 } 172 173 // SubscribePendingTransactions subscribes to new pending transactions. 174 func (ec *Client) SubscribePendingTransactions(ctx context.Context, ch chan<- common.Hash) (*rpc.ClientSubscription, error) { 175 return ec.c.EthSubscribe(ctx, ch, "newPendingTransactions") 176 } 177 178 func toCallArg(msg interfaces.CallMsg) interface{} { 179 arg := map[string]interface{}{ 180 "from": msg.From, 181 "to": msg.To, 182 } 183 if len(msg.Data) > 0 { 184 arg["data"] = hexutil.Bytes(msg.Data) 185 } 186 if msg.Value != nil { 187 arg["value"] = (*hexutil.Big)(msg.Value) 188 } 189 if msg.Gas != 0 { 190 arg["gas"] = hexutil.Uint64(msg.Gas) 191 } 192 if msg.GasPrice != nil { 193 arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) 194 } 195 return arg 196 } 197 198 func toOverrideMap(overrides *map[common.Address]OverrideAccount) interface{} { 199 if overrides == nil { 200 return nil 201 } 202 type overrideAccount struct { 203 Nonce hexutil.Uint64 `json:"nonce"` 204 Code hexutil.Bytes `json:"code"` 205 Balance *hexutil.Big `json:"balance"` 206 State map[common.Hash]common.Hash `json:"state"` 207 StateDiff map[common.Hash]common.Hash `json:"stateDiff"` 208 } 209 result := make(map[common.Address]overrideAccount) 210 for addr, override := range *overrides { 211 result[addr] = overrideAccount{ 212 Nonce: hexutil.Uint64(override.Nonce), 213 Code: override.Code, 214 Balance: (*hexutil.Big)(override.Balance), 215 State: override.State, 216 StateDiff: override.StateDiff, 217 } 218 } 219 return &result 220 }