Make injectable versions of plugin calls

This commit is contained in:
Austin Roberts 2021-06-25 23:02:25 -05:00
parent 03808de29a
commit b821bd9948
3 changed files with 78 additions and 29 deletions

View File

@ -3,10 +3,11 @@ package core
import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/log"
)
func pluginPreProcessBlock(block *types.Block) {
fnList := plugins.Lookup("ProcessBlock", func(item interface{}) bool {
func PluginPreProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
fnList := pl.Lookup("ProcessBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block))
return ok
})
@ -16,8 +17,15 @@ func pluginPreProcessBlock(block *types.Block) {
}
}
}
func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
fnList := plugins.Lookup("ProcessTransaction", func(item interface{}) bool {
func pluginPreProcessBlock(block *types.Block) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PreProcessBlock, but default PluginLoader has not been initialized")
return
}
PluginPreProcessBlock(plugins.DefaultPluginLoader, block) // TODO
}
func PluginPreProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int) {
fnList := pl.Lookup("ProcessTransaction", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, int))
return ok
})
@ -27,8 +35,15 @@ func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i in
}
}
}
func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
fnList := plugins.Lookup("ProcessingError", func(item interface{}) bool {
func pluginPreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PreProcessTransaction, but default PluginLoader has not been initialized")
return
}
PluginPreProcessTransaction(plugins.DefaultPluginLoader, tx, block, i)
}
func PluginBlockProcessingError(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, err error) {
fnList := pl.Lookup("ProcessingError", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, error))
return ok
})
@ -38,8 +53,15 @@ func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err e
}
}
}
func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
fnList := plugins.Lookup("ProcessTransaction", func(item interface{}) bool {
func pluginBlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized")
return
}
PluginBlockProcessingError(plugins.DefaultPluginLoader, tx, block, err)
}
func PluginPostProcessTransaction(pl *plugins.PluginLoader, tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
fnList := pl.Lookup("ProcessTransaction", func(item interface{}) bool {
_, ok := item.(func(*types.Transaction, *types.Block, int, *types.Receipt))
return ok
})
@ -49,8 +71,15 @@ func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i i
}
}
}
func pluginPostProcessBlock(block *types.Block) {
fnList := plugins.Lookup("ProcessBlock", func(item interface{}) bool {
func pluginPostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PostProcessTransaction, but default PluginLoader has not been initialized")
return
}
PluginPostProcessTransaction(plugins.DefaultPluginLoader, tx, block, i, receipt)
}
func PluginPostProcessBlock(pl *plugins.PluginLoader, block *types.Block) {
fnList := pl.Lookup("ProcessBlock", func(item interface{}) bool {
_, ok := item.(func(*types.Block))
return ok
})
@ -60,3 +89,10 @@ func pluginPostProcessBlock(block *types.Block) {
}
}
}
func pluginPostProcessBlock(block *types.Block) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PostProcessBlock, but default PluginLoader has not been initialized")
return
}
PluginPostProcessBlock(plugins.DefaultPluginLoader, block)
}

View File

@ -9,11 +9,11 @@ import (
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
)
func pluginCreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
fnList := plugins.Lookup("CreateConsensusEngine", func(item interface{}) bool {
func PluginCreateConsensusEngine(pl *plugins.PluginLoader, stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
fnList := pl.Lookup("CreateConsensusEngine", func(item interface{}) bool {
_, ok := item.(func(*node.Node, *params.ChainConfig, *ethash.Config, []string, bool, ethdb.Database) consensus.Engine)
return ok
})
@ -24,7 +24,16 @@ import (
}
return ethconfig.CreateConsensusEngine(stack, chainConfig, config, notify, noverify, db)
}
func pluginUpdateBlockchainVMConfig(cfg *vm.Config) {
func pluginCreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting CreateConsensusEngine, but default PluginLoader has not been initialized")
return ethconfig.CreateConsensusEngine(stack, chainConfig, config, notify, noverify, db)
}
return PluginCreateConsensusEngine(plugins.DefaultPluginLoader, stack, chainConfig, config, notify, noverify, db)
}
func PluginUpdateBlockchainVMConfig(pl *plugins.PluginLoader, cfg *vm.Config) {
fnList := plugins.Lookup("UpdateBlockchainVMConfig", func(item interface{}) bool {
_, ok := item.(func(*vm.Config))
return ok
@ -37,16 +46,11 @@ func pluginUpdateBlockchainVMConfig(cfg *vm.Config) {
}
}
// ce, err := plug.Lookup("CreateConsensusEngine")
// if err == nil {
// cce, ok := ce.(func (stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine)
// if !ok {
// log.Warn("Could not cast plugin.CreateConsensusEngine to appropriate function", "file", fpath)
// } else {
// if setConsensus {
// log.Warn("CreateConsensusEngine redeclared", "file", fpath)
// }
// pl.CreateConsensusEngine = cce
// setConsensus = true
// }
// }
func pluginUpdateBlockchainVMConfig(cfg *vm.Config) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting CreateConsensusEngine, but default PluginLoader has not been initialized")
return
}
PluginUpdateBlockchainVMConfig(plugins.DefaultPluginLoader, cfg)
}

View File

@ -4,6 +4,7 @@ import (
"github.com/ethereum/go-ethereum/plugins"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/log"
)
@ -12,8 +13,8 @@ type TracerResult interface {
GetResult() (interface{}, error)
}
func getPluginTracer(name string) (func(*state.StateDB)TracerResult, bool) {
tracers := plugins.Lookup("Tracers", func(item interface{}) bool {
func GetPluginTracer(pl *plugins.PluginLoader, name string) (func(*state.StateDB)TracerResult, bool) {
tracers := pl.Lookup("Tracers", func(item interface{}) bool {
_, ok := item.(map[string]func(*state.StateDB)TracerResult)
return ok
})
@ -26,3 +27,11 @@ func getPluginTracer(name string) (func(*state.StateDB)TracerResult, bool) {
}
return nil, false
}
func getPluginTracer(name string) (func(*state.StateDB)TracerResult, bool) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting GetPluginTracer, but default PluginLoader has not been initialized")
return nil, false
}
return GetPluginTracer(plugins.DefaultPluginLoader, name)
}