mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
cmd, core, plugins: introduce config struct, allow user flags
This commit is contained in:
parent
b82813de40
commit
c4427192cb
5 changed files with 43 additions and 20 deletions
|
|
@ -981,7 +981,12 @@ func ExExPluginFlags() []cli.Flag {
|
||||||
for _, name := range exex.Plugins() {
|
for _, name := range exex.Plugins() {
|
||||||
flagset = append(flagset, &cli.BoolFlag{
|
flagset = append(flagset, &cli.BoolFlag{
|
||||||
Name: fmt.Sprintf("exex.%s", name),
|
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,
|
Category: flags.ExExCategory,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -1925,12 +1930,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
|
||||||
}
|
}
|
||||||
// Execution extension plugins
|
// Execution extension plugins
|
||||||
for _, flag := range ExExPluginFlags() {
|
for _, flag := range ExExPluginFlags() {
|
||||||
if ctx.IsSet(flag.(*cli.BoolFlag).Name) {
|
if flag, ok := flag.(*cli.BoolFlag); ok {
|
||||||
plugin := strings.TrimLeft(flag.(*cli.BoolFlag).Name, "exex.") // TODO(karalabe): Custom flag
|
if ctx.IsSet(flag.Name) {
|
||||||
if err := exex.Instantiate(plugin); err != nil {
|
plugin := strings.TrimLeft(flag.Name, "exex.") // TODO(karalabe): Custom flag
|
||||||
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2223,12 +2232,16 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, flag := range ExExPluginFlags() {
|
for _, flag := range ExExPluginFlags() {
|
||||||
if ctx.IsSet(flag.(*cli.BoolFlag).Name) {
|
if flag, ok := flag.(*cli.BoolFlag); ok {
|
||||||
plugin := strings.TrimLeft(flag.(*cli.BoolFlag).Name, "exex.") // TODO(karalabe): Custom flag
|
if ctx.IsSet(flag.Name) {
|
||||||
if err := exex.Instantiate(plugin); err != nil {
|
plugin := strings.TrimLeft(flag.Name, "exex.") // TODO(karalabe): Custom flag
|
||||||
Fatalf("Failed to instantiate ExEx plugin %s: %v", plugin, err)
|
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.
|
// Disable transaction indexing/unindexing by default.
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,15 @@ func RegisterV1(name string, constructor NewPluginV1) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPluginV1 is the constructor signature for making a new plugin.
|
// 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
|
// PluginV1 is an Execution Extension module that can be injected into Geth's
|
||||||
// processing pipeline to subscribe to different node, chain and EVM lifecycle
|
// processing pipeline to subscribe to different node, chain and EVM lifecycle
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ func init() {
|
||||||
// triggers to be invoked.
|
// triggers to be invoked.
|
||||||
type registry interface {
|
type registry interface {
|
||||||
Plugins() []string
|
Plugins() []string
|
||||||
Instantiate(name string) error
|
Instantiate(name string, userconf string) error
|
||||||
|
|
||||||
TriggerInitHook(chain exex.Chain)
|
TriggerInitHook(chain exex.Chain)
|
||||||
TriggerCloseHook()
|
TriggerCloseHook()
|
||||||
|
|
@ -46,8 +46,8 @@ func Plugins() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate constructs an execution extension plugin from a unique name.
|
// Instantiate constructs an execution extension plugin from a unique name.
|
||||||
func Instantiate(name string) error {
|
func Instantiate(name string, userconf string) error {
|
||||||
return globalRegistry.Instantiate(name)
|
return globalRegistry.Instantiate(name, userconf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TriggerInitHook triggers the OnInit hook in exex plugins.
|
// TriggerInitHook triggers the OnInit hook in exex plugins.
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,13 @@ func (reg *registry) Plugins() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instantiate constructs an execution extension plugin from a unique name.
|
// 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
|
// Try instantiating a V1 plugin
|
||||||
if constructor, ok := globalRegistry.pluginsMakersV1[name]; ok {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@ package plugins
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/core/exex"
|
"github.com/ethereum/go-ethereum/core/exex"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Register the minimal ExEx plugin into Geth.
|
// Register the minimal ExEx plugin into Geth.
|
||||||
|
|
@ -29,10 +28,10 @@ func init() {
|
||||||
|
|
||||||
// newMinimalPlugin creates a minimal Execution Extension plugin to react to some
|
// newMinimalPlugin creates a minimal Execution Extension plugin to react to some
|
||||||
// chain events.
|
// chain events.
|
||||||
func newMinimalPlugin(logger log.Logger) (*exex.PluginV1, error) {
|
func newMinimalPlugin(config *exex.ConfigV1) (*exex.PluginV1, error) {
|
||||||
return &exex.PluginV1{
|
return &exex.PluginV1{
|
||||||
OnHead: func(head *types.Header) {
|
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
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue