pass eth.Backend into live tracer

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2024-09-25 09:22:43 +08:00
parent 561c43d873
commit 9ae1444d48
6 changed files with 16 additions and 21 deletions

View file

@ -2192,7 +2192,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
if ctx.IsSet(VMTraceJsonConfigFlag.Name) { if ctx.IsSet(VMTraceJsonConfigFlag.Name) {
config = json.RawMessage(ctx.String(VMTraceJsonConfigFlag.Name)) config = json.RawMessage(ctx.String(VMTraceJsonConfigFlag.Name))
} }
t, err := tracers.LiveDirectory.New(name, config, stack) t, err := tracers.LiveDirectory.New(name, config, stack, nil)
if err != nil { if err != nil {
Fatalf("Failed to create tracer %q: %v", name, err) Fatalf("Failed to create tracer %q: %v", name, err)
} }

View file

@ -32,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/core/bloombits" "github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state/pruner" "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"
"github.com/ethereum/go-ethereum/core/txpool/blobpool" "github.com/ethereum/go-ethereum/core/txpool/blobpool"
"github.com/ethereum/go-ethereum/core/txpool/legacypool" "github.com/ethereum/go-ethereum/core/txpool/legacypool"
@ -181,6 +180,12 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion) rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
} }
} }
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil}
if eth.APIBackend.allowUnprotectedTxs {
log.Info("Unprotected transactions allowed")
}
var ( var (
vmConfig = vm.Config{ vmConfig = vm.Config{
EnablePreimageRecording: config.EnablePreimageRecording, EnablePreimageRecording: config.EnablePreimageRecording,
@ -196,19 +201,17 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
StateHistory: config.StateHistory, StateHistory: config.StateHistory,
StateScheme: scheme, StateScheme: scheme,
} }
liveTracer *tracing.Hooks
) )
if config.VMTrace != "" { if config.VMTrace != "" {
var traceConfig json.RawMessage var traceConfig json.RawMessage
if config.VMTraceJsonConfig != "" { if config.VMTraceJsonConfig != "" {
traceConfig = json.RawMessage(config.VMTraceJsonConfig) traceConfig = json.RawMessage(config.VMTraceJsonConfig)
} }
var err error t, err := tracers.LiveDirectory.New(config.VMTrace, traceConfig, stack, eth.APIBackend)
liveTracer, err = tracers.LiveDirectory.New(config.VMTrace, traceConfig, stack)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create tracer %s: %v", config.VMTrace, err) return nil, fmt.Errorf("failed to create tracer %s: %v", config.VMTrace, err)
} }
vmConfig.Tracer = liveTracer vmConfig.Tracer = t
} }
// Override the chain config with provided settings. // Override the chain config with provided settings.
var overrides core.ChainOverrides var overrides core.ChainOverrides
@ -257,10 +260,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
eth.miner = miner.New(eth, config.Miner, eth.engine) eth.miner = miner.New(eth, config.Miner, eth.engine)
eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData)) eth.miner.SetExtra(makeExtraData(config.Miner.ExtraData))
eth.APIBackend = &EthAPIBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, eth, nil} // Start the gas price oracle after blockchain is fully loaded
if eth.APIBackend.allowUnprotectedTxs {
log.Info("Unprotected transactions allowed")
}
eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, config.GPO, config.Miner.GasPrice) eth.APIBackend.gpo = gasprice.NewOracle(eth.APIBackend, config.GPO, config.Miner.GasPrice)
// Start the RPC service // Start the RPC service
@ -271,11 +271,6 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
stack.RegisterProtocols(eth.Protocols()) stack.RegisterProtocols(eth.Protocols())
stack.RegisterLifecycle(eth) stack.RegisterLifecycle(eth)
// Set live tracer's backend and register the live tracer APIs
if liveTracer != nil {
liveTracer.SetBackend(eth.APIBackend)
}
// Successful startup; push a marker and check previous unclean shutdowns. // Successful startup; push a marker and check previous unclean shutdowns.
eth.shutdownTracker.MarkStartup() eth.shutdownTracker.MarkStartup()

View file

@ -552,7 +552,7 @@ func testSupplyTracer(t *testing.T, genesis *core.Genesis, gen func(*core.BlockG
traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl") traceOutputFilename := path.Join(traceOutputPath, "supply.jsonl")
// Load supply tracer // Load supply tracer
tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)), nil) tracer, err := tracers.LiveDirectory.New("supply", json.RawMessage(fmt.Sprintf(`{"path":"%s"}`, traceOutputPath)), nil, nil)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed to create call tracer: %v", err) return nil, nil, fmt.Errorf("failed to create call tracer: %v", err)
} }

View file

@ -13,7 +13,7 @@ type LiveApiRegister interface {
RegisterAPIs(apis []rpc.API) RegisterAPIs(apis []rpc.API)
} }
type ctorFunc func(config json.RawMessage, stack LiveApiRegister) (*tracing.Hooks, error) type ctorFunc func(config json.RawMessage, stack LiveApiRegister, backend tracing.Backend) (*tracing.Hooks, error)
// LiveDirectory is the collection of tracers which can be used // LiveDirectory is the collection of tracers which can be used
// during normal block import operations. // during normal block import operations.
@ -29,9 +29,9 @@ func (d *liveDirectory) Register(name string, f ctorFunc) {
} }
// New instantiates a tracer by name. // New instantiates a tracer by name.
func (d *liveDirectory) New(name string, config json.RawMessage, stack LiveApiRegister) (*tracing.Hooks, error) { func (d *liveDirectory) New(name string, config json.RawMessage, stack LiveApiRegister, backend tracing.Backend) (*tracing.Hooks, error) {
if f, ok := d.elems[name]; ok { if f, ok := d.elems[name]; ok {
return f(config, stack) return f(config, stack, backend)
} }
return nil, errors.New("not found") return nil, errors.New("not found")
} }

View file

@ -21,7 +21,7 @@ func init() {
// as soon as we have a real live tracer. // as soon as we have a real live tracer.
type noop struct{} type noop struct{}
func newNoopTracer(_ json.RawMessage, _ tracers.LiveApiRegister) (*tracing.Hooks, error) { func newNoopTracer(_ json.RawMessage, _ tracers.LiveApiRegister, _ tracing.Backend) (*tracing.Hooks, error) {
t := &noop{} t := &noop{}
return &tracing.Hooks{ return &tracing.Hooks{
OnTxStart: t.OnTxStart, OnTxStart: t.OnTxStart,

View file

@ -74,7 +74,7 @@ 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. 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, _ tracers.LiveApiRegister) (*tracing.Hooks, error) { func newSupply(cfg json.RawMessage, _ tracers.LiveApiRegister, _ tracing.Backend) (*tracing.Hooks, error) {
var config supplyTracerConfig var config supplyTracerConfig
if cfg != nil { if cfg != nil {
if err := json.Unmarshal(cfg, &config); err != nil { if err := json.Unmarshal(cfg, &config); err != nil {