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

Improve Consensus logic for Multi-BLS Validators with quorum #4799

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
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
42 changes: 37 additions & 5 deletions consensus/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ func (consensus *Consensus) announce(block *types.Block) {
consensus.switchPhase("Announce", FBFTPrepare)
}

func (consensus *Consensus) checkFirstReceivedSignature(signerCount int64, phase quorum.Phase) (bool, bool) {
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(phase, myPubkeys)
return true, signerCount == mySignsCount
}
return false, false
}

// this method is called for each validator sent their vote message
func (consensus *Consensus) onPrepare(recvMsg *FBFTMessage) {
// TODO(audit): make FBFT lookup using map instead of looping through all items.
Expand Down Expand Up @@ -121,14 +134,21 @@ func (consensus *Consensus) onPrepare(recvMsg *FBFTMessage) {
}
}

if consensus.decider.IsQuorumAchieved(quorum.Prepare) {
signerCount := consensus.decider.SignersCount(quorum.Prepare)

// check if it is first received signatures
// it may multi bls key validators can achieve quorum on first signature
hasMultiBlsKeys, isFirstReceivedSignature := consensus.checkFirstReceivedSignature(signerCount, quorum.Prepare)

quorumPreExisting := consensus.decider.IsQuorumAchieved(quorum.Prepare)
//// Read - End

if quorumPreExisting {
// already have enough signatures
consensus.getLogger().Debug().
Interface("validatorPubKeys", recvMsg.SenderPubkeys).
Msg("[OnPrepare] Received Additional Prepare Message")
return
}
signerCount := consensus.decider.SignersCount(quorum.Prepare)
//// Read - End

consensus.UpdateLeaderMetrics(float64(signerCount), float64(consensus.getBlockNum()))
Expand Down Expand Up @@ -183,7 +203,11 @@ func (consensus *Consensus) onPrepare(recvMsg *FBFTMessage) {
//// Write - End

//// Read - Start
if consensus.decider.IsQuorumAchieved(quorum.Prepare) {
quorumFromInitialSignature := hasMultiBlsKeys && isFirstReceivedSignature && quorumPreExisting
quorumPostNewSignatures := consensus.decider.IsQuorumAchieved(quorum.Prepare)
quorumFromNewSignatures := !quorumPreExisting && quorumPostNewSignatures

if quorumFromInitialSignature || quorumFromNewSignatures {
Comment on lines +206 to +210
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old code was fine, as we just need consensus.didReachPrepareQuorum() to hit once

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed return. So, it allows adding new signatures in Prepare Phase despite existing quorum. I think this would fix the issue

// NOTE Let it handle its own logs
if err := consensus.didReachPrepareQuorum(); err != nil {
return
Expand Down Expand Up @@ -216,6 +240,11 @@ 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
hasMultiBlsKeys, isFirstReceivedSignature := consensus.checkFirstReceivedSignature(signerCount, quorum.Commit)

//// Read - End

// Verify the signature on commitPayload is correct
Expand Down Expand Up @@ -290,7 +319,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 @@ -101,6 +101,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 @@ -428,6 +429,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 @@ -104,6 +104,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