friendly apis for constructors

This commit is contained in:
Anton Kaliaev
2018-06-16 11:44:03 +04:00
parent cd11a54f7a
commit 84812145cb
11 changed files with 53 additions and 20 deletions

View File

@ -42,7 +42,7 @@ func newBlockchainReactor(logger log.Logger, maxBlockHeight int64) *BlockchainRe
bcReactor.SetLogger(logger.With("module", "blockchain")) bcReactor.SetLogger(logger.With("module", "blockchain"))
// Next: we need to set a switch in order for peers to be added in // 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 // Lastly: let's add some blocks in
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ { for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {

View File

@ -38,7 +38,7 @@ func TestByzantine(t *testing.T) {
switches := make([]*p2p.Switch, N) switches := make([]*p2p.Switch, N)
p2pLogger := logger.With("module", "p2p") p2pLogger := logger.With("module", "p2p")
for i := 0; i < N; i++ { 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)) switches[i].SetLogger(p2pLogger.With("validator", i))
} }

View File

@ -255,7 +255,7 @@ func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.S
proxyAppConnCon := abcicli.NewLocalClient(mtx, app) proxyAppConnCon := abcicli.NewLocalClient(mtx, app)
// Make Mempool // 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")) mempool.SetLogger(log.TestingLogger().With("module", "mempool"))
if thisConfig.Consensus.WaitForTxs() { if thisConfig.Consensus.WaitForTxs() {
mempool.EnableTxsAvailable() mempool.EnableTxsAvailable()
@ -267,7 +267,7 @@ func newConsensusStateWithConfigAndBlockStore(thisConfig *cfg.Config, state sm.S
// Make ConsensusState // Make ConsensusState
stateDB := dbm.NewMemDB() stateDB := dbm.NewMemDB()
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyAppConnCon, mempool, evpool) 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.SetLogger(log.TestingLogger().With("module", "consensus"))
cs.SetPrivValidator(pv) cs.SetPrivValidator(pv)

View File

@ -126,7 +126,7 @@ func (pb *playback) replayReset(count int, newStepCh chan interface{}) error {
pb.cs.Wait() pb.cs.Wait()
newCS := NewConsensusState(pb.cs.config, pb.genesisState.Copy(), pb.cs.blockExec, 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.SetEventBus(pb.cs.eventBus)
newCS.startForReplay() newCS.startForReplay()
@ -314,7 +314,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
consensusState := NewConsensusState(csConfig, state.Copy(), blockExec, consensusState := NewConsensusState(csConfig, state.Copy(), blockExec,
blockStore, mempool, evpool, NopMetrics()) blockStore, mempool, evpool)
consensusState.SetEventBus(eventBus) consensusState.SetEventBus(eventBus)
return consensusState return consensusState

View File

@ -121,7 +121,7 @@ type ConsensusState struct {
} }
// NewConsensusState returns a new ConsensusState. // 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{ cs := &ConsensusState{
config: config, config: config,
blockExec: blockExec, blockExec: blockExec,
@ -135,7 +135,7 @@ func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *s
wal: nilWAL{}, wal: nilWAL{},
evpool: evpool, evpool: evpool,
evsw: tmevents.NewEventSwitch(), evsw: tmevents.NewEventSwitch(),
metrics: metrics, metrics: NopMetrics(),
} }
// set function defaults (may be overwritten before calling Start) // set function defaults (may be overwritten before calling Start)
cs.decideProposal = cs.defaultDecideProposal cs.decideProposal = cs.defaultDecideProposal
@ -147,6 +147,9 @@ func NewConsensusState(config *cfg.ConsensusConfig, state sm.State, blockExec *s
// We do that upon Start(). // We do that upon Start().
cs.reconstructLastCommit(state) cs.reconstructLastCommit(state)
cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs) cs.BaseService = *cmn.NewBaseService(nil, "ConsensusState", cs)
for _, option := range options {
option(cs)
}
return cs return cs
} }
@ -165,6 +168,13 @@ func (cs *ConsensusState) SetEventBus(b *types.EventBus) {
cs.blockExec.SetEventBus(b) 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. // String returns a string.
func (cs *ConsensusState) String() string { func (cs *ConsensusState) String() string {
// better not to access shared variables // better not to access shared variables

View File

@ -68,7 +68,7 @@ func WALWithNBlocks(numBlocks int) (data []byte, err error) {
mempool := sm.MockMempool{} mempool := sm.MockMempool{}
evpool := sm.MockEvidencePool{} evpool := sm.MockEvidencePool{}
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) 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.SetLogger(logger)
consensusState.SetEventBus(eventBus) consensusState.SetEventBus(eventBus)
if privValidator != nil { if privValidator != nil {

View File

@ -88,7 +88,7 @@ type Mempool struct {
} }
// NewMempool returns a new Mempool with the given configuration and connection to an application. // 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{ mempool := &Mempool{
config: config, config: config,
proxyAppConn: proxyAppConn, proxyAppConn: proxyAppConn,
@ -100,9 +100,12 @@ func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, he
recheckEnd: nil, recheckEnd: nil,
logger: log.NewNopLogger(), logger: log.NewNopLogger(),
cache: newTxCache(config.CacheSize), cache: newTxCache(config.CacheSize),
metrics: metrics, metrics: NopMetrics(),
} }
proxyAppConn.SetResponseCallback(mempool.resCb) proxyAppConn.SetResponseCallback(mempool.resCb)
for _, option := range options {
option(mempool)
}
return mempool return mempool
} }
@ -118,6 +121,13 @@ func (mem *Mempool) SetLogger(l log.Logger) {
mem.logger = l 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. // CloseWAL closes and discards the underlying WAL file.
// Any further writes will not be relayed to disk. // Any further writes will not be relayed to disk.
func (mem *Mempool) CloseWAL() bool { func (mem *Mempool) CloseWAL() bool {

View File

@ -33,7 +33,7 @@ func newMempoolWithApp(cc proxy.ClientCreator) *Mempool {
if err != nil { if err != nil {
panic(err) panic(err)
} }
mempool := NewMempool(config.Mempool, appConnMem, 0, NopMetrics()) mempool := NewMempool(config.Mempool, appConnMem, 0)
mempool.SetLogger(log.TestingLogger()) mempool.SetLogger(log.TestingLogger())
return mempool return mempool
} }
@ -241,7 +241,7 @@ func TestMempoolCloseWAL(t *testing.T) {
app := kvstore.NewKVStoreApplication() app := kvstore.NewKVStoreApplication()
cc := proxy.NewLocalClientCreator(app) cc := proxy.NewLocalClientCreator(app)
appConnMem, _ := cc.NewABCIClient() appConnMem, _ := cc.NewABCIClient()
mempool := NewMempool(wcfg, appConnMem, 10, NopMetrics()) mempool := NewMempool(wcfg, appConnMem, 10)
mempool.InitWAL() mempool.InitWAL()
// 4. Ensure that the directory contains the WAL file // 4. Ensure that the directory contains the WAL file

View File

@ -321,9 +321,10 @@ func NewNode(config *cfg.Config,
// Make MempoolReactor // Make MempoolReactor
mempoolLogger := logger.With("module", "mempool") mempoolLogger := logger.With("module", "mempool")
mempool := mempl.NewMempool(config.Mempool, proxyApp.Mempool(), state.LastBlockHeight, memplMetrics) mempool := mempl.NewMempool(config.Mempool, proxyApp.Mempool(), state.LastBlockHeight,
mempool.InitWAL() // no need to have the mempool wal during tests mempl.WithMetrics(memplMetrics))
mempool.SetLogger(mempoolLogger) mempool.SetLogger(mempoolLogger)
mempool.InitWAL() // no need to have the mempool wal during tests
mempoolReactor := mempl.NewMempoolReactor(config.Mempool, mempool) mempoolReactor := mempl.NewMempoolReactor(config.Mempool, mempool)
mempoolReactor.SetLogger(mempoolLogger) mempoolReactor.SetLogger(mempoolLogger)
@ -353,7 +354,7 @@ func NewNode(config *cfg.Config,
// Make ConsensusReactor // Make ConsensusReactor
consensusState := cs.NewConsensusState(config.Consensus, state.Copy(), consensusState := cs.NewConsensusState(config.Consensus, state.Copy(),
blockExec, blockStore, mempool, evidencePool, csMetrics) blockExec, blockStore, mempool, evidencePool, cs.WithMetrics(csMetrics))
consensusState.SetLogger(consensusLogger) consensusState.SetLogger(consensusLogger)
if privValidator != nil { if privValidator != nil {
consensusState.SetPrivValidator(privValidator) consensusState.SetPrivValidator(privValidator)
@ -363,7 +364,7 @@ func NewNode(config *cfg.Config,
p2pLogger := logger.With("module", "p2p") p2pLogger := logger.With("module", "p2p")
sw := p2p.NewSwitch(config.P2P, p2pMetrics) sw := p2p.NewSwitch(config.P2P, p2p.WithMetrics(p2pMetrics))
sw.SetLogger(p2pLogger) sw.SetLogger(p2pLogger)
sw.AddReactor("MEMPOOL", mempoolReactor) sw.AddReactor("MEMPOOL", mempoolReactor)
sw.AddReactor("BLOCKCHAIN", bcReactor) sw.AddReactor("BLOCKCHAIN", bcReactor)

View File

@ -78,7 +78,7 @@ type Switch struct {
} }
// NewSwitch creates a new Switch with the given config. // 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{ sw := &Switch{
config: cfg, config: cfg,
reactors: make(map[string]Reactor), reactors: make(map[string]Reactor),
@ -87,7 +87,7 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch {
peers: NewPeerSet(), peers: NewPeerSet(),
dialing: cmn.NewCMap(), dialing: cmn.NewCMap(),
reconnecting: cmn.NewCMap(), reconnecting: cmn.NewCMap(),
metrics: metrics, metrics: NopMetrics(),
} }
// Ensure we have a completely undeterministic PRNG. // Ensure we have a completely undeterministic PRNG.
@ -102,9 +102,21 @@ func NewSwitch(cfg *config.P2PConfig, metrics *Metrics) *Switch {
sw.mConfig = mConfig sw.mConfig = mConfig
sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw) sw.BaseService = *cmn.NewBaseService(nil, "P2P Switch", sw)
for _, option := range options {
option(sw)
}
return sw return sw
} }
// WithMetrics sets the metrics.
func WithMetrics(metrics *Metrics) func(*Switch) {
return func(sw *Switch) {
sw.metrics = metrics
}
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Switch setup // Switch setup

View File

@ -137,7 +137,7 @@ func MakeSwitch(cfg *config.P2PConfig, i int, network, version string, initSwitc
nodeKey := &NodeKey{ nodeKey := &NodeKey{
PrivKey: crypto.GenPrivKeyEd25519(), PrivKey: crypto.GenPrivKeyEd25519(),
} }
sw := NewSwitch(cfg, NopMetrics()) sw := NewSwitch(cfg)
sw.SetLogger(log.TestingLogger()) sw.SetLogger(log.TestingLogger())
sw = initSwitch(i, sw) sw = initSwitch(i, sw)
ni := NodeInfo{ ni := NodeInfo{