add OnTerminate in Hooks which triggered on blockchain Stop

This commit is contained in:
Chris Ziogas 2024-03-30 19:18:05 +02:00
parent 4708a19319
commit 00ef42b541
No known key found for this signature in database
GPG key ID: 2329CF479E36E2DA
3 changed files with 20 additions and 6 deletions

View file

@ -1171,6 +1171,10 @@ func (bc *BlockChain) Stop() {
if err := bc.triedb.Close(); err != nil { if err := bc.triedb.Close(); err != nil {
log.Error("Failed to close trie database", "err", err) log.Error("Failed to close trie database", "err", err)
} }
// Call OnTerminate hook on Tracers if it is set.
if bc.vmConfig.Tracer != nil && bc.vmConfig.Tracer.OnTerminate != nil {
bc.vmConfig.Tracer.OnTerminate()
}
log.Info("Blockchain stopped") log.Info("Blockchain stopped")
} }

View file

@ -140,6 +140,9 @@ type (
// LogHook is called when a log is emitted. // LogHook is called when a log is emitted.
LogHook = func(log *types.Log) LogHook = func(log *types.Log)
// TerminateHook is called when the tracer is terminated.
TerminateHook = func()
) )
type Hooks struct { type Hooks struct {
@ -163,6 +166,7 @@ type Hooks struct {
OnCodeChange CodeChangeHook OnCodeChange CodeChangeHook
OnStorageChange StorageChangeHook OnStorageChange StorageChangeHook
OnLog LogHook OnLog LogHook
OnTerminate TerminateHook
} }
// BalanceChangeReason is used to indicate the reason for a balance change, useful // BalanceChangeReason is used to indicate the reason for a balance change, useful

View file

@ -46,6 +46,7 @@ type supplyTxCallstack struct {
type supply struct { type supply struct {
delta supplyInfo delta supplyInfo
txCallstack []supplyTxCallstack // Callstack for current transaction txCallstack []supplyTxCallstack // Callstack for current transaction
loggerOutputFile *lumberjack.Logger
logger *log.Logger logger *log.Logger
} }
@ -70,7 +71,6 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
loggerOutputFile := &lumberjack.Logger{ loggerOutputFile := &lumberjack.Logger{
Filename: filepath.Join(config.Path, "supply.jsonl"), Filename: filepath.Join(config.Path, "supply.jsonl"),
} }
defer loggerOutputFile.Close()
if config.MaxSize > 0 { if config.MaxSize > 0 {
loggerOutputFile.MaxSize = config.MaxSize loggerOutputFile.MaxSize = config.MaxSize
@ -82,6 +82,7 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
t := &supply{ t := &supply{
delta: supplyInfo, delta: supplyInfo,
loggerOutputFile: loggerOutputFile,
logger: logger, logger: logger,
} }
return &tracing.Hooks{ return &tracing.Hooks{
@ -92,6 +93,7 @@ func newSupply(cfg json.RawMessage) (*tracing.Hooks, error) {
OnBalanceChange: t.OnBalanceChange, OnBalanceChange: t.OnBalanceChange,
OnEnter: t.OnEnter, OnEnter: t.OnEnter,
OnExit: t.OnExit, OnExit: t.OnExit,
OnTerminate: t.OnTerminate,
}, nil }, nil
} }
@ -238,3 +240,7 @@ func (s *supply) OnExit(depth int, output []byte, gasUsed uint64, err error, rev
} }
s.txCallstack[size-1].calls = append(s.txCallstack[size-1].calls, call) s.txCallstack[size-1].calls = append(s.txCallstack[size-1].calls, call)
} }
func (s *supply) OnTerminate() {
s.loggerOutputFile.Close()
}