Skip to content

Commit

Permalink
Revert "[viewchange] fix getNextLeader"
Browse files Browse the repository at this point in the history
This reverts commit 9dc8f43.
  • Loading branch information
Leo Chen committed Sep 24, 2020
1 parent 5260aee commit aba83d4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions consensus/consensus_viewchange_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// construct the view change message
func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWrapper, nextLeaderPubKey *bls.PublicKeyWrapper) []byte {
func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWrapper) []byte {
message := &msg_pb.Message{
ServiceType: msg_pb.ServiceType_CONSENSUS,
Type: msg_pb.MessageType_VIEWCHANGE,
Expand All @@ -23,7 +23,7 @@ func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWra
BlockNum: consensus.blockNum,
ShardId: consensus.ShardID,
SenderPubkey: priKey.Pub.Bytes[:],
LeaderPubkey: nextLeaderPubKey.Bytes[:],
LeaderPubkey: consensus.LeaderPubKey.Bytes[:],
},
},
}
Expand Down
6 changes: 3 additions & 3 deletions consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type ParticipantTracker interface {
Participants() multibls.PublicKeys
IndexOf(bls.SerializedPublicKey) int
ParticipantsCount() int64
NextAfter(*bls.PublicKeyWrapper, int) (bool, *bls.PublicKeyWrapper)
NextAfter(*bls.PublicKeyWrapper) (bool, *bls.PublicKeyWrapper)
UpdateParticipants(pubKeys []bls.PublicKeyWrapper)
}

Expand Down Expand Up @@ -187,14 +187,14 @@ func (s *cIdentities) IndexOf(pubKey bls.SerializedPublicKey) int {
return -1
}

func (s *cIdentities) NextAfter(pubKey *bls.PublicKeyWrapper, next int) (bool, *bls.PublicKeyWrapper) {
func (s *cIdentities) NextAfter(pubKey *bls.PublicKeyWrapper) (bool, *bls.PublicKeyWrapper) {
found := false

idx := s.IndexOf(pubKey.Bytes)
if idx != -1 {
found = true
}
idx = (idx + next) % int(s.ParticipantsCount())
idx = (idx + 1) % int(s.ParticipantsCount())
return found, &s.publicKeys[idx]
}

Expand Down
16 changes: 7 additions & 9 deletions consensus/view_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,8 @@ func (consensus *Consensus) switchPhase(desired FBFTPhase, override bool) {
}

// GetNextLeaderKey uniquely determine who is the leader for given viewID
func (consensus *Consensus) GetNextLeaderKey(viewID uint64) *bls.PublicKeyWrapper {
gap := 1
if viewID > consensus.GetCurBlockViewID() {
gap = int(viewID - consensus.GetCurBlockViewID())
}
wasFound, next := consensus.Decider.NextAfter(consensus.LeaderPubKey, gap)
func (consensus *Consensus) GetNextLeaderKey() *bls.PublicKeyWrapper {
wasFound, next := consensus.Decider.NextAfter(consensus.LeaderPubKey)
if !wasFound {
consensus.getLogger().Warn().
Str("key", consensus.LeaderPubKey.Bytes.Hex()).
Expand Down Expand Up @@ -167,20 +163,20 @@ func (consensus *Consensus) startViewChange(viewID uint64) {
consensus.consensusTimeout[timeoutBootstrap].Stop()
consensus.current.SetMode(ViewChanging)
consensus.SetViewChangingID(viewID)
nextLeaderPubKey := consensus.GetNextLeaderKey(viewID)
consensus.LeaderPubKey = consensus.GetNextLeaderKey()

duration := consensus.current.GetViewChangeDuraion()
consensus.getLogger().Warn().
Uint64("viewChangingID", consensus.GetViewChangingID()).
Dur("timeoutDuration", duration).
Str("NextLeader", nextLeaderPubKey.Bytes.Hex()).
Str("NextLeader", consensus.LeaderPubKey.Bytes.Hex()).
Msg("[startViewChange]")

for _, key := range consensus.priKey {
if !consensus.IsValidatorInCommittee(key.Pub.Bytes) {
continue
}
msgToSend := consensus.constructViewChangeMessage(&key, nextLeaderPubKey)
msgToSend := consensus.constructViewChangeMessage(&key)
consensus.host.SendMessageToGroups([]nodeconfig.GroupID{
nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)),
},
Expand Down Expand Up @@ -684,6 +680,8 @@ func (consensus *Consensus) onNewView(msg *msg_pb.Message) {
consensus.getLogger().Info().
Str("newLeaderKey", consensus.LeaderPubKey.Bytes.Hex()).
Msg("new leader changed")
consensus.getLogger().Info().
Msg("validator start consensus timer and stop view change timer")
consensus.consensusTimeout[timeoutConsensus].Start()
consensus.consensusTimeout[timeoutViewChange].Stop()
}
4 changes: 2 additions & 2 deletions consensus/view_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestGetNextLeaderKeyShouldFailForStandardGeneratedConsensus(t *testing.T) {

// The below results in: "panic: runtime error: integer divide by zero"
// This happens because there's no check for if there are any participants or not in https://github.com/harmony-one/harmony/blob/main/consensus/quorum/quorum.go#L188-L197
assert.Panics(t, func() { consensus.GetNextLeaderKey(uint64(1)) })
assert.Panics(t, func() { consensus.GetNextLeaderKey() })
}

func TestGetNextLeaderKeyShouldSucceed(t *testing.T) {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestGetNextLeaderKeyShouldSucceed(t *testing.T) {
assert.Equal(t, keyCount, consensus.Decider.ParticipantsCount())

consensus.LeaderPubKey = &wrappedBLSKeys[0]
nextKey := consensus.GetNextLeaderKey(uint64(1))
nextKey := consensus.GetNextLeaderKey()

assert.Equal(t, nextKey, &wrappedBLSKeys[1])
}

0 comments on commit aba83d4

Please sign in to comment.