Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Consensus Quorum Calculation for Multi-BLS Key Validators #4799

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion consensus/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ func (consensus *Consensus) onCommit(recvMsg *FBFTMessage) {
quorumWasMet := consensus.decider.IsQuorumAchieved(quorum.Commit)

signerCount := consensus.decider.SignersCount(quorum.Commit)

// check if it is first received commit
// it may multi bls key validators can achieve quorum on first commit
isFirstReceivedSignature := false
hasMultiBlsKeys := len(consensus.priKey) > 0
if hasMultiBlsKeys {
var myPubkeys []bls.SerializedPublicKey
for _, key := range consensus.priKey {
myPubkeys = append(myPubkeys, key.Pub.Bytes)
}
mySignsCount := consensus.decider.GetBallotsCount(quorum.Commit, myPubkeys)
isFirstReceivedSignature = signerCount == mySignsCount
}

//// Read - End

// Verify the signature on commitPayload is correct
Expand Down Expand Up @@ -290,7 +304,10 @@ func (consensus *Consensus) onCommit(recvMsg *FBFTMessage) {
quorumIsMet := consensus.decider.IsQuorumAchieved(quorum.Commit)
//// Read - End

if !quorumWasMet && quorumIsMet {
quorumAchievedByFirstCommit := hasMultiBlsKeys && isFirstReceivedSignature && quorumWasMet
quorumAchievedByThisCommit := !quorumWasMet && quorumIsMet

if quorumAchievedByFirstCommit || quorumAchievedByThisCommit {
logger.Info().Msg("[OnCommit] 2/3 Enough commits received")
consensus.fBFTLog.MarkBlockVerified(blockObj)

Expand Down
23 changes: 23 additions & 0 deletions consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type SignatoryTracker interface {
type SignatureReader interface {
SignatoryTracker
ReadBallot(p Phase, pubkey bls.SerializedPublicKey) *votepower.Ballot
GetBallotsCount(p Phase, pubkeys []bls.SerializedPublicKey) int64
TwoThirdsSignersCount() int64
// 96 bytes aggregated signature
AggregateVotes(p Phase) *bls_core.Sign
Expand Down Expand Up @@ -388,6 +389,28 @@ func (s *cIdentities) ReadBallot(p Phase, pubkey bls.SerializedPublicKey) *votep
return payload
}

func (s *cIdentities) GetBallotsCount(p Phase, pubkeys []bls.SerializedPublicKey) int64 {
ballotBox := map[bls.SerializedPublicKey]*votepower.Ballot{}

switch p {
case Prepare:
ballotBox = s.prepare.BallotBox
case Commit:
ballotBox = s.commit.BallotBox
case ViewChange:
ballotBox = s.viewChange.BallotBox
}

count := int64(0)
for _, pubkey := range pubkeys {
_, ok := ballotBox[pubkey]
if ok {
count++
}
}
return count
}

func (s *cIdentities) ReadAllBallots(p Phase) []*votepower.Ballot {
m := map[bls.SerializedPublicKey]*votepower.Ballot{}
switch p {
Expand Down
6 changes: 6 additions & 0 deletions consensus/quorum/thread_safe_decider.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func (a threadSafeDeciderImpl) ReadBallot(p Phase, pubkey bls.SerializedPublicKe
return a.decider.ReadBallot(p, pubkey)
}

func (a threadSafeDeciderImpl) GetBallotsCount(p Phase, pubkeys []bls.SerializedPublicKey) int64 {
a.mu.Lock()
defer a.mu.Unlock()
return a.decider.GetBallotsCount(p, pubkeys)
}

func (a threadSafeDeciderImpl) TwoThirdsSignersCount() int64 {
a.mu.Lock()
defer a.mu.Unlock()
Expand Down