Fix refactor; add test for AggregateVotes

pull/3374/head
Rongjian Lan 4 years ago
parent 0141604162
commit b6171945cf
  1. 18
      consensus/quorum/one-node-staked-vote_test.go
  2. 17
      consensus/quorum/quorom_test.go
  3. 2
      consensus/quorum/quorum.go
  4. 2
      consensus/view_change.go

@ -148,21 +148,21 @@ func TestEvenNodes(t *testing.T) {
sign(stakedVote, sKeys[reg], Prepare) sign(stakedVote, sKeys[reg], Prepare)
achieved := stakedVote.IsQuorumAchieved(Prepare) achieved := stakedVote.IsQuorumAchieved(Prepare)
if achieved { if achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: false (All Staker nodes = 32%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: false (All Staker nodes = 32%%)",
Prepare, strconv.FormatBool(achieved)) Prepare, strconv.FormatBool(achieved))
} }
// Commit // Commit
sign(stakedVote, sKeys[reg], Commit) sign(stakedVote, sKeys[reg], Commit)
achieved = stakedVote.IsQuorumAchieved(Commit) achieved = stakedVote.IsQuorumAchieved(Commit)
if achieved { if achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: false (All Staker nodes = 32%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: false (All Staker nodes = 32%%)",
Commit, strconv.FormatBool(achieved)) Commit, strconv.FormatBool(achieved))
} }
// ViewChange // ViewChange
sign(stakedVote, sKeys[reg], ViewChange) sign(stakedVote, sKeys[reg], ViewChange)
achieved = stakedVote.IsQuorumAchieved(ViewChange) achieved = stakedVote.IsQuorumAchieved(ViewChange)
if achieved { if achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, Got: %s, Expected: false (All Staker nodes = 32%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, Got: %s, Expected: false (All Staker nodes = 32%%)",
ViewChange, strconv.FormatBool(achieved)) ViewChange, strconv.FormatBool(achieved))
} }
// RewardThreshold // RewardThreshold
@ -176,21 +176,21 @@ func TestEvenNodes(t *testing.T) {
sign(stakedVote, sKeys[hmy], Prepare) sign(stakedVote, sKeys[hmy], Prepare)
achieved = stakedVote.IsQuorumAchieved(Prepare) achieved = stakedVote.IsQuorumAchieved(Prepare)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: true (All nodes = 100%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: true (All nodes = 100%%)",
Prepare, strconv.FormatBool(achieved)) Prepare, strconv.FormatBool(achieved))
} }
// Commit // Commit
sign(stakedVote, sKeys[hmy], Commit) sign(stakedVote, sKeys[hmy], Commit)
achieved = stakedVote.IsQuorumAchieved(Commit) achieved = stakedVote.IsQuorumAchieved(Commit)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: true (All nodes = 100%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: true (All nodes = 100%%)",
Commit, strconv.FormatBool(achieved)) Commit, strconv.FormatBool(achieved))
} }
// ViewChange // ViewChange
sign(stakedVote, sKeys[hmy], ViewChange) sign(stakedVote, sKeys[hmy], ViewChange)
achieved = stakedVote.IsQuorumAchieved(ViewChange) achieved = stakedVote.IsQuorumAchieved(ViewChange)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, Got: %s, Expected: true (All nodes = 100%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, Got: %s, Expected: true (All nodes = 100%%)",
ViewChange, strconv.FormatBool(achieved)) ViewChange, strconv.FormatBool(achieved))
} }
// RewardThreshold // RewardThreshold
@ -215,21 +215,21 @@ func Test33HarmonyNodes(t *testing.T) {
sign(stakedVote, sKeys, Prepare) sign(stakedVote, sKeys, Prepare)
achieved := stakedVote.IsQuorumAchieved(Prepare) achieved := stakedVote.IsQuorumAchieved(Prepare)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: true (All Harmony nodes = 68%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: true (All Harmony nodes = 68%%)",
Prepare, strconv.FormatBool(achieved)) Prepare, strconv.FormatBool(achieved))
} }
// Commit // Commit
sign(stakedVote, sKeys, Commit) sign(stakedVote, sKeys, Commit)
achieved = stakedVote.IsQuorumAchieved(Commit) achieved = stakedVote.IsQuorumAchieved(Commit)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, QuorumAchieved: %s, Expected: true (All Harmony nodes = 68%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, QuorumAchieved: %s, Expected: true (All Harmony nodes = 68%%)",
Commit, strconv.FormatBool(achieved)) Commit, strconv.FormatBool(achieved))
} }
// ViewChange // ViewChange
sign(stakedVote, sKeys, ViewChange) sign(stakedVote, sKeys, ViewChange)
achieved = stakedVote.IsQuorumAchieved(ViewChange) achieved = stakedVote.IsQuorumAchieved(ViewChange)
if !achieved { if !achieved {
t.Errorf("[IsQuorumAchieved] MessageType: %s, Got: %s, Expected: true (All Harmony nodes = 68%%)", t.Errorf("[IsQuorumAchieved] Phase: %s, Got: %s, Expected: true (All Harmony nodes = 68%%)",
ViewChange, strconv.FormatBool(achieved)) ViewChange, strconv.FormatBool(achieved))
} }
// RewardThreshold // RewardThreshold

