This commit is contained in:
allen 2025-10-10 11:01:11 -04:00
parent c8b67825a2
commit ab4049d119
5 changed files with 109 additions and 138 deletions

View file

@ -28,7 +28,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
clienttypes "github.com/ethereum/go-ethereum/ethclient/types"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -831,16 +830,16 @@ type SimulateOptions struct {
// SimulateBlock represents a batch of calls to be simulated. // SimulateBlock represents a batch of calls to be simulated.
type SimulateBlock struct { type SimulateBlock struct {
BlockOverrides *clienttypes.BlockOverrides `json:"blockOverrides,omitempty"` BlockOverrides *ethereum.BlockOverrides `json:"blockOverrides,omitempty"`
StateOverrides *map[common.Address]clienttypes.OverrideAccount `json:"stateOverrides,omitempty"` StateOverrides *map[common.Address]ethereum.OverrideAccount `json:"stateOverrides,omitempty"`
Calls []ethereum.CallMsg `json:"calls"` Calls []ethereum.CallMsg `json:"calls"`
} }
// MarshalJSON implements json.Marshaler for SimulateBlock. // MarshalJSON implements json.Marshaler for SimulateBlock.
func (s SimulateBlock) MarshalJSON() ([]byte, error) { func (s SimulateBlock) MarshalJSON() ([]byte, error) {
type Alias struct { type Alias struct {
BlockOverrides *clienttypes.BlockOverrides `json:"blockOverrides,omitempty"` BlockOverrides *ethereum.BlockOverrides `json:"blockOverrides,omitempty"`
StateOverrides *map[common.Address]clienttypes.OverrideAccount `json:"stateOverrides,omitempty"` StateOverrides *map[common.Address]ethereum.OverrideAccount `json:"stateOverrides,omitempty"`
Calls []interface{} `json:"calls"` Calls []interface{} `json:"calls"`
} }
calls := make([]interface{}, len(s.Calls)) calls := make([]interface{}, len(s.Calls))

View file

@ -37,7 +37,6 @@ import (
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
clienttypes "github.com/ethereum/go-ethereum/ethclient/types"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
@ -851,7 +850,7 @@ func TestSimulateV1WithBlockOverrides(t *testing.T) {
opts := ethclient.SimulateOptions{ opts := ethclient.SimulateOptions{
BlockStateCalls: []ethclient.SimulateBlock{ BlockStateCalls: []ethclient.SimulateBlock{
{ {
BlockOverrides: &clienttypes.BlockOverrides{ BlockOverrides: &ethereum.BlockOverrides{
Time: timestamp, Time: timestamp,
}, },
Calls: []ethereum.CallMsg{ Calls: []ethereum.CallMsg{
@ -912,7 +911,7 @@ func TestSimulateV1WithStateOverrides(t *testing.T) {
balance := new(big.Int) balance := new(big.Int)
balance.SetString(balanceStr, 10) balance.SetString(balanceStr, 10)
stateOverrides := map[common.Address]clienttypes.OverrideAccount{ stateOverrides := map[common.Address]ethereum.OverrideAccount{
from: { from: {
Balance: balance, Balance: balance,
}, },

View file

@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/tracers" "github.com/ethereum/go-ethereum/eth/tracers"
clienttypes "github.com/ethereum/go-ethereum/ethclient/types"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
@ -280,10 +279,8 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
return arg return arg
} }
// OverrideAccount is an alias for ethclient/types.OverrideAccount. // OverrideAccount is an alias for ethereum.OverrideAccount.
// Deprecated: Use clienttypes.OverrideAccount instead. type OverrideAccount = ethereum.OverrideAccount
type OverrideAccount = clienttypes.OverrideAccount
// BlockOverrides is an alias for ethclient/types.BlockOverrides. // BlockOverrides is an alias for ethereum.BlockOverrides.
// Deprecated: Use clienttypes.BlockOverrides instead. type BlockOverrides = ethereum.BlockOverrides
type BlockOverrides = clienttypes.BlockOverrides

View file

@ -1,121 +0,0 @@
// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package types contains common types.
package types
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// OverrideAccount specifies the state of an account to be overridden.
type OverrideAccount struct {
// Nonce sets nonce of the account. Note: the nonce override will only
// be applied when it is set to a non-zero value.
Nonce uint64
// Code sets the contract code. The override will be applied
// when the code is non-nil, i.e. setting empty code is possible
// using an empty slice.
Code []byte
// Balance sets the account balance.
Balance *big.Int
// State sets the complete storage. The override will be applied
// when the given map is non-nil. Using an empty map wipes the
// entire contract storage during the call.
State map[common.Hash]common.Hash
// StateDiff allows overriding individual storage slots.
StateDiff map[common.Hash]common.Hash
}
func (a OverrideAccount) MarshalJSON() ([]byte, error) {
type acc struct {
Nonce hexutil.Uint64 `json:"nonce,omitempty"`
Code string `json:"code,omitempty"`
Balance *hexutil.Big `json:"balance,omitempty"`
State interface{} `json:"state,omitempty"`
StateDiff map[common.Hash]common.Hash `json:"stateDiff,omitempty"`
}
output := acc{
Nonce: hexutil.Uint64(a.Nonce),
Balance: (*hexutil.Big)(a.Balance),
StateDiff: a.StateDiff,
}
if a.Code != nil {
output.Code = hexutil.Encode(a.Code)
}
if a.State != nil {
output.State = a.State
}
return json.Marshal(output)
}
// BlockOverrides specifies the set of header fields to override.
type BlockOverrides struct {
// Number overrides the block number.
Number *big.Int
// Difficulty overrides the block difficulty.
Difficulty *big.Int
// Time overrides the block timestamp. Time is applied only when
// it is non-zero.
Time uint64
// GasLimit overrides the block gas limit. GasLimit is applied only when
// it is non-zero.
GasLimit uint64
// Coinbase overrides the block coinbase. Coinbase is applied only when
// it is different from the zero address.
Coinbase common.Address
// Random overrides the block extra data which feeds into the RANDOM opcode.
// Random is applied only when it is a non-zero hash.
Random common.Hash
// BaseFee overrides the block base fee.
BaseFee *big.Int
}
func (o BlockOverrides) MarshalJSON() ([]byte, error) {
type override struct {
Number *hexutil.Big `json:"number,omitempty"`
Difficulty *hexutil.Big `json:"difficulty,omitempty"`
Time hexutil.Uint64 `json:"time,omitempty"`
GasLimit hexutil.Uint64 `json:"gasLimit,omitempty"`
Coinbase *common.Address `json:"feeRecipient,omitempty"`
Random *common.Hash `json:"prevRandao,omitempty"`
BaseFee *hexutil.Big `json:"baseFeePerGas,omitempty"`
}
output := override{
Number: (*hexutil.Big)(o.Number),
Difficulty: (*hexutil.Big)(o.Difficulty),
Time: hexutil.Uint64(o.Time),
GasLimit: hexutil.Uint64(o.GasLimit),
BaseFee: (*hexutil.Big)(o.BaseFee),
}
if o.Coinbase != (common.Address{}) {
output.Coinbase = &o.Coinbase
}
if o.Random != (common.Hash{}) {
output.Random = &o.Random
}
return json.Marshal(output)
}

View file

@ -19,10 +19,12 @@ package ethereum
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
@ -281,3 +283,98 @@ type BlockNumberReader interface {
type ChainIDReader interface { type ChainIDReader interface {
ChainID(ctx context.Context) (*big.Int, error) ChainID(ctx context.Context) (*big.Int, error)
} }
// OverrideAccount specifies the state of an account to be overridden.
type OverrideAccount struct {
// Nonce sets nonce of the account. Note: the nonce override will only
// be applied when it is set to a non-zero value.
Nonce uint64
// Code sets the contract code. The override will be applied
// when the code is non-nil, i.e. setting empty code is possible
// using an empty slice.
Code []byte
// Balance sets the account balance.
Balance *big.Int
// State sets the complete storage. The override will be applied
// when the given map is non-nil. Using an empty map wipes the
// entire contract storage during the call.
State map[common.Hash]common.Hash
// StateDiff allows overriding individual storage slots.
StateDiff map[common.Hash]common.Hash
}
func (a OverrideAccount) MarshalJSON() ([]byte, error) {
type acc struct {
Nonce hexutil.Uint64 `json:"nonce,omitempty"`
Code string `json:"code,omitempty"`
Balance *hexutil.Big `json:"balance,omitempty"`
State interface{} `json:"state,omitempty"`
StateDiff map[common.Hash]common.Hash `json:"stateDiff,omitempty"`
}
output := acc{
Nonce: hexutil.Uint64(a.Nonce),
Balance: (*hexutil.Big)(a.Balance),
StateDiff: a.StateDiff,
}
if a.Code != nil {
output.Code = hexutil.Encode(a.Code)
}
if a.State != nil {
output.State = a.State
}
return json.Marshal(output)
}
// BlockOverrides specifies the set of header fields to override.
type BlockOverrides struct {
// Number overrides the block number.
Number *big.Int
// Difficulty overrides the block difficulty.
Difficulty *big.Int
// Time overrides the block timestamp. Time is applied only when
// it is non-zero.
Time uint64
// GasLimit overrides the block gas limit. GasLimit is applied only when
// it is non-zero.
GasLimit uint64
// Coinbase overrides the block coinbase. Coinbase is applied only when
// it is different from the zero address.
Coinbase common.Address
// Random overrides the block extra data which feeds into the RANDOM opcode.
// Random is applied only when it is a non-zero hash.
Random common.Hash
// BaseFee overrides the block base fee.
BaseFee *big.Int
}
func (o BlockOverrides) MarshalJSON() ([]byte, error) {
type override struct {
Number *hexutil.Big `json:"number,omitempty"`
Difficulty *hexutil.Big `json:"difficulty,omitempty"`
Time hexutil.Uint64 `json:"time,omitempty"`
GasLimit hexutil.Uint64 `json:"gasLimit,omitempty"`
Coinbase *common.Address `json:"feeRecipient,omitempty"`
Random *common.Hash `json:"prevRandao,omitempty"`
BaseFee *hexutil.Big `json:"baseFeePerGas,omitempty"`
}
output := override{
Number: (*hexutil.Big)(o.Number),
Difficulty: (*hexutil.Big)(o.Difficulty),
Time: hexutil.Uint64(o.Time),
GasLimit: hexutil.Uint64(o.GasLimit),
BaseFee: (*hexutil.Big)(o.BaseFee),
}
if o.Coinbase != (common.Address{}) {
output.Coinbase = &o.Coinbase
}
if o.Random != (common.Hash{}) {
output.Random = &o.Random
}
return json.Marshal(output)
}