From 16328f0f7bd1a2382e8b15af127b3b987fcb3bbb Mon Sep 17 00:00:00 2001 From: Zarko Milosevic Date: Wed, 12 Dec 2018 13:55:40 +0100 Subject: [PATCH] Add few tests --- state/execution.go | 5 ++++- state/execution_test.go | 39 +++++++++++++++++++++++++++++++++++++++ types/block_test.go | 7 +++++++ types/validator_set.go | 38 ++++++++++++++++++-------------------- 4 files changed, 68 insertions(+), 21 deletions(-) diff --git a/state/execution.go b/state/execution.go index 3b1056e0b..b3d7268eb 100644 --- a/state/execution.go +++ b/state/execution.go @@ -345,9 +345,12 @@ func validateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, func NextValidators(currentSet *types.ValidatorSet, updates []*types.Validator) (*types.ValidatorSet, error) { nValSet := currentSet.Copy() - // update proposer priority + // update proposer priority. Increase proposer priority for every process for ammount + // equal to their voting power and decrease proposer priority of the initial proposer + // equal to total voting power nValSet.UpdateProposerPriority() + // apply updates for _, valUpdate := range updates { // should already have been checked if valUpdate.VotingPower < 0 { diff --git a/state/execution_test.go b/state/execution_test.go index 21df1ee56..799b15ff6 100644 --- a/state/execution_test.go +++ b/state/execution_test.go @@ -354,6 +354,45 @@ func TestEndBlockValidatorUpdates(t *testing.T) { } } +func TestNextValidatorsWithEmptyUpdates(t *testing.T) { + pubkey1 := ed25519.GenPrivKey().PubKey() + val1 := types.NewValidator(pubkey1, 10) + pubkey2 := ed25519.GenPrivKey().PubKey() + val2 := types.NewValidator(pubkey2, 20) + + currentSet := types.NewValidatorSet([]*types.Validator{val1, val2}) + t.Log(currentSet) + + newSet, error := NextValidators(currentSet, nil) + assert.Nil(t, error) + t.Log(newSet) + + newSet2, error := NextValidators(currentSet, []*types.Validator{}) + assert.Nil(t, error) + t.Log(newSet2) +} + +func TestNextValidatorsWithUpdates(t *testing.T) { + pubkey1 := ed25519.GenPrivKey().PubKey() + val1 := types.NewValidator(pubkey1, 10) + pubkey2 := ed25519.GenPrivKey().PubKey() + val2 := types.NewValidator(pubkey2, 20) + pubkey3 := ed25519.GenPrivKey().PubKey() + val3 := types.NewValidator(pubkey3, 30) + + currentSet := types.NewValidatorSet([]*types.Validator{val1, val2, val3}) + t.Log(currentSet) + + val1.VotingPower = 0 + newSet, error := NextValidators(currentSet, []*types.Validator{val1}) + assert.Nil(t, error) + t.Log("Remove v1", newSet) + + newSet2, error := NextValidators(newSet, []*types.Validator{val2}) + assert.Nil(t, error) + t.Log("add v2", newSet2) +} + //---------------------------------------------------------------------------- // make some bogus txs diff --git a/types/block_test.go b/types/block_test.go index bedd8c8da..0c66003df 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -341,3 +341,10 @@ func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) { } } } + +func TestNilBlock(t *testing.T) { + nilBlock := BlockID{nil, PartSetHeader{}} + t.Log(nilBlock) + t.Log(len(nilBlock.Hash)) + t.Log(nilBlock.IsZero()) +} diff --git a/types/validator_set.go b/types/validator_set.go index c24bab158..effb7e071 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -62,9 +62,9 @@ func NewValidatorSet(valz []*Validator) *ValidatorSet { round: 0, } - if len(valz) > 0 { - vals.IncrementProposerPriority(1) - } + //if len(valz) > 0 { + // vals.IncrementProposerPriority(1) + //} return vals } @@ -93,11 +93,11 @@ func (vals *ValidatorSet) FindProposer(round int) *Validator { val.ProposerPriority = initialValSet.initProposerPriorities[i] } for i := initialValSet.round; i <= round; i++ { - proposer = vals.updateProposerPriority() + proposer = vals.UpdateProposerPriority() } } else { for i := vals.round; i <= round; i++ { - proposer = vals.updateProposerPriority() + proposer = vals.UpdateProposerPriority() } vals.round = round } @@ -127,24 +127,22 @@ func (vals *ValidatorSet) IncrementProposerPriority(times int) { vals.Proposer = proposer } -func (vals *ValidatorSet) UpdateProposerPriority() *Validator { - for _, val := range vals.Validators { +func (vals *ValidatorSet) UpdateProposerPriority() *ValidatorSet { + nValSet := vals.Copy() + + // just pick the proposer with the highest proposer priority + // without any modification to it + proposer := vals.FindProposer(0) // maybe use getProposer here + + // update proposer priority + for _, val := range nValSet.Validators { // Check for overflow for sum. val.ProposerPriority = safeAddClip(val.ProposerPriority, val.VotingPower) + if bytes.Equal(proposer.Address, val.Address) { + val.ProposerPriority = safeSubClip(val.ProposerPriority, nValSet.TotalVotingPower()) + } } - - validatorsHeap := cmn.NewHeap() - // just update the heap - for _, val := range vals.Validators { - validatorsHeap.PushComparable(val, proposerPriorityComparable{val}) - } - - // Decrement the validator with most ProposerPriority: - mostest := validatorsHeap.Peek().(*Validator) - // mind underflow - mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower()) - - return mostest + return nValSet } func (vals *ValidatorSet) incrementProposerPriority(subAvg bool) *Validator {