From bc983e2afd5f8520dc2448b4ddf989cc102e5973 Mon Sep 17 00:00:00 2001 From: Chris Ziogas Date: Wed, 27 Mar 2024 14:00:20 +0200 Subject: [PATCH] allow test to use different file for tracer output --- eth/tracers/internal/tracetest/supply_test.go | 13 ++++++++----- eth/tracers/live/supply.go | 11 ++++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/eth/tracers/internal/tracetest/supply_test.go b/eth/tracers/internal/tracetest/supply_test.go index d9834d398e..5578d8ddb0 100644 --- a/eth/tracers/internal/tracetest/supply_test.go +++ b/eth/tracers/internal/tracetest/supply_test.go @@ -22,7 +22,6 @@ import ( "fmt" "math/big" "os" - "path" "path/filepath" "reflect" "testing" @@ -518,11 +517,15 @@ func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]live.S ) traceOutputPath := filepath.ToSlash(os.TempDir()) - traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl") - defer os.Remove(traceOutputFilename) + traceOutputFile, err := os.CreateTemp(traceOutputPath, "supply-*.jsonl") + traceOutputFilename := filepath.Base(traceOutputFile.Name()) + if err != nil { + return nil, nil, fmt.Errorf("failed to create temp file for supply logs: %v", err) + } + defer os.Remove(traceOutputFile.Name()) // Load supply tracer - tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath))) + tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s","filename":"%s"}`, traceOutputPath, traceOutputFilename))) if err != nil { return nil, nil, fmt.Errorf("failed to create call tracer: %v", err) } @@ -544,7 +547,7 @@ func testSupplyTracer(genesis *core.Genesis, gen func(*core.BlockGen)) ([]live.S // Check and compare the results // TODO: replace file to pass results - file, err := os.OpenFile(traceOutputFilename, os.O_RDONLY, 0666) + file, err := os.OpenFile(traceOutputFile.Name(), os.O_RDONLY, 0666) if err != nil { return nil, chain, fmt.Errorf("failed to open output file: %v", err) } diff --git a/eth/tracers/live/supply.go b/eth/tracers/live/supply.go index bc17c01eb3..3b5fa9d987 100644 --- a/eth/tracers/live/supply.go +++ b/eth/tracers/live/supply.go @@ -50,8 +50,9 @@ type Supply struct { } type supplyTracerConfig struct { - Path string `json:"path"` // Path to the directory where the tracer logs will be stored - MaxSize int `json:"maxSize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes. + Path string `json:"path"` // Path to the directory where the tracer logs will be stored + Filename string `json:"filename"` // Filename of the tracer log file. Defaults to "supply.jsonl". + MaxSize int `json:"maxsize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes. } func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) { @@ -66,9 +67,13 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) { return nil, errors.New("supply tracer output path is required") } + if config.Filename == "" { + config.Filename = "supply.jsonl" + } + // Store traces in a rotating file loggerOutput := &lumberjack.Logger{ - Filename: filepath.Join(config.Path, "supply.jsonl"), + Filename: filepath.Join(config.Path, config.Filename), } if config.MaxSize > 0 {