Skip to content

Commit 81e109a

Browse files
authored
feat: PrecompileEnvironment.ChainConfig() (ethereum#32)
1 parent df13389 commit 81e109a

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

core/vm/contracts.libevm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (p statefulPrecompile) Run([]byte) ([]byte, error) {
9494
// A PrecompileEnvironment provides information about the context in which a
9595
// precompiled contract is being run.
9696
type PrecompileEnvironment interface {
97+
ChainConfig() *params.ChainConfig
9798
Rules() params.Rules
9899
ReadOnly() bool
99100
// StateDB will be non-nil i.f.f !ReadOnly().
@@ -121,7 +122,8 @@ type PrecompileEnvironment interface {
121122

122123
var _ PrecompileEnvironment = (*evmCallArgs)(nil)
123124

124-
func (args *evmCallArgs) Rules() params.Rules { return args.evm.chainRules }
125+
func (args *evmCallArgs) ChainConfig() *params.ChainConfig { return args.evm.chainConfig }
126+
func (args *evmCallArgs) Rules() params.Rules { return args.evm.chainRules }
125127

126128
func (args *evmCallArgs) ReadOnly() bool {
127129
if args.readWrite == inheritReadOnly {

core/vm/contracts.libevm_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestPrecompileOverride(t *testing.T) {
8484
}
8585

8686
type statefulPrecompileOutput struct {
87+
ChainID *big.Int
8788
Caller, Self common.Address
8889
StateValue common.Hash
8990
ReadOnly bool
@@ -127,6 +128,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
127128

128129
addrs := env.Addresses()
129130
out := &statefulPrecompileOutput{
131+
ChainID: env.ChainConfig().ChainID,
130132
Caller: addrs.Caller,
131133
Self: addrs.Self,
132134
StateValue: env.ReadOnlyState().GetState(precompile, slot),
@@ -153,10 +155,17 @@ func TestNewStatefulPrecompile(t *testing.T) {
153155
caller := rng.Address()
154156
input := rng.Bytes(8)
155157
value := rng.Hash()
156-
157-
state, evm := ethtest.NewZeroEVM(t, ethtest.WithBlockContext(
158-
core.NewEVMBlockContext(header, nil, rng.AddressPtr()),
159-
))
158+
chainID := rng.BigUint64()
159+
160+
state, evm := ethtest.NewZeroEVM(
161+
t,
162+
ethtest.WithBlockContext(
163+
core.NewEVMBlockContext(header, nil, rng.AddressPtr()),
164+
),
165+
ethtest.WithChainConfig(
166+
&params.ChainConfig{ChainID: chainID},
167+
),
168+
)
160169
state.SetState(precompile, slot, value)
161170

162171
tests := []struct {
@@ -199,6 +208,7 @@ func TestNewStatefulPrecompile(t *testing.T) {
199208
for _, tt := range tests {
200209
t.Run(tt.name, func(t *testing.T) {
201210
wantReturnData := statefulPrecompileOutput{
211+
ChainID: chainID,
202212
Caller: caller,
203213
Self: precompile,
204214
StateValue: value,

libevm/ethtest/evm.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewZeroEVM(tb testing.TB, opts ...EVMOption) (*state.StateDB, *vm.EVM) {
2525
sdb, err := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
2626
require.NoError(tb, err, "state.New()")
2727

28-
vm := vm.NewEVM(
28+
args := &evmConstructorArgs{
2929
vm.BlockContext{
3030
CanTransfer: core.CanTransfer,
3131
Transfer: core.Transfer,
@@ -34,28 +34,49 @@ func NewZeroEVM(tb testing.TB, opts ...EVMOption) (*state.StateDB, *vm.EVM) {
3434
sdb,
3535
&params.ChainConfig{},
3636
vm.Config{},
37-
)
37+
}
3838
for _, o := range opts {
39-
o.apply(vm)
39+
o.apply(args)
4040
}
4141

42-
return sdb, vm
42+
return sdb, vm.NewEVM(
43+
args.blockContext,
44+
args.txContext,
45+
args.stateDB,
46+
args.chainConfig,
47+
args.config,
48+
)
49+
}
50+
51+
type evmConstructorArgs struct {
52+
blockContext vm.BlockContext
53+
txContext vm.TxContext
54+
stateDB vm.StateDB
55+
chainConfig *params.ChainConfig
56+
config vm.Config
4357
}
4458

4559
// An EVMOption configures the EVM returned by [NewZeroEVM].
4660
type EVMOption interface {
47-
apply(*vm.EVM)
61+
apply(*evmConstructorArgs)
4862
}
4963

50-
type funcOption func(*vm.EVM)
64+
type funcOption func(*evmConstructorArgs)
5165

5266
var _ EVMOption = funcOption(nil)
5367

54-
func (f funcOption) apply(vm *vm.EVM) { f(vm) }
68+
func (f funcOption) apply(args *evmConstructorArgs) { f(args) }
5569

5670
// WithBlockContext overrides the default context.
5771
func WithBlockContext(c vm.BlockContext) EVMOption {
58-
return funcOption(func(vm *vm.EVM) {
59-
vm.Context = c
72+
return funcOption(func(args *evmConstructorArgs) {
73+
args.blockContext = c
74+
})
75+
}
76+
77+
// WithBlockContext overrides the default context.
78+
func WithChainConfig(c *params.ChainConfig) EVMOption {
79+
return funcOption(func(args *evmConstructorArgs) {
80+
args.chainConfig = c
6081
})
6182
}

0 commit comments

Comments
 (0)