Skip to content

Commit

Permalink
cmd, core, plugins: introduce config struct, allow user flags
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Oct 16, 2024
1 parent b82813d commit c442719
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
35 changes: 24 additions & 11 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -1925,12 +1930,16 @@ 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 {
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
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)
}
log.Info("Instantiated ExEx plugin", "name", plugin)
}
}
}
Expand Down Expand Up @@ -2223,12 +2232,16 @@ 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 {
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
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)
}
log.Info("Instantiated ExEx plugin", "name", plugin)
}
}
// Disable transaction indexing/unindexing by default.
Expand Down
10 changes: 9 additions & 1 deletion core/exex/exex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/exex/exex/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions core/exex/registry_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions plugins/minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit c442719

Please sign in to comment.