From 875e5061489a1c514985b482a53d455d5db2bea2 Mon Sep 17 00:00:00 2001 From: Austin Roberts Date: Mon, 12 Jul 2021 11:38:52 -0500 Subject: [PATCH] Invoke plugins when blocks are added as new heads or sidechain blocks --- core/blockchain.go | 2 ++ core/plugin_hooks.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/core/blockchain.go b/core/blockchain.go index 18e126657..79e2da4aa 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1566,6 +1566,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.futureBlocks.Remove(block.Hash()) if status == CanonStatTy { + pluginNewHead(block, block.Hash(), logs) bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs}) if len(logs) > 0 { bc.logsFeed.Send(logs) @@ -1579,6 +1580,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. bc.chainHeadFeed.Send(ChainHeadEvent{Block: block}) } } else { + pluginNewSideBlock(block, block.Hash(), logs) bc.chainSideFeed.Send(ChainSideEvent{Block: block}) } return status, nil diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index 7cc480772..4c20f1320 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -2,6 +2,7 @@ package core import ( "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/plugins" "github.com/ethereum/go-ethereum/log" ) @@ -96,3 +97,42 @@ func pluginPostProcessBlock(block *types.Block) { } PluginPostProcessBlock(plugins.DefaultPluginLoader, block) } + + +func PluginNewHead(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) { + fnList := pl.Lookup("NewHead", func(item interface{}) bool { + _, ok := item.(func(*types.Block, common.Hash, []*types.Log)) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func(*types.Block, common.Hash, []*types.Log)); ok { + fn(block, hash, logs) + } + } +} +func pluginNewHead(block *types.Block, hash common.Hash, logs []*types.Log) { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting NewHead, but default PluginLoader has not been initialized") + return + } + PluginNewHead(plugins.DefaultPluginLoader, block, hash, logs) +} + +func PluginNewSideBlock(pl *plugins.PluginLoader, block *types.Block, hash common.Hash, logs []*types.Log) { + fnList := pl.Lookup("NewSideBlock", func(item interface{}) bool { + _, ok := item.(func(*types.Block, common.Hash, []*types.Log)) + return ok + }) + for _, fni := range fnList { + if fn, ok := fni.(func(*types.Block, common.Hash, []*types.Log)); ok { + fn(block, hash, logs) + } + } +} +func pluginNewSideBlock(block *types.Block, hash common.Hash, logs []*types.Log) { + if plugins.DefaultPluginLoader == nil { + log.Warn("Attempting NewSideBlock, but default PluginLoader has not been initialized") + return + } + PluginNewSideBlock(plugins.DefaultPluginLoader, block, hash, logs) +}