eth/tracers: refactor block context in test runner (#29450)

This commit contains a minor refactoring of the block context
used within the test runners.

---------

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
Delweng 2025-02-20 22:04:35 +08:00 committed by GitHub
parent b1f88ef9bd
commit dcc0b3704d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 35 deletions

View File

@ -65,10 +65,7 @@ type callTrace struct {
// callTracerTest defines a single test to check the call tracer against.
type callTracerTest struct {
Genesis *core.Genesis `json:"genesis"`
Context *callContext `json:"context"`
Input string `json:"input"`
TracerConfig json.RawMessage `json:"tracerConfig"`
tracerTestEnv
Result *callTrace `json:"result"`
}

View File

@ -77,10 +77,7 @@ type flatCallTraceResult struct {
// flatCallTracerTest defines a single test to check the call tracer against.
type flatCallTracerTest struct {
Genesis *core.Genesis `json:"genesis"`
Context *callContext `json:"context"`
Input string `json:"input"`
TracerConfig json.RawMessage `json:"tracerConfig"`
tracerTestEnv
Result []flatCallTrace `json:"result"`
}

View File

@ -31,6 +31,9 @@ var makeTest = function(tx, traceConfig) {
delete genesis.transactions;
delete genesis.transactionsRoot;
delete genesis.uncles;
delete genesis.withdrawals;
delete genesis.withdrawalsRoot;
delete genesis.baseFeePerGas;
genesis.gasLimit = genesis.gasLimit.toString();
genesis.number = genesis.number.toString();
@ -60,11 +63,15 @@ var makeTest = function(tx, traceConfig) {
context.baseFeePerGas = block.baseFeePerGas.toString();
}
console.log(JSON.stringify({
var data = {
genesis: genesis,
context: context,
input: eth.getRawTransaction(tx),
result: result,
tracerConfig: traceConfig.tracerConfig,
}, null, 2));
};
if (traceConfig && traceConfig.tracerConfig) {
data.tracerConfig = traceConfig.tracerConfig;
}
console.log(JSON.stringify(data, null, 2));
}

View File

@ -43,28 +43,25 @@ type account struct {
Storage map[common.Hash]common.Hash `json:"storage"`
}
// 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"`
TracerConfig json.RawMessage `json:"tracerConfig"`
// prestateTracerTest defines a single test to check the stateDiff tracer against.
type prestateTracerTest struct {
tracerTestEnv
Result interface{} `json:"result"`
}
func TestPrestateTracerLegacy(t *testing.T) {
testPrestateDiffTracer("prestateTracerLegacy", "prestate_tracer_legacy", t)
testPrestateTracer("prestateTracerLegacy", "prestate_tracer_legacy", t)
}
func TestPrestateTracer(t *testing.T) {
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t)
testPrestateTracer("prestateTracer", "prestate_tracer", t)
}
func TestPrestateWithDiffModeTracer(t *testing.T) {
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
testPrestateTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
}
func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
func testPrestateTracer(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)
@ -77,7 +74,7 @@ func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
t.Parallel()
var (
test = new(testcase)
test = new(prestateTracerTest)
tx = new(types.Transaction)
)
// Call tracer test found, read if from disk

View File

@ -14,8 +14,6 @@
"parentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateRoot": "0x577f42ab21ccfd946511c57869ace0bdf7c217c36f02b7cd3459df0ed1cffc1a",
"timestamp": "1709626771",
"withdrawals": [],
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"alloc": {
"0x0000000000000000000000000000000000000000": {
"balance": "0x272e0528"

View File

@ -14,8 +14,6 @@
"parentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"stateRoot": "0x577f42ab21ccfd946511c57869ace0bdf7c217c36f02b7cd3459df0ed1cffc1a",
"timestamp": "1709626771",
"withdrawals": [],
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"alloc": {
"0x0000000000000000000000000000000000000000": {
"balance": "0x272e0528"

View File

@ -11,8 +11,6 @@
"number": "1",
"stateRoot": "0xd2ebe0a7f3572ffe3e5b4c78147376d3fca767f236e4dd23f9151acfec7cb0d1",
"timestamp": "1699617692",
"withdrawals": [],
"withdrawalsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"alloc": {
"0x0000000000000000000000000000000000000000": {
"balance": "0x5208"

View File

@ -17,6 +17,7 @@
package tracetest
import (
"encoding/json"
"math/big"
"strings"
"unicode"
@ -42,7 +43,8 @@ func camel(str string) string {
return strings.Join(pieces, "")
}
type callContext struct {
// traceContext defines a context used to construct the block context
type traceContext struct {
Number math.HexOrDecimal64 `json:"number"`
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
Time math.HexOrDecimal64 `json:"timestamp"`
@ -51,7 +53,7 @@ type callContext struct {
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas"`
}
func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
func (c *traceContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
context := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
@ -77,3 +79,11 @@ func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
}
return context
}
// tracerTestEnv defines a tracer test required fields
type tracerTestEnv struct {
Genesis *core.Genesis `json:"genesis"`
Context *traceContext `json:"context"`
Input string `json:"input"`
TracerConfig json.RawMessage `json:"tracerConfig"`
}