refactor similar code out as IsQuorumAchievedByMask

pull/1926/head
Chao Ma 5 years ago
parent 925afa59ee
commit 0b2f4d137b
  1. 29
      consensus/consensus_v2.go
  2. 21
      consensus/quorum/one-node-one-vote.go
  3. 22
      consensus/quorum/one-node-staked-vote.go
  4. 2
      consensus/quorum/quorum.go
  5. 23
      consensus/view_change.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)

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

@ -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 &currentTotalPower, 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 {

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

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

Loading…
Cancel
Save