Commit 0bdc5921 authored by vipwzw's avatar vipwzw

fix reset timer

parent 29677ba5
...@@ -193,14 +193,11 @@ func (cs *ConsensusState) Stop() { ...@@ -193,14 +193,11 @@ func (cs *ConsensusState) Stop() {
// Attempt to reset the timer // Attempt to reset the timer
func (cs *ConsensusState) resetTimer(duration time.Duration, stateType int) { func (cs *ConsensusState) resetTimer(duration time.Duration, stateType int) {
dposlog.Info("set timer", "duration", duration, "state", StateTypeMapping[stateType]) dposlog.Info("set timer", "duration", duration, "state", StateTypeMapping[stateType])
cs.timer.Reset(duration)
}
// Attempt to reset the timer
func (cs *ConsensusState) stopAndResetTimer(duration time.Duration, stateType int) {
dposlog.Info("set timer", "duration", duration, "state", StateTypeMapping[stateType])
if !cs.timer.Stop() { if !cs.timer.Stop() {
<-cs.timer.C select {
case <-cs.timer.C:
default:
}
} }
cs.timer.Reset(duration) cs.timer.Reset(duration)
} }
......
...@@ -529,7 +529,7 @@ func (voting *VotingState) recvVote(cs *ConsensusState, vote *dpostype.DPosVote) ...@@ -529,7 +529,7 @@ func (voting *VotingState) recvVote(cs *ConsensusState, vote *dpostype.DPosVote)
} }
} }
//1s后检查是否出块,是否需要重新投票 //1s后检查是否出块,是否需要重新投票
cs.stopAndResetTimer(time.Millisecond*500, VotedStateType) cs.resetTimer(time.Millisecond*500, VotedStateType)
} else if result == continueToVote { } else if result == continueToVote {
dposlog.Info("VotingState get a vote, but don't get an agreement,waiting for new votes...") dposlog.Info("VotingState get a vote, but don't get an agreement,waiting for new votes...")
} else { } else {
...@@ -538,7 +538,7 @@ func (voting *VotingState) recvVote(cs *ConsensusState, vote *dpostype.DPosVote) ...@@ -538,7 +538,7 @@ func (voting *VotingState) recvVote(cs *ConsensusState, vote *dpostype.DPosVote)
cs.ClearVotes() cs.ClearVotes()
cs.SetState(InitStateObj) cs.SetState(InitStateObj)
dposlog.Info("Change state because of vote failed.", "from", "VotingState", "to", "InitState") dposlog.Info("Change state because of vote failed.", "from", "VotingState", "to", "InitState")
cs.stopAndResetTimer(time.Duration(timeoutCheckConnections)*time.Millisecond, InitStateType) cs.resetTimer(time.Duration(timeoutCheckConnections)*time.Millisecond, InitStateType)
} }
} }
...@@ -776,7 +776,7 @@ func (voted *VotedState) recvNotify(cs *ConsensusState, notify *dpostype.DPosNot ...@@ -776,7 +776,7 @@ func (voted *VotedState) recvNotify(cs *ConsensusState, notify *dpostype.DPosNot
cs.ClearVotes() cs.ClearVotes()
cs.SetState(WaitNotifyStateObj) cs.SetState(WaitNotifyStateObj)
dposlog.Info("Change state because of recv notify.", "from", "VotedState", "to", "WaitNotifyState") dposlog.Info("Change state because of recv notify.", "from", "VotedState", "to", "WaitNotifyState")
cs.stopAndResetTimer(time.Duration(timeoutWaitNotify)*time.Millisecond, WaitNotifyStateType) cs.resetTimer(time.Duration(timeoutWaitNotify)*time.Millisecond, WaitNotifyStateType)
if cs.cachedNotify != nil { if cs.cachedNotify != nil {
cs.dposState.recvNotify(cs, cs.cachedNotify) cs.dposState.recvNotify(cs, cs.cachedNotify)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment