diff --git a/consensus/consensus.go b/consensus/consensus.go index 09bdef51a..b396f6ead 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -296,7 +296,7 @@ func New( // viewID has to be initialized as the height of // the blockchain during initialization as it was // displayed on explorer as Height right now - consensus.SetCurBlockViewID(0) + consensus.setCurBlockViewID(0) consensus.SlashChan = make(chan slash.Record) consensus.readySignal = make(chan ProposalType) consensus.commitSigChannel = make(chan []byte) diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 23f0b4751..cd15333a0 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -514,12 +514,14 @@ func (consensus *Consensus) setViewIDs(height uint64) { // SetCurBlockViewID set the current view ID func (consensus *Consensus) SetCurBlockViewID(viewID uint64) uint64 { - return consensus.current.SetCurBlockViewID(viewID) + consensus.mutex.Lock() + defer consensus.mutex.Unlock() + return consensus.setCurBlockViewID(viewID) } // SetCurBlockViewID set the current view ID -func (consensus *Consensus) setCurBlockViewID(viewID uint64) { - consensus.current.SetCurBlockViewID(viewID) +func (consensus *Consensus) setCurBlockViewID(viewID uint64) uint64 { + return consensus.current.SetCurBlockViewID(viewID) } // SetViewChangingID set the current view change ID diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index bc0d185bd..5beb54ed7 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -323,9 +323,7 @@ func (consensus *Consensus) Start( consensus.mutex.Unlock() }() - if consensus.dHelper != nil { - consensus.dHelper.start() - } + consensus.dHelper.start() } func (consensus *Consensus) StartChannel() { @@ -448,10 +446,6 @@ func (consensus *Consensus) BlockChannel(newBlock *types.Block) { Msg("[ConsensusMainLoop] STARTING CONSENSUS") consensus.announce(newBlock) }) - - if consensus.dHelper != nil { - consensus.dHelper.start() - } } // LastMileBlockIter is the iterator to iterate over the last mile blocks in consensus cache. diff --git a/consensus/downloader.go b/consensus/downloader.go index 26755bbd2..1fdc131e7 100644 --- a/consensus/downloader.go +++ b/consensus/downloader.go @@ -39,7 +39,7 @@ func newDownloadHelper(c *Consensus, d downloader) *downloadHelper { finishedCh := make(chan struct{}, 1) finishedSub := d.SubscribeDownloadFinished(finishedCh) - return &downloadHelper{ + out := &downloadHelper{ c: c, d: d, startedCh: startedCh, @@ -47,16 +47,12 @@ func newDownloadHelper(c *Consensus, d downloader) *downloadHelper { startedSub: startedSub, finishedSub: finishedSub, } + go out.downloadStartedLoop() + go out.downloadFinishedLoop() + return out } func (dh *downloadHelper) start() { - go dh.downloadStartedLoop() - go dh.downloadFinishedLoop() -} - -func (dh *downloadHelper) close() { - dh.startedSub.Unsubscribe() - dh.finishedSub.Unsubscribe() } func (dh *downloadHelper) downloadStartedLoop() { @@ -107,21 +103,10 @@ func (consensus *Consensus) AddConsensusLastMile() error { } func (consensus *Consensus) spinUpStateSync() { - if consensus.dHelper != nil { - consensus.dHelper.d.DownloadAsync() - consensus.current.SetMode(Syncing) - for _, v := range consensus.consensusTimeout { - v.Stop() - } - } else { - select { - case consensus.BlockNumLowChan <- struct{}{}: - consensus.current.SetMode(Syncing) - for _, v := range consensus.consensusTimeout { - v.Stop() - } - default: - } + consensus.dHelper.d.DownloadAsync() + consensus.current.SetMode(Syncing) + for _, v := range consensus.consensusTimeout { + v.Stop() } }