Skip to content

Commit

Permalink
Merge pull request #2613 from OffchainLabs/gligneul/stylus-tracer-fixes
Browse files Browse the repository at this point in the history
[NIT-2754] Two small comments on stylus tracer
  • Loading branch information
tsahee authored Aug 26, 2024
2 parents 057e26d + 0669dfc commit c424cc0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
31 changes: 23 additions & 8 deletions execution/gethexec/stylus_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package gethexec

import (
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"
Expand All @@ -14,7 +15,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/offchainlabs/nitro/util/stack"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/util/containers"
)

func init() {
Expand All @@ -24,8 +26,8 @@ func init() {
// stylusTracer captures Stylus HostIOs and returns them in a structured format to be used in Cargo
// Stylus Replay.
type stylusTracer struct {
open *stack.Stack[HostioTraceInfo]
stack *stack.Stack[*stack.Stack[HostioTraceInfo]]
open *containers.Stack[HostioTraceInfo]
stack *containers.Stack[*containers.Stack[HostioTraceInfo]]
interrupt atomic.Bool
reason error
}
Expand Down Expand Up @@ -55,7 +57,7 @@ type HostioTraceInfo struct {
Address *common.Address `json:"address,omitempty"`

// For *call HostIOs, the steps performed by the called contract.
Steps *stack.Stack[HostioTraceInfo] `json:"steps,omitempty"`
Steps *containers.Stack[HostioTraceInfo] `json:"steps,omitempty"`
}

// nestsHostios contains the hostios with nested calls.
Expand All @@ -67,8 +69,8 @@ var nestsHostios = map[string]bool{

func newStylusTracer(ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
return &stylusTracer{
open: stack.NewStack[HostioTraceInfo](),
stack: stack.NewStack[*stack.Stack[HostioTraceInfo]](),
open: containers.NewStack[HostioTraceInfo](),
stack: containers.NewStack[*containers.Stack[HostioTraceInfo]](),
}, nil
}

Expand Down Expand Up @@ -126,7 +128,7 @@ func (t *stylusTracer) CaptureEnter(typ vm.OpCode, from common.Address, to commo
name = "evm_self_destruct"
}

inner := stack.NewStack[HostioTraceInfo]()
inner := containers.NewStack[HostioTraceInfo]()
info := HostioTraceInfo{
Name: name,
Address: &to,
Expand All @@ -152,9 +154,22 @@ func (t *stylusTracer) GetResult() (json.RawMessage, error) {
if t.reason != nil {
return nil, t.reason
}

var internalErr error
if t.open == nil {
return nil, fmt.Errorf("trace is nil")
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.open is nil"))
}
if t.stack == nil {
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.stack is nil"))
}
if !t.stack.Empty() {
internalErr = errors.Join(internalErr, fmt.Errorf("tracer.stack should be empty, but has %d values", t.stack.Len()))
}
if internalErr != nil {
log.Error("stylusTracer: internal error when generating a trace", "error", internalErr)
return nil, fmt.Errorf("internal error: %w", internalErr)
}

msg, err := json.Marshal(t.open)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions system_tests/stylus_tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/offchainlabs/nitro/execution/gethexec"
"github.com/offchainlabs/nitro/solgen/go/mocksgen"
"github.com/offchainlabs/nitro/util/stack"
"github.com/offchainlabs/nitro/util/containers"
"github.com/offchainlabs/nitro/util/testhelpers"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestStylusTracer(t *testing.T) {
Args: append(stylusMulticall.Bytes(), common.Hex2Bytes("ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000")...),
Outs: common.Hex2Bytes("0000000000"),
Address: &stylusMulticall,
Steps: (*stack.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
Steps: (*containers.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
{Name: "user_entrypoint", Args: intToBe32(1)},
{Name: "pay_for_memory_grow", Args: []byte{0x00, 0x01}},
{Name: "read_args", Outs: []byte{0x00}},
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestStylusTracer(t *testing.T) {
Args: append(evmMulticall.Bytes(), common.Hex2Bytes("ffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000")...),
Outs: common.Hex2Bytes("0000000000"),
Address: &evmMulticall,
Steps: stack.NewStack[gethexec.HostioTraceInfo](),
Steps: containers.NewStack[gethexec.HostioTraceInfo](),
},
{Name: "storage_flush_cache", Args: []byte{0x00}},
{Name: "write_result"},
Expand All @@ -133,7 +133,7 @@ func TestStylusTracer(t *testing.T) {
{
Name: "evm_call_contract",
Address: &stylusMulticall,
Steps: (*stack.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
Steps: (*containers.Stack[gethexec.HostioTraceInfo])(&[]gethexec.HostioTraceInfo{
{Name: "user_entrypoint", Args: intToBe32(1)},
{Name: "pay_for_memory_grow", Args: []byte{0x00, 0x01}},
{Name: "read_args", Outs: []byte{0x00}},
Expand Down
15 changes: 13 additions & 2 deletions util/stack/stack.go → util/containers/stack.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

package stack
package containers

import (
"fmt"
Expand All @@ -28,7 +28,7 @@ func (s *Stack[T]) Pop() (T, error) {
var zeroVal T
return zeroVal, fmt.Errorf("trying to pop nil stack")
}
if len(*s) == 0 {
if s.Empty() {
var zeroVal T
return zeroVal, fmt.Errorf("trying to pop empty stack")
}
Expand All @@ -37,3 +37,14 @@ func (s *Stack[T]) Pop() (T, error) {
*s = (*s)[:i]
return val, nil
}

func (s *Stack[T]) Empty() bool {
return s == nil || len(*s) == 0
}

func (s *Stack[T]) Len() int {
if s == nil {
return 0
}
return len(*s)
}

0 comments on commit c424cc0

Please sign in to comment.