forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eth/tracers: add diffMode to prestateTracer (ethereum#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 <delweng@gmail.com>
- Loading branch information
Showing
11 changed files
with
1,245 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright 2021 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package tracetest | ||
|
||
import ( | ||
"encoding/json" | ||
"math/big" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/rawdb" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/eth/tracers" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"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 { | ||
Genesis *core.Genesis `json:"genesis"` | ||
Context *callContext `json:"context"` | ||
Input string `json:"input"` | ||
TracerConfig json.RawMessage `json:"tracerConfig"` | ||
Result interface{} `json:"result"` | ||
} | ||
|
||
func TestPrestateTracer(t *testing.T) { | ||
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t, func() interface{} { return new(prestateTrace) }) | ||
} | ||
|
||
func TestPrestateWithDiffModeTracer(t *testing.T) { | ||
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t, func() interface{} { return new(prePostStateTrace) }) | ||
} | ||
|
||
func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typeBuilder func() interface{}) { | ||
files, err := os.ReadDir(filepath.Join("testdata", dirPath)) | ||
if err != nil { | ||
t.Fatalf("failed to retrieve tracer test suite: %v", err) | ||
} | ||
for _, file := range files { | ||
if !strings.HasSuffix(file.Name(), ".json") { | ||
continue | ||
} | ||
file := file // capture range variable | ||
t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var ( | ||
test = new(prestateTraceTest) | ||
tx = new(types.Transaction) | ||
) | ||
// Call tracer test found, read if from disk | ||
if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { | ||
t.Fatalf("failed to read testcase: %v", err) | ||
} else if err := json.Unmarshal(blob, test); err != nil { | ||
t.Fatalf("failed to parse testcase: %v", err) | ||
} | ||
if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil { | ||
t.Fatalf("failed to parse testcase input: %v", err) | ||
} | ||
// Configure a blockchain with the given prestate | ||
var ( | ||
signer = types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number))) | ||
origin, _ = signer.Sender(tx) | ||
txContext = vm.TxContext{ | ||
Origin: origin, | ||
GasPrice: tx.GasPrice(), | ||
} | ||
context = vm.BlockContext{ | ||
CanTransfer: core.CanTransfer, | ||
Transfer: core.Transfer, | ||
Coinbase: test.Context.Miner, | ||
BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)), | ||
Time: new(big.Int).SetUint64(uint64(test.Context.Time)), | ||
Difficulty: (*big.Int)(test.Context.Difficulty), | ||
GasLimit: uint64(test.Context.GasLimit), | ||
} | ||
_, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false) | ||
) | ||
tracer, err := tracers.New(tracerName, new(tracers.Context), test.TracerConfig) | ||
if err != nil { | ||
t.Fatalf("failed to create call tracer: %v", err) | ||
} | ||
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer}) | ||
msg, err := tx.AsMessage(signer, nil) | ||
if err != nil { | ||
t.Fatalf("failed to prepare transaction for tracing: %v", err) | ||
} | ||
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas())) | ||
if _, err = st.TransitionDb(); err != nil { | ||
t.Fatalf("failed to execute transaction: %v", err) | ||
} | ||
// Retrieve the trace result and compare against the etalon | ||
res, err := tracer.GetResult() | ||
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) | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.