Skip to content

Commit

Permalink
eth,cmd: set tracer's backend after eth initalized
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa committed Aug 22, 2024
1 parent 8d18af5 commit 0625e8b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
if ctx.IsSet(VMTraceJsonConfigFlag.Name) {
config = json.RawMessage(ctx.String(VMTraceJsonConfigFlag.Name))
}
t, err := tracers.LiveDirectory.New(name, config)
t, _, err := tracers.LiveDirectory.New(name, config)
if err != nil {
Fatalf("Failed to create tracer %q: %v", name, err)
}
Expand Down
18 changes: 18 additions & 0 deletions core/tracing/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package tracing

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -67,6 +69,16 @@ type BlockEvent struct {
Safe *types.Header
}

// Backend interface provides the common API services (that are provided by
// both full and light clients) with access to necessary functions.
type Backend interface {
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
GetTransaction(ctx context.Context, txHash common.Hash) (bool, *types.Transaction, common.Hash, uint64, uint64, error)
}

type (
/*
- VM events -
Expand Down Expand Up @@ -170,6 +182,7 @@ type (
)

type Hooks struct {
Backend Backend
// VM events
OnTxStart TxStartHook
OnTxEnd TxEndHook
Expand All @@ -195,6 +208,11 @@ type Hooks struct {
OnLog LogHook
}

// SetBackend sets a backend to the hooks, use this backend to read chain data.
func (h *Hooks) SetBackend(backend Backend) {
h.Backend = backend
}

// BalanceChangeReason is used to indicate the reason for a balance change, useful
// for tracing and reporting.
type BalanceChangeReason byte
Expand Down
16 changes: 14 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state/pruner"
"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/txpool/blobpool"
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
Expand Down Expand Up @@ -196,17 +197,20 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
StateHistory: config.StateHistory,
StateScheme: scheme,
}
liveTracer *tracing.Hooks
liveAPIs []rpc.API
)
if config.VMTrace != "" {
var traceConfig json.RawMessage
if config.VMTraceJsonConfig != "" {
traceConfig = json.RawMessage(config.VMTraceJsonConfig)
}
t, err := tracers.LiveDirectory.New(config.VMTrace, traceConfig)
var err error
liveTracer, liveAPIs, err = tracers.LiveDirectory.New(config.VMTrace, traceConfig)
if err != nil {
return nil, fmt.Errorf("failed to create tracer %s: %v", config.VMTrace, err)
}
vmConfig.Tracer = t
vmConfig.Tracer = liveTracer
}
// Override the chain config with provided settings.
var overrides core.ChainOverrides
Expand Down Expand Up @@ -273,6 +277,14 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
stack.RegisterProtocols(eth.Protocols())
stack.RegisterLifecycle(eth)

// Set live tracer's backend and register the live tracer APIs
if liveTracer != nil {
liveTracer.SetBackend(eth.APIBackend)
if liveAPIs != nil {
stack.RegisterAPIs(liveAPIs)
}
}

// Successful startup; push a marker and check previous unclean shutdowns.
eth.shutdownTracker.MarkStartup()

Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/internal/tracetest/supply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")

// Load supply tracer
tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)))
tracer, _, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)))
if err != nil {
return nil, nil, fmt.Errorf("failed to create call tracer: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions eth/tracers/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"errors"

"github.com/ethereum/go-ethereum/core/tracing"
"github.com/ethereum/go-ethereum/rpc"
)

type ctorFunc func(config json.RawMessage) (*tracing.Hooks, error)
type ctorFunc func(config json.RawMessage) (*tracing.Hooks, []rpc.API, error)

// LiveDirectory is the collection of tracers which can be used
// during normal block import operations.
Expand All @@ -23,9 +24,9 @@ func (d *liveDirectory) Register(name string, f ctorFunc) {
}

// New instantiates a tracer by name.
func (d *liveDirectory) New(name string, config json.RawMessage) (*tracing.Hooks, error) {
func (d *liveDirectory) New(name string, config json.RawMessage) (*tracing.Hooks, []rpc.API, error) {
if f, ok := d.elems[name]; ok {
return f(config)
}
return nil, errors.New("not found")
return nil, nil, errors.New("not found")
}
5 changes: 3 additions & 2 deletions eth/tracers/live/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)

func init() {
Expand All @@ -21,7 +22,7 @@ func init() {
// as soon as we have a real live tracer.
type noop struct{}

func newNoopTracer(_ json.RawMessage) (*tracing.Hooks, error) {
func newNoopTracer(_ json.RawMessage) (*tracing.Hooks, []rpc.API, error) {
t := &noop{}
return &tracing.Hooks{
OnTxStart: t.OnTxStart,
Expand All @@ -41,7 +42,7 @@ func newNoopTracer(_ json.RawMessage) (*tracing.Hooks, error) {
OnCodeChange: t.OnCodeChange,
OnStorageChange: t.OnStorageChange,
OnLog: t.OnLog,
}, nil
}, nil, nil
}

func (t *noop) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) {
Expand Down
9 changes: 5 additions & 4 deletions eth/tracers/live/supply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/eth/tracers"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rpc"
"gopkg.in/natefinch/lumberjack.v2"
)

Expand Down Expand Up @@ -74,15 +75,15 @@ type supplyTracerConfig struct {
MaxSize int `json:"maxSize"` // MaxSize is the maximum size in megabytes of the tracer log file before it gets rotated. It defaults to 100 megabytes.
}

func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
func newSupply(cfg json.RawMessage) (*tracing.Hooks, []rpc.API, error) {
var config supplyTracerConfig
if cfg != nil {
if err := json.Unmarshal(cfg, &config); err != nil {
return nil, fmt.Errorf("failed to parse config: %v", err)
return nil, nil, fmt.Errorf("failed to parse config: %v", err)
}
}
if config.Path == "" {
return nil, errors.New("supply tracer output path is required")
return nil, nil, errors.New("supply tracer output path is required")
}

// Store traces in a rotating file
Expand All @@ -106,7 +107,7 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
OnEnter: t.OnEnter,
OnExit: t.OnExit,
OnClose: t.OnClose,
}, nil
}, nil, nil
}

func newSupplyInfo() supplyInfo {
Expand Down

0 comments on commit 0625e8b

Please sign in to comment.