Skip to content

Commit abd110e

Browse files
committed
refactor: remove echoed gas argument from evmCallArgs.run()
1 parent f651814 commit abd110e

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

core/vm/contracts.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ func (args *evmCallArgs) RunPrecompiledContract(p PrecompiledContract, input []b
177177
return nil, 0, ErrOutOfGas
178178
}
179179
suppliedGas -= gasCost
180-
return args.run(p, input, suppliedGas)
180+
args.gasRemaining = suppliedGas
181+
output, err := args.run(p, input)
182+
return output, args.gasRemaining, err
181183
}
182184

183185
// ECRECOVER implemented as a native contract.

core/vm/contracts.libevm.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ type evmCallArgs struct {
4545
callType CallType
4646

4747
// args:start
48-
caller ContractRef
49-
addr common.Address
50-
input []byte
51-
gas uint64
52-
value *uint256.Int
48+
caller ContractRef
49+
addr common.Address
50+
input []byte
51+
gasRemaining uint64
52+
value *uint256.Int
5353
// args:end
5454
}
5555

@@ -89,16 +89,17 @@ func (t CallType) OpCode() OpCode {
8989
}
9090

9191
// run runs the [PrecompiledContract], differentiating between stateful and
92-
// regular types.
93-
func (args *evmCallArgs) run(p PrecompiledContract, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
94-
if p, ok := p.(statefulPrecompile); ok {
95-
// `suppliedGas` is already held by the args, and captured by `env()`.
96-
return p.run(args.env(), input)
92+
// regular types, updating `gasRemaining` in the stateful case.
93+
func (args *evmCallArgs) run(p PrecompiledContract, input []byte) (ret []byte, err error) {
94+
switch p := p.(type) {
95+
default:
96+
return p.Run(input)
97+
case statefulPrecompile:
98+
env := args.env()
99+
ret, err := p(env, input)
100+
args.gasRemaining = env.Gas()
101+
return ret, err
97102
}
98-
// Gas consumption for regular precompiles was already handled by the native
99-
// RunPrecompiledContract(), which called this method.
100-
ret, err = p.Run(input)
101-
return ret, suppliedGas, err
102103
}
103104

104105
// PrecompiledStatefulContract is the stateful equivalent of a
@@ -123,11 +124,6 @@ func NewStatefulPrecompile(run PrecompiledStatefulContract) PrecompiledContract
123124
// [PrecompiledStatefulContract] to hide implementation details.
124125
type statefulPrecompile PrecompiledStatefulContract
125126

126-
func (p statefulPrecompile) run(env *environment, input []byte) ([]byte, uint64, error) {
127-
ret, err := p(env, input)
128-
return ret, env.self.Gas, err
129-
}
130-
131127
// RequiredGas always returns zero as this gas is consumed by native geth code
132128
// before the contract is run.
133129
func (statefulPrecompile) RequiredGas([]byte) uint64 { return 0 }
@@ -189,7 +185,7 @@ func (args *evmCallArgs) env() *environment {
189185

190186
// This is equivalent to the `contract` variables created by evm.*Call*()
191187
// methods, for non precompiles, to pass to [EVMInterpreter.Run].
192-
contract := NewContract(args.caller, AccountRef(self), value, args.gas)
188+
contract := NewContract(args.caller, AccountRef(self), value, args.gasRemaining)
193189
if args.callType == DelegateCall {
194190
contract = contract.AsDelegate()
195191
}

core/vm/libevm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ package vm
1818
// The original RunPrecompiledContract was migrated to being a method on
1919
// [evmCallArgs]. We need to replace it for use by regular geth tests.
2020
func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
21-
return (*evmCallArgs)(nil).RunPrecompiledContract(p, input, suppliedGas)
21+
return new(evmCallArgs).RunPrecompiledContract(p, input, suppliedGas)
2222
}

0 commit comments

Comments
 (0)