cmd, core, plugins: introduce config struct, allow user flags

This commit is contained in:
Péter Szilágyi 2024-10-16 15:37:47 +03:00
parent b82813de40
commit c4427192cb
5 changed files with 43 additions and 20 deletions

View file

@ -981,7 +981,12 @@ func ExExPluginFlags() []cli.Flag {
for _, name := range exex.Plugins() {
flagset = append(flagset, &cli.BoolFlag{
Name: fmt.Sprintf("exex.%s", name),
Usage: fmt.Sprintf("Enables the %s execution extension plugin", name),
Usage: fmt.Sprintf("Enables the '%s' execution extension plugin", name),
Category: flags.ExExCategory,
})
flagset = append(flagset, &cli.StringFlag{
Name: fmt.Sprintf("exex.%s.config", name),
Usage: fmt.Sprintf("Opaque config to pass to the '%s' execution extension plugin", name),
Category: flags.ExExCategory,
})
}
@ -1925,15 +1930,19 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
// Execution extension plugins
for _, flag := range ExExPluginFlags() {
if ctx.IsSet(flag.(*cli.BoolFlag).Name) {
plugin := strings.TrimLeft(flag.(*cli.BoolFlag).Name, "exex.") // TODO(karalabe): Custom flag
if err := exex.Instantiate(plugin); err != nil {
if flag, ok := flag.(*cli.BoolFlag); ok {
if ctx.IsSet(flag.Name) {
plugin := strings.TrimLeft(flag.Name, "exex.") // TODO(karalabe): Custom flag
config := ctx.String(flag.Name + ".config") // TODO(karalabe): Custom flag
if err := exex.Instantiate(plugin, config); err != nil {
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
}
log.Info("Instantiated ExEx plugin", "name", plugin)
}
}
}
}
// SetDNSDiscoveryDefaults configures DNS discovery with the given URL if
// no URLs are set.
@ -2223,14 +2232,18 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
}
}
for _, flag := range ExExPluginFlags() {
if ctx.IsSet(flag.(*cli.BoolFlag).Name) {
plugin := strings.TrimLeft(flag.(*cli.BoolFlag).Name, "exex.") // TODO(karalabe): Custom flag
if err := exex.Instantiate(plugin); err != nil {
if flag, ok := flag.(*cli.BoolFlag); ok {
if ctx.IsSet(flag.Name) {
plugin := strings.TrimLeft(flag.Name, "exex.") // TODO(karalabe): Custom flag
config := ctx.String(flag.Name + ".config") // TODO(karalabe): Custom flag
if err := exex.Instantiate(plugin, config); err != nil {
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
}
log.Info("Instantiated ExEx plugin", "name", plugin)
}
}
}
// Disable transaction indexing/unindexing by default.
chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil)
if err != nil {

View file

@ -27,7 +27,15 @@ func RegisterV1(name string, constructor NewPluginV1) {
}
// NewPluginV1 is the constructor signature for making a new plugin.
type NewPluginV1 func(logger log.Logger) (*PluginV1, error)
type NewPluginV1 func(config *ConfigV1) (*PluginV1, error)
// ConfigV1 contains some configurations for initializing exex plugins. Some of
// the fields originate from Geth, other fields from user configs.
type ConfigV1 struct {
Logger log.Logger // Geth's logger with the plugin name injected
User string // Opaque flag provided by the user on the CLI
}
// PluginV1 is an Execution Extension module that can be injected into Geth's
// processing pipeline to subscribe to different node, chain and EVM lifecycle

View file

@ -33,7 +33,7 @@ func init() {
// triggers to be invoked.
type registry interface {
Plugins() []string
Instantiate(name string) error
Instantiate(name string, userconf string) error
TriggerInitHook(chain exex.Chain)
TriggerCloseHook()
@ -46,8 +46,8 @@ func Plugins() []string {
}
// Instantiate constructs an execution extension plugin from a unique name.
func Instantiate(name string) error {
return globalRegistry.Instantiate(name)
func Instantiate(name string, userconf string) error {
return globalRegistry.Instantiate(name, userconf)
}
// TriggerInitHook triggers the OnInit hook in exex plugins.

View file

@ -35,10 +35,13 @@ func (reg *registry) Plugins() []string {
}
// Instantiate constructs an execution extension plugin from a unique name.
func (reg *registry) Instantiate(name string) error {
func (reg *registry) Instantiate(name string, userconf string) error {
// Try instantiating a V1 plugin
if constructor, ok := globalRegistry.pluginsMakersV1[name]; ok {
plugin, err := constructor(log.New("exex", name))
plugin, err := constructor(&ConfigV1{
Logger: log.New("exex", name),
User: userconf,
})
if err != nil {
return err
}

View file

@ -19,7 +19,6 @@ package plugins
import (
"github.com/ethereum/go-ethereum/core/exex"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)
// Register the minimal ExEx plugin into Geth.
@ -29,10 +28,10 @@ func init() {
// newMinimalPlugin creates a minimal Execution Extension plugin to react to some
// chain events.
func newMinimalPlugin(logger log.Logger) (*exex.PluginV1, error) {
func newMinimalPlugin(config *exex.ConfigV1) (*exex.PluginV1, error) {
return &exex.PluginV1{
OnHead: func(head *types.Header) {
logger.Info("Chain head updated", "number", head.Number, "hash", head.Hash())
config.Logger.Info("Chain head updated", "number", head.Number, "hash", head.Hash())
},
}, nil
}