Skip to content

Commit

Permalink
eth/tracers: simplify test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Oct 13, 2022
1 parent fb75f11 commit 1169eb9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 81 deletions.
37 changes: 18 additions & 19 deletions eth/tracers/internal/tracetest/calltrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,21 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err)
}
ret := new(callTrace)
if err := json.Unmarshal(res, ret); err != nil {
t.Fatalf("failed to unmarshal trace result: %v", err)
// The legacy javascript calltracer marshals json in js, which
// is not deterministic (as opposed to the golang json encoder).
{
// This is a tweak to make it deterministic. Can be removed when
// we remove the legacy tracer.
var x callTrace
json.Unmarshal(res, &x)
res, _ = json.Marshal(x)
}

if !jsonEqual(ret, test.Result, new(callTrace), new(callTrace)) {
// uncomment this for easier debugging
//have, _ := json.MarshalIndent(ret, "", " ")
//want, _ := json.MarshalIndent(test.Result, "", " ")
//t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", string(have), string(want))
t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", ret, test.Result)
want, err := json.Marshal(test.Result)
if err != nil {
t.Fatalf("failed to marshal test: %v", err)
}
if string(want) != string(res) {
t.Fatalf("trace mismatch\n have: %v\n want: %v\n", string(res), string(want))
}
})
}
Expand Down Expand Up @@ -298,14 +302,9 @@ func TestZeroValueToNotExitCall(t *testing.T) {
if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err)
}
have := new(callTrace)
if err := json.Unmarshal(res, have); err != nil {
t.Fatalf("failed to unmarshal trace result: %v", err)
}
wantStr := `{"type":"CALL","from":"0x682a80a6f560eec50d54e63cbeda1c324c5f8d1b","to":"0x00000000000000000000000000000000deadbeef","value":"0x0","gas":"0x7148","gasUsed":"0x2d0","input":"0x","output":"0x","calls":[{"type":"CALL","from":"0x00000000000000000000000000000000deadbeef","to":"0x00000000000000000000000000000000000000ff","value":"0x0","gas":"0x6cbf","gasUsed":"0x0","input":"0x","output":"0x"}]}`
want := new(callTrace)
json.Unmarshal([]byte(wantStr), want)
if !jsonEqual(have, want, new(callTrace), new(callTrace)) {
t.Error("have != want")
wantStr := `{"from":"0x682a80a6f560eec50d54e63cbeda1c324c5f8d1b","gas":"0x7148","gasUsed":"0x2d0","to":"0x00000000000000000000000000000000deadbeef","input":"0x","calls":[{"from":"0x00000000000000000000000000000000deadbeef","gas":"0x6cbf","gasUsed":"0x0","to":"0x00000000000000000000000000000000000000ff","input":"0x","value":"0x0","type":"CALL"}],"value":"0x0","type":"CALL"}`
//want := new(callTrace)
if string(res) != wantStr {
t.Fatalf("trace mismatch\n have: %v\n want: %v\n", string(res), wantStr)
}
}
40 changes: 11 additions & 29 deletions eth/tracers/internal/tracetest/prestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,8 @@ import (
"github.com/ethereum/go-ethereum/tests"
)

// prestateTrace is the result of a prestateTrace run.
type prestateTrace = map[common.Address]*account
type account struct {
Balance string `json:"balance,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
Code string `json:"code,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
}
type prePostStateTrace struct {
Pre prestateTrace `json:"pre"`
Post prestateTrace `json:"post"`
}

// prestateTraceTest defines a single test to check the stateDiff tracer against.
type prestateTraceTest struct {
// testcase defines a single test to check the stateDiff tracer against.
type testcase struct {
Genesis *core.Genesis `json:"genesis"`
Context *callContext `json:"context"`
Input string `json:"input"`
Expand All @@ -56,14 +43,14 @@ type prestateTraceTest struct {
}

func TestPrestateTracer(t *testing.T) {
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t, func() interface{} { return new(prestateTrace) })
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t)
}

func TestPrestateWithDiffModeTracer(t *testing.T) {
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t, func() interface{} { return new(prePostStateTrace) })
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
}

func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typeBuilder func() interface{}) {
func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
if err != nil {
t.Fatalf("failed to retrieve tracer test suite: %v", err)
Expand All @@ -77,7 +64,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typ
t.Parallel()

var (
test = new(prestateTraceTest)
test = new(testcase)
tx = new(types.Transaction)
)
// Call tracer test found, read if from disk
Expand Down Expand Up @@ -127,17 +114,12 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typ
if err != nil {
t.Fatalf("failed to retrieve trace result: %v", err)
}
ret := typeBuilder()
if err := json.Unmarshal(res, ret); err != nil {
t.Fatalf("failed to unmarshal trace result: %v", err)
want, err := json.Marshal(test.Result)
if err != nil {
t.Fatalf("failed to marshal test: %v", err)
}

if !jsonEqual(ret, test.Result, typeBuilder(), typeBuilder()) {
// uncomment this for easier debugging
// have, _ := json.MarshalIndent(ret, "", " ")
// want, _ := json.MarshalIndent(test.Result, "", " ")
// t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", string(have), string(want))
t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", ret, test.Result)
if string(want) != string(res) {
t.Fatalf("trace mismatch\n have: %v\n want: %v\n", string(res), string(want))
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@
"nonce": 29072
},
"0x1585936b53834b021f68cc13eeefdec2efc8e724": {
"balance": "0x0",
"nonce": 0
"balance": "0x0"
}
}
}
18 changes: 0 additions & 18 deletions eth/tracers/internal/tracetest/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tracetest

import (
"encoding/json"
"reflect"
"strings"
"unicode"

Expand Down Expand Up @@ -64,22 +62,6 @@ var makeTest = function(tx, rewind) {
}
*/

// jsonEqual is similar to reflect.DeepEqual, but does a 'bounce' via json prior to
// comparison
func jsonEqual(xi, yi, xt, yt interface{}) bool {
if xj, err := json.Marshal(xi); err == nil {
json.Unmarshal(xj, xt)
} else {
return false
}
if yj, err := json.Marshal(yi); err == nil {
json.Unmarshal(yj, yt)
} else {
return false
}
return reflect.DeepEqual(xt, yt)
}

// camel converts a snake cased input string into a camel cased output.
func camel(str string) string {
pieces := strings.Split(str, "_")
Expand Down
1 change: 0 additions & 1 deletion eth/tracers/js/internal/tracers/call_tracer_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
gasUsed: '0x' + bigInt(ctx.gasUsed).toString(16),
input: toHex(ctx.input),
output: toHex(ctx.output),
time: ctx.time,
};
if (this.callstack[0].calls !== undefined) {
result.calls = this.callstack[0].calls;
Expand Down
18 changes: 9 additions & 9 deletions eth/tracers/native/gen_account_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions eth/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type state = map[common.Address]*account

type account struct {
Balance *big.Int `json:"balance,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
Code []byte `json:"code,omitempty"`
Nonce uint64 `json:"nonce,omitempty"`
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
}

Expand Down Expand Up @@ -244,9 +244,9 @@ func (t *prestateTracer) GetResult() (json.RawMessage, error) {
var err error
if t.config.DiffMode {
res, err = json.Marshal(struct {
Pre state `json:"pre"`
Post state `json:"post"`
}{t.pre, t.post})
Pre state `json:"pre"`
}{t.post, t.pre})
} else {
res, err = json.Marshal(t.pre)
}
Expand Down

0 comments on commit 1169eb9

Please sign in to comment.