From f088bff4ec57f6a7a26841ddf1841ac5d31902f2 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 21 Apr 2026 15:24:23 +0800 Subject: [PATCH] core: rework precompile touch --- core/state/statedb.go | 5 +++++ core/state/statedb_hooked.go | 4 ++++ core/vm/contracts.go | 6 +++--- core/vm/contracts_fuzz_test.go | 3 ++- core/vm/contracts_test.go | 9 +++++---- core/vm/evm.go | 24 ++++-------------------- core/vm/interface.go | 3 +++ 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 9c0bcdd9dd..636026694e 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -321,6 +321,11 @@ func (s *StateDB) Empty(addr common.Address) bool { return so == nil || so.empty() } +// Touch accesses the specific account without returning anything. +func (s *StateDB) Touch(addr common.Address) { + s.getStateObject(addr) +} + // GetBalance retrieves the balance from the given address or 0 if object not found func (s *StateDB) GetBalance(addr common.Address) *uint256.Int { stateObject := s.getStateObject(addr) diff --git a/core/state/statedb_hooked.go b/core/state/statedb_hooked.go index f5d69a4de4..c5faa7c98e 100644 --- a/core/state/statedb_hooked.go +++ b/core/state/statedb_hooked.go @@ -115,6 +115,10 @@ func (s *hookedStateDB) Exist(addr common.Address) bool { return s.inner.Exist(addr) } +func (s *hookedStateDB) Touch(addr common.Address) { + s.inner.Touch(addr) +} + func (s *hookedStateDB) Empty(addr common.Address) bool { return s.inner.Empty(addr) } diff --git a/core/vm/contracts.go b/core/vm/contracts.go index b1eed79282..6dadb64873 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -262,7 +262,7 @@ func ActivePrecompiles(rules params.Rules) []common.Address { // - the returned bytes, // - the remaining gas budget, // - any error that occurred -func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, gas GasBudget, logger *tracing.Hooks) (ret []byte, remaining GasBudget, err error) { +func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address common.Address, input []byte, gas GasBudget, logger *tracing.Hooks, rules params.Rules) (ret []byte, remaining GasBudget, err error) { gasCost := p.RequiredGas(input) prior, ok := gas.Charge(GasCosts{RegularGas: gasCost}) if !ok { @@ -274,8 +274,8 @@ func RunPrecompiledContract(stateDB StateDB, p PrecompiledContract, address comm } // Touch the precompile for block-level accessList recording once Amsterdam // fork is activated. - if stateDB != nil { - stateDB.Exist(address) + if rules.IsAmsterdam { + stateDB.Touch(address) } output, err := p.Run(input) return output, gas, err diff --git a/core/vm/contracts_fuzz_test.go b/core/vm/contracts_fuzz_test.go index 35a9bd0257..988cdb91f2 100644 --- a/core/vm/contracts_fuzz_test.go +++ b/core/vm/contracts_fuzz_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" ) func FuzzPrecompiledContracts(f *testing.F) { @@ -36,7 +37,7 @@ func FuzzPrecompiledContracts(f *testing.F) { return } inWant := string(input) - RunPrecompiledContract(nil, p, a, input, NewGasBudget(gas), nil) + RunPrecompiledContract(nil, p, a, input, NewGasBudget(gas), nil, params.Rules{}) if inHave := string(input); inWant != inHave { t.Errorf("Precompiled %v modified input data", a) } diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go index dda753f504..e7841c8552 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -25,6 +25,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" ) // precompiledTest defines the input/output pairs for precompiled contract tests. @@ -99,7 +100,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) { in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil); err != nil { + if res, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}); err != nil { t.Error(err) } else if common.Bytes2Hex(res) != test.Expected { t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) @@ -121,7 +122,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) { gas := test.Gas - 1 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}) if err.Error() != "out of gas" { t.Errorf("Expected error [out of gas], got [%v]", err) } @@ -138,7 +139,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing in := common.Hex2Bytes(test.Input) gas := p.RequiredGas(in) t.Run(test.Name, func(t *testing.T) { - _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil) + _, _, err := RunPrecompiledContract(nil, p, common.HexToAddress(addr), in, NewGasBudget(gas), nil, params.Rules{}) if err.Error() != test.ExpectedError { t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) } @@ -169,7 +170,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { start := time.Now() for bench.Loop() { copy(data, in) - res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, NewGasBudget(reqGas), nil) + res, _, err = RunPrecompiledContract(nil, p, common.HexToAddress(addr), data, NewGasBudget(reqGas), nil, params.Rules{}) } elapsed := uint64(time.Since(start)) if elapsed < 1 { diff --git a/core/vm/evm.go b/core/vm/evm.go index ca8d8967ec..59e301c0a7 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -287,11 +287,7 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g } if isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. code := evm.resolveCode(addr) @@ -354,11 +350,7 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt // It is allowed to call precompiles, even via delegatecall if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. // The contract is a scoped environment for this execution context only. @@ -401,11 +393,7 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, // It is allowed to call precompiles, even via delegatecall if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and make initialise the delegate values // @@ -457,11 +445,7 @@ func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []b evm.StateDB.AddBalance(addr, new(uint256.Int), tracing.BalanceChangeTouchAccount) if p, isPrecompile := evm.precompile(addr); isPrecompile { - var stateDB StateDB - if evm.chainRules.IsAmsterdam { - stateDB = evm.StateDB - } - ret, gas, err = RunPrecompiledContract(stateDB, p, addr, input, gas, evm.Config.Tracer) + ret, gas, err = RunPrecompiledContract(evm.StateDB, p, addr, input, gas, evm.Config.Tracer, evm.chainRules) } else { // Initialise a new contract and set the code that is to be used by the EVM. // The contract is a scoped environment for this execution context only. diff --git a/core/vm/interface.go b/core/vm/interface.go index d09075933e..487d8002f9 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -64,6 +64,9 @@ type StateDB interface { // Notably this also returns true for self-destructed accounts within the current transaction. Exist(common.Address) bool + // Touch accesses the state without returning anything. + Touch(common.Address) + // IsNewContract reports whether the contract at the given address was deployed // during the current transaction. IsNewContract(addr common.Address) bool