From 6176caea8858e425cb194b3dfb3c92af6c590a33 Mon Sep 17 00:00:00 2001 From: Chris Ziogas Date: Wed, 24 Apr 2024 08:54:59 +0300 Subject: [PATCH] core/tracing: Add OnClose Trace Hook (#29629) The OnClose trace hook is being triggered on blockchain Stop, so as tracers can release any resources. --- core/blockchain.go | 4 ++++ core/tracing/hooks.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 8c97740752bc..b45cd92e523d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1153,6 +1153,10 @@ func (bc *BlockChain) Stop() { } } } + // Allow tracers to clean-up and release resources. + if bc.logger != nil && bc.logger.OnClose != nil { + bc.logger.OnClose() + } // Close the trie database, release all the held resources as the last step. if err := bc.triedb.Close(); err != nil { log.Error("Failed to close trie database", "err", err) diff --git a/core/tracing/hooks.go b/core/tracing/hooks.go index 48cb4d20275c..9ca6ee39fbe7 100644 --- a/core/tracing/hooks.go +++ b/core/tracing/hooks.go @@ -107,6 +107,9 @@ type ( // BlockchainInitHook is called when the blockchain is initialized. BlockchainInitHook = func(chainConfig *params.ChainConfig) + // CloseHook is called when the blockchain closes. + CloseHook = func() + // BlockStartHook is called before executing `block`. // `td` is the total difficulty prior to `block`. BlockStartHook = func(event BlockEvent) @@ -153,6 +156,7 @@ type Hooks struct { OnGasChange GasChangeHook // Chain events OnBlockchainInit BlockchainInitHook + OnClose CloseHook OnBlockStart BlockStartHook OnBlockEnd BlockEndHook OnSkippedBlock SkippedBlockHook