core: support finality event on exec

This commit is contained in:
Péter Szilágyi 2024-10-16 22:58:21 +03:00
parent f6cbab924b
commit 062209fff8
5 changed files with 20 additions and 0 deletions

View file

@ -613,6 +613,7 @@ func (bc *BlockChain) SetFinalized(header *types.Header) {
if header != nil { if header != nil {
rawdb.WriteFinalizedBlockHash(bc.db, header.Hash()) rawdb.WriteFinalizedBlockHash(bc.db, header.Hash())
headFinalizedBlockGauge.Update(int64(header.Number.Uint64())) headFinalizedBlockGauge.Update(int64(header.Number.Uint64()))
exex.TriggerFinalHook(header)
} else { } else {
rawdb.WriteFinalizedBlockHash(bc.db, common.Hash{}) rawdb.WriteFinalizedBlockHash(bc.db, common.Hash{})
headFinalizedBlockGauge.Update(0) headFinalizedBlockGauge.Update(0)

View file

@ -48,4 +48,5 @@ type PluginV1 struct {
OnClose CloseHook // Called when the chain gets torn down within Geth OnClose CloseHook // Called when the chain gets torn down within Geth
OnHead HeadHook // Called when the chain head block is updated in Geth OnHead HeadHook // Called when the chain head block is updated in Geth
OnReorg ReorgHook // Called wnen the chain reorgs to a sidechain within Geth OnReorg ReorgHook // Called wnen the chain reorgs to a sidechain within Geth
OnFinal FinalHook // Called when the chain finalizes a block within Geth
} }

View file

@ -39,6 +39,7 @@ type registry interface {
TriggerCloseHook() TriggerCloseHook()
TriggerHeadHook(head *types.Header) TriggerHeadHook(head *types.Header)
TriggerReorgHook(headers []*types.Header, revert bool) TriggerReorgHook(headers []*types.Header, revert bool)
TriggerFinalHook(header *types.Header)
} }
// Plugins returns a list of all registered plugins to generate CLI flags. // Plugins returns a list of all registered plugins to generate CLI flags.
@ -70,3 +71,8 @@ func TriggerHeadHook(head *types.Header) {
func TriggerReorgHook(headers []*types.Header, revert bool) { func TriggerReorgHook(headers []*types.Header, revert bool) {
globalRegistry.TriggerReorgHook(headers, revert) globalRegistry.TriggerReorgHook(headers, revert)
} }
// TriggerFinalHook triggers the OnFinal hook in exex plugins.
func TriggerFinalHook(header *types.Header) {
globalRegistry.TriggerFinalHook(header)
}

View file

@ -51,3 +51,6 @@ type HeadHook = func(head *types.Header)
// to pass in both the reverted and applied headers at the same time, but that // to pass in both the reverted and applied headers at the same time, but that
// would require chain accessorts to support sidechains, which complicate APIs. // would require chain accessorts to support sidechains, which complicate APIs.
type ReorgHook = func(headers []*types.Header, revert bool) type ReorgHook = func(headers []*types.Header, revert bool)
// FinalHook is called when the chain finalizes a past block.
type FinalHook = func(header *types.Header)

View file

@ -87,3 +87,12 @@ func (reg *registry) TriggerReorgHook(headers []*types.Header, revert bool) {
} }
} }
} }
// TriggerFinalHook triggers the OnFinal hook in exex plugins.
func (reg *registry) TriggerFinalHook(header *types.Header) {
for _, plugin := range globalRegistry.pluginsV1 {
if plugin.OnFinal != nil {
plugin.OnFinal(header)
}
}
}