Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: struct-based live chain tracing #29189

Merged
merged 156 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
156 commits
Select commit Hold shift + click to select a range
642a374
Initial support for extended tracer
s1na Jun 15, 2023
e25065b
capture keccak preimage
s1na Jun 15, 2023
f67ac09
add hooks to state object & genesis
s1na Jun 20, 2023
84d6432
add log and newAccount hooks
s1na Jun 22, 2023
ee791b2
add gas consumption hook
s1na Jun 23, 2023
0d25fbc
pass logger to BC through vmConfig
s1na Jun 26, 2023
8db1078
fix genesis logging
s1na Jun 26, 2023
788851b
disable tracing for miner processed txes
s1na Jun 26, 2023
057bbda
add comments
s1na Jun 26, 2023
a7de17e
only emit event for new accounts
s1na Jun 26, 2023
3787816
Upgrade TxStart and TxEnd hooks
s1na Jun 28, 2023
3975c7c
minor
s1na Jun 28, 2023
619ac29
provide StateLogger for short-lived tracers
s1na Jun 28, 2023
54171ee
pass env in TxStart
s1na Jun 28, 2023
3cabef4
capture block end errors
s1na Jun 30, 2023
f1bd415
rm extra CaptureTxEnd
s1na Jul 10, 2023
d799c68
nicer printer output
s1na Jul 10, 2023
add825e
fix BlockEnd in case of err
s1na Jul 10, 2023
0aeec7f
mv td to OnBlockStart
s1na Jul 12, 2023
b692a68
add final & safe headers to BlockStart
s1na Jul 12, 2023
2a59d24
add genesis alloc to hook
s1na Jul 12, 2023
4370527
fix prefetcher double emit
s1na Jul 24, 2023
5466fa3
Capture balance reason (#12)
maoueh Jul 26, 2023
2faf3db
emit err on tx validation failure
s1na Jul 26, 2023
b2b32e6
fix merge conflict
s1na Jul 27, 2023
09ee197
fix merge conflict
s1na Jul 27, 2023
f9f377d
capture call validation errors
s1na Jul 27, 2023
b7ff573
minor refactor
s1na Jul 28, 2023
00a19d3
Fixed tests compilation (#13)
maoueh Aug 2, 2023
216a4b0
Added writing of `genesis.Alloc` on bootstrap (#15)
maoueh Aug 30, 2023
659043a
pass tracer name via cli
s1na Aug 30, 2023
9c999c3
Use noopTracer as base for loggers
s1na Aug 31, 2023
0be6e22
add comment to statedb logger
s1na Aug 31, 2023
abd8807
use defer for OnBlockEnd
s1na Aug 31, 2023
f7ca31e
fix import cycle in blockchain test
s1na Sep 4, 2023
651c438
fix runtime tests
s1na Sep 4, 2023
74c1f30
Fixed `BlockchainLogger` tracer not being correctly set up up to `Blo…
maoueh Sep 12, 2023
ff3c15f
Full OnGasConsumed loop and added `GasChangeReason` (#16)
maoueh Sep 12, 2023
6ee4fb8
resolve merge conflicts
s1na Sep 13, 2023
1914bc6
error code for VM failures (#18)
maoueh Oct 27, 2023
429dcc9
fix merge conflict
s1na Nov 2, 2023
e65f14a
update state processor
s1na Nov 2, 2023
e4399a6
Use applyTransaction in tracer instead of applyMessage
s1na Nov 3, 2023
14d603b
add comment re newAccount precompile
s1na Nov 3, 2023
023ade6
instrument when inserting known block
s1na Nov 3, 2023
ee58cc7
statedb: precompile check before onNewAccount
s1na Nov 7, 2023
dc1175e
rm unnecessary line
s1na Nov 7, 2023
da8b362
minor
s1na Nov 7, 2023
0d268b7
fix TxStart for traceCall
s1na Nov 8, 2023
cc6b68e
Fix prestate create issue, add test
s1na Nov 11, 2023
6b8d216
fix setBalance reasons
s1na Nov 13, 2023
b80aa1e
fix double-allocation
s1na Nov 24, 2023
1d13645
move balance reason to metadata file
s1na Nov 24, 2023
856c079
move traced block processing to own func
s1na Nov 24, 2023
9c00b12
fix merge conflict
s1na Nov 24, 2023
379b8d3
fix atomic followupInterrupt
s1na Nov 24, 2023
56600e9
fix VMError.Unwrap
s1na Nov 24, 2023
8a1f67c
forgot prestate testcase
s1na Nov 24, 2023
5bc3f62
move makeTest to own file
s1na Nov 24, 2023
cd8c4c5
force load live tracer set
s1na Nov 24, 2023
8610f8f
add hooks for beacon block root processing
s1na Nov 24, 2023
00ee953
rm comment
s1na Nov 28, 2023
72410c3
Fixed `BlockchainLogger.OnBlockEnd` called too much times (#19)
maoueh Nov 30, 2023
c341c79
Merge remote-tracking branch 's1na/extended-tracer' into extended-tracer
s1na Dec 4, 2023
5190cfc
rm extra galloc capturing
s1na Dec 5, 2023
9e5c96f
add chainConfig to block events
s1na Dec 5, 2023
f862531
rm chainConfig from OnGenesisBlock
s1na Dec 5, 2023
c5e407b
resolve merge conflicts
s1na Dec 5, 2023
9360cab
assign values for balance reasons
s1na Dec 7, 2023
0967291
Track burnt and selfdestruct withdraw bal changes
s1na Dec 7, 2023
ddd1b0a
fix gas purchase
s1na Dec 11, 2023
0484068
fix lint issues
s1na Dec 11, 2023
5e82cbe
nil check
s1na Dec 11, 2023
0d254e4
fix test
s1na Dec 11, 2023
a52c80d
Fix test case
s1na Dec 12, 2023
89d79d6
fix t8n tracer nil check
s1na Dec 12, 2023
54f4bc7
fix balance check
s1na Dec 13, 2023
64e35e9
revert buyGas refactor
s1na Dec 13, 2023
8b3180d
revert gas refund
s1na Dec 13, 2023
2d14bb5
rename and document balance change reasons
s1na Dec 13, 2023
68bd6c6
Removed the `OnGenesisBlock` in `genesis.Commit` (#20)
maoueh Jan 11, 2024
f241a76
fix comment'
s1na Jan 12, 2024
580f025
has call reverted to snapshot
s1na Jan 12, 2024
aa0db68
resolve merge conflict
s1na Jan 12, 2024
f21efbc
fix traceWriter
s1na Jan 12, 2024
c331930
add test case for frontier create out of storage
s1na Jan 15, 2024
626fee4
fix withdraw balance change name
s1na Jan 15, 2024
d07264a
no balance change reason on 0 balance selfdestruct
s1na Jan 16, 2024
4c1d675
resolve merge conflict
s1na Jan 29, 2024
9a6b9a2
re-add simulated backend removed accidentally
s1na Jan 30, 2024
ae522d6
rm empty line
s1na Jan 30, 2024
56862cf
emit onNewAccount when reseting account
s1na Jan 30, 2024
68342ef
improve processBlock
s1na Jan 31, 2024
3078dfe
minor fix
s1na Jan 31, 2024
6c44a59
fix self destruct burn condition
s1na Feb 5, 2024
188cd41
remove precompile check for newAccount
s1na Feb 5, 2024
f3c0a89
add OnBlockchainInit for chainConfig
s1na Feb 6, 2024
95b5029
replace printer with noop live tracer
s1na Feb 6, 2024
2cc0954
rename
s1na Feb 6, 2024
57cd0c3
rm OnNewAccount
s1na Feb 8, 2024
056f229
indicate known block
s1na Feb 8, 2024
27f662d
fix merge conflict
s1na Feb 8, 2024
706c869
fix remaining conflict
s1na Feb 9, 2024
03206be
fix blockchain -> logger circular import
s1na Feb 9, 2024
08cb623
live tracer err -> warn
s1na Feb 9, 2024
cf6a315
fix mdLogger
s1na Feb 9, 2024
4cd7cb3
resolve merge conflict
s1na Feb 14, 2024
674a38e
add config for tracers
s1na Feb 14, 2024
0db7a19
minor fix
s1na Feb 14, 2024
35291e6
resolve merge conflict
s1na Feb 15, 2024
3ba6b92
remove onBeaconBlockRoot events
s1na Feb 15, 2024
5f0a543
refactor onBlockStart params, new skip method
s1na Feb 15, 2024
5009590
use struct for tracing events
s1na Feb 16, 2024
490efc5
resolve merge conflict
s1na Feb 16, 2024
8997f51
Merge branch 'extended-tracer' into extended-tracer-struct
s1na Feb 16, 2024
fc35780
full struct-based tracing infra
s1na Feb 21, 2024
f434c8d
Add canceler interface
s1na Feb 21, 2024
22234e9
fix memory copy util func
s1na Feb 27, 2024
60c29ac
fix goja scope object wrappers
s1na Feb 27, 2024
a1877ab
fixes, rm t8ntool tracewriter
s1na Feb 27, 2024
8cc747f
moaar fixes
s1na Feb 28, 2024
d65bad2
renaming GetLogger
s1na Feb 28, 2024
7d8db9b
move logger interface to core/tracing
s1na Feb 28, 2024
a2829c6
rename LiveLogger to hooks
s1na Feb 28, 2024
507c072
rename GetLogger to Hooks
s1na Feb 28, 2024
f358e57
rename scopeContext to opContext
s1na Feb 29, 2024
021d6a4
assign const values for gas change reasons
s1na Feb 29, 2024
ad7b9e8
a type for each hook
s1na Feb 29, 2024
d10d4b7
rm old logger file
s1na Feb 29, 2024
923c180
rename Capture hooks to On
s1na Feb 29, 2024
37ce159
rm OnStart and onEnd
s1na Mar 6, 2024
8b3cdda
rename tracer methods to On pattern
s1na Mar 6, 2024
73595f0
rm embedding of noop tracer
s1na Mar 6, 2024
1d01019
rm OnKeccakPreimage
s1na Mar 6, 2024
1d2af89
fix eth config
s1na Mar 6, 2024
935ff70
replace opcode with byte
s1na Mar 7, 2024
5bdbf07
add fn refundGas
s1na Mar 7, 2024
eeb0a46
rm canceler
s1na Mar 7, 2024
73ca96b
resolve merge conflict
s1na Mar 12, 2024
65bdbc1
fix lint issue
s1na Mar 13, 2024
0b5975f
fix merge conflict
s1na Mar 13, 2024
cff9fba
rm extra param
s1na Mar 13, 2024
817e261
Call `OnBlockchainInit` before `OnGenesisBlock` (#22)
maoueh Mar 14, 2024
17f95b9
fix empty tracer config
s1na Mar 14, 2024
4636dd3
cmd/evm: fix tracing
holiman Mar 18, 2024
25051c9
cmd/evm: add testcase for js-tracer
holiman Mar 18, 2024
e320d1e
core/vm: documentation + minor refactoring
holiman Mar 18, 2024
4f87f17
eth/tracers, cmd/evm: unexport JSONlogger
holiman Mar 18, 2024
d21e92c
refactor args to msg and tx
s1na Mar 19, 2024
3f18501
fix merge conflict
s1na Mar 20, 2024
f5ad154
minor improvements
s1na Mar 20, 2024
3e455ab
fix nil checks in runtime
s1na Mar 20, 2024
bf884eb
move live directory to main directory module
s1na Mar 20, 2024
b91355b
export toTransaction
s1na Mar 22, 2024
1c512ad
move utils and noopT out of directory pkg
s1na Mar 22, 2024
8fc8eaa
drop directory pkg
s1na Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/evm/blockrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
"github.com/ethereum/go-ethereum/tests"
"github.com/urfave/cli/v2"
Expand All @@ -51,7 +51,7 @@ func blockTestCmd(ctx *cli.Context) error {
return errors.New("path-to-test argument required")
}

var tracer vm.EVMLogger
var tracer *tracing.Hooks
// Configure the EVM logger
if ctx.Bool(MachineFlag.Name) {
tracer = logger.NewJSONLogger(&logger.Config{
Expand Down
50 changes: 43 additions & 7 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package t8ntool

import (
"encoding/json"
"fmt"
"io"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -28,9 +30,11 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -119,7 +123,7 @@ type rejectedTx struct {
// Apply applies a set of transactions to a pre-state
func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
txIt txIterator, miningReward int64,
getTracerFn func(txIndex int, txHash common.Hash) (vm.EVMLogger, error)) (*state.StateDB, *ExecutionResult, []byte, error) {
getTracerFn func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error)) (*state.StateDB, *ExecutionResult, []byte, error) {
// Capture errors for BLOCKHASH operation, if we haven't been supplied the
// required blockhashes
var hashError error
Expand Down Expand Up @@ -222,11 +226,13 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
continue
}
}
tracer, err := getTracerFn(txIndex, tx.Hash())
tracer, traceOutput, err := getTracerFn(txIndex, tx.Hash())
if err != nil {
return nil, nil, nil, err
}
vmConfig.Tracer = tracer
if tracer != nil {
vmConfig.Tracer = tracer.Hooks
}
statedb.SetTxContext(tx.Hash(), txIndex)

var (
Expand All @@ -236,13 +242,24 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
)
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)

if tracer != nil && tracer.OnTxStart != nil {
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
}
// (ret []byte, usedGas uint64, failed bool, err error)
msgResult, err := core.ApplyMessage(evm, msg, gaspool)
if err != nil {
statedb.RevertToSnapshot(snapshot)
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
gaspool.SetGas(prevGas)
if tracer != nil {
if tracer.OnTxEnd != nil {
tracer.OnTxEnd(nil, err)
}
if err := writeTraceResult(tracer, traceOutput); err != nil {
log.Warn("Error writing tracer output", "err", err)
}
}
continue
}
includedTxs = append(includedTxs, tx)
Expand Down Expand Up @@ -285,6 +302,12 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
//receipt.BlockNumber
receipt.TransactionIndex = uint(txIndex)
receipts = append(receipts, receipt)
if tracer != nil {
if tracer.Hooks.OnTxEnd != nil {
tracer.Hooks.OnTxEnd(receipt, nil)
}
writeTraceResult(tracer, traceOutput)
}
}

txIndex++
Expand All @@ -310,15 +333,15 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
reward.Sub(reward, new(big.Int).SetUint64(ommer.Delta))
reward.Mul(reward, blockReward)
reward.Div(reward, big.NewInt(8))
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward))
statedb.AddBalance(ommer.Address, uint256.MustFromBig(reward), tracing.BalanceIncreaseRewardMineUncle)
}
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward))
statedb.AddBalance(pre.Env.Coinbase, uint256.MustFromBig(minerReward), tracing.BalanceIncreaseRewardMineBlock)
}
// Apply withdrawals
for _, w := range pre.Env.Withdrawals {
// Amount is in gwei, turn into wei
amount := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), big.NewInt(params.GWei))
statedb.AddBalance(w.Address, uint256.MustFromBig(amount))
statedb.AddBalance(w.Address, uint256.MustFromBig(amount), tracing.BalanceIncreaseWithdrawal)
}
// Commit block
root, err := statedb.Commit(vmContext.BlockNumber.Uint64(), chainConfig.IsEIP158(vmContext.BlockNumber))
Expand Down Expand Up @@ -361,7 +384,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
statedb.SetNonce(addr, a.Nonce)
statedb.SetBalance(addr, uint256.MustFromBig(a.Balance))
statedb.SetBalance(addr, uint256.MustFromBig(a.Balance), tracing.BalanceIncreaseGenesisBalance)
for k, v := range a.Storage {
statedb.SetState(addr, k, v)
}
Expand Down Expand Up @@ -398,3 +421,16 @@ func calcDifficulty(config *params.ChainConfig, number, currentTime, parentTime
}
return ethash.CalcDifficulty(config, currentTime, parent)
}

func writeTraceResult(tracer *tracers.Tracer, f io.WriteCloser) error {
defer f.Close()
result, err := tracer.GetResult()
if err != nil || result == nil {
return err
}
err = json.NewEncoder(f).Encode(result)
if err != nil {
return err
}
return nil
}
81 changes: 0 additions & 81 deletions cmd/evm/internal/t8ntool/tracewriter.go

This file was deleted.

24 changes: 16 additions & 8 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
Expand Down Expand Up @@ -80,7 +81,7 @@ type input struct {
}

func Transition(ctx *cli.Context) error {
var getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) { return nil, nil }
var getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) { return nil, nil, nil }

baseDir, err := createBasedir(ctx)
if err != nil {
Expand All @@ -95,28 +96,35 @@ func Transition(ctx *cli.Context) error {
EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name),
Debug: true,
}
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) {
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.jsonl", txIndex, txHash.String())))
if err != nil {
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
return nil, nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
}
return &traceWriter{logger.NewJSONLogger(logConfig, traceFile), traceFile}, nil
logger := logger.NewJSONLogger(logConfig, traceFile)
tracer := &tracers.Tracer{
Hooks: logger,
// jsonLogger streams out result to file.
GetResult: func() (json.RawMessage, error) { return nil, nil },
Stop: func(err error) {},
}
return tracer, traceFile, nil
}
} else if ctx.IsSet(TraceTracerFlag.Name) {
var config json.RawMessage
if ctx.IsSet(TraceTracerConfigFlag.Name) {
config = []byte(ctx.String(TraceTracerConfigFlag.Name))
}
getTracer = func(txIndex int, txHash common.Hash) (vm.EVMLogger, error) {
getTracer = func(txIndex int, txHash common.Hash) (*tracers.Tracer, io.WriteCloser, error) {
traceFile, err := os.Create(filepath.Join(baseDir, fmt.Sprintf("trace-%d-%v.json", txIndex, txHash.String())))
if err != nil {
return nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
return nil, nil, NewError(ErrorIO, fmt.Errorf("failed creating trace-file: %v", err))
}
tracer, err := tracers.DefaultDirectory.New(ctx.String(TraceTracerFlag.Name), nil, config)
if err != nil {
return nil, NewError(ErrorConfig, fmt.Errorf("failed instantiating tracer: %w", err))
return nil, nil, NewError(ErrorConfig, fmt.Errorf("failed instantiating tracer: %w", err))
}
return &traceWriter{tracer, traceFile}, nil
return tracer, traceFile, nil
}
}
// We need to load three things: alloc, env and transactions. May be either in
Expand Down
5 changes: 3 additions & 2 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/vm/runtime"
"github.com/ethereum/go-ethereum/eth/tracers/logger"
Expand Down Expand Up @@ -116,7 +117,7 @@ func runCmd(ctx *cli.Context) error {
}

var (
tracer vm.EVMLogger
tracer *tracing.Hooks
debugLogger *logger.StructLogger
statedb *state.StateDB
chainConfig *params.ChainConfig
Expand All @@ -130,7 +131,7 @@ func runCmd(ctx *cli.Context) error {
tracer = logger.NewJSONLogger(logconfig, os.Stdout)
} else if ctx.Bool(DebugFlag.Name) {
debugLogger = logger.NewStructLogger(logconfig)
tracer = debugLogger
tracer = debugLogger.Hooks()
} else {
debugLogger = logger.NewStructLogger(logconfig)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/evm/staterunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func stateTestCmd(ctx *cli.Context) error {
cfg.Tracer = logger.NewJSONLogger(config, os.Stderr)

case ctx.Bool(DebugFlag.Name):
cfg.Tracer = logger.NewStructLogger(config)
cfg.Tracer = logger.NewStructLogger(config).Hooks()
}
// Load the test content from the input file
if len(ctx.Args().First()) != 0 {
Expand Down
Loading
Loading