-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node: allow replacing existing p2p.Reactor(s) #3846
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Package node is the main entry point, where the Node struct, which | ||
represents a full node, is defined. | ||
|
||
Adding new p2p.Reactor(s) | ||
|
||
To add a new p2p.Reactor, use the CustomReactors option: | ||
|
||
node, err := NewNode( | ||
config, | ||
privVal, | ||
nodeKey, | ||
clientCreator, | ||
genesisDocProvider, | ||
dbProvider, | ||
metricsProvider, | ||
logger, | ||
CustomReactors(map[string]p2p.Reactor{"CUSTOM": customReactor}), | ||
) | ||
|
||
Replacing existing p2p.Reactor(s) | ||
|
||
To replace the built-in p2p.Reactor, use the CustomReactors option: | ||
|
||
node, err := NewNode( | ||
config, | ||
privVal, | ||
nodeKey, | ||
clientCreator, | ||
genesisDocProvider, | ||
dbProvider, | ||
metricsProvider, | ||
logger, | ||
CustomReactors(map[string]p2p.Reactor{"BLOCKCHAIN": customBlockchainReactor}), | ||
) | ||
|
||
The list of existing reactors can be found in CustomReactors documentation. | ||
|
||
*/ | ||
package node |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,6 @@ import ( | |
dbm "github.com/tendermint/tm-cmn/db" | ||
) | ||
|
||
// 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. | ||
|
@@ -144,11 +140,26 @@ 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. | ||
// CustomReactors allows you to add custom reactors (name -> p2p.Reactor) to | ||
// the node's Switch. | ||
// | ||
// WARNING: using any name from the below list of the existing reactors will | ||
// result in replacing it with the custom one. | ||
// | ||
// - MEMPOOL | ||
// - BLOCKCHAIN | ||
// - CONSENSUS | ||
// - EVIDENCE | ||
// - PEX | ||
func CustomReactors(reactors map[string]p2p.Reactor) Option { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to call this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think This whole approach is largely based on Dave's article https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis. He's not using Set prefixes either. |
||
return func(n *Node) { | ||
for name, reactor := range reactors { | ||
n.sw.AddReactor(CustomReactorNamePrefix+name, reactor) | ||
if existingReactor := n.sw.Reactor(name); existingReactor != nil { | ||
n.sw.Logger.Info("Replacing existing reactor with a custom one", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, do we typically use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're currently using structured logging with https://github.com/go-kit/kit/tree/master/log under the hood. There're no such methods as |
||
"name", name, "existing", existingReactor, "custom", reactor) | ||
n.sw.RemoveReactor(name, existingReactor) | ||
} | ||
n.sw.AddReactor(name, reactor) | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