Skip to content

Commit

Permalink
Merge branch 'gligneul/fix-update-gethpin-v1.14.0' into replace_bls
Browse files Browse the repository at this point in the history
  • Loading branch information
amsanghi authored Oct 11, 2024
2 parents 0014823 + a0be041 commit f3b9c2a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 43 deletions.
4 changes: 3 additions & 1 deletion arbos/programs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ func newApiClosures(
return memoryModel.GasCost(pages, open, ever)
}
captureHostio := func(name string, args, outs []byte, startInk, endInk uint64) {
tracingInfo.Tracer.CaptureStylusHostio(name, args, outs, startInk, endInk)
if tracingInfo.Tracer != nil && tracingInfo.Tracer.CaptureStylusHostio != nil {
tracingInfo.Tracer.CaptureStylusHostio(name, args, outs, startInk, endInk)
}
tracingInfo.CaptureEVMTraceForHostio(name, args, outs, startInk, endInk)
}

Expand Down
6 changes: 3 additions & 3 deletions arbos/util/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (info *TracingInfo) RecordStorageGet(key common.Hash) {
if tracer.OnOpcode != nil {
tracer.OnOpcode(0, byte(vm.SLOAD), 0, 0, scope, []byte{}, info.Depth, nil)
}
} else {
} else if tracer.CaptureArbitrumStorageGet != nil {
tracer.CaptureArbitrumStorageGet(key, info.Depth, info.Scenario == TracingBeforeEVM)
}
}
Expand All @@ -79,7 +79,7 @@ func (info *TracingInfo) RecordStorageSet(key, value common.Hash) {
if tracer.OnOpcode != nil {
tracer.OnOpcode(0, byte(vm.SSTORE), 0, 0, scope, []byte{}, info.Depth, nil)
}
} else {
} else if tracer.CaptureArbitrumStorageSet != nil {
tracer.CaptureArbitrumStorageSet(key, value, info.Depth, info.Scenario == TracingBeforeEVM)
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (info *TracingInfo) MockCall(input []byte, gas uint64, from, to common.Addr
tracer.OnOpcode(0, byte(vm.RETURN), 0, 0, retScope, []byte{}, depth+1, nil)
}
if tracer.OnExit != nil {
tracer.OnExit(depth+1, nil, 0, nil, false)
tracer.OnExit(depth, nil, 0, nil, false)
}

popScope := &vm.ScopeContext{
Expand Down
2 changes: 1 addition & 1 deletion arbos/util/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TransferBalance(
return errors.New("tracing scenario mismatch")
}

if scenario != TracingDuringEVM {
if scenario != TracingDuringEVM && tracer.CaptureArbitrumTransfer != nil {
tracer.CaptureArbitrumTransfer(from, to, amount, scenario == TracingBeforeEVM, purpose)
} else {
fromCopy := from
Expand Down
34 changes: 9 additions & 25 deletions execution/gethexec/stylus_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -77,16 +76,9 @@ func newStylusTracer(ctx *tracers.Context, _ json.RawMessage) (*tracers.Tracer,

return &tracers.Tracer{
Hooks: &tracing.Hooks{
OnExit: t.OnExit,
OnEnter: t.OnEnter,
OnOpcode: t.OnOpcode,
OnFault: t.OnFault,
OnTxStart: t.OnTxStart,
OnTxEnd: t.OnTxEnd,
CaptureArbitrumTransfer: t.CaptureArbitrumTransfer,
CaptureArbitrumStorageGet: t.CaptureArbitrumStorageGet,
CaptureArbitrumStorageSet: t.CaptureArbitrumStorageSet,
CaptureStylusHostio: t.CaptureStylusHostio,
OnEnter: t.OnEnter,
OnExit: t.OnExit,
CaptureStylusHostio: t.CaptureStylusHostio,
},
GetResult: t.GetResult,
Stop: t.Stop,
Expand Down Expand Up @@ -127,6 +119,9 @@ func (t *stylusTracer) OnEnter(depth int, typ byte, from common.Address, to comm
if t.interrupt.Load() {
return
}
if depth == 0 {
return
}

// This function adds the prefix evm_ because it assumes the opcode came from the EVM.
// If the opcode comes from WASM, the CaptureStylusHostio function will remove the evm prefix.
Expand Down Expand Up @@ -161,6 +156,9 @@ func (t *stylusTracer) OnExit(depth int, output []byte, gasUsed uint64, _ error,
if t.interrupt.Load() {
return
}
if depth == 0 {
return
}
var err error
t.open, err = t.stack.Pop()
if err != nil {
Expand Down Expand Up @@ -199,17 +197,3 @@ func (t *stylusTracer) Stop(err error) {
t.reason = err
t.interrupt.Store(true)
}

// Unimplemented EVMLogger interface methods

func (t *stylusTracer) CaptureArbitrumTransfer(from, to *common.Address, value *big.Int, before bool, purpose string) {
}
func (t *stylusTracer) CaptureArbitrumStorageGet(key common.Hash, depth int, before bool) {}
func (t *stylusTracer) CaptureArbitrumStorageSet(key, value common.Hash, depth int, before bool) {}
func (t *stylusTracer) OnOpcode(pc uint64, opcode byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
}
func (t *stylusTracer) OnFault(pc uint64, op byte, gas, cost uint64, _ tracing.OpContext, depth int, err error) {
}
func (t *stylusTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from common.Address) {
}
func (t *stylusTracer) OnTxEnd(receipt *types.Receipt, err error) {}
35 changes: 23 additions & 12 deletions system_tests/program_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,23 @@ func TestProgramStorageCost(t *testing.T) {
writeZeroData = multicallAppendStore(writeZeroData, slot, common.Hash{}, false)
}

writePair := compareGasPair{vm.SSTORE, "storage_flush_cache"}
readPair := compareGasPair{vm.SLOAD, "storage_load_bytes32"}

for _, tc := range []struct {
name string
data []byte
pair compareGasPair
}{
{"initialWrite", writeRandAData},
{"read", readData},
{"writeAgain", writeRandBData},
{"delete", writeZeroData},
{"readZeros", readData},
{"writeAgainAgain", writeRandAData},
{"initialWrite", writeRandAData, writePair},
{"read", readData, readPair},
{"writeAgain", writeRandBData, writePair},
{"delete", writeZeroData, writePair},
{"readZeros", readData, readPair},
{"writeAgainAgain", writeRandAData, writePair},
} {
t.Run(tc.name, func(t *testing.T) {
compareGasUsage(t, builder, evmMulticall, stylusMulticall, tc.data, nil, compareGasSum, 0,
compareGasPair{vm.SSTORE, "storage_flush_cache"}, compareGasPair{vm.SLOAD, "storage_load_bytes32"})
compareGasUsage(t, builder, evmMulticall, stylusMulticall, tc.data, nil, compareGasSum, 0, tc.pair)
})
}
}
Expand Down Expand Up @@ -360,10 +363,12 @@ func compareGasUsage(
}
case compareGasSum:
evmSum := float64(0)
for _, v := range evmGasUsage[opcode] {
evmSum += float64(v)
}
stylusSum := float64(0)
for i := range evmGasUsage[opcode] {
evmSum += float64(evmGasUsage[opcode][i])
stylusSum += stylusGasUsage[hostio][i]
for _, v := range stylusGasUsage[hostio] {
stylusSum += v
}
t.Logf("evm %v usage: %v - stylus %v usage: %v", opcode, evmSum, hostio, stylusSum)
checkPercentDiff(t, evmSum, stylusSum, maxAllowedDifference)
Expand All @@ -385,6 +390,10 @@ func evmOpcodesGasUsage(ctx context.Context, rpcClient rpc.ClientInterface, tx *
op := vm.StringToOp(result.StructLogs[i].Op)
gasUsed := uint64(0)
if op == vm.CALL || op == vm.STATICCALL || op == vm.DELEGATECALL || op == vm.CREATE || op == vm.CREATE2 {
if result.StructLogs[i].GasCost == 0 {
// ignore mock call emitted by arbos
continue
}
// For the CALL* opcodes, the GasCost in the tracer represents the gas sent
// to the callee contract, which is 63/64 of the remaining gas. This happens
// because the tracer is evaluated before the call is executed, so the EVM
Expand All @@ -400,14 +409,16 @@ func evmOpcodesGasUsage(ctx context.Context, rpcClient rpc.ClientInterface, tx *
// in the caller's depth. Then, we subtract the gas before the call by the
// gas after the call returned.
var gasAfterCall uint64
var found bool
for j := i + 1; j < len(result.StructLogs); j++ {
if result.StructLogs[j].Depth == result.StructLogs[i].Depth {
// back to the original call
gasAfterCall = result.StructLogs[j].Gas + result.StructLogs[j].GasCost
found = true
break
}
}
if gasAfterCall == 0 {
if !found {
return nil, fmt.Errorf("malformed log: didn't get back to call original depth")
}
if i == 0 {
Expand Down

0 comments on commit f3b9c2a

Please sign in to comment.