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

Update Quorum Check to Use GTE for Consistency #4806

Draft
wants to merge 4 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
3 changes: 2 additions & 1 deletion cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ func setupChain(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfig.ConfigTyp
}

func setupConsensusAndNode(hc harmonyconfig.HarmonyConfig, nodeConfig *nodeconfig.ConfigType, registry *registry.Registry) *node.Node {
decider := quorum.NewDecider(quorum.SuperMajorityVote, uint32(hc.General.ShardID))
useGT := registry.GetBlockchain().Config().IsLeaderRotationV2Epoch(registry.GetBlockchain().CurrentBlock().Epoch())
decider := quorum.NewDecider(quorum.SuperMajorityVote, uint32(hc.General.ShardID), useGT)

// Parse minPeers from harmonyconfig.HarmonyConfig
var minPeers int
Expand Down
3 changes: 2 additions & 1 deletion consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ func (consensus *Consensus) updateConsensusInformation(reason string) Mode {

// Only happens once, the flip-over to a new Decider policy
if isFirstTimeStaking || haventUpdatedDecider {
decider := quorum.NewDecider(quorum.SuperMajorityStake, consensus.ShardID)
useGT := consensus.Blockchain().Config().IsLeaderRotationV2Epoch(consensus.Blockchain().CurrentBlock().Epoch())
decider := quorum.NewDecider(quorum.SuperMajorityStake, consensus.ShardID, useGT)
consensus.decider = decider
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/consensus_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestSignAndMarshalConsensusMessage(t *testing.T) {
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(quorum.SuperMajorityVote, shard.BeaconChainShardID)
decider := quorum.NewDecider(quorum.SuperMajorityVote, shard.BeaconChainShardID, false)
blsPriKey := bls.RandPrivateKey()
reg := registry.New()
consensus, err := New(host, shard.BeaconChainShardID, multibls.GetPrivateKeys(blsPriKey), reg, decider, 3, false)
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestSetViewID(t *testing.T) {
t.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blsPriKey := bls.RandPrivateKey()
reg := registry.New()
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func GenerateConsensusForTesting() (p2p.Host, multibls.PrivateKeys, *Consensus,
return nil, nil, nil, nil, err
}

decider := quorum.NewDecider(quorum.SuperMajorityVote, shard.BeaconChainShardID)
decider := quorum.NewDecider(quorum.SuperMajorityVote, shard.BeaconChainShardID, false)
multiBLSPrivateKey := multibls.GetPrivateKeys(bls.RandPrivateKey())

consensus, err := New(host, shard.BeaconChainShardID, multiBLSPrivateKey, registry.New(), decider, 3, false)
Expand Down
10 changes: 5 additions & 5 deletions consensus/construct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestConstructAnnounceMessage(test *testing.T) {
test.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blsPriKey := bls.RandPrivateKey()
reg := registry.New()
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestConstructPreparedMessage(test *testing.T) {
test.Fatalf("newhost failure: %v", err)
}
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blsPriKey := bls.RandPrivateKey()
reg := registry.New()
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestConstructPrepareMessage(test *testing.T) {
priKeyWrapper2 := bls.PrivateKeyWrapper{Pri: blsPriKey2, Pub: &pubKeyWrapper2}

decider := quorum.NewDecider(
quorum.SuperMajorityStake, shard.BeaconChainShardID,
quorum.SuperMajorityStake, shard.BeaconChainShardID, false,
)

consensus, err := New(
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestConstructCommitMessage(test *testing.T) {
priKeyWrapper2 := bls.PrivateKeyWrapper{Pri: blsPriKey2, Pub: &pubKeyWrapper2}

decider := quorum.NewDecider(
quorum.SuperMajorityStake, shard.BeaconChainShardID,
quorum.SuperMajorityStake, shard.BeaconChainShardID, false,
)

consensus, err := New(host, shard.BeaconChainShardID, multibls.GetPrivateKeys(blsPriKey1), registry.New(), decider, 3, false)
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestPopulateMessageFields(t *testing.T) {
}
blsPriKey := bls.RandPrivateKey()
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
consensus, err := New(
host, shard.BeaconChainShardID, multibls.GetPrivateKeys(blsPriKey), registry.New(), decider, 3, false,
Expand Down
8 changes: 7 additions & 1 deletion consensus/quorum/one-node-one-vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type uniformVoteWeight struct {

lastPowerSignersCountCache map[Phase]int64
lastParticipantsCount int64

useGTforQuorumChecking bool
}

// Policy ..
Expand Down Expand Up @@ -60,7 +62,11 @@ func (v *uniformVoteWeight) IsQuorumAchievedByMask(mask *bls_cosi.Mask) bool {
}
threshold := v.TwoThirdsSignersCount()
currentTotalPower := utils.CountOneBits(mask.Bitmap)
if currentTotalPower < threshold {
enoughVotingPower := currentTotalPower >= threshold
if v.useGTforQuorumChecking {
enoughVotingPower = currentTotalPower > threshold
}
if !enoughVotingPower {
const msg = "[IsQuorumAchievedByMask] Not enough voting power: need %+v, have %+v"
utils.Logger().Warn().Msgf(msg, threshold, currentTotalPower)
return false
Expand Down
6 changes: 3 additions & 3 deletions consensus/quorum/one-node-staked-vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
type secretKeyMap map[bls.SerializedPublicKey]bls_core.SecretKey

func init() {
basicDecider = NewDecider(SuperMajorityStake, shard.BeaconChainShardID)
basicDecider = NewDecider(SuperMajorityStake, shard.BeaconChainShardID, false)
shard.Schedule = shardingconfig.LocalnetSchedule
}

Expand Down Expand Up @@ -71,7 +71,7 @@ func setupBaseCase() (Decider, *TallyResult, shard.SlotList, map[string]secretKe
pubKeys = append(pubKeys, wrapper)
}

decider := NewDecider(SuperMajorityStake, shard.BeaconChainShardID)
decider := NewDecider(SuperMajorityStake, shard.BeaconChainShardID, false)
decider.UpdateParticipants(pubKeys, []bls.PublicKeyWrapper{})
tally, err := decider.SetVoters(&shard.Committee{
ShardID: shard.BeaconChainShardID, Slots: slotList,
Expand Down Expand Up @@ -100,7 +100,7 @@ func setupEdgeCase() (Decider, *TallyResult, shard.SlotList, secretKeyMap) {
pubKeys = append(pubKeys, wrapper)
}

decider := NewDecider(SuperMajorityStake, shard.BeaconChainShardID)
decider := NewDecider(SuperMajorityStake, shard.BeaconChainShardID, false)
decider.UpdateParticipants(pubKeys, []bls.PublicKeyWrapper{})
tally, err := decider.SetVoters(&shard.Committee{
ShardID: shard.BeaconChainShardID, Slots: slotList,
Expand Down
12 changes: 6 additions & 6 deletions consensus/quorum/quorom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestPolicyStrings(t *testing.T) {
}

func TestAddingQuoromParticipants(t *testing.T) {
decider := NewDecider(SuperMajorityVote, shard.BeaconChainShardID)
decider := NewDecider(SuperMajorityVote, shard.BeaconChainShardID, false)

assert.Equal(t, int64(0), decider.ParticipantsCount())

Expand All @@ -77,7 +77,7 @@ func TestSubmitVote(test *testing.T) {
viewID := uint64(2)

decider := NewDecider(
SuperMajorityStake, shard.BeaconChainShardID,
SuperMajorityStake, shard.BeaconChainShardID, false,
)

message := "test string"
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestSubmitVoteAggregateSig(test *testing.T) {
viewID := uint64(2)

decider := NewDecider(
SuperMajorityStake, shard.BeaconChainShardID,
SuperMajorityStake, shard.BeaconChainShardID, false,
)

blsPriKey1 := bls.RandPrivateKey()
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestAddNewVote(test *testing.T) {
viewID := uint64(2)

decider := NewDecider(
SuperMajorityStake, shard.BeaconChainShardID,
SuperMajorityStake, shard.BeaconChainShardID, false,
)

slotList := shard.SlotList{}
Expand Down Expand Up @@ -305,7 +305,7 @@ func TestAddNewVoteAggregateSig(test *testing.T) {
viewID := uint64(2)

decider := NewDecider(
SuperMajorityStake, shard.BeaconChainShardID,
SuperMajorityStake, shard.BeaconChainShardID, false,
)

slotList := shard.SlotList{}
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestAddNewVoteInvalidAggregateSig(test *testing.T) {
viewID := uint64(2)

decider := NewDecider(
SuperMajorityStake, shard.BeaconChainShardID,
SuperMajorityStake, shard.BeaconChainShardID, false,
)

slotList := shard.SlotList{}
Expand Down
3 changes: 2 additions & 1 deletion consensus/quorum/quorum.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,13 @@ func newBallotsBackedSignatureReader() *cIdentities {
}

// NewDecider ..
func NewDecider(p Policy, shardID uint32) Decider {
func NewDecider(p Policy, shardID uint32, useGTforQuorumChecking bool) Decider {
switch p {
case SuperMajorityVote:
return &uniformVoteWeight{
SignatureReader: newBallotsBackedSignatureReader(),
lastPowerSignersCountCache: make(map[Phase]int64),
useGTforQuorumChecking: useGTforQuorumChecking,
}
case SuperMajorityStake:
return &stakedVoteWeight{
Expand Down
2 changes: 1 addition & 1 deletion core_test/shardchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAddNewBlock(t *testing.T) {
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, engine, &chainconfig,
)
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions hmy/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (hmy *Harmony) getSuperCommittees() (*quorum.Transition, error) {
rawStakes := []effective.SlotPurchase{}
validatorSpreads := map[common.Address]numeric.Dec{}
for _, comm := range prevCommittee.Shards {
decider := quorum.NewDecider(quorum.SuperMajorityStake, comm.ShardID)
decider := quorum.NewDecider(quorum.SuperMajorityStake, comm.ShardID, false)
// before staking skip computing
if hmy.BlockChain.Config().IsStaking(prevCommittee.Epoch) {
if _, err := decider.SetVoters(&comm, prevCommittee.Epoch); err != nil {
Expand All @@ -111,7 +111,7 @@ func (hmy *Harmony) getSuperCommittees() (*quorum.Transition, error) {
rawStakes = []effective.SlotPurchase{}
validatorSpreads = map[common.Address]numeric.Dec{}
for _, comm := range nowCommittee.Shards {
decider := quorum.NewDecider(quorum.SuperMajorityStake, comm.ShardID)
decider := quorum.NewDecider(quorum.SuperMajorityStake, comm.ShardID, false)
if _, err := decider.SetVoters(&comm, nowCommittee.Epoch); err != nil {
return nil, errors.Wrapf(
err,
Expand Down
6 changes: 3 additions & 3 deletions node/harmony/node_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestAddNewBlock(t *testing.T) {
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, engine, &chainconfig,
)
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
if err != nil {
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestVerifyNewBlock(t *testing.T) {
nil, testDBFactory, &core.GenesisInitializer{NetworkType: nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType()}, engine, &chainconfig,
)
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
blockchain, err := collection.ShardChain(shard.BeaconChainShardID)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestVerifyVRF(t *testing.T) {
t.Fatal("cannot get blockchain")
}
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
reg := registry.New().
SetBlockchain(blockchain).
Expand Down
2 changes: 1 addition & 1 deletion node/harmony/node_newblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestFinalizeNewBlockAsync(t *testing.T) {
require.NoError(t, err)

decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
reg := registry.New().
SetBlockchain(blockchain).
Expand Down
2 changes: 1 addition & 1 deletion node/harmony/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestNewNode(t *testing.T) {
}
engine := chain.NewEngine()
decider := quorum.NewDecider(
quorum.SuperMajorityVote, shard.BeaconChainShardID,
quorum.SuperMajorityVote, shard.BeaconChainShardID, false,
)
chainconfig := nodeconfig.GetShardConfig(shard.BeaconChainShardID).GetNetworkType().ChainConfig()
collection := shardchain.NewCollection(
Expand Down