From c43faa3d9d88337824e8210e1835cfd82c15cd96 Mon Sep 17 00:00:00 2001 From: Danno Ferrin Date: Tue, 21 Jan 2025 15:02:17 -0700 Subject: [PATCH] cmd/evm: restore --bench flag to evm statetest (#31055) Refactoring of the `evm` command moved where some commands were valid. One command, `--bench`, used to work in `evm statetest`. The pluming is still in place. This PR puts the `--bench` flag in the place the trace flags were moved, and adds tests to validate the bench flag operates in `run` and `statetest` --------- Co-authored-by: Felix Lange --- cmd/evm/staterunner.go | 1 + cmd/evm/t8n_test.go | 56 ++++++++++++++++++++++++++++ cmd/evm/testdata/evmrun/10.out.1.txt | 15 ++++++++ cmd/evm/testdata/evmrun/10.out.2.txt | 0 cmd/evm/testdata/evmrun/9.out.1.txt | 0 cmd/evm/testdata/evmrun/9.out.2.txt | 4 ++ 6 files changed, 76 insertions(+) create mode 100644 cmd/evm/testdata/evmrun/10.out.1.txt create mode 100644 cmd/evm/testdata/evmrun/10.out.2.txt create mode 100644 cmd/evm/testdata/evmrun/9.out.1.txt create mode 100644 cmd/evm/testdata/evmrun/9.out.2.txt diff --git a/cmd/evm/staterunner.go b/cmd/evm/staterunner.go index 4c2dfeabc6..8f81a95315 100644 --- a/cmd/evm/staterunner.go +++ b/cmd/evm/staterunner.go @@ -51,6 +51,7 @@ var stateTestCommand = &cli.Command{ Usage: "Executes the given state tests. Filenames can be fed via standard input (batch mode) or as an argument (one-off execution).", ArgsUsage: "", Flags: slices.Concat([]cli.Flag{ + BenchFlag, DumpFlag, HumanReadableFlag, RunFlag, diff --git a/cmd/evm/t8n_test.go b/cmd/evm/t8n_test.go index 985dac968d..46146be787 100644 --- a/cmd/evm/t8n_test.go +++ b/cmd/evm/t8n_test.go @@ -24,6 +24,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "strings" "testing" @@ -670,6 +671,61 @@ func TestEvmRun(t *testing.T) { } } +func TestEvmRunRegEx(t *testing.T) { + t.Parallel() + tt := cmdtest.NewTestCmd(t, nil) + for i, tc := range []struct { + input []string + wantStdout string + wantStderr string + }{ + { // json tracing + input: []string{"run", "--bench", "6040"}, + wantStdout: "./testdata/evmrun/9.out.1.txt", + wantStderr: "./testdata/evmrun/9.out.2.txt", + }, + { // statetest subcommand + input: []string{"statetest", "--bench", "./testdata/statetest.json"}, + wantStdout: "./testdata/evmrun/10.out.1.txt", + wantStderr: "./testdata/evmrun/10.out.2.txt", + }, + } { + tt.Logf("args: go run ./cmd/evm %v\n", strings.Join(tc.input, " ")) + tt.Run("evm-test", tc.input...) + + haveStdOut := tt.Output() + tt.WaitExit() + haveStdErr := tt.StderrText() + + if have, wantFile := haveStdOut, tc.wantStdout; wantFile != "" { + want, err := os.ReadFile(wantFile) + if err != nil { + t.Fatalf("test %d: could not read expected output: %v", i, err) + } + re, err := regexp.Compile(string(want)) + if err != nil { + t.Fatalf("test %d: could not compile regular expression: %v", i, err) + } + if !re.Match(have) { + t.Fatalf("test %d, output wrong, have \n%v\nwant\n%v\n", i, string(have), re) + } + } + if have, wantFile := haveStdErr, tc.wantStderr; wantFile != "" { + want, err := os.ReadFile(wantFile) + if err != nil { + t.Fatalf("test %d: could not read expected output: %v", i, err) + } + re, err := regexp.Compile(string(want)) + if err != nil { + t.Fatalf("test %d: could not compile regular expression: %v", i, err) + } + if !re.MatchString(have) { + t.Fatalf("test %d, output wrong, have \n%v\nwant\n%v\n", i, have, re) + } + } + } +} + // cmpJson compares the JSON in two byte slices. func cmpJson(a, b []byte) (bool, error) { var j, j2 interface{} diff --git a/cmd/evm/testdata/evmrun/10.out.1.txt b/cmd/evm/testdata/evmrun/10.out.1.txt new file mode 100644 index 0000000000..5f94bdd261 --- /dev/null +++ b/cmd/evm/testdata/evmrun/10.out.1.txt @@ -0,0 +1,15 @@ +\[ + \{ + "name": "00000006-naivefuzz-0", + "pass": false, + "stateRoot": "0xad1024c87b5548e77c937aa50f72b6cb620d278f4dd79bae7f78f71ff75af458", + "fork": "London", + "error": "post state root mismatch: got ad1024c87b5548e77c937aa50f72b6cb620d278f4dd79bae7f78f71ff75af458, want 0000000000000000000000000000000000000000000000000000000000000000", + "benchStats": \{ + "time": \d+, + "allocs": \d+, + "bytesAllocated": \d+, + "gasUsed": \d+ + \} + \} +\] \ No newline at end of file diff --git a/cmd/evm/testdata/evmrun/10.out.2.txt b/cmd/evm/testdata/evmrun/10.out.2.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cmd/evm/testdata/evmrun/9.out.1.txt b/cmd/evm/testdata/evmrun/9.out.1.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/cmd/evm/testdata/evmrun/9.out.2.txt b/cmd/evm/testdata/evmrun/9.out.2.txt new file mode 100644 index 0000000000..90def283e0 --- /dev/null +++ b/cmd/evm/testdata/evmrun/9.out.2.txt @@ -0,0 +1,4 @@ +EVM gas used: \d+ +execution time: \d+\.\d+.s +allocations: \d+ +allocated bytes: \d+