2021-11-09 11:09:35 +00: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
|
2021-11-05 10:48:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
2022-05-16 09:59:35 +00:00
|
|
|
"os"
|
2021-11-05 10:48:21 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
"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"
|
2021-11-09 11:09:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-11-05 10:48:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-11-09 11:09:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-11-05 10:48:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
type callContext struct {
|
|
|
|
Number math.HexOrDecimal64 `json:"number"`
|
|
|
|
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
|
|
|
|
Time math.HexOrDecimal64 `json:"timestamp"`
|
|
|
|
GasLimit math.HexOrDecimal64 `json:"gasLimit"`
|
|
|
|
Miner common.Address `json:"miner"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// callTrace is the result of a callTracer run.
|
|
|
|
type callTrace struct {
|
2022-09-29 09:36:07 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
From common.Address `json:"from"`
|
|
|
|
To common.Address `json:"to"`
|
|
|
|
Input hexutil.Bytes `json:"input"`
|
|
|
|
Output hexutil.Bytes `json:"output"`
|
|
|
|
Gas *hexutil.Uint64 `json:"gas,omitempty"`
|
|
|
|
GasUsed *hexutil.Uint64 `json:"gasUsed,omitempty"`
|
|
|
|
Value *hexutil.Big `json:"value,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Revertal string `json:"revertReason,omitempty"`
|
|
|
|
Calls []callTrace `json:"calls,omitempty"`
|
2021-11-05 10:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// callTracerTest defines a single test to check the call tracer against.
|
|
|
|
type callTracerTest struct {
|
2022-08-09 09:04:57 +00:00
|
|
|
Genesis *core.Genesis `json:"genesis"`
|
|
|
|
Context *callContext `json:"context"`
|
|
|
|
Input string `json:"input"`
|
|
|
|
TracerConfig json.RawMessage `json:"tracerConfig"`
|
|
|
|
Result *callTrace `json:"result"`
|
2021-11-05 10:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterates over all the input-output datasets in the tracer test harness and
|
|
|
|
// runs the JavaScript tracers against them.
|
|
|
|
func TestCallTracerLegacy(t *testing.T) {
|
|
|
|
testCallTracer("callTracerLegacy", "call_tracer_legacy", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCallTracerNative(t *testing.T) {
|
2021-11-08 13:08:12 +00:00
|
|
|
testCallTracer("callTracer", "call_tracer", t)
|
2021-11-05 10:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCallTracer(tracerName string, dirPath string, t *testing.T) {
|
2022-05-17 12:27:04 +00:00
|
|
|
files, err := os.ReadDir(filepath.Join("testdata", dirPath))
|
2021-11-05 10:48:21 +00:00
|
|
|
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(callTracerTest)
|
|
|
|
tx = new(types.Transaction)
|
|
|
|
)
|
|
|
|
// Call tracer test found, read if from disk
|
2022-05-16 09:59:35 +00:00
|
|
|
if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil {
|
2021-11-05 10:48:21 +00:00
|
|
|
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 10:50:01 +00:00
|
|
|
if err := tx.UnmarshalBinary(common.FromHex(test.Input)); err != nil {
|
2021-11-05 10:48:21 +00:00
|
|
|
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),
|
2022-10-12 10:50:01 +00:00
|
|
|
BaseFee: test.Genesis.BaseFee,
|
2021-11-05 10:48:21 +00:00
|
|
|
}
|
|
|
|
_, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false)
|
|
|
|
)
|
2022-08-09 09:04:57 +00:00
|
|
|
tracer, err := tracers.New(tracerName, new(tracers.Context), test.TracerConfig)
|
2021-11-05 10:48:21 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-10-11 07:37:00 +00:00
|
|
|
// Retrieve the trace result and compare against the expected.
|
2021-11-05 10:48:21 +00:00
|
|
|
res, err := tracer.GetResult()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve trace result: %v", err)
|
|
|
|
}
|
|
|
|
ret := new(callTrace)
|
|
|
|
if err := json.Unmarshal(res, ret); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal trace result: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-06 00:05:43 +00:00
|
|
|
if !jsonEqual(ret, test.Result, new(callTrace), new(callTrace)) {
|
2021-11-05 10:48:21 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTracers(b *testing.B) {
|
2022-05-17 12:27:04 +00:00
|
|
|
files, err := os.ReadDir(filepath.Join("testdata", "call_tracer"))
|
2021-11-05 10:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
b.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
|
|
|
|
b.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(b *testing.B) {
|
2022-05-16 09:59:35 +00:00
|
|
|
blob, err := os.ReadFile(filepath.Join("testdata", "call_tracer", file.Name()))
|
2021-11-05 10:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("failed to read testcase: %v", err)
|
|
|
|
}
|
|
|
|
test := new(callTracerTest)
|
|
|
|
if err := json.Unmarshal(blob, test); err != nil {
|
|
|
|
b.Fatalf("failed to parse testcase: %v", err)
|
|
|
|
}
|
2022-05-18 14:34:18 +00:00
|
|
|
benchTracer("callTracer", test, b)
|
2021-11-05 10:48:21 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
|
|
|
|
// Configure a blockchain with the given prestate
|
|
|
|
tx := new(types.Transaction)
|
|
|
|
if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil {
|
|
|
|
b.Fatalf("failed to parse testcase input: %v", err)
|
|
|
|
}
|
|
|
|
signer := types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)))
|
|
|
|
msg, err := tx.AsMessage(signer, nil)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("failed to prepare transaction for tracing: %v", err)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-08-09 09:04:57 +00:00
|
|
|
tracer, err := tracers.New(tracerName, new(tracers.Context), nil)
|
2021-11-05 10:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("failed to create call tracer: %v", err)
|
|
|
|
}
|
|
|
|
evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer})
|
|
|
|
snap := statedb.Snapshot()
|
|
|
|
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
|
|
|
|
if _, err = st.TransitionDb(); err != nil {
|
|
|
|
b.Fatalf("failed to execute transaction: %v", err)
|
|
|
|
}
|
|
|
|
if _, err = tracer.GetResult(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
statedb.RevertToSnapshot(snap)
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 11:09:35 +00:00
|
|
|
|
|
|
|
// TestZeroValueToNotExitCall tests the calltracer(s) on the following:
|
|
|
|
// Tx to A, A calls B with zero value. B does not already exist.
|
|
|
|
// Expected: that enter/exit is invoked and the inner call is shown in the result
|
|
|
|
func TestZeroValueToNotExitCall(t *testing.T) {
|
|
|
|
var to = common.HexToAddress("0x00000000000000000000000000000000deadbeef")
|
|
|
|
privkey, err := crypto.HexToECDSA("0000000000000000deadbeef00000000000000000000000000000000deadbeef")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err %v", err)
|
|
|
|
}
|
|
|
|
signer := types.NewEIP155Signer(big.NewInt(1))
|
|
|
|
tx, err := types.SignNewTx(privkey, signer, &types.LegacyTx{
|
|
|
|
GasPrice: big.NewInt(0),
|
|
|
|
Gas: 50000,
|
|
|
|
To: &to,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err %v", err)
|
|
|
|
}
|
|
|
|
origin, _ := signer.Sender(tx)
|
|
|
|
txContext := vm.TxContext{
|
|
|
|
Origin: origin,
|
|
|
|
GasPrice: big.NewInt(1),
|
|
|
|
}
|
|
|
|
context := vm.BlockContext{
|
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
|
|
|
Coinbase: common.Address{},
|
|
|
|
BlockNumber: new(big.Int).SetUint64(8000000),
|
|
|
|
Time: new(big.Int).SetUint64(5),
|
|
|
|
Difficulty: big.NewInt(0x30000),
|
|
|
|
GasLimit: uint64(6000000),
|
|
|
|
}
|
|
|
|
var code = []byte{
|
|
|
|
byte(vm.PUSH1), 0x0, byte(vm.DUP1), byte(vm.DUP1), byte(vm.DUP1), // in and outs zero
|
|
|
|
byte(vm.DUP1), byte(vm.PUSH1), 0xff, byte(vm.GAS), // value=0,address=0xff, gas=GAS
|
|
|
|
byte(vm.CALL),
|
|
|
|
}
|
|
|
|
var alloc = core.GenesisAlloc{
|
|
|
|
to: core.GenesisAccount{
|
|
|
|
Nonce: 1,
|
|
|
|
Code: code,
|
|
|
|
},
|
|
|
|
origin: core.GenesisAccount{
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: big.NewInt(500000000000000),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
|
|
|
|
// Create the tracer, the EVM environment and run it
|
2022-08-09 09:04:57 +00:00
|
|
|
tracer, err := tracers.New("callTracer", nil, nil)
|
2021-11-09 11:09:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create call tracer: %v", err)
|
|
|
|
}
|
|
|
|
evm := vm.NewEVM(context, txContext, statedb, params.MainnetChainConfig, 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)
|
|
|
|
}
|
|
|
|
have := new(callTrace)
|
|
|
|
if err := json.Unmarshal(res, have); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal trace result: %v", err)
|
|
|
|
}
|
|
|
|
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"}]}`
|
|
|
|
want := new(callTrace)
|
|
|
|
json.Unmarshal([]byte(wantStr), want)
|
2022-10-06 00:05:43 +00:00
|
|
|
if !jsonEqual(have, want, new(callTrace), new(callTrace)) {
|
2021-11-09 11:09:35 +00:00
|
|
|
t.Error("have != want")
|
|
|
|
}
|
|
|
|
}
|