diff --git a/blockchain/reactor_test.go b/blockchain/reactor_test.go index 672cb83d6..c7f7e9afd 100644 --- a/blockchain/reactor_test.go +++ b/blockchain/reactor_test.go @@ -42,7 +42,7 @@ func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainRe bcReactor.SetLogger(logger.With("module", "blockchain")) // Next: we need to set a switch in order for peers to be added in - bcReactor.Switch = p2p.NewSwitch(cfg.DefaultP2PConfig(), p2p.NopMetrics()) + bcReactor.Switch = p2p.NewSwitch(cfg.DefaultP2PConfig()) // Lastly: let's add some blocks in for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ { diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index c0d2e636d..d3be8c358 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -38,7 +38,7 @@ func TestByzantine(t *testing.T) { switches := make([]*p2p.Switch, N) p2pLogger := logger.With("module", "p2p") for i := 0; i < N; i++ { - switches[i] = p2p.NewSwitch(config.P2P, p2p.NopMetrics()) + switches[i] = p2p.NewSwitch(config.P2P) switches[i].SetLogger(p2pLogger.With("validator", i)) } diff --git a/consensus/common_test.go b/consensus/common_test.go index 55f4d5828..f50e57699 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -255,7 +255,7 @@ func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.S proxyAppConnCon := abcicli.NewLocalClient(mtx, app) // Make Mempool - mempool := mempl.NewMempool(thisConfig.Mempool, proxyAppConnMem, 0, mempl.NopMetrics()) + mempool := mempl.NewMempool(thisConfig.Mempool, proxyAppConnMem, 0) mempool.SetLogger(log.TestingLogger().With("module", "mempool")) if thisConfig.Consensus.WaitForTxs() { mempool.EnableTxsAvailable() @@ -267,7 +267,7 @@ func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.S // Make ConsensusState stateDB := dbm.NewMemDB() blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool) - cs := NewConsensusState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool, NopMetrics()) + cs := NewConsensusState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool) cs.SetLogger(log.TestingLogger().With("module", "consensus")) cs.SetPrivValidator(pv) diff --git a/consensus/replay_file.go b/consensus/replay_file.go index 113c69c86..57204b01a 100644 --- a/consensus/replay_file.go +++ b/consensus/replay_file.go @@ -126,7 +126,7 @@ func (pb *playback) replayReset(count int, newStepCh chan interface{}) error { pb.cs.Wait() newCS := NewConsensusState(pb.cs.config, pb.genesisState.Copy(), pb.cs.blockExec, - pb.cs.blockStore, pb.cs.mempool, pb.cs.evpool, pb.cs.metrics) + pb.cs.blockStore, pb.cs.mempool, pb.cs.evpool) newCS.SetEventBus(pb.cs.eventBus) newCS.startForReplay() @@ -314,7 +314,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) consensusState := NewConsensusState(csConfig, state.Copy(), blockExec, - blockStore, mempool, evpool, NopMetrics()) + blockStore, mempool, evpool) consensusState.SetEventBus(eventBus) return consensusState diff --git a/consensus/state.go b/consensus/state.go index a92584532..686d2e7d3 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -121,7 +121,7 @@ type ConsensusState struct { } // NewConsensusState returns a new ConsensusState. -func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *sm.BlockExecutor, blockStore sm.BlockStore, mempool sm.Mempool, evpool sm.EvidencePool, metrics *Metrics) *ConsensusState { +func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *sm.BlockExecutor, blockStore sm.BlockStore, mempool sm.Mempool, evpool sm.EvidencePool, options ...func(*ConsensusState)) *ConsensusState { cs := &ConsensusState{ config: config, blockExec: blockExec, @@ -135,7 +135,7 @@ func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *s wal: nilWAL{}, evpool: evpool, evsw: tmevents.NewEventSwitch(), - metrics: metrics, + metrics: NopMetrics(), } // set function defaults (may be overwritten before calling Start) cs.decideProposal = cs.defaultDecideProposal @@ -147,6 +147,9 @@ func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *s // We do that upon Start(). cs.reconstructLastCommit(state) cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs) + for _, option := range options { + option(cs) + } return cs } @@ -165,6 +168,13 @@ func (cs *ConsensusState) SetEventBus(b *types.EventBus) { cs.blockExec.SetEventBus(b) } +// WithMetrics sets the metrics. +func WithMetrics(metrics *Metrics) func(*ConsensusState) { + return func(cs *ConsensusState) { + cs.metrics = metrics + } +} + // String returns a string. func (cs *ConsensusState) String() string { // better not to access shared variables diff --git a/consensus/wal_generator.go b/consensus/wal_generator.go index dab93c343..f61af15f5 100644 --- a/consensus/wal_generator.go +++ b/consensus/wal_generator.go @@ -68,7 +68,7 @@ func WALWithNBlocks(numBlocks int) (data []byte, err error) { mempool := sm.MockMempool{} evpool := sm.MockEvidencePool{} blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) - consensusState := NewConsensusState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, evpool, NopMetrics()) + consensusState := NewConsensusState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, evpool) consensusState.SetLogger(logger) consensusState.SetEventBus(eventBus) if privValidator != nil { diff --git a/mempool/mempool.go b/mempool/mempool.go index a1f9cd179..1df1651e6 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -88,7 +88,7 @@ type Mempool struct { } // NewMempool returns a new Mempool with the given configuration and connection to an application. -func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, height int64, metrics *Metrics) *Mempool { +func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, height int64, options ...func(*Mempool)) *Mempool { mempool := &Mempool{ config: config, proxyAppConn: proxyAppConn, @@ -100,9 +100,12 @@ func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, he recheckEnd: nil, logger: log.NewNopLogger(), cache: newTxCache(config.CacheSize), - metrics: metrics, + metrics: NopMetrics(), } proxyAppConn.SetResponseCallback(mempool.resCb) + for _, option := range options { + option(mempool) + } return mempool } @@ -118,6 +121,13 @@ func (mem *Mempool) SetLogger(l log.Logger) { mem.logger = l } +// WithMetrics sets the metrics. +func WithMetrics(metrics *Metrics) func(*Mempool) { + return func(mem *Mempool) { + mem.metrics = metrics + } +} + // CloseWAL closes and discards the underlying WAL file. // Any further writes will not be relayed to disk. func (mem *Mempool) CloseWAL() bool { diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index 45aa27a8b..a67adf6d3 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -33,7 +33,7 @@ func newMempoolWithApp(cc proxy.ClientCreator) *Mempool { if err != nil { panic(err) } - mempool := NewMempool(config.Mempool, appConnMem, 0, NopMetrics()) + mempool := NewMempool(config.Mempool, appConnMem, 0) mempool.SetLogger(log.TestingLogger()) return mempool } @@ -241,7 +241,7 @@ func TestMempoolCloseWAL(t *testing.T) { app := kvstore.NewKVStoreApplication() cc := proxy.NewLocalClientCreator(app) appConnMem, _ := cc.NewABCIClient() - mempool := NewMempool(wcfg, appConnMem, 10, NopMetrics()) + mempool := NewMempool(wcfg, appConnMem, 10) mempool.InitWAL() // 4. Ensure that the directory contains the WAL file diff --git a/node/node.go b/node/node.go index df1d10cbd..67f992474 100644 --- a/node/node.go +++ b/node/node.go @@ -321,9 +321,10 @@ func NewNode(config *cfg.Config, // Make MempoolReactor mempoolLogger := logger.With("module", "mempool") - mempool := mempl.NewMempool(config.Mempool, proxyApp.Mempool(), state.LastBlockHeight, memplMetrics) - mempool.InitWAL() // no need to have the mempool wal during tests + mempool := mempl.NewMempool(config.Mempool, proxyApp.Mempool(), state.LastBlockHeight, + mempl.WithMetrics(memplMetrics)) mempool.SetLogger(mempoolLogger) + mempool.InitWAL() // no need to have the mempool wal during tests mempoolReactor := mempl.NewMempoolReactor(config.Mempool, mempool) mempoolReactor.SetLogger(mempoolLogger) @@ -353,7 +354,7 @@ func NewNode(config *cfg.Config, // Make ConsensusReactor consensusState := cs.NewConsensusState(config.Consensus, state.Copy(), - blockExec, blockStore, mempool, evidencePool, csMetrics) + blockExec, blockStore, mempool, evidencePool, cs.WithMetrics(csMetrics)) consensusState.SetLogger(consensusLogger) if privValidator != nil { consensusState.SetPrivValidator(privValidator) @@ -363,7 +364,7 @@ func NewNode(config *cfg.Config, p2pLogger := logger.With("module", "p2p") - sw := p2p.NewSwitch(config.P2P, p2pMetrics) + sw := p2p.NewSwitch(config.P2P, p2p.WithMetrics(p2pMetrics)) sw.SetLogger(p2pLogger) sw.AddReactor("MEMPOOL", mempoolReactor) sw.AddReactor("BLOCKCHAIN", bcReactor) diff --git a/p2p/switch.go b/p2p/switch.go index 2eb10cf8d..ed7a80987 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -78,7 +78,7 @@ type Switch struct { } // NewSwitch creates a new Switch with the given config. -func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch { +func NewSwitch(cfg *config.P2PConfig, options ...func(*Switch)) *Switch { sw := &Switch{ config: cfg, reactors: make(map[string]Reactor), @@ -87,7 +87,7 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch { peers: NewPeerSet(), dialing: cmn.NewCMap(), reconnecting: cmn.NewCMap(), - metrics: metrics, + metrics: NopMetrics(), } // Ensure we have a completely undeterministic PRNG. @@ -102,9 +102,21 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch { sw.mConfig = mConfig sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw) + + for _, option := range options { + option(sw) + } + return sw } +// WithMetrics sets the metrics. +func WithMetrics(metrics *Metrics) func(*Switch) { + return func(sw *Switch) { + sw.metrics = metrics + } +} + //--------------------------------------------------------------------- // Switch setup diff --git a/p2p/test_util.go b/p2p/test_util.go index 02e4f644b..0d2ba6c5e 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -137,7 +137,7 @@ func MakeSwitch(cfg *config.P2PConfig, i int, network, version string, initSwitc nodeKey := &NodeKey{ PrivKey: crypto.GenPrivKeyEd25519(), } - sw := NewSwitch(cfg, NopMetrics()) + sw := NewSwitch(cfg) sw.SetLogger(log.TestingLogger()) sw = initSwitch(i, sw) ni := NodeInfo{