Browse Source

friendly apis for constructors

pull/1737/head
Anton Kaliaev 7 years ago
parent
commit
84812145cb
No known key found for this signature in database GPG Key ID: 7B6881D965918214
11 changed files with 53 additions and 20 deletions
  1. +1
    -1
      blockchain/reactor_test.go
  2. +1
    -1
      consensus/byzantine_test.go
  3. +2
    -2
      consensus/common_test.go
  4. +2
    -2
      consensus/replay_file.go
  5. +12
    -2
      consensus/state.go
  6. +1
    -1
      consensus/wal_generator.go
  7. +12
    -2
      mempool/mempool.go
  8. +2
    -2
      mempool/mempool_test.go
  9. +5
    -4
      node/node.go
  10. +14
    -2
      p2p/switch.go
  11. +1
    -1
      p2p/test_util.go

+ 1
- 1
blockchain/reactor_test.go View File

@ -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++ {


+ 1
- 1
consensus/byzantine_test.go View File

@ -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))
}


+ 2
- 2
consensus/common_test.go View File

@ -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)


+ 2
- 2
consensus/replay_file.go View File

@ -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


+ 12
- 2
consensus/state.go View File

@ -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


+ 1
- 1
consensus/wal_generator.go View File

@ -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 {


+ 12
- 2
mempool/mempool.go View File

@ -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 {


+ 2
- 2
mempool/mempool_test.go View File

@ -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


+ 5
- 4
node/node.go View File

@ -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)


+ 14
- 2
p2p/switch.go View File

@ -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


+ 1
- 1
p2p/test_util.go View File

@ -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{


Loading…
Cancel
Save