459bb4a647
This PR moves the logging/tracing-facilities out of `*state.StateDB`, in to a wrapping struct which implements `vm.StateDB` instead. In most places, it is a pretty straight-forward change: - First, hoisting the invocations from state objects up to the statedb. - Then making the mutation-methods simply return the previous value, so that the external logging layer could log everything. Some internal code uses the direct object-accessors to mutate the state, particularly in testing and in setting up state overrides, which means that these changes are unobservable for the hooked layer. Thus, configuring the overrides are not necessarily part of the API we want to publish. The trickiest part about the layering is that when the selfdestructs are finally deleted during `Finalise`, there's the possibility that someone sent some ether to it, which is burnt at that point, and thus needs to be logged. The hooked layer reaches into the inner layer to figure out these events. In package `vm`, the conversion from `state.StateDB + hooks` into a hooked `vm.StateDB` is performed where needed. --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>
140 lines
5.0 KiB
Go
140 lines
5.0 KiB
Go
// 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
|
|
|
|
type account struct {
|
|
Balance string `json:"balance"`
|
|
Code string `json:"code"`
|
|
Nonce uint64 `json:"nonce"`
|
|
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"`
|
|
Result interface{} `json:"result"`
|
|
}
|
|
|
|
func TestPrestateTracerLegacy(t *testing.T) {
|
|
testPrestateDiffTracer("prestateTracerLegacy", "prestate_tracer_legacy", t)
|
|
}
|
|
|
|
func TestPrestateTracer(t *testing.T) {
|
|
testPrestateDiffTracer("prestateTracer", "prestate_tracer", t)
|
|
}
|
|
|
|
func TestPrestateWithDiffModeTracer(t *testing.T) {
|
|
testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t)
|
|
}
|
|
|
|
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)
|
|
}
|
|
for _, file := range files {
|
|
if !strings.HasSuffix(file.Name(), ".json") {
|
|
continue
|
|
}
|
|
t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
test = new(testcase)
|
|
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 := tx.UnmarshalBinary(common.FromHex(test.Input)); 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)), uint64(test.Context.Time))
|
|
context = test.Context.toBlockContext(test.Genesis)
|
|
state = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false, rawdb.HashScheme)
|
|
)
|
|
defer state.Close()
|
|
|
|
tracer, err := tracers.DefaultDirectory.New(tracerName, new(tracers.Context), test.TracerConfig, test.Genesis.Config)
|
|
if err != nil {
|
|
t.Fatalf("failed to create call tracer: %v", err)
|
|
}
|
|
|
|
msg, err := core.TransactionToMessage(tx, signer, context.BaseFee)
|
|
if err != nil {
|
|
t.Fatalf("failed to prepare transaction for tracing: %v", err)
|
|
}
|
|
evm := vm.NewEVM(context, core.NewEVMTxContext(msg), state.StateDB, test.Genesis.Config, vm.Config{Tracer: tracer.Hooks})
|
|
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
|
|
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
|
|
if err != nil {
|
|
t.Fatalf("failed to execute transaction: %v", err)
|
|
}
|
|
tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, nil)
|
|
// Retrieve the trace result and compare against the expected
|
|
res, err := tracer.GetResult()
|
|
if err != nil {
|
|
t.Fatalf("failed to retrieve trace result: %v", err)
|
|
}
|
|
// 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)
|
|
}
|
|
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))
|
|
}
|
|
})
|
|
}
|
|
}
|