@@ -19,6 +19,7 @@ package gethclient
1919
2020import (
2121 "context"
22+ "encoding/json"
2223 "fmt"
2324 "math/big"
2425 "runtime"
@@ -119,15 +120,6 @@ func (ec *Client) GetProof(ctx context.Context, account common.Address, keys []s
119120 return & result , err
120121}
121122
122- // OverrideAccount specifies the state of an account to be overridden.
123- type OverrideAccount struct {
124- Nonce uint64 `json:"nonce"`
125- Code []byte `json:"code"`
126- Balance * big.Int `json:"balance"`
127- State map [common.Hash ]common.Hash `json:"state"`
128- StateDiff map [common.Hash ]common.Hash `json:"stateDiff"`
129- }
130-
131123// CallContract executes a message call transaction, which is directly executed in the VM
132124// of the node, but never mined into the blockchain.
133125//
@@ -147,6 +139,28 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN
147139 return hex , err
148140}
149141
142+ // CallContractWithBlockOverrides executes a message call transaction, which is directly executed
143+ // in the VM of the node, but never mined into the blockchain.
144+ //
145+ // blockNumber selects the block height at which the call runs. It can be nil, in which
146+ // case the code is taken from the latest known block. Note that state from very old
147+ // blocks might not be available.
148+ //
149+ // overrides specifies a map of contract states that should be overwritten before executing
150+ // the message call.
151+ //
152+ // blockOverrides specifies block fields exposed to the EVM that can be overridden for the call.
153+ //
154+ // Please use ethclient.CallContract instead if you don't need the override functionality.
155+ func (ec * Client ) CallContractWithBlockOverrides (ctx context.Context , msg ethereum.CallMsg , blockNumber * big.Int , overrides * map [common.Address ]OverrideAccount , blockOverrides BlockOverrides ) ([]byte , error ) {
156+ var hex hexutil.Bytes
157+ err := ec .c .CallContext (
158+ ctx , & hex , "eth_call" , toCallArg (msg ),
159+ toBlockNumArg (blockNumber ), overrides , blockOverrides ,
160+ )
161+ return hex , err
162+ }
163+
150164// GCStats retrieves the current garbage collection stats from a geth node.
151165func (ec * Client ) GCStats (ctx context.Context ) (* debug.GCStats , error ) {
152166 var result debug.GCStats
@@ -175,6 +189,11 @@ func (ec *Client) GetNodeInfo(ctx context.Context) (*p2p.NodeInfo, error) {
175189 return & result , err
176190}
177191
192+ // SubscribeFullPendingTransactions subscribes to new pending transactions.
193+ func (ec * Client ) SubscribeFullPendingTransactions (ctx context.Context , ch chan <- * types.Transaction ) (* rpc.ClientSubscription , error ) {
194+ return ec .c .EthSubscribe (ctx , ch , "newPendingTransactions" , true )
195+ }
196+
178197// SubscribePendingTransactions subscribes to new pending transactions.
179198func (ec * Client ) SubscribePendingTransactions (ctx context.Context , ch chan <- common.Hash ) (* rpc.ClientSubscription , error ) {
180199 return ec .c .EthSubscribe (ctx , ch , "newPendingTransactions" )
@@ -224,6 +243,29 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
224243 return arg
225244}
226245
246+ // OverrideAccount specifies the state of an account to be overridden.
247+ type OverrideAccount struct {
248+ // Nonce sets nonce of the account. Note: the nonce override will only
249+ // be applied when it is set to a non-zero value.
250+ Nonce uint64 `json:"nonce"`
251+
252+ // Code sets the contract code. The override will be applied
253+ // when the code is non-nil, i.e. setting empty code is possible
254+ // using an empty slice.
255+ Code []byte `json:"code"`
256+
257+ // Balance sets the account balance.
258+ Balance * big.Int `json:"balance"`
259+
260+ // State sets the complete storage. The override will be applied
261+ // when the given map is non-nil. Using an empty map wipes the
262+ // entire contract storage during the call.
263+ State map [common.Hash ]common.Hash `json:"state"`
264+
265+ // StateDiff allows overriding individual storage slots.
266+ StateDiff map [common.Hash ]common.Hash `json:"stateDiff"`
267+ }
268+
227269func toOverrideMap (overrides * map [common.Address ]OverrideAccount ) interface {} {
228270 if overrides == nil {
229271 return nil
@@ -247,3 +289,52 @@ func toOverrideMap(overrides *map[common.Address]OverrideAccount) interface{} {
247289 }
248290 return & result
249291}
292+
293+ // BlockOverrides specifies the set of header fields to override.
294+ type BlockOverrides struct {
295+ // Number overrides the block number.
296+ Number * big.Int
297+ // Difficulty overrides the block difficulty.
298+ Difficulty * big.Int
299+ // Time overrides the block timestamp. Time is applied only when
300+ // it is non-zero.
301+ Time uint64
302+ // GasLimit overrides the block gas limit. GasLimit is applied only when
303+ // it is non-zero.
304+ GasLimit uint64
305+ // Coinbase overrides the block coinbase. Coinbase is applied only when
306+ // it is different from the zero address.
307+ Coinbase common.Address
308+ // Random overrides the block extra data which feeds into the RANDOM opcode.
309+ // Random is applied only when it is a non-zero hash.
310+ Random common.Hash
311+ // BaseFee overrides the block base fee.
312+ BaseFee * big.Int
313+ }
314+
315+ func (o BlockOverrides ) MarshalJSON () ([]byte , error ) {
316+ type override struct {
317+ Number * hexutil.Big `json:"number,omitempty"`
318+ Difficulty * hexutil.Big `json:"difficulty,omitempty"`
319+ Time hexutil.Uint64 `json:"time,omitempty"`
320+ GasLimit hexutil.Uint64 `json:"gasLimit,omitempty"`
321+ Coinbase * common.Address `json:"coinbase,omitempty"`
322+ Random * common.Hash `json:"random,omitempty"`
323+ BaseFee * hexutil.Big `json:"baseFee,omitempty"`
324+ }
325+
326+ output := override {
327+ Number : (* hexutil .Big )(o .Number ),
328+ Difficulty : (* hexutil .Big )(o .Difficulty ),
329+ Time : hexutil .Uint64 (o .Time ),
330+ GasLimit : hexutil .Uint64 (o .GasLimit ),
331+ BaseFee : (* hexutil .Big )(o .BaseFee ),
332+ }
333+ if o .Coinbase != (common.Address {}) {
334+ output .Coinbase = & o .Coinbase
335+ }
336+ if o .Random != (common.Hash {}) {
337+ output .Random = & o .Random
338+ }
339+ return json .Marshal (output )
340+ }
0 commit comments