From b821bd994875bd852feaf2717a73ef5a8a64cc6e Mon Sep 17 00:00:00 2001 From: Austin Roberts Date: Fri, 25 Jun 2021 23:02:25 -0500 Subject: [PATCH] Make injectable versions of plugin calls --- core/plugin_hooks.go | 56 ++++++++++++++++++++++++++++++------- eth/plugin_hooks.go | 38 ++++++++++++++----------- eth/tracers/plugin_hooks.go | 13 +++++++-- 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/core/plugin_hooks.go b/core/plugin_hooks.go index ba5f5f662..7cc480772 100644 --- a/core/plugin_hooks.go +++ b/core/plugin_hooks.go @@ -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) +} diff --git a/eth/plugin_hooks.go b/eth/plugin_hooks.go index d3da88453..38a56659c 100644 --- a/eth/plugin_hooks.go +++ b/eth/plugin_hooks.go @@ -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) +} diff --git a/eth/tracers/plugin_hooks.go b/eth/tracers/plugin_hooks.go index 3eaaeba7b..375fa03dd 100644 --- a/eth/tracers/plugin_hooks.go +++ b/eth/tracers/plugin_hooks.go @@ -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) +}