cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 11:56:11 +00:00
|
|
|
// Copyright 2017 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 tracers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-09-24 12:57:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 11:56:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2019-01-05 20:26:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-11-25 12:17:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
2019-01-05 20:26:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
cmd, core, eth/tracers: support fancier js tracing (#15516)
* cmd, core, eth/tracers: support fancier js tracing
* eth, internal/web3ext: rework trace API, concurrency, chain tracing
* eth/tracers: add three more JavaScript tracers
* eth/tracers, vendor: swap ottovm to duktape for tracing
* core, eth, internal: finalize call tracer and needed extras
* eth, tests: prestate tracer, call test suite, rewinding
* vendor: fix windows builds for tracer js engine
* vendor: temporary duktape fix
* eth/tracers: fix up 4byte and evmdis tracer
* vendor: pull in latest duktape with my upstream fixes
* eth: fix some review comments
* eth: rename rewind to reexec to make it more obvious
* core/vm: terminate tracing using defers
2017-12-21 11:56:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/tests"
|
|
|
|
)
|
|
|
|
|
2021-07-01 07:15:04 +00:00
|
|
|
func BenchmarkTransactionTrace(b *testing.B) {
|
|
|
|
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
from := crypto.PubkeyToAddress(key.PublicKey)
|
|
|
|
gas := uint64(1000000) // 1M gas
|
|
|
|
to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
|
|
|
|
signer := types.LatestSignerForChainID(big.NewInt(1337))
|
|
|
|
tx, err := types.SignNewTx(key, signer,
|
|
|
|
&types.LegacyTx{
|
|
|
|
Nonce: 1,
|
|
|
|
GasPrice: big.NewInt(500),
|
|
|
|
Gas: gas,
|
|
|
|
To: &to,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
txContext := vm.TxContext{
|
|
|
|
Origin: from,
|
|
|
|
GasPrice: tx.GasPrice(),
|
|
|
|
}
|
|
|
|
context := vm.BlockContext{
|
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
|
|
|
Coinbase: common.Address{},
|
|
|
|
BlockNumber: new(big.Int).SetUint64(uint64(5)),
|
2023-01-25 11:12:28 +00:00
|
|
|
Time: 5,
|
2021-07-01 07:15:04 +00:00
|
|
|
Difficulty: big.NewInt(0xffffffff),
|
|
|
|
GasLimit: gas,
|
2021-10-29 12:37:00 +00:00
|
|
|
BaseFee: big.NewInt(8),
|
2021-07-01 07:15:04 +00:00
|
|
|
}
|
2024-02-16 18:05:33 +00:00
|
|
|
alloc := types.GenesisAlloc{}
|
2021-07-01 07:15:04 +00:00
|
|
|
// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
|
|
|
|
// the address
|
|
|
|
loop := []byte{
|
|
|
|
byte(vm.JUMPDEST), // [ count ]
|
|
|
|
byte(vm.PUSH1), 0, // jumpdestination
|
|
|
|
byte(vm.JUMP),
|
|
|
|
}
|
2024-02-16 18:05:33 +00:00
|
|
|
alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = types.Account{
|
2021-07-01 07:15:04 +00:00
|
|
|
Nonce: 1,
|
|
|
|
Code: loop,
|
|
|
|
Balance: big.NewInt(1),
|
|
|
|
}
|
2024-02-16 18:05:33 +00:00
|
|
|
alloc[from] = types.Account{
|
2021-07-01 07:15:04 +00:00
|
|
|
Nonce: 1,
|
|
|
|
Code: []byte{},
|
|
|
|
Balance: big.NewInt(500000000000000),
|
|
|
|
}
|
2024-02-14 16:02:56 +00:00
|
|
|
state := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false, rawdb.HashScheme)
|
|
|
|
defer state.Close()
|
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 19:21:36 +00:00
|
|
|
|
2021-07-01 07:15:04 +00:00
|
|
|
// Create the tracer, the EVM environment and run it
|
2021-11-25 12:17:09 +00:00
|
|
|
tracer := logger.NewStructLogger(&logger.Config{
|
2021-07-01 07:15:04 +00:00
|
|
|
Debug: false,
|
|
|
|
//DisableStorage: true,
|
2021-09-13 16:59:52 +00:00
|
|
|
//EnableMemory: false,
|
|
|
|
//EnableReturnData: false,
|
2021-07-01 07:15:04 +00:00
|
|
|
})
|
2024-02-14 16:02:56 +00:00
|
|
|
evm := vm.NewEVM(context, txContext, state.StateDB, params.AllEthashProtocolChanges, vm.Config{Tracer: tracer})
|
2024-01-16 11:20:26 +00:00
|
|
|
msg, err := core.TransactionToMessage(tx, signer, context.BaseFee)
|
2021-07-01 07:15:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("failed to prepare transaction for tracing: %v", err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-02-14 16:02:56 +00:00
|
|
|
snap := state.StateDB.Snapshot()
|
2021-07-01 07:15:04 +00:00
|
|
|
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
|
|
|
|
_, err = st.TransitionDb()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2024-02-14 16:02:56 +00:00
|
|
|
state.StateDB.RevertToSnapshot(snap)
|
2021-07-01 07:15:04 +00:00
|
|
|
if have, want := len(tracer.StructLogs()), 244752; have != want {
|
|
|
|
b.Fatalf("trace wrong, want %d steps, have %d", want, have)
|
|
|
|
}
|
|
|
|
tracer.Reset()
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 09:46:53 +00:00
|
|
|
|
|
|
|
func TestMemCopying(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
memsize int64
|
|
|
|
offset int64
|
|
|
|
size int64
|
|
|
|
wantErr string
|
|
|
|
wantSize int
|
|
|
|
}{
|
|
|
|
{0, 0, 100, "", 100}, // Should pad up to 100
|
|
|
|
{0, 100, 0, "", 0}, // No need to pad (0 size)
|
|
|
|
{100, 50, 100, "", 100}, // Should pad 100-150
|
|
|
|
{100, 50, 5, "", 5}, // Wanted range fully within memory
|
2024-02-05 21:16:32 +00:00
|
|
|
{100, -50, 0, "offset or size must not be negative", 0}, // Error
|
|
|
|
{0, 1, 1024*1024 + 1, "reached limit for padding memory slice: 1048578", 0}, // Error
|
|
|
|
{10, 0, 1024*1024 + 100, "reached limit for padding memory slice: 1048666", 0}, // Error
|
2023-03-28 09:46:53 +00:00
|
|
|
|
|
|
|
} {
|
|
|
|
mem := vm.NewMemory()
|
|
|
|
mem.Resize(uint64(tc.memsize))
|
|
|
|
cpy, err := GetMemoryCopyPadded(mem, tc.offset, tc.size)
|
|
|
|
if want := tc.wantErr; want != "" {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("test %d: want '%v' have no error", i, want)
|
|
|
|
}
|
|
|
|
if have := err.Error(); want != have {
|
|
|
|
t.Fatalf("test %d: want '%v' have '%v'", i, want, have)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("test %d: unexpected error: %v", i, err)
|
|
|
|
}
|
|
|
|
if want, have := tc.wantSize, len(cpy); have != want {
|
|
|
|
t.Fatalf("test %d: want %v have %v", i, want, have)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|