2021-03-15 20:02:37 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"plugin"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2021-06-25 22:05:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
// "github.com/ethereum/go-ethereum/eth/ethconfig"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2021-06-25 18:55:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2021-06-25 22:05:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-03-15 20:02:37 +00:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"path"
|
|
|
|
"fmt"
|
2021-06-25 19:57:24 +00:00
|
|
|
"reflect"
|
2021-03-15 20:02:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Subcommand func(*cli.Context, []string) error
|
2021-06-25 19:57:24 +00:00
|
|
|
type TracerResult interface {
|
|
|
|
vm.Tracer
|
|
|
|
GetResult() (interface{}, error)
|
|
|
|
}
|
2021-03-15 20:02:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
type PluginLoader struct{
|
2021-06-25 22:05:17 +00:00
|
|
|
Plugins []plugin.Plugin
|
|
|
|
Tracers map[string]func(StateDB)TracerResult
|
2021-03-15 20:02:37 +00:00
|
|
|
StateHooks []interface{} // TODO: Set interface
|
2021-06-25 22:05:17 +00:00
|
|
|
// RPCPlugins []APILoader
|
2021-03-15 20:02:37 +00:00
|
|
|
Subcommands map[string]Subcommand
|
|
|
|
Flags []*flag.FlagSet
|
2021-06-25 22:05:17 +00:00
|
|
|
CreateConsensusEngine func(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine
|
|
|
|
UpdateBlockchainVMConfig func(*vm.Config)
|
|
|
|
PreProcessBlockList []func(*types.Block)
|
|
|
|
PreProcessTransactionList []func(*types.Transaction, *types.Block, int)
|
|
|
|
BlockProcessingErrorList []func(*types.Transaction, *types.Block, error)
|
|
|
|
PostProcessTransactionList []func(*types.Transaction, *types.Block, int, *types.Receipt)
|
|
|
|
PostProcessBlockList []func(*types.Block)
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 22:05:17 +00:00
|
|
|
|
|
|
|
var DefaultPluginLoader *PluginLoader
|
2021-06-25 15:57:56 +00:00
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
|
|
|
|
func NewPluginLoader(target string) (*PluginLoader, error) {
|
|
|
|
pl := &PluginLoader{
|
2021-06-25 22:05:17 +00:00
|
|
|
Plugins: []plugin.Plugin,
|
|
|
|
// RPCPlugins: []APILoader{},
|
2021-03-15 20:02:37 +00:00
|
|
|
Subcommands: make(map[string]Subcommand),
|
2021-06-25 22:05:17 +00:00
|
|
|
Tracers: make(map[string]func(StateDB)TracerResult),
|
2021-03-15 20:02:37 +00:00
|
|
|
Flags: []*flag.FlagSet{},
|
2021-06-25 22:05:17 +00:00
|
|
|
// CreateConsensusEngine: ethconfig.CreateConsensusEngine,
|
|
|
|
UpdateBlockchainVMConfig: func(cfg *vm.Config) {},
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
files, err := ioutil.ReadDir(target)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Could not load plugins directory. Skipping.", "path", target)
|
|
|
|
return pl, nil
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
setConsensus := false
|
|
|
|
setUpdateBCVMCfg := false
|
2021-03-15 20:02:37 +00:00
|
|
|
for _, file := range files {
|
|
|
|
fpath := path.Join(target, file.Name())
|
|
|
|
if !strings.HasSuffix(file.Name(), ".so") {
|
2021-06-25 15:57:56 +00:00
|
|
|
log.Debug("File inplugin directory is not '.so' file. Skipping.", "file", fpath)
|
2021-03-15 20:02:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
plug, err := plugin.Open(fpath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("File in plugin directory could not be loaded: %v", "file", fpath, "error", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Any type of plugin can potentially specify flags
|
|
|
|
f, err := plug.Lookup("Flags")
|
|
|
|
if err == nil {
|
|
|
|
flagset, ok := f.(*flag.FlagSet)
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Found plugin.Flags, but it its not a *FlagSet", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.Flags = append(pl.Flags, flagset)
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 15:57:56 +00:00
|
|
|
sb, err := plug.Lookup("Subcommands")
|
|
|
|
if err == nil {
|
2021-06-25 19:57:24 +00:00
|
|
|
subcommands, ok := sb.(*map[string]func(*cli.Context, []string) error)
|
2021-03-15 20:02:37 +00:00
|
|
|
if !ok {
|
2021-06-25 19:57:24 +00:00
|
|
|
log.Warn("Could not cast plugin.Subcommands to `map[string]func(*cli.Context, []string) error`", "file", fpath, "type", reflect.TypeOf(sb))
|
2021-06-25 15:57:56 +00:00
|
|
|
} else {
|
2021-06-25 19:57:24 +00:00
|
|
|
for k, v := range *subcommands {
|
2021-06-25 15:57:56 +00:00
|
|
|
if _, ok := pl.Subcommands[k]; ok {
|
|
|
|
log.Warn("Subcommand redeclared", "file", fpath, "subcommand", k)
|
|
|
|
}
|
|
|
|
pl.Subcommands[k] = v
|
|
|
|
}
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-25 18:55:31 +00:00
|
|
|
tr, err := plug.Lookup("Tracers")
|
|
|
|
if err == nil {
|
2021-06-25 22:05:17 +00:00
|
|
|
tracers, ok := tr.(*map[string]func(StateDB)TracerResult)
|
2021-06-25 18:55:31 +00:00
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.Tracers to `map[string]vm.Tracer`", "file", fpath)
|
|
|
|
} else {
|
2021-06-25 19:57:24 +00:00
|
|
|
for k, v := range *tracers {
|
2021-06-25 18:55:31 +00:00
|
|
|
if _, ok := pl.Tracers[k]; ok {
|
|
|
|
log.Warn("Tracer redeclared", "file", fpath, "tracer", k)
|
|
|
|
}
|
|
|
|
pl.Tracers[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vmcfgu, err := plug.Lookup("UpdateBlockchainVMConfig")
|
|
|
|
if err == nil {
|
|
|
|
vmcfgfn, ok := vmcfgu.(func(*vm.Config))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.UpdateBlockchainVMConfig to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
if setUpdateBCVMCfg {
|
|
|
|
log.Warn("UpdateBlockchainVMConfig redeclared", "file", fpath)
|
|
|
|
}
|
|
|
|
pl.UpdateBlockchainVMConfig = vmcfgfn
|
|
|
|
setUpdateBCVMCfg = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
prepb, err := plug.Lookup("PreProcessBlock")
|
|
|
|
if err == nil {
|
|
|
|
prepbfn, ok := prepb.(func(*types.Block))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.PreProcessBlock to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.PreProcessBlockList = append(pl.PreProcessBlockList, prepbfn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prept, err := plug.Lookup("PreProcessTransaction")
|
|
|
|
if err == nil {
|
|
|
|
preptfn, ok := prept.(func(*types.Transaction, *types.Block, int))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.PreProcessTransaction to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.PreProcessTransactionList = append(pl.PreProcessTransactionList, preptfn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bpe, err := plug.Lookup("BlockProcessingError")
|
|
|
|
if err == nil {
|
|
|
|
bpefn, ok := bpe.(func(*types.Transaction, *types.Block, error))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.BlockProcessingError to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.BlockProcessingErrorList = append(pl.BlockProcessingErrorList, bpefn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prept, err := plug.Lookup("PostProcessTransaction")
|
|
|
|
if err == nil {
|
|
|
|
preptfn, ok := prept.(func(*types.Transaction, *types.Block, int, *types.Receipt))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.PostProcessTransaction to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.PostProcessTransactionList = append(pl.PostProcessTransactionList, preptfn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prepb, err := plug.Lookup("PostProcessBlock")
|
|
|
|
if err == nil {
|
|
|
|
prepbfn, ok := prepb.(func(*types.Block))
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Could not cast plugin.PostProcessBlock to appropriate function", "file", fpath)
|
|
|
|
} else {
|
|
|
|
pl.PostProcessBlockList = append(pl.PostProcessBlockList, prepbfn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:57:56 +00:00
|
|
|
func Initialize(target string) (err error) {
|
2021-06-25 22:05:17 +00:00
|
|
|
DefaultPluginLoader, err = NewPluginLoader(target)
|
2021-06-25 15:57:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
func (pl *PluginLoader) RunSubcommand(ctx *cli.Context) (bool, error) {
|
|
|
|
args := ctx.Args()
|
|
|
|
if len(args) == 0 { return false, fmt.Errorf("No subcommand arguments")}
|
|
|
|
subcommand, ok := pl.Subcommands[args[0]]
|
|
|
|
if !ok { return false, fmt.Errorf("Subcommand %v does not exist", args[0])}
|
|
|
|
return true, subcommand(ctx, args[1:])
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:57:56 +00:00
|
|
|
func RunSubcommand(ctx *cli.Context) (bool, error) {
|
2021-06-25 22:05:17 +00:00
|
|
|
if DefaultPluginLoader == nil { return false, fmt.Errorf("Plugin loader not initialized") }
|
|
|
|
return DefaultPluginLoader.RunSubcommand(ctx)
|
2021-06-25 15:57:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 20:02:37 +00:00
|
|
|
func (pl *PluginLoader) ParseFlags(args []string) bool {
|
|
|
|
for _, flagset := range pl.Flags {
|
|
|
|
flagset.Parse(args)
|
|
|
|
}
|
|
|
|
return len(pl.Flags) > 0
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:57:56 +00:00
|
|
|
func ParseFlags(args []string) bool {
|
2021-06-25 22:05:17 +00:00
|
|
|
if DefaultPluginLoader == nil {
|
2021-06-25 15:57:56 +00:00
|
|
|
log.Warn("Attempting to parse flags, but default PluginLoader has not been initialized")
|
|
|
|
return false
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
return DefaultPluginLoader.ParseFlags(args)
|
2021-03-15 20:02:37 +00:00
|
|
|
}
|
2021-06-25 15:57:56 +00:00
|
|
|
|
2021-06-25 22:05:17 +00:00
|
|
|
func (pl *PluginLoader) GetTracer(s string) (func(StateDB)TracerResult, bool) {
|
2021-06-25 18:55:31 +00:00
|
|
|
tr, ok := pl.Tracers[s]
|
|
|
|
return tr, ok
|
|
|
|
}
|
|
|
|
|
2021-06-25 22:05:17 +00:00
|
|
|
func GetTracer(s string) (func(StateDB)TracerResult, bool) {
|
|
|
|
if DefaultPluginLoader == nil {
|
2021-06-25 18:55:31 +00:00
|
|
|
log.Warn("Attempting GetTracer, but default PluginLoader has not been initialized")
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-06-25 22:05:17 +00:00
|
|
|
return DefaultPluginLoader.GetTracer(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// func CreateConsensusEngine(stack *node.Node, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, db ethdb.Database) consensus.Engine {
|
|
|
|
// if DefaultPluginLoader == nil {
|
|
|
|
// log.Warn("Attempting CreateConsensusEngine, but default PluginLoader has not been initialized")
|
|
|
|
// return ethconfig.CreateConsensusEngine(stack, chainConfig, config, notify, noverify, db)
|
|
|
|
// }
|
|
|
|
// return DefaultPluginLoader.CreateConsensusEngine(stack, chainConfig, config, notify, noverify, db)
|
|
|
|
// }
|
|
|
|
|
|
|
|
func UpdateBlockchainVMConfig(cfg *vm.Config) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting UpdateBlockchainVMConfig, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.UpdateBlockchainVMConfig(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (pl *PluginLoader) PreProcessBlock(block *types.Block) {
|
|
|
|
for _, fn := range pl.PreProcessBlockList {
|
|
|
|
fn(block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func PreProcessBlock(block *types.Block) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PreProcessBlock, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.PreProcessBlock(block)
|
|
|
|
}
|
|
|
|
func (pl *PluginLoader) PreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
|
|
|
|
for _, fn := range pl.PreProcessTransactionList {
|
|
|
|
fn(tx, block, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func PreProcessTransaction(tx *types.Transaction, block *types.Block, i int) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PreProcessTransaction, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.PreProcessTransaction(tx, block, i)
|
|
|
|
}
|
|
|
|
func (pl *PluginLoader) BlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
|
|
|
|
for _, fn := range pl.BlockProcessingErrorList {
|
|
|
|
fn(tx, block, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func BlockProcessingError(tx *types.Transaction, block *types.Block, err error) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting BlockProcessingError, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.BlockProcessingError(tx, block, err)
|
|
|
|
}
|
|
|
|
func (pl *PluginLoader) PostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
|
|
|
for _, fn := range pl.PostProcessTransactionList {
|
|
|
|
fn(tx, block, i, receipt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func PostProcessTransaction(tx *types.Transaction, block *types.Block, i int, receipt *types.Receipt) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PostProcessTransaction, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.PostProcessTransaction(tx, block, i, receipt)
|
|
|
|
}
|
|
|
|
func (pl *PluginLoader) PostProcessBlock(block *types.Block) {
|
|
|
|
for _, fn := range pl.PostProcessBlockList {
|
|
|
|
fn(block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func PostProcessBlock(block *types.Block) {
|
|
|
|
if DefaultPluginLoader == nil {
|
|
|
|
log.Warn("Attempting PostProcessBlock, but default PluginLoader has not been initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
DefaultPluginLoader.PostProcessBlock(block)
|
2021-06-25 18:55:31 +00:00
|
|
|
}
|