Rebased over leader rotation.

pull/4509/head
frozen 2 years ago committed by Casey Gardiner
parent 53dfc9d357
commit 31a937474b
  1. 18
      consensus/consensus_service.go
  2. 18
      consensus/consensus_v2.go
  3. 2
      consensus/view_change.go

@ -459,11 +459,7 @@ func (consensus *Consensus) updateConsensusInformation() Mode {
// IsLeader check if the node is a leader or not by comparing the public key of
// the node with the leader public key
func (consensus *Consensus) IsLeader() bool {
_ = utils.AssertNoLongerThan0(5*time.Second, func() error {
consensus.mutex.RLock()
return nil
})
consensus.mutex.RLock()
defer consensus.mutex.RUnlock()
return consensus.isLeader()
@ -481,18 +477,6 @@ func (consensus *Consensus) isLeader() bool {
return false
}
// isLeader check if the node is a leader or not by comparing the public key of
// the node with the leader public key. This function assume it runs under lock.
func (consensus *Consensus) isLeader() bool {
obj := consensus.LeaderPubKey.Object
for _, key := range consensus.priKey {
if key.Pub.Object.IsEqual(obj) {
return true
}
}
return false
}
// SetViewIDs set both current view ID and view changing ID to the height
// of the blockchain. It is used during client startup to recover the state
func (consensus *Consensus) SetViewIDs(height uint64) {

@ -460,16 +460,6 @@ func (consensus *Consensus) BlockChannel(newBlock *types.Block) {
}
}
// Close closes the consensus. If current is in normal commit phase, wait until the commit
// phase end.
func (consensus *Consensus) Close() error {
if consensus.dHelper != nil {
consensus.dHelper.close()
}
consensus.waitForCommit()
return nil
}
// waitForCommit wait extra 2 seconds for commit phase to finish
func (consensus *Consensus) waitForCommit() {
if consensus.Mode() != Normal || consensus.phase.Get() != FBFTCommit {
@ -717,11 +707,11 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess
// rotateLeader rotates the leader to the next leader in the committee.
// This function must be called with enabled leader rotation.
func (consensus *Consensus) rotateLeader(epoch *big.Int) {
prev := consensus.GetLeaderPubKey()
prev := consensus.getLeaderPubKey()
bc := consensus.Blockchain()
curNumber := bc.CurrentHeader().Number().Uint64()
utils.Logger().Info().Msgf("[Rotating leader] epoch: %v rotation:%v numblocks:%d", epoch.Uint64(), bc.Config().IsLeaderRotation(epoch), bc.Config().LeaderRotationBlocksCount)
leader := consensus.GetLeaderPubKey()
leader := consensus.getLeaderPubKey()
for i := 0; i < bc.Config().LeaderRotationBlocksCount; i++ {
header := bc.GetHeaderByNumber(curNumber - uint64(i))
if header == nil {
@ -748,9 +738,9 @@ func (consensus *Consensus) rotateLeader(epoch *big.Int) {
utils.Logger().Error().Msg("Failed to get next leader")
return
} else {
consensus.SetLeaderPubKey(next)
consensus.setLeaderPubKey(next)
}
if consensus.IsLeader() && !consensus.GetLeaderPubKey().Object.IsEqual(prev.Object) {
if consensus.isLeader() && !consensus.getLeaderPubKey().Object.IsEqual(prev.Object) {
// leader changed
go func() {
consensus.ReadySignal <- SyncProposal

@ -340,7 +340,7 @@ func (consensus *Consensus) startNewView(viewID uint64, newLeaderPriKey *bls.Pri
if reset {
consensus.resetState()
}
consensus.SetLeaderPubKey(newLeaderPriKey.Pub)
consensus.setLeaderPubKey(newLeaderPriKey.Pub)
return nil
}

Loading…
Cancel
Save