@ -436,6 +436,13 @@ func TestAddNewVoteInvalidAggregateSig(test *testing.T) {
if decider.SignersCount(Prepare) != 2 { if decider.SignersCount(Prepare) != 2 {
test.Errorf("signers are incorrect for harmony nodes signing with aggregate sig: have %d, expect %d", decider.SignersCount(Prepare), 2) test.Errorf("signers are incorrect for harmony nodes signing with aggregate sig: have %d, expect %d", decider.SignersCount(Prepare), 2)
} }
aggSig = &bls_core.Sign{}
for _, priKey := range []*bls_core.SecretKey{&sKeys[3], &sKeys[4]} {
if s := priKey.SignHash(blockHash[:]); s != nil {
aggSig.Add(s)
}
}
// aggregate sig from all of 2 external nodes // aggregate sig from all of 2 external nodes
_, err := decider.AddNewVote(Prepare, _, err := decider.AddNewVote(Prepare,
[]*bls.PublicKeyWrapper{&pubKeys[3], &pubKeys[4]}, []*bls.PublicKeyWrapper{&pubKeys[3], &pubKeys[4]},
@ -451,6 +458,16 @@ func TestAddNewVoteInvalidAggregateSig(test *testing.T) {
test.Errorf("signers are incorrect for harmony nodes signing with aggregate sig: have %d, expect %d", decider.SignersCount(Prepare), 4) test.Errorf("signers are incorrect for harmony nodes signing with aggregate sig: have %d, expect %d", decider.SignersCount(Prepare), 4)
} }
// Aggregate Vote should only contain sig from 0, 1, 3, 4
fourSigs := decider.AggregateVotes(Prepare)
aggPubKey := &bls_core.PublicKey{}
for _, priKey := range []*bls_core.PublicKey{pubKeys[0].Object, pubKeys[1].Object, pubKeys[3].Object, pubKeys[4].Object} {
aggPubKey.Add(priKey)
}
if !fourSigs.VerifyHash(aggPubKey, blockHash[:]) {
test.Error("Failed to aggregate votes for 4 keys from 2 aggregate sigs")
}
_, err = decider.AddNewVote(Prepare, _, err = decider.AddNewVote(Prepare,
[]*bls.PublicKeyWrapper{&pubKeys[3], &pubKeys[7]}, []*bls.PublicKeyWrapper{&pubKeys[3], &pubKeys[7]},
aggSig, aggSig,

@ -41,7 +41,7 @@ func (p Phase) String() string {
if name, ok := phaseNames[p]; ok { if name, ok := phaseNames[p]; ok {
return name return name
} }
return fmt.Sprintf("Unknown Quorum MessageType %+v", byte(p)) return fmt.Sprintf("Unknown Quorum Phase %+v", byte(p))
} }
// Policy is the rule we used to decide is quorum achieved // Policy is the rule we used to decide is quorum achieved

@ -133,7 +133,7 @@ func (consensus *Consensus) GetNextLeaderKey() *bls.PublicKeyWrapper {
// ResetViewChangeState reset the state for viewchange // ResetViewChangeState reset the state for viewchange
func (consensus *Consensus) ResetViewChangeState() { func (consensus *Consensus) ResetViewChangeState() {
consensus.getLogger().Debug(). consensus.getLogger().Debug().
Str("MessageType", consensus.phase.String()). Str("Phase", consensus.phase.String()).
Msg("[ResetViewChangeState] Resetting view change state") Msg("[ResetViewChangeState] Resetting view change state")
consensus.current.SetMode(Normal) consensus.current.SetMode(Normal)
consensus.m1Payload = []byte{} consensus.m1Payload = []byte{}

Loading…
Cancel
Save