From 5d14fa58beb121e3f2f1ac8bd12cb2ed9c26e5ee Mon Sep 17 00:00:00 2001 From: codchen Date: Thu, 18 Jan 2024 11:24:44 +0800 Subject: [PATCH] Pass IsDelegateCall to precompile calls --- core/vm/contracts.go | 6 +++--- core/vm/contracts_fuzz_test.go | 2 +- core/vm/contracts_test.go | 8 ++++---- core/vm/evm.go | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 7c27366198..313ae9f09d 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -43,7 +43,7 @@ type PrecompiledContract interface { } type DynamicGasPrecompiledContract interface { - RunAndCalculateGas(evm *EVM, sender common.Address, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) // Run runs the precompiled contract and calculate gas dynamically + RunAndCalculateGas(evm *EVM, sender common.Address, isDelegateCall bool, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) // Run runs the precompiled contract and calculate gas dynamically } // PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum @@ -172,9 +172,9 @@ func ActivePrecompiles(rules params.Rules) []common.Address { // - the returned bytes, // - the _remaining_ gas, // - any error that occurred -func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Address, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { +func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Address, isDelegateCall bool, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { if dp, ok := p.(DynamicGasPrecompiledContract); ok { - return dp.RunAndCalculateGas(evm, sender, input, suppliedGas) + return dp.RunAndCalculateGas(evm, sender, isDelegateCall, input, suppliedGas) } gasCost := p.RequiredGas(input) if suppliedGas < gasCost { diff --git a/core/vm/contracts_fuzz_test.go b/core/vm/contracts_fuzz_test.go index fb77fa0361..5dd4d5ea15 100644 --- a/core/vm/contracts_fuzz_test.go +++ b/core/vm/contracts_fuzz_test.go @@ -37,7 +37,7 @@ func FuzzPrecompiledContracts(f *testing.F) { return } inWant := string(input) - vm.RunPrecompiledContract(p, nil, common.Address{}, input, gas) + vm.RunPrecompiledContract(p, nil, common.Address{}, false, input, gas) 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 21a78c57cf..ce0c4bc39a 100644 --- a/core/vm/contracts_test.go +++ b/core/vm/contracts_test.go @@ -99,7 +99,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 := vm.RunPrecompiledContract(p, nil, common.Address{}, in, gas); err != nil { + if res, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas); 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 +121,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) { gas := p.RequiredGas(in) - 1 t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { - _, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, in, gas) + _, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas) if err.Error() != "out of gas" { t.Errorf("Expected error [out of gas], got [%v]", err) } @@ -138,7 +138,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 := vm.RunPrecompiledContract(p, nil, common.Address{}, in, gas) + _, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas) if err.Error() != test.ExpectedError { t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) } @@ -170,7 +170,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) { bench.ResetTimer() for i := 0; i < bench.N; i++ { copy(data, in) - res, _, err = vm.RunPrecompiledContract(p, nil, common.Address{}, data, reqGas) + res, _, err = vm.RunPrecompiledContract(p, nil, common.Address{}, false, data, reqGas) } bench.StopTimer() elapsed := uint64(time.Since(start)) diff --git a/core/vm/evm.go b/core/vm/evm.go index 8b6ef55175..66f28ccbdb 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -232,7 +232,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas } if isPrecompile { - ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), input, gas) + ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) } 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. @@ -295,7 +295,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, // It is allowed to call precompiles, even via delegatecall if p, isPrecompile := evm.precompile(addr); isPrecompile { - ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), input, gas) + ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) } else { addrCopy := addr // Initialise a new contract and set the code that is to be used by the EVM. @@ -343,7 +343,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by // NOTE: caller must, at all times be a contract. It should never happen // that caller is something other than a Contract. parent := caller.(*Contract) - ret, gas, err = RunPrecompiledContract(p, evm, parent.CallerAddress, input, gas) + ret, gas, err = RunPrecompiledContract(p, evm, parent.CallerAddress, true, input, gas) } else { addrCopy := addr // Initialise a new contract and make initialise the delegate values @@ -392,7 +392,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte } if p, isPrecompile := evm.precompile(addr); isPrecompile { - ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), input, gas) + ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) } else { // At this point, we use a copy of address. If we don't, the go compiler will // leak the 'contract' to the outer scope, and make allocation for 'contract'