From 148094c3bc3e950636e84179c2ce702b98064e90 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Sat, 23 Sep 2023 15:46:35 -0400 Subject: [PATCH 1/4] tests: Allow specifying a tracer test with -file. --- .../internal/tracetest/blocknative_test.go | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/eth/tracers/internal/tracetest/blocknative_test.go b/eth/tracers/internal/tracetest/blocknative_test.go index be0652f07f5b..533e67ab103a 100644 --- a/eth/tracers/internal/tracetest/blocknative_test.go +++ b/eth/tracers/internal/tracetest/blocknative_test.go @@ -2,6 +2,7 @@ package tracetest import ( "encoding/json" + "flag" "fmt" "math/big" "os" @@ -23,6 +24,13 @@ import ( "github.com/ethereum/go-ethereum/tests" ) +var testFileFlag = flag.String("file", "", "Name of the file to run the test for") + +func TestMain(m *testing.M) { + flag.Parse() + os.Exit(m.Run()) +} + type blocknativeTracerTest struct { Genesis *core.Genesis `json:"genesis"` Context *callContext `json:"context"` @@ -44,8 +52,29 @@ type blocknativeTracerTest struct { func TestBlocknativeTracer(t *testing.T) { log.Root().SetHandler(log.StreamHandler(os.Stdout, log.TerminalFormat(true))) - testBlocknativeTracer("blocknative", t) - testBlocknativeTracer("blocknative/with_decoding", t) + + testsCases, err := loadTestTxs("blocknative") + if err != nil { + t.Fatal(err) + } + decodingTestsCases, err := loadTestTxs("blocknative/with_decoding") + if err != nil { + t.Fatal(err) + } + testsCases = append(testsCases, decodingTestsCases...) + + for _, test := range testsCases { + if *testFileFlag != "" { + a := strings.ToLower(*testFileFlag) + b := strings.ToLower(test.name) + if a != b { + continue + } + } + t.Run(test.name, func(t *testing.T) { + executeTestCase(test, t, true) + }) + } } func BenchmarkBlocknativeTracerWithoutDecoding(b *testing.B) { @@ -139,19 +168,6 @@ func benchmarkBlocknativeTracer(b *testing.B, decode bool, dirPaths ...string) { } } -func testBlocknativeTracer(dirPath string, t *testing.T) { - testsCases, err := loadTestTxs(dirPath) - if err != nil { - t.Fatal(err) - } - - for _, test := range testsCases { - t.Run(test.name, func(t *testing.T) { - executeTestCase(test, t, true) - }) - } -} - func loadTestTxs(dirPath string) ([]*blocknativeTracerTest, error) { files, err := os.ReadDir(filepath.Join("testdata", dirPath)) if err != nil { @@ -213,7 +229,14 @@ func loadTestTxs(dirPath string) ([]*blocknativeTracerTest, error) { return nil, err } - test.name = camel(strings.TrimSuffix(file.Name(), ".json")) + name := strings.TrimPrefix(dirPath, "blocknative") + name = strings.TrimPrefix(name, "/") + if name != "" { + name = name + "/" + } + name = name + camel(strings.TrimSuffix(file.Name(), ".json")) + + test.name = name test.evm = evm test.tx = tx test.msg = msg From ed335484d5f58ffde1921573cc92565bbba38e42 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Sat, 23 Sep 2023 15:59:19 -0400 Subject: [PATCH 2/4] tests: Allow setting log level in tracer tests. --- .../internal/tracetest/blocknative_test.go | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/eth/tracers/internal/tracetest/blocknative_test.go b/eth/tracers/internal/tracetest/blocknative_test.go index 533e67ab103a..70025ee28916 100644 --- a/eth/tracers/internal/tracetest/blocknative_test.go +++ b/eth/tracers/internal/tracetest/blocknative_test.go @@ -24,7 +24,10 @@ import ( "github.com/ethereum/go-ethereum/tests" ) -var testFileFlag = flag.String("file", "", "Name of the file to run the test for") +var ( + testFlagLogLevl = flag.String("loglevel", "info", "Log level to use") + testFlagFile = flag.String("file", "", "Name of the file to run the test for") +) func TestMain(m *testing.M) { flag.Parse() @@ -51,7 +54,7 @@ type blocknativeTracerTest struct { } func TestBlocknativeTracer(t *testing.T) { - log.Root().SetHandler(log.StreamHandler(os.Stdout, log.TerminalFormat(true))) + setLogging(t) testsCases, err := loadTestTxs("blocknative") if err != nil { @@ -64,8 +67,8 @@ func TestBlocknativeTracer(t *testing.T) { testsCases = append(testsCases, decodingTestsCases...) for _, test := range testsCases { - if *testFileFlag != "" { - a := strings.ToLower(*testFileFlag) + if *testFlagFile != "" { + a := strings.ToLower(*testFlagFile) b := strings.ToLower(test.name) if a != b { continue @@ -168,6 +171,19 @@ func benchmarkBlocknativeTracer(b *testing.B, decode bool, dirPaths ...string) { } } +func setLogging(t testing.TB) { + logHandler := log.StreamHandler(os.Stdout, log.TerminalFormat(true)) + level := log.LvlDebug + if *testFlagLogLevl != "" { + var err error + level, err = log.LvlFromString(*testFlagLogLevl) + if err != nil { + t.Fatal(err) + } + } + log.Root().SetHandler(log.LvlFilterHandler(level, logHandler)) +} + func loadTestTxs(dirPath string) ([]*blocknativeTracerTest, error) { files, err := os.ReadDir(filepath.Join("testdata", dirPath)) if err != nil { From 1fd5854d568d21e6b1cc1463c54a7b3a8bd3bb07 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Sat, 23 Sep 2023 15:59:57 -0400 Subject: [PATCH 3/4] tests: Cleanup tracer test failure output. --- eth/tracers/internal/tracetest/blocknative_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/eth/tracers/internal/tracetest/blocknative_test.go b/eth/tracers/internal/tracetest/blocknative_test.go index 70025ee28916..c09d29ebdb6b 100644 --- a/eth/tracers/internal/tracetest/blocknative_test.go +++ b/eth/tracers/internal/tracetest/blocknative_test.go @@ -280,15 +280,14 @@ func executeTestCase(test *blocknativeTracerTest, t testing.TB, checkResult bool if checkResult && !tracesEqual(ret, test.Result) { // Below are prints to show differences if we fail, can always just check against the specific test json files too! - fmt.Println("Trace return: ") + fmt.Println("Got Trace:") x, _ := json.Marshal(ret) - // //x, _ := json.MarshalIndent(ret, "", " ") - // y, _ := json.Marshal(test.Result) fmt.Println(string(x)) - fmt.Println("test.Result") + + // fmt.Println("Expected Trace:") + // y, _ := json.Marshal(test.Result) // fmt.Println(string(y)) t.Fatal("traces mismatch") - // t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", ret, test.Result) } } From 17941a579a34921f1df8fdd75448722268b563b8 Mon Sep 17 00:00:00 2001 From: Tyler Smith Date: Sat, 23 Sep 2023 16:18:27 -0400 Subject: [PATCH 4/4] tests,fix: Clear decoder cache between tests. --- eth/tracers/blocknative/tracer.go | 6 ++++++ eth/tracers/internal/tracetest/blocknative_test.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/eth/tracers/blocknative/tracer.go b/eth/tracers/blocknative/tracer.go index c407548b309d..7fa4841a7b35 100644 --- a/eth/tracers/blocknative/tracer.go +++ b/eth/tracers/blocknative/tracer.go @@ -242,6 +242,12 @@ func finalizeCallFrame(call *CallFrame, output []byte, gasUsed uint64, err error call.Output = bytesToHex(output) } +// EmptyCache is for testing purposes. It clears the global cache so tests don't +// interfere with each other. +func EmptyCache() { + decoderCache = decoder.NewCaches() +} + // // Unused interface methods. // diff --git a/eth/tracers/internal/tracetest/blocknative_test.go b/eth/tracers/internal/tracetest/blocknative_test.go index c09d29ebdb6b..cb5f9c07d1e7 100644 --- a/eth/tracers/internal/tracetest/blocknative_test.go +++ b/eth/tracers/internal/tracetest/blocknative_test.go @@ -264,6 +264,8 @@ func loadTestTxs(dirPath string) ([]*blocknativeTracerTest, error) { } func executeTestCase(test *blocknativeTracerTest, t testing.TB, checkResult bool) { + blocknative.EmptyCache() + st := core.NewStateTransition(test.evm, test.msg, new(core.GasPool).AddGas(test.tx.Gas())) if _, err := st.TransitionDb(); err != nil { t.Fatalf("failed to execute transaction: %v", err)