2022-10-05 19:05:43 -05:00
|
|
|
// 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/tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
// prestateTrace is the result of a prestateTrace run.
|
|
|
|
type prestateTrace = map[common.Address]*account
|
2022-10-21 10:55:15 -05:00
|
|
|
|
2022-10-05 19:05:43 -05:00
|
|
|
type account struct {
|
2022-10-21 10:55:15 -05:00
|
|
|
Balance string `json:"balance"`
|
|
|
|
Code string `json:"code"`
|
|
|
|
Nonce uint64 `json:"nonce"`
|
|
|
|
Storage map[common.Hash]common.Hash `json:"storage"`
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 10:55:15 -05:00
|
|
|
// testcase defines a single test to check the stateDiff tracer against.
|
|
|
|
type testcase struct {
|
2022-10-05 19:05:43 -05:00
|
|
|
Genesis *core.Genesis `json:"genesis"`
|
|
|
|
Context *callContext `json:"context"`
|
|
|
|
Input string `json:"input"`
|
|
|
|
TracerConfig json.RawMessage `json:"tracerConfig"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
2022-10-21 10:55:15 -05:00
|
|
|
func TestPrestateTracerLegacy(t *testing.T) {
|
|
|
|
testPrestateDiffTracer("prestateTracerLegacy", "prestate_tracer_legacy", t)
|
|
|
|
}
|
|
|
|
|
2022-10-05 19:05:43 -05:00
|
|
|
func TestPrestateTracer(t *testing.T) {
|
2022-10-21 10:55:15 -05:00
|
|
|
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t)
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrestateWithDiffModeTracer(t *testing.T) {
|
2022-10-21 10:55:15 -05:00
|
|
|
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 10:55:15 -05:00
|
|
|
func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T) {
|
2022-10-05 19:05:43 -05:00
|
|
|
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 (
|
2022-10-21 10:55:15 -05:00
|
|
|
test = new(testcase)
|
2022-10-05 19:05:43 -05:00
|
|
|
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)
|
|
|
|
}
|
2022-10-12 05:50:01 -05:00
|
|
|
if err := tx.UnmarshalBinary(common.FromHex(test.Input)); err != nil {
|
2022-10-05 19:05:43 -05:00
|
|
|
t.Fatalf("failed to parse testcase input: %v", err)
|
|
|
|
}
|
|
|
|
// Configure a blockchain with the given prestate
|
|
|
|
var (
|
2023-04-21 04:52:02 -05:00
|
|
|
signer = types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)), uint64(test.Context.Time))
|
2022-10-05 19:05:43 -05:00
|
|
|
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)),
|
2023-01-25 05:12:28 -06:00
|
|
|
Time: uint64(test.Context.Time),
|
2022-10-05 19:05:43 -05:00
|
|
|
Difficulty: (*big.Int)(test.Context.Difficulty),
|
|
|
|
GasLimit: uint64(test.Context.GasLimit),
|
2022-10-12 05:50:01 -05:00
|
|
|
BaseFee: test.Genesis.BaseFee,
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
triedb, _, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false, rawdb.HashScheme)
|
2022-10-05 19:05:43 -05:00
|
|
|
)
|
all: activate pbss as experimental feature (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 14:21:36 -05:00
|
|
|
defer triedb.Close()
|
|
|
|
|
2023-01-10 08:11:53 -06:00
|
|
|
tracer, err := tracers.DefaultDirectory.New(tracerName, new(tracers.Context), test.TracerConfig)
|
2022-10-05 19:05:43 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create call tracer: %v", err)
|
|
|
|
}
|
2023-04-04 08:50:13 -05:00
|
|
|
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Tracer: tracer})
|
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and
types.Message gets removed.
This is a breaking change to packages core and core/types. While we do
not promise API stability for package core, we do for core/types. An
exception can be made for types.Message, since it doesn't have any
purpose apart from invoking the state transition in package core.
types.Message was also marked deprecated by the same commit it
got added in, 4dca5d4db7 (November 2016).
The core.Message interface was added in December 2014, in commit
db494170dc, for the purpose of 'testing' state transitions. It's the
same change that made transaction struct fields private. Before that,
the state transition used *types.Transaction directly.
Over time, multiple implementations of the interface accrued across
different packages, since constructing a Message is required whenever
one wants to invoke the state transition. These implementations all
looked very similar, a struct with private fields exposing the fields
as accessor methods.
By changing Message into a struct with public fields we can remove all
these useless interface implementations. It will also hopefully
simplify future changes to the type with less updates to apply across
all of go-ethereum when a field is added to Message.
---------
Co-authored-by: Felix Lange <fjl@twurst.com>
2023-03-09 07:19:12 -06:00
|
|
|
msg, err := core.TransactionToMessage(tx, signer, nil)
|
2022-10-05 19:05:43 -05:00
|
|
|
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)
|
|
|
|
}
|
2022-10-11 02:37:00 -05:00
|
|
|
// Retrieve the trace result and compare against the expected
|
2022-10-05 19:05:43 -05:00
|
|
|
res, err := tracer.GetResult()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve trace result: %v", err)
|
|
|
|
}
|
2022-10-21 10:55:15 -05:00
|
|
|
// The legacy javascript calltracer marshals json in js, which
|
|
|
|
// is not deterministic (as opposed to the golang json encoder).
|
|
|
|
if strings.HasSuffix(dirPath, "_legacy") {
|
|
|
|
// This is a tweak to make it deterministic. Can be removed when
|
|
|
|
// we remove the legacy tracer.
|
|
|
|
var x prestateTrace
|
|
|
|
json.Unmarshal(res, &x)
|
|
|
|
res, _ = json.Marshal(x)
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
2022-10-21 10:55:15 -05:00
|
|
|
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))
|
2022-10-05 19:05:43 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|