diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 9ff6b8bd2..8e73f654e 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -506,20 +506,11 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) { return } - threshold := consensus.Decider.QuorumThreshold() - currentTotalPower := consensus.Decider.ComputeTotalPowerByMask(mask) - if currentTotalPower == nil { + if !consensus.Decider.IsQuorumAchievedByMask(mask) { utils.Logger().Warn(). - Msgf("[OnPrepared] currentTotalPower is NIL") + Msgf("[OnPrepared] Quorum Not achieved") return } - if (*currentTotalPower).LT(threshold) { - utils.Logger().Warn(). - Msgf("[OnPrepared] Not enough voting power in prepared msg: need %+v, have %+v", threshold, currentTotalPower) - return - } - utils.Logger().Debug(). - Msgf("[onPrepared] prepared totalvoting power exceed threshold: need %+v, have %+v", threshold, currentTotalPower) if !aggSig.VerifyHash(mask.AggregatePublic, blockHash[:]) { myBlockHash := common.Hash{} @@ -899,23 +890,11 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) { return } - threshold := consensus.Decider.QuorumThreshold() - currentTotalPower := consensus.Decider.ComputeTotalPowerByMask(mask) - if currentTotalPower == nil { - utils.Logger().Warn(). - Msgf("[OnCommitted] currentTotalPower is NIL") - return - } - - if (*currentTotalPower).LT(threshold) { + if !consensus.Decider.IsQuorumAchievedByMask(mask) { utils.Logger().Warn(). - Msgf("[OnCommitted] Not enough voting power in committed msg: need %+v, have %+v", threshold, currentTotalPower) + Msgf("[OnCommitted] Quorum Not achieved") return } - utils.Logger().Debug(). - Msgf("[OnCommitted] committed totalvoting power exceed threshold: need %+v, have %+v", threshold, currentTotalPower) - - // check has 2f+1 signatures blockNumBytes := make([]byte, 8) binary.LittleEndian.PutUint64(blockNumBytes, recvMsg.BlockNum) diff --git a/consensus/quorum/one-node-one-vote.go b/consensus/quorum/one-node-one-vote.go index 3152a1807..364a198ca 100644 --- a/consensus/quorum/one-node-one-vote.go +++ b/consensus/quorum/one-node-one-vote.go @@ -34,6 +34,25 @@ func (v *uniformVoteWeight) IsQuorumAchieved(p Phase) bool { return r } +// IsQuorumAchivedByMask .. +func (v *uniformVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool { + threshold := v.QuorumThreshold() + currentTotalPower := v.computeTotalPowerByMask(mask) + if currentTotalPower == nil { + utils.Logger().Warn(). + Msgf("[IsQuorumAchievedByMask] currentTotalPower is nil") + return false + } + if (*currentTotalPower).LT(threshold) { + utils.Logger().Warn(). + Msgf("[IsQuorumAchievedByMask] Not enough voting power: need %+v, have %+v", threshold, currentTotalPower) + return false + } + utils.Logger().Debug(). + Msgf("[IsQuorumAchievedByMask] have enough voting power: need %+v, have %+v", threshold, currentTotalPower) + return true +} + // QuorumThreshold .. func (v *uniformVoteWeight) QuorumThreshold() numeric.Dec { return numeric.NewDec(v.TwoThirdsSignersCount()) @@ -45,7 +64,7 @@ func (v *uniformVoteWeight) IsRewardThresholdAchieved() bool { } // ComputeTotalPowerByMask computes the total power indicated by bitmap mask -func (v *uniformVoteWeight) ComputeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec { +func (v *uniformVoteWeight) computeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec { counts := utils.CountOneBits(mask.Bitmap) dec := numeric.NewDec(counts) return &dec diff --git a/consensus/quorum/one-node-staked-vote.go b/consensus/quorum/one-node-staked-vote.go index c7bd3879b..590d7242d 100644 --- a/consensus/quorum/one-node-staked-vote.go +++ b/consensus/quorum/one-node-staked-vote.go @@ -63,6 +63,25 @@ func (v *stakedVoteWeight) IsQuorumAchieved(p Phase) bool { return currentTotalPower.GT(t) } +// IsQuorumAchivedByMask .. +func (v *stakedVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool { + threshold := v.QuorumThreshold() + currentTotalPower := v.computeTotalPowerByMask(mask) + if currentTotalPower == nil { + utils.Logger().Warn(). + Msgf("[IsQuorumAchievedByMask] currentTotalPower is nil") + return false + } + if (*currentTotalPower).LT(threshold) { + utils.Logger().Warn(). + Msgf("[IsQuorumAchievedByMask] Not enough voting power: need %+v, have %+v", threshold, currentTotalPower) + return false + } + utils.Logger().Debug(). + Msgf("[IsQuorumAchievedByMask] have enough voting power: need %+v, have %+v", threshold, currentTotalPower) + return true +} + func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) (*numeric.Dec, error) { w := shard.BlsPublicKey{} members := v.Participants() @@ -79,12 +98,11 @@ func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) (*numeric.Dec, erro ) } } - return ¤tTotalPower, nil } // ComputeTotalPowerByMask computes the total power indicated by bitmap mask -func (v *stakedVoteWeight) ComputeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec { +func (v *stakedVoteWeight) computeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec { currentTotalPower := numeric.ZeroDec() pubKeys := mask.GetPubKeyFromMask(true) for _, key := range pubKeys { diff --git a/consensus/quorum/quorum.go b/consensus/quorum/quorum.go index 80e32e995..dd5177af7 100644 --- a/consensus/quorum/quorum.go +++ b/consensus/quorum/quorum.go @@ -114,7 +114,7 @@ type Decider interface { SetVoters(shard.SlotList) (*TallyResult, error) Policy() Policy IsQuorumAchieved(Phase) bool - ComputeTotalPowerByMask(*bls_cosi.Mask) *numeric.Dec + IsQuorumAchievedByMask(*bls_cosi.Mask) bool QuorumThreshold() numeric.Dec AmIMemberOfCommitee() bool IsRewardThresholdAchieved() bool diff --git a/consensus/view_change.go b/consensus/view_change.go index 29f5ea953..cb9443ec5 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -282,17 +282,9 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) { return } - threshold := consensus.Decider.QuorumThreshold() - currentTotalPower := consensus.Decider.ComputeTotalPowerByMask(mask) - if currentTotalPower == nil { + if !consensus.Decider.IsQuorumAchievedByMask(mask) { utils.Logger().Warn(). - Msgf("[onViewChange] currentTotalPower is NIL") - return - } - - if (*currentTotalPower).LT(threshold) { - utils.Logger().Warn(). - Msgf("[onViewChange] Not enough voting power in committed msg: need %+v, have %+v", threshold, currentTotalPower) + Msgf("[onViewChange] Quorum Not achieved") return } @@ -453,16 +445,9 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) { viewIDBytes := make([]byte, 8) binary.LittleEndian.PutUint64(viewIDBytes, recvMsg.ViewID) - threshold := consensus.Decider.QuorumThreshold() - currentTotalPower := consensus.Decider.ComputeTotalPowerByMask(m3Mask) - if currentTotalPower == nil { - utils.Logger().Warn(). - Msgf("[onNewView] currentTotalPower is NIL") - return - } - if (*currentTotalPower).LT(threshold) { + if !consensus.Decider.IsQuorumAchievedByMask(m3Mask) { utils.Logger().Warn(). - Msgf("[onNewView] Not enough voting power in M3 (ViewID) msg: need %+v, have %+v", threshold, currentTotalPower) + Msgf("[onNewView] Quorum Not achieved") return }