Skip to content

Commit

Permalink
opt-in approach to enabling plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Ergels Gaxhaj committed Feb 4, 2022
1 parent d11bcf8 commit beafe76
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion plugin/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
on = true

# List of plugin names to enable from the plugin/plugins/*
disabled = []
enabled = ["trace"]

# The directory to load non-preloaded plugins from; defaults $GOPATH/src/github.com/cosmos/cosmos-sdk/plugin/plugins
dir = ""
Expand Down
22 changes: 11 additions & 11 deletions plugin/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ func (ls loaderState) String() string {
// 4. Optionally call Start to start plugins.
// 5. Call Close to close all plugins.
type PluginLoader struct {
state loaderState
plugins map[string]plugin.Plugin
started []plugin.Plugin
opts serverTypes.AppOptions
logger logging.Logger
disabled []string
state loaderState
plugins map[string]plugin.Plugin
started []plugin.Plugin
opts serverTypes.AppOptions
logger logging.Logger
enabled []string
}

// NewPluginLoader creates new plugin loader
func NewPluginLoader(opts serverTypes.AppOptions, logger logging.Logger) (*PluginLoader, error) {
loader := &PluginLoader{plugins: make(map[string]plugin.Plugin, len(preloadPlugins)), opts: opts, logger: logger}
loader.disabled = cast.ToStringSlice(opts.Get(fmt.Sprintf("%s.%s", plugin.PLUGINS_TOML_KEY, plugin.PLUGINS_DISABLED_TOML_KEY)))
loader.enabled = cast.ToStringSlice(opts.Get(fmt.Sprintf("%s.%s", plugin.PLUGINS_TOML_KEY, plugin.PLUGINS_ENABLED_TOML_KEY)))
for _, v := range preloadPlugins {
if err := loader.Load(v); err != nil {
return nil, err
}
}
pluginDir := cast.ToString(opts.Get(plugin.PLUGINS_DIR_TOML_KEY))
pluginDir := cast.ToString(opts.Get(fmt.Sprintf("%s.%s", plugin.PLUGINS_TOML_KEY, plugin.PLUGINS_DIR_TOML_KEY)))
if pluginDir == "" {
pluginDir = filepath.Join(os.Getenv("GOPATH"), plugin.DEFAULT_PLUGINS_DIRECTORY)
}
Expand Down Expand Up @@ -137,11 +137,11 @@ func (loader *PluginLoader) Load(pl plugin.Plugin) error {
"while trying to load dynamically: %s",
name, ppl.Version(), pl.Version())
}
if sliceContainsStr(loader.disabled, name) {
loader.logger.Info("not loading disabled plugin", "name", name)
if sliceContainsStr(loader.enabled, name) {
loader.plugins[name] = pl
loader.logger.Info("loading enabled plugin", "name", name)
return nil
}
loader.plugins[name] = pl
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
// PLUGINS_DIR_TOML_KEY is the second-level TOML key for the directory to load plugins from
PLUGINS_DIR_TOML_KEY = "dir"

// PLUGINS_DISABLED_TOML_KEY is the second-level TOML key for a list of plugins to disable
PLUGINS_DISABLED_TOML_KEY = "disabled"
// PLUGINS_ENABLED_TOML_KEY is the second-level TOML key for a list of plugins to disable
PLUGINS_ENABLED_TOML_KEY = "enabled"

// DEFAULT_PLUGINS_DIRECTORY is the default directory to load plugins from
DEFAULT_PLUGINS_DIRECTORY = "src/github.com/cosmos/cosmos-sdk/plugin/plugins"
Expand Down
4 changes: 2 additions & 2 deletions plugin/plugins/kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ The plugin has been hooked up to run with `test-sim-nondeterminism` task. For a
# turn the plugin system, as a whole, on or off
on = true
# List of plugin names to disable
disabled = ["file", "trace"]
# List of plugin names to enable from the plugin/plugins/*
enabled = ["kafka"]
# The directory to load non-preloaded plugins from; defaults to ./plugin/plugins
dir = ""
Expand Down
4 changes: 2 additions & 2 deletions plugin/plugins/trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The plugin is setup to run as the `default` plugin. See `./plugin/loader/preload
# turn the plugin system, as a whole, on or off
on = true
# list of plugins to disable
disabled = []
# List of plugin names to enable from the plugin/plugins/*
enabled = ["trace"]
# The directory to load non-preloaded plugins from; defaults to
dir = ""
Expand Down
10 changes: 3 additions & 7 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,13 @@ func TestAppStateDeterminism(t *testing.T) {
}

appOpts := loadAppOptions()
disabledPlugins := cast.ToStringSlice(appOpts.Get(fmt.Sprintf("%s.%s", plugin.PLUGINS_TOML_KEY, plugin.PLUGINS_DISABLED_TOML_KEY)))
var kafkaDisabled bool = false
for _, p := range disabledPlugins {
enabledPlugins := cast.ToStringSlice(appOpts.Get(fmt.Sprintf("%s.%s", plugin.PLUGINS_TOML_KEY, plugin.PLUGINS_ENABLED_TOML_KEY)))
for _, p := range enabledPlugins {
if kafkaplugin.PLUGIN_NAME == p {
kafkaDisabled = true
prepKafkaTopics(appOpts)
break
}
}
if !kafkaDisabled {
prepKafkaTopics(appOpts)
}

db := dbm.NewMemDB()
app := NewSimApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeTestEncodingConfig(), appOpts, interBlockCacheOpt())
Expand Down

0 comments on commit beafe76

Please sign in to comment.