From 38ea6101dec69f108bf0aee019893c8f06b13dc4 Mon Sep 17 00:00:00 2001 From: philip-morlier Date: Thu, 28 Sep 2023 21:19:54 -0700 Subject: [PATCH] Config plugin hooks and injections with test coverage. All config hooks have been written with the exception of writing the genesis. --- cmd/utils/flags.go | 10 +++++-- cmd/utils/plugin_hooks.go | 16 +++++------ plugins/test-plugin/hooks.go | 39 +++++++++++++++++++++++++- plugins/test-plugin/main.go | 10 ++++++- plugins/test-plugin/test/config01.toml | 2 -- 5 files changed, 62 insertions(+), 15 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index a7710eaf7..c70ebe31b 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1510,7 +1510,12 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) { } func SetDataDir(ctx *cli.Context, cfg *node.Config) { + // begin PluGeth injection + pluginPath := pluginDefaultDataDir(node.DefaultDataDir()) switch { + case pluginPath != "" && cfg.DataDir == node.DefaultDataDir(): + cfg.DataDir = pluginPath + // end PluGeth injection case ctx.IsSet(DataDirFlag.Name): cfg.DataDir = ctx.String(DataDirFlag.Name) case ctx.Bool(DeveloperFlag.Name): @@ -1693,13 +1698,14 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { if pluginNetworkId := pluginNetworkId(); pluginNetworkId != nil { cfg.NetworkId = *pluginNetworkId } + if cfg.EthDiscoveryURLs == nil { var lightMode bool if cfg.SyncMode == downloader.LightSync { lightMode = true } - cfg.EthDiscoveryURLs := pluginETHDiscoveryURLs(lightMode) { - cfg.SnapDiscoveryURLs := pluginSnapDiscoveryURLs() + cfg.EthDiscoveryURLs = pluginETHDiscoveryURLs(lightMode) + cfg.SnapDiscoveryURLs = pluginSnapDiscoveryURLs() } //end PluGeth injection diff --git a/cmd/utils/plugin_hooks.go b/cmd/utils/plugin_hooks.go index d0fdbff91..8456c1776 100644 --- a/cmd/utils/plugin_hooks.go +++ b/cmd/utils/plugin_hooks.go @@ -6,9 +6,8 @@ import ( ) func DefaultDataDir(pl *plugins.PluginLoader, path string) string { - log.Error("inside default data dir hook") dataDirPath := "" - fnList := pl.Lookup("DefaultDataDir", func(item interface{}) bool { + fnList := pl.Lookup("SetDefaultDataDir", func(item interface{}) bool { _, ok := item.(func(string) string) return ok }) @@ -21,7 +20,6 @@ func DefaultDataDir(pl *plugins.PluginLoader, path string) string { } func pluginDefaultDataDir(path string) string { - log.Error("inside default data dir injection") if plugins.DefaultPluginLoader == nil { log.Warn("Attempting DefaultDataDir, but default PluginLoader has not been initialized") return "" @@ -73,26 +71,26 @@ func pluginNetworkId() *uint64 { return PluginNetworkId(plugins.DefaultPluginLoader) } -func PluginETHDiscoveryURLs(pl *plugins.PluginLoader) []string { +func PluginETHDiscoveryURLs(pl *plugins.PluginLoader, mode bool) []string { var ethDiscoveryURLs []string fnList := pl.Lookup("SetETHDiscoveryURLs", func(item interface{}) bool { - _, ok := item.(func() []string) + _, ok := item.(func(bool) []string) return ok }) for _, fni := range fnList { - if fn, ok := fni.(func() []string); ok { - ethDiscoveryURLs = fn() + if fn, ok := fni.(func(bool) []string); ok { + ethDiscoveryURLs = fn(mode) } } return ethDiscoveryURLs } -func pluginETHDiscoveryURLs() []string { +func pluginETHDiscoveryURLs(mode bool) []string { if plugins.DefaultPluginLoader == nil { log.Warn("Attempting pluginETHDiscoveryURLs, but default PluginLoader has not been initialized") return nil } - return PluginETHDiscoveryURLs(plugins.DefaultPluginLoader) + return PluginETHDiscoveryURLs(plugins.DefaultPluginLoader, mode) } func PluginSnapDiscoveryURLs(pl *plugins.PluginLoader) []string { diff --git a/plugins/test-plugin/hooks.go b/plugins/test-plugin/hooks.go index 950339da8..72a1c76ae 100644 --- a/plugins/test-plugin/hooks.go +++ b/plugins/test-plugin/hooks.go @@ -43,11 +43,44 @@ func GetAPIs(stack core.Node, backend core.Backend) []core.API { // cmd/utils/ -func SetSnapDiscoveryURLs() { +func SetDefaultDataDir(arg string) string { + m := map[string]struct{}{ + "SetDefaultDataDir":struct{}{}, + } + hookChan <- m + return "test" +} + +func SetBootstrapNodes() []string { + m := map[string]struct{}{ + "SetBootstrapNodes":struct{}{}, + } + hookChan <- m + return nil +} + +func SetNetworkId() *uint64 { + m := map[string]struct{}{ + "SetNetworkId":struct{}{}, + } + hookChan <- m + return nil +} + +func SetETHDiscoveryURLs(arg bool) []string { + m := map[string]struct{}{ + "SetETHDiscoveryURLs":struct{}{}, + } + hookChan <- m + return nil +} + +func SetSnapDiscoveryURLs() []string { m := map[string]struct{}{ "SetSnapDiscoveryURLs":struct{}{}, } hookChan <- m + return nil } // core/ @@ -192,6 +225,10 @@ var plugins map[string]struct{} = map[string]struct{}{ // "LiveCaptureEnter": struct{}{}, // "LiveCaptureExit": struct{}{}, // "LiveTracerResult": struct{}{}, + "SetDefaultDataDir":struct{}{}, + "SetBootstrapNodes":struct{}{}, + "SetNetworkId":struct{}{}, + "SetETHDiscoveryURLs": struct{}{}, "SetSnapDiscoveryURLs": struct{}{}, } diff --git a/plugins/test-plugin/main.go b/plugins/test-plugin/main.go index 0d52747a7..9acd1b908 100644 --- a/plugins/test-plugin/main.go +++ b/plugins/test-plugin/main.go @@ -116,8 +116,16 @@ func BlockChain() { // delete(plugins, "LiveCaptureExit") // case f("LiveTracerResult"): // delete(plugins, "LiveTracerResult") + case f("SetDefaultDataDir"): + delete(plugins, "SetDefaultDataDir") + case f("SetBootstrapNodes"): + delete(plugins, "SetBootstrapNodes") + case f("SetNetworkId"): + delete(plugins, "SetNetworkId") + case f("SetETHDiscoveryURLs"): + delete(plugins, "SetETHDiscoveryURLs") case f("SetSnapDiscoveryURLs"): - delete(plugins, "SetSnapDiscoveryURLs") + delete(plugins, "SetSnapDiscoveryURLs") } } } diff --git a/plugins/test-plugin/test/config01.toml b/plugins/test-plugin/test/config01.toml index 166c37c3f..31417687c 100644 --- a/plugins/test-plugin/test/config01.toml +++ b/plugins/test-plugin/test/config01.toml @@ -1,8 +1,6 @@ [Eth] NetworkId = 6448 SyncMode = "snap" -EthDiscoveryURLs = [] -SnapDiscoveryURLs = [] NoPruning = false NoPrefetch = false TxLookupLimit = 2350000