Skip to content

Commit 71d540f

Browse files
authored
Merge pull request #21 from crypto-org-chain/refactor/remove-active-precompiles-from-vm-context
chore: remove active precompiles from vm context
2 parents 0af6c98 + a4fbafc commit 71d540f

File tree

16 files changed

+57
-69
lines changed

16 files changed

+57
-69
lines changed

.github/workflows/go.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
name: i386 linux tests
22

33
on:
4-
push:
5-
branches: [ master ]
4+
push:
65
pull_request:
76
branches: [ master ]
87
workflow_dispatch:
98

109
jobs:
1110
lint:
1211
name: Lint
13-
runs-on: self-hosted
12+
runs-on: ubuntu-latest
1413
steps:
1514
- uses: actions/checkout@v4
1615

@@ -33,7 +32,7 @@ jobs:
3332
go run build/ci.go check_baddeps
3433
3534
build:
36-
runs-on: self-hosted
35+
runs-on: ubuntu-latest
3736
steps:
3837
- uses: actions/checkout@v4
3938
- name: Set up Go
@@ -42,7 +41,7 @@ jobs:
4241
go-version: 1.24.0
4342
cache: false
4443
- name: Run tests
45-
run: go test -short ./...
44+
run: make test
4645
env:
4746
GOOS: linux
48-
GOARCH: 386
47+
GOARCH: 386

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
### State Machine Breaking
66

77
- [#20](https://github.com/crypto-org-chain/go-ethereum/pull/20) feat: support cronos precompiles
8+
- [#21](https://github.com/crypto-org-chain/go-ethereum/pull/21) chore: Remove active precompiles from VMContext and fixed tests

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
.PHONY: geth all test lint fmt clean devtools help
66

7+
TESTPATH?= ./...
8+
79
GOBIN = ./build/bin
810
GO ?= latest
911
GORUN = go run
@@ -19,8 +21,10 @@ all:
1921
$(GORUN) build/ci.go install
2022

2123
#? test: Run the tests.
24+
#? to test a specific package eg `make test TESTPATH=./core/vm`
25+
#? to test specific packages eg `make test TESTPATH="./core/vm ./eth/tracers/native ./internal/ethapi"`
2226
test: all
23-
$(GORUN) build/ci.go test
27+
$(GORUN) build/ci.go test $(TESTPATH)
2428

2529
#? lint: Run certain pre-selected linters.
2630
lint: ## Run linters.

build/checksums.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# version:spec-tests pectra-devnet-6@v1.0.0
44
# https://github.com/ethereum/execution-spec-tests/releases
55
# https://github.com/ethereum/execution-spec-tests/releases/download/pectra-devnet-6%40v1.0.0/
6-
b69211752a3029083c020dc635fe12156ca1a6725a08559da540a0337586a77e fixtures_pectra-devnet-6.tar.gz
6+
5ed3bec40a02776a4096c17b84b59fb20fd99fbabba8101fdac3d35a001cf82a fixtures_pectra-devnet-6.tar.gz
77

88
# version:golang 1.24.2
99
# https://go.dev/dl/

core/state_transition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
476476
// Execute the preparatory steps for state transition which includes:
477477
// - prepare accessList(post-berlin)
478478
// - reset transient storage(eip 1153)
479-
st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, st.evm.ActivePrecompiles(rules), msg.AccessList)
479+
st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, vm.ActivePrecompiles(rules), msg.AccessList)
480480

