Skip to content

Commit

Permalink
Added option to disable structLogs for debug_trace* rpc methods. (#1931)
Browse files Browse the repository at this point in the history
* feat(jsonrpc/debug_trace*): add option to disable structLogs

* fix(jsonrpc/debug_trace*): lint errors
  • Loading branch information
ohijiho committed Oct 12, 2023
1 parent 7662ce0 commit e0d2891
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 62 deletions.
18 changes: 10 additions & 8 deletions jsonrpc/debug_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ func NewDebug(store debugStore, requestsPerSecond uint64) *Debug {
}

type TraceConfig struct {
EnableMemory bool `json:"enableMemory"`
DisableStack bool `json:"disableStack"`
DisableStorage bool `json:"disableStorage"`
EnableReturnData bool `json:"enableReturnData"`
Timeout *string `json:"timeout"`
EnableMemory bool `json:"enableMemory"`
DisableStack bool `json:"disableStack"`
DisableStorage bool `json:"disableStorage"`
EnableReturnData bool `json:"enableReturnData"`
DisableStructLogs bool `json:"disableStructLogs"`
Timeout *string `json:"timeout"`
}

func (d *Debug) TraceBlockByNumber(
Expand Down Expand Up @@ -248,10 +249,11 @@ func newTracer(config *TraceConfig) (
}

tracer := structtracer.NewStructTracer(structtracer.Config{
EnableMemory: config.EnableMemory,
EnableStack: !config.DisableStack,
EnableStorage: !config.DisableStorage,
EnableMemory: config.EnableMemory && !config.DisableStructLogs,
EnableStack: !config.DisableStack && !config.DisableStructLogs,
EnableStorage: !config.DisableStorage && !config.DisableStructLogs,
EnableReturnData: config.EnableReturnData,
EnableStructLogs: !config.DisableStructLogs,
})

timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
48 changes: 47 additions & 1 deletion jsonrpc/debug_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/state/runtime/tracer"
"github.com/0xPolygon/polygon-edge/state/runtime/tracer/structtracer"
"github.com/0xPolygon/polygon-edge/types"
"github.com/stretchr/testify/assert"
)

type debugEndpointMockStore struct {
Expand Down Expand Up @@ -154,6 +157,20 @@ func TestDebugTraceConfigDecode(t *testing.T) {
Timeout: &timeout15s,
},
},
{
input: `{
"disableStack": true,
"disableStorage": true,
"enableReturnData": true,
"disableStructLogs": true
}`,
expected: TraceConfig{
DisableStack: true,
DisableStorage: true,
EnableReturnData: true,
DisableStructLogs: true,
},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -787,4 +804,33 @@ func Test_newTracer(t *testing.T) {
assert.NotNil(t, res)
assert.NoError(t, err)
})

t.Run("should disable everything if struct logs are disabled", func(t *testing.T) {
t.Parallel()

tracer, cancel, err := newTracer(&TraceConfig{
EnableMemory: true,
EnableReturnData: true,
DisableStack: false,
DisableStorage: false,
DisableStructLogs: true,
})

t.Cleanup(func() {
cancel()
})

assert.NoError(t, err)

st, ok := tracer.(*structtracer.StructTracer)
require.True(t, ok)

assert.Equal(t, structtracer.Config{
EnableMemory: false,
EnableStack: false,
EnableStorage: false,
EnableReturnData: true,
EnableStructLogs: false,
}, st.Config)
})
}
35 changes: 19 additions & 16 deletions state/runtime/tracer/structtracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
EnableStack bool // enable stack capture
EnableStorage bool // enable storage capture
EnableReturnData bool // enable return data capture
EnableStructLogs bool // enable struct logs capture
}

type StructLog struct {
Expand Down Expand Up @@ -305,22 +306,24 @@ func (t *StructTracer) ExecuteState(
errStr = err.Error()
}

t.logs = append(
t.logs,
StructLog{
Pc: ip,
Op: opCode,
Gas: availableGas,
GasCost: cost,
Memory: memory,
Stack: stack,
ReturnData: returnData,
Storage: storage,
Depth: depth,
RefundCounter: host.GetRefund(),
Error: errStr,
},
)
if t.Config.EnableStructLogs {
t.logs = append(
t.logs,
StructLog{
Pc: ip,
Op: opCode,
Gas: availableGas,
GasCost: cost,
Memory: memory,
Stack: stack,
ReturnData: returnData,
Storage: storage,
Depth: depth,
RefundCounter: host.GetRefund(),
Error: errStr,
},
)
}
}

type StructTraceResult struct {
Expand Down
Loading

0 comments on commit e0d2891

Please sign in to comment.