Skip to content

Commit f0e7382

Browse files
cmd/evm, eth/tracers: refactor structlogger and make it streaming (#30806)
This PR refactors the structlog a bit, making it so that it can be used in a streaming mode. ------------- OBS: this PR makes a change in the input `config` config, the third input-parem field to `debug.traceCall`. Previously, seteting it to e.g. ` {"enableMemory": true, "limit": 1024}` would mean that the response was limited to `1024` items. Since an 'item' may include both memory and storage, the actual size of the response was undertermined. After this change, the response will be limited to `1024` __`bytes`__ (or thereabouts). ----------- The commandline usage of structlog now uses the streaming mode, leaving the non-streaming mode of operation for the eth_Call. There are two benefits of streaming mode 1. Not have to maintain a long list of operations, 2. Not have to duplicate / n-plicate data, e.g. memory / stack / returndata so that each entry has their own private slice. --------- Co-authored-by: Gary Rong <[email protected]>
1 parent 84cabb5 commit f0e7382

File tree

6 files changed

+200
-189
lines changed

6 files changed

+200
-189
lines changed

cmd/evm/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ func tracerFromFlags(ctx *cli.Context) *tracing.Hooks {
240240
}
241241
switch {
242242
case ctx.Bool(TraceFlag.Name) && ctx.String(TraceFormatFlag.Name) == "struct":
243-
return logger.NewStructLogger(config).Hooks()
243+
return logger.NewStreamingStructLogger(config, os.Stderr).Hooks()
244244
case ctx.Bool(TraceFlag.Name) && ctx.String(TraceFormatFlag.Name) == "json":
245245
return logger.NewJSONLogger(config, os.Stderr)
246246
case ctx.Bool(MachineFlag.Name):
247247
return logger.NewJSONLogger(config, os.Stderr)
248248
case ctx.Bool(DebugFlag.Name):
249-
return logger.NewStructLogger(config).Hooks()
249+
return logger.NewStreamingStructLogger(config, os.Stderr).Hooks()
250250
default:
251251
return nil
252252
}

cmd/evm/runner.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"bytes"
21+
"encoding/hex"
2122
"encoding/json"
2223
"fmt"
2324
"io"
@@ -35,6 +36,7 @@ import (
3536
"github.com/ethereum/go-ethereum/core/rawdb"
3637
"github.com/ethereum/go-ethereum/core/state"
3738
"github.com/ethereum/go-ethereum/core/tracing"
39+
"github.com/ethereum/go-ethereum/core/types"
3840
"github.com/ethereum/go-ethereum/core/vm"
3941
"github.com/ethereum/go-ethereum/core/vm/runtime"
4042
"github.com/ethereum/go-ethereum/eth/tracers/logger"
@@ -205,7 +207,6 @@ func runCmd(ctx *cli.Context) error {
205207

206208
var (
207209
tracer *tracing.Hooks
208-
debugLogger *logger.StructLogger
209210
prestate *state.StateDB
210211
chainConfig *params.ChainConfig
211212
sender = common.BytesToAddress([]byte("sender"))
@@ -217,10 +218,7 @@ func runCmd(ctx *cli.Context) error {
217218
if ctx.Bool(MachineFlag.Name) {
218219
tracer = logger.NewJSONLogger(logconfig, os.Stdout)
219220
} else if ctx.Bool(DebugFlag.Name) {
220-
debugLogger = logger.NewStructLogger(logconfig)
221-
tracer = debugLogger.Hooks()
222-
} else {
223-
debugLogger = logger.NewStructLogger(logconfig)
221+
tracer = logger.NewStreamingStructLogger(logconfig, os.Stderr).Hooks()
224222
}
225223

226224
initialGas := ctx.Uint64(GasFlag.Name)
@@ -365,12 +363,10 @@ func runCmd(ctx *cli.Context) error {
365363
}
366364

367365
if ctx.Bool(DebugFlag.Name) {
368-
if debugLogger != nil {
369-
fmt.Fprintln(os.Stderr, "#### TRACE ####")
370-
logger.WriteTrace(os.Stderr, debugLogger.StructLogs())
366+
if logs := runtimeConfig.State.Logs(); len(logs) > 0 {
367+
fmt.Fprintln(os.Stderr, "### LOGS")
368+
writeLogs(os.Stderr, logs)
371369
}
372-
fmt.Fprintln(os.Stderr, "#### LOGS ####")
373-
logger.WriteLogs(os.Stderr, runtimeConfig.State.Logs())
374370
}
375371

376372
if bench || ctx.Bool(StatDumpFlag.Name) {
@@ -389,3 +385,16 @@ allocated bytes: %d
389385

390386
return nil
391387
}
388+
389+
// writeLogs writes vm logs in a readable format to the given writer
390+
func writeLogs(writer io.Writer, logs []*types.Log) {
391+
for _, log := range logs {
392+
fmt.Fprintf(writer, "LOG%d: %x bn=%d txi=%x\n", len(log.Topics), log.Address, log.BlockNumber, log.TxIndex)
393+
394+
for i, topic := range log.Topics {
395+
fmt.Fprintf(writer, "%08d %x\n", i, topic)
396+
}
397+
fmt.Fprint(writer, hex.Dump(log.Data))
398+
fmt.Fprintln(writer)
399+
}
400+
}

core/vm/runtime/runtime_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/core"
3232
"github.com/ethereum/go-ethereum/core/asm"
3333
"github.com/ethereum/go-ethereum/core/state"
34+
"github.com/ethereum/go-ethereum/core/tracing"
3435
"github.com/ethereum/go-ethereum/core/types"
3536
"github.com/ethereum/go-ethereum/core/vm"
3637
"github.com/ethereum/go-ethereum/core/vm/program"
@@ -670,17 +671,23 @@ func TestColdAccountAccessCost(t *testing.T) {
670671
want: 7600,
671672
},
672673
} {
673-
tracer := logger.NewStructLogger(nil)
674+
var step = 0
675+
var have = uint64(0)
674676
Execute(tc.code, nil, &Config{
675677
EVMConfig: vm.Config{
676-
Tracer: tracer.Hooks(),
678+
Tracer: &tracing.Hooks{
679+
OnOpcode: func(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
680+
// Uncomment to investigate failures:
681+
//t.Logf("%d: %v %d", step, vm.OpCode(op).String(), cost)
682+
if step == tc.step {
683+
have = cost
684+
}
685+
step++
686+
},
687+
},
677688
},
678689
})
679-
have := tracer.StructLogs()[tc.step].GasCost
680690
if want := tc.want; have != want {
681-
for ii, op := range tracer.StructLogs() {
682-
t.Logf("%d: %v %d", ii, op.OpName(), op.GasCost)
683-
}
684691
t.Fatalf("testcase %d, gas report wrong, step %d, have %d want %d", i, tc.step, have, want)
685692
}
686693
}

eth/tracers/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func TestTraceTransaction(t *testing.T) {
535535
Gas: params.TxGas,
536536
Failed: false,
537537
ReturnValue: "",
538-
StructLogs: []logger.StructLogRes{},
538+
StructLogs: []json.RawMessage{},
539539
}) {
540540
t.Error("Transaction tracing result is different")
541541
}

0 commit comments

Comments
 (0)