Invoke plugins when blocks are added as new heads or sidechain blocks

This commit is contained in:
Austin Roberts 2021-07-12 11:38:52 -05:00
parent c89b72ed5c
commit 875e506148
2 changed files with 42 additions and 0 deletions

View File

@ -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

View File

@ -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)
}