Skip to content

Commit a312417

Browse files
committed
Merge branch 'libevm' into arr4n/jumptable-override
2 parents 524167e + 53ef071 commit a312417

30 files changed

+483
-19
lines changed

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- gci
1717
- gocheckcompilerdirectives
1818
- gofmt
19+
- goheader
1920
- goimports
2021
- gomodguard
2122
- gosec
@@ -53,6 +54,10 @@ linters-settings:
5354
- alias
5455
- dot
5556
- blank
57+
goheader:
58+
values:
59+
template-path: .libevm-header
60+
5661
gomodguard:
5762
blocked:
5863
modules:
@@ -78,6 +83,7 @@ issues:
7883
- errcheck
7984
- gci
8085
- gofmt
86+
- goheader
8187
- gosec
8288
- gosimple
8389
- govet
@@ -92,6 +98,7 @@ issues:
9298
- testifylint
9399
- thelper
94100
- tparallel
101+
- typecheck
95102
- usestdlibvars
96103
- varnamelen
97104
- wastedassign

.libevm-header

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Copyright {{ MOD-YEAR }} the libevm authors.
2+
3+
The libevm additions to go-ethereum are free software: you can redistribute
4+
them and/or modify them under the terms of the GNU Lesser General Public License
5+
as published by the Free Software Foundation, either version 3 of the License,
6+
or (at your option) any later version.
7+
8+
The libevm additions are distributed in the hope that they will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
General Public License for more details.
12+
13+
You should have received a copy of the GNU Lesser General Public License
14+
along with the go-ethereum library. If not, see
15+
<http://www.gnu.org/licenses/>.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,9 @@ also included in our repository in the `COPYING.LESSER` file.
356356
The go-ethereum binaries (i.e. all code inside of the `cmd` directory) are licensed under the
357357
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), also
358358
included in our repository in the `COPYING` file.
359+
360+
The libevm (i) _additions_ to the go-ethereum library (i.e. all code in files with `libevm` in their full path,
361+
be it a directory or file name); and (ii) _modifications_ to existing go-ethereum code; are licensed under the
362+
[GNU Lesser General Public License v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html),
363+
also included in our repository in the `COPYING.LESSER` file. A comprehensive outline of _modifications_ is
364+
produced by the [libevm delta workflow](https://github.com/ava-labs/go-ethereum/actions/workflows/libevm-delta.yml).

core/state_transition.libevm.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
116
package core
217

318
// canExecuteTransaction is a convenience wrapper for calling the

core/state_transition.libevm_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
116
package core_test
217

318
import (

core/vm/contracts.libevm.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
117
package vm
218

319
import (

core/vm/contracts.libevm_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
116
package vm_test
217

318
import (

core/vm/evm.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
159159
// Reset resets the EVM with a new transaction context.Reset
160160
// This is not threadsafe and should only be done very cautiously.
161161
func (evm *EVM) Reset(txCtx TxContext, statedb StateDB) {
162-
evm.TxContext = txCtx
163-
evm.StateDB = statedb
162+
evm.TxContext, evm.StateDB = overrideEVMResetArgs(txCtx, statedb)
164163
}
165164

166165
// Cancel cancels any running EVM operation. This may be called concurrently and

core/vm/evm.libevm_test.go

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
116
package vm
217

318
import (
@@ -9,16 +24,35 @@ import (
924
"github.com/ethereum/go-ethereum/params"
1025
)
1126

12-
type chainIDOverrider struct {
13-
chainID int64
27+
type evmArgOverrider struct {
28+
newEVMchainID int64
29+
30+
resetTxCtx TxContext
31+
resetStateDB StateDB
1432
}
1533

16-
func (o chainIDOverrider) OverrideNewEVMArgs(args *NewEVMArgs) *NewEVMArgs {
17-
args.ChainConfig = &params.ChainConfig{ChainID: big.NewInt(o.chainID)}
34+
func (o evmArgOverrider) OverrideNewEVMArgs(args *NewEVMArgs) *NewEVMArgs {
35+
args.ChainConfig = &params.ChainConfig{ChainID: big.NewInt(o.newEVMchainID)}
1836
return args
1937
}
2038

21-
func (chainIDOverrider) OverrideJumpTable(_ params.Rules, jt *JumpTable) *JumpTable { return jt }
39+
func (evmArgOverrider) OverrideJumpTable(_ params.Rules, jt *JumpTable) *JumpTable { return jt }
40+
41+
func (o evmArgOverrider) OverrideEVMResetArgs(*EVMResetArgs) *EVMResetArgs {
42+
return &EVMResetArgs{
43+
TxContext: o.resetTxCtx,
44+
StateDB: o.resetStateDB,
45+
}
46+
}
47+
48+
func (o evmArgOverrider) register(t *testing.T) {
49+
t.Helper()
50+
libevmHooks = nil
51+
RegisterHooks(o)
52+
t.Cleanup(func() {
53+
libevmHooks = nil
54+
})
55+
}
2256

2357
func TestOverrideNewEVMArgs(t *testing.T) {
2458
// The overrideNewEVMArgs function accepts and returns all arguments to
@@ -27,10 +61,27 @@ func TestOverrideNewEVMArgs(t *testing.T) {
2761
var _ func(BlockContext, TxContext, StateDB, *params.ChainConfig, Config) *EVM = NewEVM
2862

2963
const chainID = 13579
30-
libevmHooks = nil
31-
RegisterHooks(chainIDOverrider{chainID: chainID})
32-
defer func() { libevmHooks = nil }()
64+
hooks := evmArgOverrider{newEVMchainID: chainID}
65+
hooks.register(t)
66+
67+
evm := NewEVM(BlockContext{}, TxContext{}, nil, nil, Config{})
68+
got := evm.ChainConfig().ChainID
69+
require.Equalf(t, big.NewInt(chainID), got, "%T.ChainConfig().ChainID set by NewEVM() hook", evm)
70+
}
71+
72+
func TestOverrideEVMResetArgs(t *testing.T) {
73+
// Equivalent to rationale for TestOverrideNewEVMArgs above.
74+
var _ func(TxContext, StateDB) = (*EVM)(nil).Reset
75+
76+
const gasPrice = 1357924680
77+
hooks := evmArgOverrider{
78+
resetTxCtx: TxContext{
79+
GasPrice: big.NewInt(gasPrice),
80+
},
81+
}
82+
hooks.register(t)
3383

34-
got := NewEVM(BlockContext{}, TxContext{}, nil, nil, Config{}).ChainConfig().ChainID
35-
require.Equal(t, big.NewInt(chainID), got)
84+
evm := NewEVM(BlockContext{}, TxContext{}, nil, nil, Config{})
85+
evm.Reset(TxContext{}, nil)
86+
require.Equalf(t, big.NewInt(gasPrice), evm.GasPrice, "%T.GasPrice set by Reset() hook", evm)
3687
}

core/vm/hooks.libevm.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
117
package vm
218

319
import "github.com/ethereum/go-ethereum/params"
@@ -14,12 +30,15 @@ func RegisterHooks(h Hooks) {
1430
var libevmHooks Hooks
1531

1632
// Hooks are arbitrary configuration functions to modify default VM behaviour.
33+
// See [RegisterHooks].
1734
type Hooks interface {
1835
OverrideNewEVMArgs(*NewEVMArgs) *NewEVMArgs
36+
OverrideEVMResetArgs(*EVMResetArgs) *EVMResetArgs
1937
OverrideJumpTable(params.Rules, *JumpTable) *JumpTable
2038
}
2139

22-
// NewEVMArgs are the arguments received by [NewEVM], available for override.
40+
// NewEVMArgs are the arguments received by [NewEVM], available for override
41+
// via [Hooks].
2342
type NewEVMArgs struct {
2443
BlockContext BlockContext
2544
TxContext TxContext
@@ -28,11 +47,11 @@ type NewEVMArgs struct {
2847
Config Config
2948
}
3049

31-
func overrideJumpTable(r params.Rules, jt *JumpTable) *JumpTable {
32-
if libevmHooks == nil {
33-
return jt
34-
}
35-
return libevmHooks.OverrideJumpTable(r, jt)
50+
// EVMResetArgs are the arguments received by [EVM.Reset], available for
51+
// override via [Hooks].
52+
type EVMResetArgs struct {
53+
TxContext TxContext
54+
StateDB StateDB
3655
}
3756

3857
func overrideNewEVMArgs(
@@ -48,3 +67,18 @@ func overrideNewEVMArgs(
4867
args := libevmHooks.OverrideNewEVMArgs(&NewEVMArgs{blockCtx, txCtx, statedb, chainConfig, config})
4968
return args.BlockContext, args.TxContext, args.StateDB, args.ChainConfig, args.Config
5069
}
70+
71+
func overrideEVMResetArgs(txCtx TxContext, statedb StateDB) (TxContext, StateDB) {
72+
if libevmHooks == nil {
73+
return txCtx, statedb
74+
}
75+
args := libevmHooks.OverrideEVMResetArgs(&EVMResetArgs{txCtx, statedb})
76+
return args.TxContext, args.StateDB
77+
}
78+
79+
func overrideJumpTable(r params.Rules, jt *JumpTable) *JumpTable {
80+
if libevmHooks == nil {
81+
return jt
82+
}
83+
return libevmHooks.OverrideJumpTable(r, jt)
84+
}

0 commit comments

Comments
 (0)