Add few tests

This commit is contained in:
Zarko Milosevic
2019-01-09 12:48:50 +01:00
parent 79ddf9d473
commit 16328f0f7b
4 changed files with 68 additions and 21 deletions
+4 -1
View File
@@ -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 {
+39
View File
@@ -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
+7
View File
@@ -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())
}
+18 -20
View File
@@ -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 {