Merge branch 'master' of github.com:harmony-one/harmony into staking_devnet_fixes

pull/1927/head
Rongjian Lan 5 years ago
commit cb32737c01
  1. 29
      consensus/consensus_v2.go
  2. 15
      consensus/quorum/one-node-one-vote.go
  3. 38
      consensus/quorum/one-node-staked-vote.go
  4. 2
      consensus/quorum/quorum.go
  5. 18
      consensus/view_change.go

@ -505,14 +505,13 @@ func (consensus *Consensus) onPrepared(msg *msg_pb.Message) {
utils.Logger().Error().Err(err).Msg("ReadSignatureBitmapPayload failed!!") utils.Logger().Error().Err(err).Msg("ReadSignatureBitmapPayload failed!!")
return return
} }
prepareCount := consensus.Decider.SignersCount(quorum.Prepare)
if count := utils.CountOneBits(mask.Bitmap); count < prepareCount { if !consensus.Decider.IsQuorumAchievedByMask(mask) {
utils.Logger().Debug(). utils.Logger().Warn().
Int64("Need", prepareCount). Msgf("[OnPrepared] Quorum Not achieved")
Int64("Got", count).
Msg("Not enough signatures in the Prepared msg")
return return
} }
if !aggSig.VerifyHash(mask.AggregatePublic, blockHash[:]) { if !aggSig.VerifyHash(mask.AggregatePublic, blockHash[:]) {
myBlockHash := common.Hash{} myBlockHash := common.Hash{}
myBlockHash.SetBytes(consensus.blockHash[:]) myBlockHash.SetBytes(consensus.blockHash[:])
@ -892,22 +891,12 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
return return
} }
switch consensus.Decider.Policy() { if !consensus.Decider.IsQuorumAchievedByMask(mask) {
case quorum.SuperMajorityVote: utils.Logger().Warn().
threshold := consensus.Decider.TwoThirdsSignersCount() Msgf("[OnCommitted] Quorum Not achieved")
if count := utils.CountOneBits(mask.Bitmap); int64(count) < threshold { return
utils.Logger().Warn().
Int64("need", threshold).
Int64("got", count).
Msg("[OnCommitted] Not enough signature in committed msg")
return
}
case quorum.SuperMajorityStake:
// Come back to thinking about this situation for Bitmap
} }
// check has 2f+1 signatures
blockNumBytes := make([]byte, 8) blockNumBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(blockNumBytes, recvMsg.BlockNum) binary.LittleEndian.PutUint64(blockNumBytes, recvMsg.BlockNum)
commitPayload := append(blockNumBytes, recvMsg.BlockHash[:]...) commitPayload := append(blockNumBytes, recvMsg.BlockHash[:]...)

@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
@ -33,6 +34,20 @@ func (v *uniformVoteWeight) IsQuorumAchieved(p Phase) bool {
return r return r
} }
// IsQuorumAchivedByMask ..
func (v *uniformVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool {
threshold := v.TwoThirdsSignersCount()
currentTotalPower := utils.CountOneBits(mask.Bitmap)
if currentTotalPower < 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 .. // QuorumThreshold ..
func (v *uniformVoteWeight) QuorumThreshold() numeric.Dec { func (v *uniformVoteWeight) QuorumThreshold() numeric.Dec {
return numeric.NewDec(v.TwoThirdsSignersCount()) return numeric.NewDec(v.TwoThirdsSignersCount())

@ -5,6 +5,7 @@ import (
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/consensus/votepower" "github.com/harmony-one/harmony/consensus/votepower"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
@ -62,6 +63,25 @@ func (v *stakedVoteWeight) IsQuorumAchieved(p Phase) bool {
return currentTotalPower.GT(t) 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) { func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) (*numeric.Dec, error) {
w := shard.BlsPublicKey{} w := shard.BlsPublicKey{}
members := v.Participants() members := v.Participants()
@ -78,10 +98,26 @@ func (v *stakedVoteWeight) computeCurrentTotalPower(p Phase) (*numeric.Dec, erro
) )
} }
} }
return &currentTotalPower, nil return &currentTotalPower, nil
} }
// ComputeTotalPowerByMask computes the total power indicated by bitmap mask
func (v *stakedVoteWeight) computeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec {
currentTotalPower := numeric.ZeroDec()
pubKeys := mask.GetPubKeyFromMask(true)
for _, key := range pubKeys {
w := shard.BlsPublicKey{}
err := w.FromLibBLSPublicKey(key)
if err != nil {
return nil
}
currentTotalPower = currentTotalPower.Add(
v.roster.Voters[w].EffectivePercent,
)
}
return &currentTotalPower
}
// QuorumThreshold .. // QuorumThreshold ..
func (v *stakedVoteWeight) QuorumThreshold() numeric.Dec { func (v *stakedVoteWeight) QuorumThreshold() numeric.Dec {
return twoThird return twoThird

@ -5,6 +5,7 @@ import (
"github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/consensus/votepower" "github.com/harmony-one/harmony/consensus/votepower"
bls_cosi "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/numeric"
"github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/shard"
"github.com/harmony-one/harmony/staking/slash" "github.com/harmony-one/harmony/staking/slash"
@ -113,6 +114,7 @@ type Decider interface {
SetVoters(shard.SlotList) (*TallyResult, error) SetVoters(shard.SlotList) (*TallyResult, error)
Policy() Policy Policy() Policy
IsQuorumAchieved(Phase) bool IsQuorumAchieved(Phase) bool
IsQuorumAchievedByMask(*bls_cosi.Mask) bool
QuorumThreshold() numeric.Dec QuorumThreshold() numeric.Dec
AmIMemberOfCommitee() bool AmIMemberOfCommitee() bool
IsRewardThresholdAchieved() bool IsRewardThresholdAchieved() bool

@ -281,11 +281,10 @@ func (consensus *Consensus) onViewChange(msg *msg_pb.Message) {
utils.Logger().Error().Err(err).Msg("[onViewChange] M1 RecvMsg Payload Read Error") utils.Logger().Error().Err(err).Msg("[onViewChange] M1 RecvMsg Payload Read Error")
return return
} }
// check has 2f+1 signature in m1 type message
need := consensus.Decider.TwoThirdsSignersCount() if !consensus.Decider.IsQuorumAchievedByMask(mask) {
if count := utils.CountOneBits(mask.Bitmap); count < need { utils.Logger().Warn().
utils.Logger().Debug().Int64("need", need).Int64("have", count). Msgf("[onViewChange] Quorum Not achieved")
Msg("[onViewChange] M1 Payload Not Have Enough Signature")
return return
} }
@ -445,11 +444,10 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
viewIDBytes := make([]byte, 8) viewIDBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(viewIDBytes, recvMsg.ViewID) binary.LittleEndian.PutUint64(viewIDBytes, recvMsg.ViewID)
// check total number of sigs >= 2f+1
need := consensus.Decider.TwoThirdsSignersCount() if !consensus.Decider.IsQuorumAchievedByMask(m3Mask) {
if count := utils.CountOneBits(m3Mask.Bitmap); count < need { utils.Logger().Warn().
utils.Logger().Debug().Int64("need", need).Int64("have", count). Msgf("[onNewView] Quorum Not achieved")
Msg("[onNewView] Not Have Enough M3 (ViewID) Signature")
return return
} }

Loading…
Cancel
Save