cmd/evm: add testcases for evm run output

This commit is contained in:
Martin Holst Swende 2024-12-03 10:58:10 +01:00
parent 9e3ddadf2f
commit a4853ad7b8
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
11 changed files with 135 additions and 0 deletions

View file

@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"reflect"
"slices"
"strings"
"testing"
@ -672,6 +673,74 @@ func TestB11r(t *testing.T) {
}
}
func TestEvmRun(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{"--trace", "--trace.format=json", "6040"},
wantStdout: "./testdata/evmrun/1.out.1.txt",
wantStderr: "./testdata/evmrun/1.out.2.txt",
},
{ // Same as above, using the deprecated --json
input: []string{"--json", "6040"},
wantStdout: "./testdata/evmrun/1.out.1.txt",
wantStderr: "./testdata/evmrun/1.out.2.txt",
},
{ // default tracing (struct)
input: []string{"--trace", "0x6040"},
wantStdout: "./testdata/evmrun/2.out.1.txt",
wantStderr: "./testdata/evmrun/2.out.2.txt",
},
{ // default tracing (struct), plus alloc-dump
input: []string{"--trace", "--dump", "0x6040"},
wantStdout: "./testdata/evmrun/3.out.1.txt",
//wantStderr: "./testdata/evmrun/3.out.2.txt",
},
{ // json-tracing, plus alloc-dump
input: []string{"--trace", "--trace.format=json", "--dump", "0x6040"},
wantStdout: "./testdata/evmrun/4.out.1.txt",
//wantStderr: "./testdata/evmrun/4.out.2.txt",
},
{ // md-tracing
input: []string{"--trace", "--trace.format=md", "0x6040"},
wantStdout: "./testdata/evmrun/5.out.1.txt",
wantStderr: "./testdata/evmrun/5.out.2.txt",
},
} {
args := slices.Concat([]string{"run"}, tc.input)
tt.Logf("args: go run ./cmd/evm %v\n", strings.Join(args, " "))
tt.Run("evm-test", args...)
haveStdOut := tt.Output()
haveStdErr := tt.StderrText()
tt.WaitExit()
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)
}
if string(haveStdOut) != string(want) {
t.Fatalf("test %d, output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
}
}
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)
}
if string(have) != string(want) {
t.Fatalf("test %d, output wrong\nhave %q\nwant %q\n", i, string(have), string(want))
}
}
}
}
// cmpJson compares the JSON in two byte slices.
func cmpJson(a, b []byte) (bool, error) {
var j, j2 interface{}

0
cmd/evm/testdata/evmrun/1.out.1.txt vendored Normal file
View file

3
cmd/evm/testdata/evmrun/1.out.2.txt vendored Normal file
View file

@ -0,0 +1,3 @@
{"pc":0,"op":96,"gas":"0x2540be400","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":2,"op":0,"gas":"0x2540be3fd","gasCost":"0x0","memSize":0,"stack":["0x40"],"depth":1,"refund":0,"opName":"STOP"}
{"output":"","gasUsed":"0x3"}

0
cmd/evm/testdata/evmrun/2.out.1.txt vendored Normal file
View file

6
cmd/evm/testdata/evmrun/2.out.2.txt vendored Normal file
View file

@ -0,0 +1,6 @@
PUSH1 pc=00000000 gas=10000000000 cost=3
STOP pc=00000002 gas=9999999997 cost=0
Stack:
00000000 0x40

13
cmd/evm/testdata/evmrun/3.out.1.txt vendored Normal file
View file

@ -0,0 +1,13 @@
{
"root": "b444481d1367188172f8c6091e948aaa68bae763fd26d6b9e994306a66bf69f9",
"accounts": {
"pre(0x30d7a0694cb29af31b982480e11d7ebb003a3fca4026939149071f014689b142)": {
"balance": "0",
"nonce": 0,
"root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"codeHash": "0x3e48ef54b89079a075f3b8fc253c657a86b110a7aed3568c1517b10edf2c3eb6",
"code": "0x6040",
"key": "0x30d7a0694cb29af31b982480e11d7ebb003a3fca4026939149071f014689b142"
}
}
}

9
cmd/evm/testdata/evmrun/3.out.2.txt vendored Normal file
View file

@ -0,0 +1,9 @@
PUSH1 pc=00000000 gas=10000000000 cost=3
STOP pc=00000002 gas=9999999997 cost=0
Stack:
00000000 0x40
INFO [12-03|10:37:15.827] Trie dumping started root=b44448..bf69f9
WARN [12-03|10:37:15.827] Dump incomplete due to missing preimages missing=1
INFO [12-03|10:37:15.827] Trie dumping complete accounts=1 elapsed="163.513µs"

13
cmd/evm/testdata/evmrun/4.out.1.txt vendored Normal file
View file

@ -0,0 +1,13 @@
{
"root": "b444481d1367188172f8c6091e948aaa68bae763fd26d6b9e994306a66bf69f9",
"accounts": {
"pre(0x30d7a0694cb29af31b982480e11d7ebb003a3fca4026939149071f014689b142)": {
"balance": "0",
"nonce": 0,
"root": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"codeHash": "0x3e48ef54b89079a075f3b8fc253c657a86b110a7aed3568c1517b10edf2c3eb6",
"code": "0x6040",
"key": "0x30d7a0694cb29af31b982480e11d7ebb003a3fca4026939149071f014689b142"
}
}
}

6
cmd/evm/testdata/evmrun/4.out.2.txt vendored Normal file
View file

@ -0,0 +1,6 @@
{"pc":0,"op":96,"gas":"0x2540be400","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
{"pc":2,"op":0,"gas":"0x2540be3fd","gasCost":"0x0","memSize":0,"stack":["0x40"],"depth":1,"refund":0,"opName":"STOP"}
{"output":"","gasUsed":"0x3"}
INFO [12-03|10:38:33.360] Trie dumping started root=b44448..bf69f9
WARN [12-03|10:38:33.361] Dump incomplete due to missing preimages missing=1
INFO [12-03|10:38:33.361] Trie dumping complete accounts=1 elapsed="240.811µs"

0
cmd/evm/testdata/evmrun/5.out.1.txt vendored Normal file
View file

16
cmd/evm/testdata/evmrun/5.out.2.txt vendored Normal file
View file

@ -0,0 +1,16 @@
Pre-execution info:
- from: `0x000000000000000000000000000073656E646572`
- to: `0x0000000000000000000000007265636569766572`
- data: ``
- gas: `10000000000`
- value: `0` wei
| Pc | Op | Cost | Refund | Stack |
|-------|-------------|------|-----------|-----------|
| 0 | PUSH1 | 3 | 0 | [] |
| 2 | STOP | 0 | 0 | [0x40] |
Post-execution info:
- output: ``
- consumed gas: `3`
- error: `<nil>`