Skip to content

Commit 45609bb

Browse files
committed
core/vm: introduce a tracer wrapper
to clean up the callsites a little and allow compile time disabling of tracing.
1 parent c3ef6c7 commit 45609bb

File tree

10 files changed

+289
-150
lines changed

10 files changed

+289
-150
lines changed

core/vm/contract.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package vm
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21-
"github.com/ethereum/go-ethereum/core/tracing"
2221
"github.com/holiman/uint256"
2322
)
2423

@@ -125,29 +124,6 @@ func (c *Contract) Caller() common.Address {
125124
return c.caller
126125
}
127126

128-
// UseGas attempts the use gas and subtracts it and returns true on success
129-
func (c *Contract) UseGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) (ok bool) {
130-
if c.Gas < gas {
131-
return false
132-
}
133-
if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored {
134-
logger.OnGasChange(c.Gas, c.Gas-gas, reason)
135-
}
136-
c.Gas -= gas
137-
return true
138-
}
139-
140-
// RefundGas refunds gas to the contract
141-
func (c *Contract) RefundGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) {
142-
if gas == 0 {
143-
return
144-
}
145-
if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored {
146-
logger.OnGasChange(c.Gas, c.Gas+gas, reason)
147-
}
148-
c.Gas += gas
149-
}
150-
151127
// Address returns the contracts address
152128
func (c *Contract) Address() common.Address {
153129
return c.address

core/vm/contracts.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,12 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
257257
// - the returned bytes,
258258
// - the _remaining_ gas,
259259
// - any error that occurred
260-
func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64, logger *tracing.Hooks) (ret []byte, remainingGas uint64, err error) {
260+
func RunPrecompiledContract[TS TracingSwitch](p PrecompiledContract, input []byte, suppliedGas uint64, tracer tracer[TS]) (ret []byte, remainingGas uint64, err error) {
261261
gasCost := p.RequiredGas(input)
262262
if suppliedGas < gasCost {
263263
return nil, 0, ErrOutOfGas
264264
}
265-
if logger != nil && logger.OnGasChange != nil {
266-
logger.OnGasChange(suppliedGas, suppliedGas-gasCost, tracing.GasChangeCallPrecompiledContract)
267-
}
265+
tracer.OnGasChange(suppliedGas, suppliedGas-gasCost, tracing.GasChangeCallPrecompiledContract)
268266
suppliedGas -= gasCost
269267
output, err := p.Run(input)
270268
return output, suppliedGas, err

core/vm/contracts_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func FuzzPrecompiledContracts(f *testing.F) {
3636
return
3737
}
3838
inWant := string(input)
39-
RunPrecompiledContract(p, input, gas, nil)
39+
RunPrecompiledContract(p, input, gas, NewTracer[TracingDisabled](nil))
4040
if inHave := string(input); inWant != inHave {
4141
t.Errorf("Precompiled %v modified input data", a)
4242
}

core/vm/contracts_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func testPrecompiled(addr string, test precompiledTest, t *testing.T) {
9999
in := common.Hex2Bytes(test.Input)
100100
gas := p.RequiredGas(in)
101101
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
102-
if res, _, err := RunPrecompiledContract(p, in, gas, nil); err != nil {
102+
if res, _, err := RunPrecompiledContract(p, in, gas, NewTracer[TracingDisabled](nil)); err != nil {
103103
t.Error(err)
104104
} else if common.Bytes2Hex(res) != test.Expected {
105105
t.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
@@ -121,7 +121,7 @@ func testPrecompiledOOG(addr string, test precompiledTest, t *testing.T) {
121121
gas := p.RequiredGas(in) - 1
122122

123123
t.Run(fmt.Sprintf("%s-Gas=%d", test.Name, gas), func(t *testing.T) {
124-
_, _, err := RunPrecompiledContract(p, in, gas, nil)
124+
_, _, err := RunPrecompiledContract(p, in, gas, NewTracer[TracingDisabled](nil))
125125
if err.Error() != "out of gas" {
126126
t.Errorf("Expected error [out of gas], got [%v]", err)
127127
}
@@ -138,7 +138,7 @@ func testPrecompiledFailure(addr string, test precompiledFailureTest, t *testing
138138
in := common.Hex2Bytes(test.Input)
139139
gas := p.RequiredGas(in)
140140
t.Run(test.Name, func(t *testing.T) {
141-
_, _, err := RunPrecompiledContract(p, in, gas, nil)
141+
_, _, err := RunPrecompiledContract(p, in, gas, NewTracer[TracingDisabled](nil))
142142
if err.Error() != test.ExpectedError {
143143
t.Errorf("Expected error [%v], got [%v]", test.ExpectedError, err)
144144
}
@@ -170,7 +170,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
170170
bench.ResetTimer()
171171
for i := 0; i < bench.N; i++ {
172172
copy(data, in)
173-
res, _, err = RunPrecompiledContract(p, data, reqGas, nil)
173+
res, _, err = RunPrecompiledContract(p, data, reqGas, NewTracer[TracingDisabled](nil))
174174
}
175175
bench.StopTimer()
176176
elapsed := uint64(time.Since(start))

core/vm/eips.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func opExtCodeCopyEIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, er
358358
code := evm.StateDB.GetCode(addr)
359359
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64())
360360
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas)
361-
scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified)
361+
evm.UseGas(scope.Contract, consumed, tracing.GasChangeUnspecified)
362362
if consumed < wanted {
363363
return nil, ErrOutOfGas
364364
}
@@ -384,7 +384,7 @@ func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
384384
// advanced past this boundary.
385385
contractAddr := scope.Contract.Address()
386386
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas)
387-
scope.Contract.UseGas(wanted, evm.Config.Tracer, tracing.GasChangeUnspecified)
387+
evm.UseGas(scope.Contract, wanted, tracing.GasChangeUnspecified)
388388
if consumed < wanted {
389389
return nil, ErrOutOfGas
390390
}
@@ -412,7 +412,7 @@ func makePushEIP4762(size uint64, pushByteSize int) executionFunc {
412412
if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall {
413413
contractAddr := scope.Contract.Address()
414414
consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas)
415-
scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified)
415+
evm.UseGas(scope.Contract, consumed, tracing.GasChangeUnspecified)
416416
if consumed < wanted {
417417
return nil, ErrOutOfGas
418418
}

0 commit comments

Comments
 (0)