481481
var (
482482
ret []byte

core/tracing/hooks.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ type StateDB interface {
5959

6060
// VMContext provides the context for the EVM execution.
6161
type VMContext struct {
62-
Coinbase common.Address
63-
BlockNumber *big.Int
64-
Time uint64
65-
Random *common.Hash
66-
BaseFee *big.Int
67-
StateDB StateDB
68-
ActivePrecompiles []common.Address
62+
Coinbase common.Address
63+
BlockNumber *big.Int
64+
Time uint64
65+
Random *common.Hash
66+
BaseFee *big.Int
67+
StateDB StateDB
6968
}
7069

7170
// BlockEvent is emitted upon tracing an incoming block.

core/vm/contracts.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ func ActivePrecompiledContracts(rules params.Rules) PrecompiledContracts {
200200
return maps.Clone(activePrecompiledContracts(rules))
201201
}
202202

203-
// DefaultActivePrecompiles returns the set of precompiles enabled with the default configuration.
204-
func DefaultActivePrecompiles(rules params.Rules) []common.Address {
203+
// ActivePrecompiles returns the precompile addresses enabled with the current configuration.
204+
func ActivePrecompiles(rules params.Rules) []common.Address {
205205
switch {
206206
case rules.IsPrague:
207207
return PrecompiledAddressesPrague
@@ -219,7 +219,7 @@ func DefaultActivePrecompiles(rules params.Rules) []common.Address {
219219
}
220220

221221
// DefaultPrecompiles define the mapping of address and precompiles from the default configuration
222-
func DefaultPrecompiles(rules params.Rules) (precompiles map[common.Address]PrecompiledContract) {
222+
func DefaultPrecompiles(rules params.Rules) (precompiles PrecompiledContracts) {
223223
switch {
224224
case rules.IsVerkle:
225225
precompiles = PrecompiledContractsVerkle
@@ -240,32 +240,13 @@ func DefaultPrecompiles(rules params.Rules) (precompiles map[common.Address]Prec
240240
return precompiles
241241
}
242242

243-
// ActivePrecompiles returns the precompiles enabled with the current configuration.
244-
//
245-
// NOTE: The rules argument is ignored as the active precompiles can be set via the WithPrecompiles
246-
// method according to the chain rules from the current block context.
247-
func (evm *EVM) ActivePrecompiles(_ params.Rules) []common.Address {
248-
return evm.activePrecompiles
249-
}
250-
251243
// Precompile returns a precompiled contract for the given address. This
252244
// function returns false if the address is not a registered precompile.
253245
func (evm *EVM) Precompile(addr common.Address) (PrecompiledContract, bool) {
254246
p, ok := evm.precompiles[addr]
255247
return p, ok
256248
}
257249

258-
// WithPrecompiles sets the precompiled contracts and the slice of actives precompiles.
259-
// IMPORTANT: This function does NOT validate the precompiles provided to the EVM. The caller should
260-
// use the ValidatePrecompiles function for this purpose prior to calling WithPrecompiles.
261-
func (evm *EVM) WithPrecompiles(
262-
precompiles map[common.Address]PrecompiledContract,
263-
activePrecompiles []common.Address,
264-
) {
265-
evm.precompiles = precompiles
266-
evm.activePrecompiles = activePrecompiles
267-
}
268-
269250
// ValidatePrecompiles validates the precompile map against the active
270251
// precompile slice.
271252
// It returns an error if the precompiled contract map has a different length
@@ -908,7 +889,7 @@ type bls12381G1Add struct{}
908889
// Address defines the precompiled contract address. This MUST match the address
909890
// set in the precompiled contract map.
910891
func (bls12381G1Add) Address() common.Address {
911-
return common.BytesToAddress([]byte{10})
892+
return common.BytesToAddress([]byte{11})
912893
}
913894

914895
// RequiredGas returns the gas required to execute the pre-compiled contract.
@@ -1059,7 +1040,7 @@ type bls12381G2MultiExp struct{}
10591040
// Address defines the precompiled contract address. This MUST match the address
10601041
// set in the precompiled contract map.
10611042
func (bls12381G2MultiExp) Address() common.Address {
1062-
return common.BytesToAddress([]byte{15})
1043+
return common.BytesToAddress([]byte{14})
10631044
}
10641045

10651046
// RequiredGas returns the gas required to execute the pre-compiled contract.
@@ -1125,7 +1106,7 @@ type bls12381Pairing struct{}
11251106
// Address defines the precompiled contract address. This MUST match the address
11261107
// set in the precompiled contract map.
11271108
func (bls12381Pairing) Address() common.Address {
1128-
return common.BytesToAddress([]byte{16})
1109+
return common.BytesToAddress([]byte{15})
11291110
}
11301111

11311112
// RequiredGas returns the gas required to execute the pre-compiled contract.
@@ -1283,7 +1264,7 @@ type bls12381MapG1 struct{}
12831264
// Address defines the precompiled contract address. This MUST match the address
12841265
// set in the precompiled contract map.
12851266
func (bls12381MapG1) Address() common.Address {
1286-
return common.BytesToAddress([]byte{17})
1267+
return common.BytesToAddress([]byte{16})
12871268
}
12881269

12891270
// RequiredGas returns the gas required to execute the pre-compiled contract.
@@ -1318,7 +1299,7 @@ type bls12381MapG2 struct{}
13181299
// Address defines the precompiled contract address. This MUST match the address
13191300
// set in the precompiled contract map.
13201301
func (bls12381MapG2) Address() common.Address {
1321-
return common.BytesToAddress([]byte{18})
1302+
return common.BytesToAddress([]byte{17})
13221303
}
13231304

13241305
// RequiredGas returns the gas required to execute the pre-compiled contract.

core/vm/contracts_fuzz_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/params"
2324
)
2425

2526
func FuzzPrecompiledContracts(f *testing.F) {
@@ -36,7 +37,8 @@ func FuzzPrecompiledContracts(f *testing.F) {
3637
return
3738
}
3839
inWant := string(input)
39-
runPrecompiledContract(nil, p, common.Address{}, input, gas, nil, false)
40+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
41+
runPrecompiledContract(evm, p, common.Address{}, input, gas, nil, false)
4042
if inHave := string(input); inWant != inHave {
4143
t.Errorf("Precompiled %v modified input data", a)
4244
}

core/vm/contracts_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/params"
2829
"github.com/holiman/uint256"
2930
)
3031

@@ -97,7 +98,8 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
9798
in := common.Hex2Bytes(test.Input)
9899
gas := p.RequiredGas(in)
99100
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
100-
if res, _, err := runPrecompiledContract(nil, p, common.Address{}, in, gas, new(uint256.Int), false); err != nil {
101+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
102+
if res, _, err := runPrecompiledContract(evm, p, common.Address{}, in, gas, new(uint256.Int), false); err != nil {
101103
t.Error(err)
102104
} else if common.Bytes2Hex(res) != test.Expected {
103105
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
@@ -119,7 +121,8 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
119121
gas := p.RequiredGas(in) - 1
120122

121123
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
122-
_, _, err := runPrecompiledContract(nil, p, common.Address{}, in, gas, new(uint256.Int), false)
124+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
125+
_, _, err := runPrecompiledContract(evm, p, common.Address{}, in, gas, new(uint256.Int), false)
123126
if err.Error() != "out of gas" {
124127
t.Errorf("Expected error [out of gas], got [%v]", err)
125128
}
@@ -136,7 +139,8 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
136139
in := common.Hex2Bytes(test.Input)
137140
gas := p.RequiredGas(in)
138141
t.Run(test.Name, func(t *testing.T) {
139-
_, _, err := runPrecompiledContract(nil, p, common.Address{}, in, gas, new(uint256.Int), false)
142+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
143+
_, _, err := runPrecompiledContract(evm, p, common.Address{}, in, gas, new(uint256.Int), false)
140144
if err.Error() != test.ExpectedError {
141145
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
142146
}
@@ -166,9 +170,10 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
166170
bench.ReportAllocs()
167171
start := time.Now()
168172
bench.ResetTimer()
173+
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})
169174
for i := 0; i < bench.N; i++ {
170175
copy(data, in)
171-
res, _, err = runPrecompiledContract(nil, p, common.Address{}, in, reqGas, new(uint256.Int), false)
176+
res, _, err = runPrecompiledContract(evm, p, common.Address{}, in, reqGas, new(uint256.Int), false)
172177
}
173178
bench.StopTimer()
174179
elapsed := uint64(time.Since(start))

core/vm/evm.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,11 @@ type EVM struct {
115115
callGasTemp uint64
116116

117117
// precompiles holds the precompiled contracts for the current epoch
118-
precompiles map[common.Address]PrecompiledContract
118+
precompiles PrecompiledContracts
119119

120120
// jumpDests is the aggregated result of JUMPDEST analysis made through
121121
// the life cycle of EVM.
122122
jumpDests map[common.Hash]bitvec
123-
124-
// activePrecompiles defines the precompiles that are currently active
125-
activePrecompiles []common.Address
126123
}
127124

128125
// NewEVM constructs an EVM instance with the supplied block context, state
@@ -138,8 +135,6 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon
138135
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
139136
jumpDests: make(map[common.Hash]bitvec),
140137
}
141-
// set the default precompiles
142-
evm.activePrecompiles = DefaultActivePrecompiles(evm.chainRules)
143138
evm.precompiles = DefaultPrecompiles(evm.chainRules)
144139
evm.interpreter = NewEVMInterpreter(evm)
145140

@@ -637,12 +632,11 @@ func (evm *EVM) captureEnd(depth int, startGas uint64, leftOverGas uint64, ret [
637632
// to the tracers.
638633
func (evm *EVM) GetVMContext() *tracing.VMContext {
639634
return &tracing.VMContext{
640-
Coinbase: evm.Context.Coinbase,
641-
BlockNumber: evm.Context.BlockNumber,
642-
Time: evm.Context.Time,
643-
Random: evm.Context.Random,
644-
BaseFee: evm.Context.BaseFee,
645-
StateDB: evm.StateDB,
646-
ActivePrecompiles: evm.activePrecompiles,
635+
Coinbase: evm.Context.Coinbase,
636+
BlockNumber: evm.Context.BlockNumber,
637+
Time: evm.Context.Time,
638+
Random: evm.Context.Random,
639+
BaseFee: evm.Context.BaseFee,
640+
StateDB: evm.StateDB,
647641
}
648642
}

0 commit comments

Comments
 (0)