node: allow registration of custom reactors while creating node (#3771)

* change invocation of NewNode across

* custom reactor name are prefixed with CUSTOM_

* upgate changelog pending

* improve comments

* node: refactor NewNode to use functional options
This commit is contained in:
Anton Kaliaev
2019-07-03 17:17:59 +04:00
committed by GitHub
parent e645442c9b
commit f76684a05c
4 changed files with 84 additions and 2 deletions
+24 -1
View File
@@ -47,6 +47,10 @@ import (
"github.com/tendermint/tendermint/version"
)
// CustomReactorNamePrefix is a prefix for all custom reactors to prevent
// clashes with built-in reactors.
const CustomReactorNamePrefix = "CUSTOM_"
//------------------------------------------------------------------------------
// DBContext specifies config information for loading a new DB.
@@ -136,6 +140,18 @@ func DefaultMetricsProvider(config *cfg.InstrumentationConfig) MetricsProvider {
}
}
// Option sets a parameter for the node.
type Option func(*Node)
// CustomReactors allows you to add custom reactors to the node's Switch.
func CustomReactors(reactors map[string]p2p.Reactor) Option {
return func(n *Node) {
for name, reactor := range reactors {
n.sw.AddReactor(CustomReactorNamePrefix+name, reactor)
}
}
}
//------------------------------------------------------------------------------
// Node is the highest level interface to a full Tendermint node.
@@ -433,6 +449,7 @@ func createSwitch(config *cfg.Config,
sw.AddReactor("BLOCKCHAIN", bcReactor)
sw.AddReactor("CONSENSUS", consensusReactor)
sw.AddReactor("EVIDENCE", evidenceReactor)
sw.SetNodeInfo(nodeInfo)
sw.SetNodeKey(nodeKey)
@@ -495,7 +512,8 @@ func NewNode(config *cfg.Config,
genesisDocProvider GenesisDocProvider,
dbProvider DBProvider,
metricsProvider MetricsProvider,
logger log.Logger) (*Node, error) {
logger log.Logger,
options ...Option) (*Node, error) {
blockStore, stateDB, err := initDBs(config, dbProvider)
if err != nil {
@@ -661,6 +679,11 @@ func NewNode(config *cfg.Config,
eventBus: eventBus,
}
node.BaseService = *cmn.NewBaseService(logger, "Node", node)
for _, option := range options {
option(node)
}
return node, nil
}