Merge pull request #12 from sei-protocol/pass-orig-caller

pass orig caller to precompile
This commit is contained in:
codchen 2024-01-18 11:35:01 +08:00 committed by GitHub
commit b6b7630a24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 12 additions and 12 deletions

View file

@ -43,7 +43,7 @@ type PrecompiledContract interface {
} }
type DynamicGasPrecompiledContract interface { type DynamicGasPrecompiledContract interface {
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 RunAndCalculateGas(evm *EVM, sender common.Address, callingContract common.Address, 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 // PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum
@ -172,9 +172,9 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
// - the returned bytes, // - the returned bytes,
// - the _remaining_ gas, // - the _remaining_ gas,
// - any error that occurred // - any error that occurred
func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Address, isDelegateCall bool, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Address, callingContract common.Address, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
if dp, ok := p.(DynamicGasPrecompiledContract); ok { if dp, ok := p.(DynamicGasPrecompiledContract); ok {
return dp.RunAndCalculateGas(evm, sender, isDelegateCall, input, suppliedGas) return dp.RunAndCalculateGas(evm, sender, callingContract, input, suppliedGas)
} }
gasCost := p.RequiredGas(input) gasCost := p.RequiredGas(input)
if suppliedGas < gasCost { if suppliedGas < gasCost {

View file

@ -37,7 +37,7 @@ func FuzzPrecompiledContracts(f *testing.F) {
return return
} }
inWant := string(input) inWant := string(input)
vm.RunPrecompiledContract(p, nil, common.Address{}, false, input, gas) vm.RunPrecompiledContract(p, nil, common.Address{}, common.Address{}, input, gas)
if inHave := string(input); inWant != inHave { if inHave := string(input); inWant != inHave {
t.Errorf("Precompiled %v modified input data", a) t.Errorf("Precompiled %v modified input data", a)
} }

View file

@ -99,7 +99,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
in := common.Hex2Bytes(test.Input) in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in) gas := p.RequiredGas(in)
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
if res, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas); err != nil { if res, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, common.Address{}, in, gas); err != nil {
t.Error(err) t.Error(err)
} else if common.Bytes2Hex(res) != test.Expected { } else if common.Bytes2Hex(res) != test.Expected {
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)) 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 gas := p.RequiredGas(in) - 1
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) { t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
_, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas) _, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, common.Address{}, in, gas)
if err.Error() != "out of gas" { if err.Error() != "out of gas" {
t.Errorf("Expected error [out of gas], got [%v]", err) 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) in := common.Hex2Bytes(test.Input)
gas := p.RequiredGas(in) gas := p.RequiredGas(in)
t.Run(test.Name, func(t *testing.T) { t.Run(test.Name, func(t *testing.T) {
_, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, false, in, gas) _, _, err := vm.RunPrecompiledContract(p, nil, common.Address{}, common.Address{}, in, gas)
if err.Error() != test.ExpectedError { if err.Error() != test.ExpectedError {
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err) 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() bench.ResetTimer()
for i := 0; i < bench.N; i++ { for i := 0; i < bench.N; i++ {
copy(data, in) copy(data, in)
res, _, err = vm.RunPrecompiledContract(p, nil, common.Address{}, false, data, reqGas) res, _, err = vm.RunPrecompiledContract(p, nil, common.Address{}, common.Address{}, data, reqGas)
} }
bench.StopTimer() bench.StopTimer()
elapsed := uint64(time.Since(start)) elapsed := uint64(time.Since(start))

View file

@ -232,7 +232,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas
} }
if isPrecompile { if isPrecompile {
ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), caller.Address(), input, gas)
} else { } else {
// Initialise a new contract and set the code that is to be used by the EVM. // 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. // 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 // It is allowed to call precompiles, even via delegatecall
if p, isPrecompile := evm.precompile(addr); isPrecompile { if p, isPrecompile := evm.precompile(addr); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), caller.Address(), input, gas)
} else { } else {
addrCopy := addr addrCopy := addr
// Initialise a new contract and set the code that is to be used by the EVM. // 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 // NOTE: caller must, at all times be a contract. It should never happen
// that caller is something other than a Contract. // that caller is something other than a Contract.
parent := caller.(*Contract) parent := caller.(*Contract)
ret, gas, err = RunPrecompiledContract(p, evm, parent.CallerAddress, true, input, gas) ret, gas, err = RunPrecompiledContract(p, evm, parent.CallerAddress, caller.Address(), input, gas)
} else { } else {
addrCopy := addr addrCopy := addr
// Initialise a new contract and make initialise the delegate values // 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 { if p, isPrecompile := evm.precompile(addr); isPrecompile {
ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), false, input, gas) ret, gas, err = RunPrecompiledContract(p, evm, caller.Address(), caller.Address(), input, gas)
} else { } else {
// At this point, we use a copy of address. If we don't, the go compiler will // 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' // leak the 'contract' to the outer scope, and make allocation for 'contract'