Skip to content

Commit 5d52a35

Browse files
authored
eth/tracers: add diffMode to prestateTracer (#25422)
Backwards compatibility warning: The result will from now on omit empty fields instead of including a zero value (e.g. no more `balance: '0x'`). The prestateTracer will now take an option `diffMode: bool`. In this mode the tracer will output the pre state and post data for the modified parts of state. Read-only accesses will be completely omitted. Creations (be it account or slot) will be signified by omission in the `pre` list and inclusion in `post`. Whereas deletion (be it account or slot) will be signified by inclusion in `pre` and omission in `post` list. Signed-off-by: Delweng <[email protected]>
1 parent e14164d commit 5d52a35

File tree

11 files changed

+1245
-114
lines changed

11 files changed

+1245
-114
lines changed

eth/tracers/internal/tracetest/calltrace_test.go

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import (
2121
"math/big"
2222
"os"
2323
"path/filepath"
24-
"reflect"
2524
"strings"
2625
"testing"
27-
"unicode"
2826

2927
"github.com/ethereum/go-ethereum/common"
3028
"github.com/ethereum/go-ethereum/common/hexutil"
@@ -38,62 +36,8 @@ import (
3836
"github.com/ethereum/go-ethereum/params"
3937
"github.com/ethereum/go-ethereum/rlp"
4038
"github.com/ethereum/go-ethereum/tests"
41-
42-
// Force-load native and js packages, to trigger registration
43-
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
44-
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
4539
)
4640

47-
// To generate a new callTracer test, copy paste the makeTest method below into
48-
// a Geth console and call it with a transaction hash you which to export.
49-
50-
/*
51-
// makeTest generates a callTracer test by running a prestate reassembled and a
52-
// call trace run, assembling all the gathered information into a test case.
53-
var makeTest = function(tx, rewind) {
54-
// Generate the genesis block from the block, transaction and prestate data
55-
var block = eth.getBlock(eth.getTransaction(tx).blockHash);
56-
var genesis = eth.getBlock(block.parentHash);
57-
58-
delete genesis.gasUsed;
59-
delete genesis.logsBloom;
60-
delete genesis.parentHash;
61-
delete genesis.receiptsRoot;
62-
delete genesis.sha3Uncles;
63-
delete genesis.size;
64-
delete genesis.transactions;
65-
delete genesis.transactionsRoot;
66-
delete genesis.uncles;
67-
68-
genesis.gasLimit = genesis.gasLimit.toString();
69-
genesis.number = genesis.number.toString();
70-
genesis.timestamp = genesis.timestamp.toString();
71-
72-
genesis.alloc = debug.traceTransaction(tx, {tracer: "prestateTracer", rewind: rewind});
73-
for (var key in genesis.alloc) {
74-
genesis.alloc[key].nonce = genesis.alloc[key].nonce.toString();
75-
}
76-
genesis.config = admin.nodeInfo.protocols.eth.config;
77-
78-
// Generate the call trace and produce the test input
79-
var result = debug.traceTransaction(tx, {tracer: "callTracer", rewind: rewind});
80-
delete result.time;
81-
82-
console.log(JSON.stringify({
83-
genesis: genesis,
84-
context: {
85-
number: block.number.toString(),
86-
difficulty: block.difficulty,
87-
timestamp: block.timestamp.toString(),
88-
gasLimit: block.gasLimit.toString(),
89-
miner: block.miner,
90-
},
91-
input: eth.getRawTransaction(tx),
92-
result: result,
93-
}, null, 2));
94-
}
95-
*/
96-
9741
type callContext struct {
9842
Number math.HexOrDecimal64 `json:"number"`
9943
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
@@ -204,7 +148,7 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
204148
t.Fatalf("failed to unmarshal trace result: %v", err)
205149
}
206150

207-
if !jsonEqual(ret, test.Result) {
151+
if !jsonEqual(ret, test.Result, new(callTrace), new(callTrace)) {
208152
// uncomment this for easier debugging
209153
//have, _ := json.MarshalIndent(ret, "", " ")
210154
//want, _ := json.MarshalIndent(test.Result, "", " ")
@@ -215,32 +159,6 @@ func testCallTracer(tracerName string, dirPath string, t *testing.T) {
215159
}
216160
}
217161

218-
// jsonEqual is similar to reflect.DeepEqual, but does a 'bounce' via json prior to
219-
// comparison
220-
func jsonEqual(x, y interface{}) bool {
221-
xTrace := new(callTrace)
222-
yTrace := new(callTrace)
223-
if xj, err := json.Marshal(x); err == nil {
224-
json.Unmarshal(xj, xTrace)
225-
} else {
226-
return false
227-
}
228-
if yj, err := json.Marshal(y); err == nil {
229-
json.Unmarshal(yj, yTrace)
230-
} else {
231-
return false
232-
}
233-
return reflect.DeepEqual(xTrace, yTrace)
234-
}
235-
236-
// camel converts a snake cased input string into a camel cased output.
237-
func camel(str string) string {
238-
pieces := strings.Split(str, "_")
239-
for i := 1; i < len(pieces); i++ {
240-
pieces[i] = string(unicode.ToUpper(rune(pieces[i][0]))) + pieces[i][1:]
241-
}
242-
return strings.Join(pieces, "")
243-
}
244162
func BenchmarkTracers(b *testing.B) {
245163
files, err := os.ReadDir(filepath.Join("testdata", "call_tracer"))
246164
if err != nil {
@@ -386,7 +304,7 @@ func TestZeroValueToNotExitCall(t *testing.T) {
386304
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"}]}`
387305
want := new(callTrace)
388306
json.Unmarshal([]byte(wantStr), want)
389-
if !jsonEqual(have, want) {
307+
if !jsonEqual(have, want, new(callTrace), new(callTrace)) {
390308
t.Error("have != want")
391309
}
392310
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2021 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package tracetest
18+
19+
import (
20+
"encoding/json"
21+
"math/big"
22+
"os"
23+
"path/filepath"
24+
"strings"
25+
"testing"
26+
27+
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/core"
29+
"github.com/ethereum/go-ethereum/core/rawdb"
30+
"github.com/ethereum/go-ethereum/core/types"
31+
"github.com/ethereum/go-ethereum/core/vm"
32+
"github.com/ethereum/go-ethereum/eth/tracers"
33+
"github.com/ethereum/go-ethereum/rlp"
34+
"github.com/ethereum/go-ethereum/tests"
35+
)
36+
37+
// prestateTrace is the result of a prestateTrace run.
38+
type prestateTrace = map[common.Address]*account
39+
type account struct {
40+
Balance string `json:"balance,omitempty"`
41+
Nonce uint64 `json:"nonce,omitempty"`
42+
Code string `json:"code,omitempty"`
43+
Storage map[common.Hash]common.Hash `json:"storage,omitempty"`
44+
}
45+
type prePostStateTrace struct {
46+
Pre prestateTrace `json:"pre"`
47+
Post prestateTrace `json:"post"`
48+
}
49+
50+
// prestateTraceTest defines a single test to check the stateDiff tracer against.
51+
type prestateTraceTest struct {
52+
Genesis *core.Genesis `json:"genesis"`
53+
Context *callContext `json:"context"`
54+
Input string `json:"input"`
55+
TracerConfig json.RawMessage `json:"tracerConfig"`
56+
Result interface{} `json:"result"`
57+
}
58+
59+
func TestPrestateTracer(t *testing.T) {
60+
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t, func() interface{} { return new(prestateTrace) })
61+
}
62+
63+
func TestPrestateWithDiffModeTracer(t *testing.T) {
64+
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t, func() interface{} { return new(prePostStateTrace) })
65+
}
66+
67+
func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typeBuilder func() interface{}) {
68+
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
69+
if err != nil {
70+
t.Fatalf("failed to retrieve tracer test suite: %v", err)
71+
}
72+
for _, file := range files {
73+
if !strings.HasSuffix(file.Name(), ".json") {
74+
continue
75+
}
76+
file := file // capture range variable
77+
t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) {
78+
t.Parallel()
79+
80+
var (
81+
test = new(prestateTraceTest)
82+
tx = new(types.Transaction)
83+
)
84+
// Call tracer test found, read if from disk
85+
if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil {
86+
t.Fatalf("failed to read testcase: %v", err)
87+
} else if err := json.Unmarshal(blob, test); err != nil {
88+
t.Fatalf("failed to parse testcase: %v", err)
89+
}
90+
if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil {
91+
t.Fatalf("failed to parse testcase input: %v", err)
92+
}
93+
// Configure a blockchain with the given prestate
94+
var (
95+
signer = types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)))
96+
origin, _ = signer.Sender(tx)
97+
txContext = vm.TxContext{
98+
Origin: origin,
99+
GasPrice: tx.GasPrice(),
100+
}
101+
context = vm.BlockContext{
102+
CanTransfer: core.CanTransfer,
103+
Transfer: core.Transfer,
104+
Coinbase: test.Context.Miner,
105+
BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)),
106+
Time: new(big.Int).SetUint64(uint64(test.Context.Time)),
107+
Difficulty: (*big.Int)(test.Context.Difficulty),
108+
GasLimit: uint64(test.Context.GasLimit),
109+
}
110+
_, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false)
111+
)
112+
tracer, err := tracers.New(tracerName, new(tracers.Context), test.TracerConfig)
113+
if err != nil {
114+
t.Fatalf("failed to create call tracer: %v", err)
115+
}
116+
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})
117+
msg, err := tx.AsMessage(signer, nil)
118+
if err != nil {
119+
t.Fatalf("failed to prepare transaction for tracing: %v", err)
120+
}
121+
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
122+
if _, err = st.TransitionDb(); err != nil {
123+
t.Fatalf("failed to execute transaction: %v", err)
124+
}
125+
// Retrieve the trace result and compare against the etalon
126+
res, err := tracer.GetResult()
127+
if err != nil {
128+
t.Fatalf("failed to retrieve trace result: %v", err)
129+
}
130+
ret := typeBuilder()
131+
if err := json.Unmarshal(res, ret); err != nil {
132+
t.Fatalf("failed to unmarshal trace result: %v", err)
133+
}
134+
135+
if !jsonEqual(ret, test.Result, typeBuilder(), typeBuilder()) {
136+
// uncomment this for easier debugging
137+
// have, _ := json.MarshalIndent(ret, "", " ")
138+
// want, _ := json.MarshalIndent(test.Result, "", " ")
139+
// t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", string(have), string(want))
140+
t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", ret, test.Result)
141+
}
142+
})
143+
}
144+
}

0 commit comments

Comments
 (0)