@@ -19,10 +19,12 @@ package ethereum
1919
2020import (
2121 "context"
22+ "encoding/json"
2223 "errors"
2324 "math/big"
2425
2526 "github.com/ethereum/go-ethereum/common"
27+ "github.com/ethereum/go-ethereum/common/hexutil"
2628 "github.com/ethereum/go-ethereum/core/types"
2729)
2830
@@ -281,3 +283,98 @@ type BlockNumberReader interface {
281283type ChainIDReader interface {
282284 ChainID (ctx context.Context ) (* big.Int , error )
283285}
286+
287+ // OverrideAccount specifies the state of an account to be overridden.
288+ type OverrideAccount struct {
289+ // Nonce sets nonce of the account. Note: the nonce override will only
290+ // be applied when it is set to a non-zero value.
291+ Nonce uint64
292+
293+ // Code sets the contract code. The override will be applied
294+ // when the code is non-nil, i.e. setting empty code is possible
295+ // using an empty slice.
296+ Code []byte
297+
298+ // Balance sets the account balance.
299+ Balance * big.Int
300+
301+ // State sets the complete storage. The override will be applied
302+ // when the given map is non-nil. Using an empty map wipes the
303+ // entire contract storage during the call.
304+ State map [common.Hash ]common.Hash
305+
306+ // StateDiff allows overriding individual storage slots.
307+ StateDiff map [common.Hash ]common.Hash
308+ }
309+
310+ func (a OverrideAccount ) MarshalJSON () ([]byte , error ) {
311+ type acc struct {
312+ Nonce hexutil.Uint64 `json:"nonce,omitempty"`
313+ Code string `json:"code,omitempty"`
314+ Balance * hexutil.Big `json:"balance,omitempty"`
315+ State interface {} `json:"state,omitempty"`
316+ StateDiff map [common.Hash ]common.Hash `json:"stateDiff,omitempty"`
317+ }
318+
319+ output := acc {
320+ Nonce : hexutil .Uint64 (a .Nonce ),
321+ Balance : (* hexutil .Big )(a .Balance ),
322+ StateDiff : a .StateDiff ,
323+ }
324+ if a .Code != nil {
325+ output .Code = hexutil .Encode (a .Code )
326+ }
327+ if a .State != nil {
328+ output .State = a .State
329+ }
330+ return json .Marshal (output )
331+ }
332+
333+ // BlockOverrides specifies the set of header fields to override.
334+ type BlockOverrides struct {
335+ // Number overrides the block number.
336+ Number * big.Int
337+ // Difficulty overrides the block difficulty.
338+ Difficulty * big.Int
339+ // Time overrides the block timestamp. Time is applied only when
340+ // it is non-zero.
341+ Time uint64
342+ // GasLimit overrides the block gas limit. GasLimit is applied only when
343+ // it is non-zero.
344+ GasLimit uint64
345+ // Coinbase overrides the block coinbase. Coinbase is applied only when
346+ // it is different from the zero address.
347+ Coinbase common.Address
348+ // Random overrides the block extra data which feeds into the RANDOM opcode.
349+ // Random is applied only when it is a non-zero hash.
350+ Random common.Hash
351+ // BaseFee overrides the block base fee.
352+ BaseFee * big.Int
353+ }
354+
355+ func (o BlockOverrides ) MarshalJSON () ([]byte , error ) {
356+ type override struct {
357+ Number * hexutil.Big `json:"number,omitempty"`
358+ Difficulty * hexutil.Big `json:"difficulty,omitempty"`
359+ Time hexutil.Uint64 `json:"time,omitempty"`
360+ GasLimit hexutil.Uint64 `json:"gasLimit,omitempty"`
361+ Coinbase * common.Address `json:"feeRecipient,omitempty"`
362+ Random * common.Hash `json:"prevRandao,omitempty"`
363+ BaseFee * hexutil.Big `json:"baseFeePerGas,omitempty"`
364+ }
365+
366+ output := override {
367+ Number : (* hexutil .Big )(o .Number ),
368+ Difficulty : (* hexutil .Big )(o .Difficulty ),
369+ Time : hexutil .Uint64 (o .Time ),
370+ GasLimit : hexutil .Uint64 (o .GasLimit ),
371+ BaseFee : (* hexutil .Big )(o .BaseFee ),
372+ }
373+ if o .Coinbase != (common.Address {}) {
374+ output .Coinbase = & o .Coinbase
375+ }
376+ if o .Random != (common.Hash {}) {
377+ output .Random = & o .Random
378+ }
379+ return json .Marshal (output )
380+ }
0 commit comments