Browse Source

rename Accum -> ProposerPriority: (#2932)

- rename fields, methods, comments, tests
pull/2945/head v0.27.0-dev0
Ismail Khoffi 6 years ago
committed by Ethan Buchman
parent
commit
b30c34e713
12 changed files with 109 additions and 109 deletions
  1. +1
    -1
      consensus/state.go
  2. +1
    -1
      docs/app-dev/subscribing-to-events-via-websocket.md
  3. +1
    -1
      evidence/pool_test.go
  4. +1
    -1
      p2p/metrics.go
  5. +5
    -5
      rpc/core/consensus.go
  6. +7
    -7
      state/execution.go
  7. +1
    -1
      state/state.go
  8. +8
    -8
      state/state_test.go
  9. +1
    -1
      state/store.go
  10. +15
    -15
      types/validator.go
  11. +36
    -36
      types/validator_set.go
  12. +32
    -32
      types/validator_set_test.go

+ 1
- 1
consensus/state.go View File

@ -745,7 +745,7 @@ func (cs *ConsensusState) enterNewRound(height int64, round int) {
validators := cs.Validators
if cs.Round < round {
validators = validators.Copy()
validators.IncrementAccum(round - cs.Round)
validators.IncrementProposerPriority(round - cs.Round)
}
// Setup new round


+ 1
- 1
docs/app-dev/subscribing-to-events-via-websocket.md View File

@ -54,7 +54,7 @@ Response:
"value": "ww0z4WaZ0Xg+YI10w43wTWbBmM3dpVza4mmSQYsd0ck="
},
"voting_power": "10",
"accum": "0"
"proposer_priority": "0"
}
]
}


+ 1
- 1
evidence/pool_test.go View File

@ -35,7 +35,7 @@ func initializeValidatorState(valAddr []byte, height int64) dbm.DB {
LastBlockHeight: 0,
LastBlockTime: tmtime.Now(),
Validators: valSet,
NextValidators: valSet.CopyIncrementAccum(1),
NextValidators: valSet.CopyIncrementProposerPriority(1),
LastHeightValidatorsChanged: 1,
ConsensusParams: types.ConsensusParams{
Evidence: types.EvidenceParams{


+ 1
- 1
p2p/metrics.go View File

@ -62,7 +62,7 @@ func PrometheusMetrics(namespace string) *Metrics {
// NopMetrics returns no-op Metrics.
func NopMetrics() *Metrics {
return &Metrics{
Peers: discard.NewGauge(),
Peers: discard.NewGauge(),
PeerReceiveBytesTotal: discard.NewCounter(),
PeerSendBytesTotal: discard.NewCounter(),
PeerPendingSendBytes: discard.NewGauge(),


+ 5
- 5
rpc/core/consensus.go View File

@ -27,7 +27,7 @@ import (
// "result": {
// "validators": [
// {
// "accum": "0",
// "proposer_priority": "0",
// "voting_power": "10",
// "pub_key": {
// "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D",
@ -92,7 +92,7 @@ func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
// "value": "SBctdhRBcXtBgdI/8a/alTsUhGXqGs9k5ylV1u5iKHg="
// },
// "voting_power": "10",
// "accum": "0"
// "proposer_priority": "0"
// }
// ],
// "proposer": {
@ -102,7 +102,7 @@ func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
// "value": "SBctdhRBcXtBgdI/8a/alTsUhGXqGs9k5ylV1u5iKHg="
// },
// "voting_power": "10",
// "accum": "0"
// "proposer_priority": "0"
// }
// },
// "proposal": null,
@ -138,7 +138,7 @@ func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
// "value": "SBctdhRBcXtBgdI/8a/alTsUhGXqGs9k5ylV1u5iKHg="
// },
// "voting_power": "10",
// "accum": "0"
// "proposer_priority": "0"
// }
// ],
// "proposer": {
@ -148,7 +148,7 @@ func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
// "value": "SBctdhRBcXtBgdI/8a/alTsUhGXqGs9k5ylV1u5iKHg="
// },
// "voting_power": "10",
// "accum": "0"
// "proposer_priority": "0"
// }
// }
// },


+ 7
- 7
state/execution.go View File

@ -373,14 +373,14 @@ func updateValidators(currentSet *types.ValidatorSet, updates []*types.Validator
types.MaxTotalVotingPower)
}
// TODO: issue #1558 update spec according to the following:
// Set Accum to -C*totalVotingPower (with C ~= 1.125) to make sure validators can't
// unbond/rebond to reset their (potentially previously negative) Accum to zero.
// Set ProposerPriority to -C*totalVotingPower (with C ~= 1.125) to make sure validators can't
// unbond/rebond to reset their (potentially previously negative) ProposerPriority to zero.
//
// Contract: totalVotingPower < MaxTotalVotingPower to ensure Accum does
// Contract: totalVotingPower < MaxTotalVotingPower to ensure ProposerPriority does
// not exceed the bounds of int64.
//
// Compute Accum = -1.125*totalVotingPower == -(totalVotingPower + (totalVotingPower >> 3)).
valUpdate.Accum = -(totalVotingPower + (totalVotingPower >> 3))
// Compute ProposerPriority = -1.125*totalVotingPower == -(totalVotingPower + (totalVotingPower >> 3)).
valUpdate.ProposerPriority = -(totalVotingPower + (totalVotingPower >> 3))
added := currentSet.Add(valUpdate)
if !added {
return fmt.Errorf("Failed to add new validator %v", valUpdate)
@ -431,8 +431,8 @@ func updateState(
lastHeightValsChanged = header.Height + 1 + 1
}
// Update validator accums and set state variables.
nValSet.IncrementAccum(1)
// Update validator proposer priority and set state variables.
nValSet.IncrementProposerPriority(1)
// Update the params with the latest abciResponses.
nextParams := state.ConsensusParams


+ 1
- 1
state/state.go View File

@ -226,7 +226,7 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
validators[i] = types.NewValidator(val.PubKey, val.Power)
}
validatorSet = types.NewValidatorSet(validators)
nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementAccum(1)
nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1)
}
return State{


+ 8
- 8
state/state_test.go View File

@ -133,8 +133,8 @@ func TestABCIResponsesSaveLoad2(t *testing.T) {
{Code: 383},
{Data: []byte("Gotcha!"),
Tags: []cmn.KVPair{
cmn.KVPair{Key: []byte("a"), Value: []byte("1")},
cmn.KVPair{Key: []byte("build"), Value: []byte("stuff")},
{Key: []byte("a"), Value: []byte("1")},
{Key: []byte("build"), Value: []byte("stuff")},
}},
},
types.ABCIResults{
@ -263,11 +263,11 @@ func TestOneValidatorChangesSaveLoad(t *testing.T) {
}
}
func TestStoreLoadValidatorsIncrementsAccum(t *testing.T) {
func TestStoreLoadValidatorsIncrementsProposerPriority(t *testing.T) {
const valSetSize = 2
tearDown, stateDB, state := setupTestCase(t)
state.Validators = genValSet(valSetSize)
state.NextValidators = state.Validators.CopyIncrementAccum(1)
state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
SaveState(stateDB, state)
defer tearDown(t)
@ -275,13 +275,13 @@ func TestStoreLoadValidatorsIncrementsAccum(t *testing.T) {
v0, err := LoadValidators(stateDB, nextHeight)
assert.Nil(t, err)
acc0 := v0.Validators[0].Accum
acc0 := v0.Validators[0].ProposerPriority
v1, err := LoadValidators(stateDB, nextHeight+1)
assert.Nil(t, err)
acc1 := v1.Validators[0].Accum
acc1 := v1.Validators[0].ProposerPriority
assert.NotEqual(t, acc1, acc0, "expected Accum value to change between heights")
assert.NotEqual(t, acc1, acc0, "expected ProposerPriority value to change between heights")
}
// TestValidatorChangesSaveLoad tests saving and loading a validator set with
@ -291,7 +291,7 @@ func TestManyValidatorChangesSaveLoad(t *testing.T) {
tearDown, stateDB, state := setupTestCase(t)
require.Equal(t, int64(0), state.LastBlockHeight)
state.Validators = genValSet(valSetSize)
state.NextValidators = state.Validators.CopyIncrementAccum(1)
state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
SaveState(stateDB, state)
defer tearDown(t)


+ 1
- 1
state/store.go View File

@ -194,7 +194,7 @@ func LoadValidators(db dbm.DB, height int64) (*types.ValidatorSet, error) {
),
)
}
valInfo2.ValidatorSet.IncrementAccum(int(height - valInfo.LastHeightChanged)) // mutate
valInfo2.ValidatorSet.IncrementProposerPriority(int(height - valInfo.LastHeightChanged)) // mutate
valInfo = valInfo2
}


+ 15
- 15
types/validator.go View File

@ -11,40 +11,40 @@ import (
)
// Volatile state for each Validator
// NOTE: The Accum is not included in Validator.Hash();
// NOTE: The ProposerPriority is not included in Validator.Hash();
// make sure to update that method if changes are made here
type Validator struct {
Address Address `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
VotingPower int64 `json:"voting_power"`
Accum int64 `json:"accum"`
ProposerPriority int64 `json:"proposer_priority"`
}
func NewValidator(pubKey crypto.PubKey, votingPower int64) *Validator {
return &Validator{
Address: pubKey.Address(),
PubKey: pubKey,
VotingPower: votingPower,
Accum: 0,
Address: pubKey.Address(),
PubKey: pubKey,
VotingPower: votingPower,
ProposerPriority: 0,
}
}
// Creates a new copy of the validator so we can mutate accum.
// Creates a new copy of the validator so we can mutate ProposerPriority.
// Panics if the validator is nil.
func (v *Validator) Copy() *Validator {
vCopy := *v
return &vCopy
}
// Returns the one with higher Accum.
func (v *Validator) CompareAccum(other *Validator) *Validator {
// Returns the one with higher ProposerPriority.
func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
if v == nil {
return other
}
if v.Accum > other.Accum {
if v.ProposerPriority > other.ProposerPriority {
return v
} else if v.Accum < other.Accum {
} else if v.ProposerPriority < other.ProposerPriority {
return other
} else {
result := bytes.Compare(v.Address, other.Address)
@ -67,19 +67,19 @@ func (v *Validator) String() string {
v.Address,
v.PubKey,
v.VotingPower,
v.Accum)
v.ProposerPriority)
}
// Hash computes the unique ID of a validator with a given voting power.
// It excludes the Accum value, which changes with every round.
// It excludes the ProposerPriority value, which changes with every round.
func (v *Validator) Hash() []byte {
return tmhash.Sum(v.Bytes())
}
// Bytes computes the unique encoding of a validator with a given voting power.
// These are the bytes that gets hashed in consensus. It excludes address
// as its redundant with the pubkey. This also excludes accum which changes
// every round.
// as its redundant with the pubkey. This also excludes ProposerPriority
// which changes every round.
func (v *Validator) Bytes() []byte {
return cdcEncode(struct {
PubKey crypto.PubKey


+ 36
- 36
types/validator_set.go View File

@ -13,7 +13,7 @@ import (
)
// The maximum allowed total voting power.
// We set the accum of freshly added validators to -1.125*totalVotingPower.
// We set the ProposerPriority of freshly added validators to -1.125*totalVotingPower.
// To compute 1.125*totalVotingPower efficiently, we do:
// totalVotingPower + (totalVotingPower >> 3) because
// x + (x >> 3) = x + x/8 = x * (1 + 0.125).
@ -25,9 +25,9 @@ const MaxTotalVotingPower = 8198552921648689607
// The validators can be fetched by address or index.
// The index is in order of .Address, so the indices are fixed
// for all rounds of a given blockchain height.
// On the other hand, the .AccumPower of each validator and
// On the other hand, the .ProposerPriority of each validator and
// the designated .GetProposer() of a set changes every round,
// upon calling .IncrementAccum().
// upon calling .IncrementProposerPriority().
// NOTE: Not goroutine-safe.
// NOTE: All get/set to validators should copy the value for safety.
type ValidatorSet struct {
@ -52,7 +52,7 @@ func NewValidatorSet(valz []*Validator) *ValidatorSet {
Validators: validators,
}
if len(valz) > 0 {
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
}
return vals
@ -63,79 +63,79 @@ func (vals *ValidatorSet) IsNilOrEmpty() bool {
return vals == nil || len(vals.Validators) == 0
}
// Increment Accum and update the proposer on a copy, and return it.
func (vals *ValidatorSet) CopyIncrementAccum(times int) *ValidatorSet {
// Increment ProposerPriority and update the proposer on a copy, and return it.
func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet {
copy := vals.Copy()
copy.IncrementAccum(times)
copy.IncrementProposerPriority(times)
return copy
}
// IncrementAccum increments accum of each validator and updates the
// IncrementProposerPriority increments ProposerPriority of each validator and updates the
// proposer. Panics if validator set is empty.
// `times` must be positive.
func (vals *ValidatorSet) IncrementAccum(times int) {
func (vals *ValidatorSet) IncrementProposerPriority(times int) {
if times <= 0 {
panic("Cannot call IncrementAccum with non-positive times")
panic("Cannot call IncrementProposerPriority with non-positive times")
}
const shiftEveryNthIter = 10
var proposer *Validator
// call IncrementAccum(1) times times:
for i := 0; i < times; i++ {
shiftByAvgAccum := i%shiftEveryNthIter == 0
proposer = vals.incrementAccum(shiftByAvgAccum)
shiftByAvgProposerPriority := i%shiftEveryNthIter == 0
proposer = vals.incrementProposerPriority(shiftByAvgProposerPriority)
}
isShiftedAvgOnLastIter := (times-1)%shiftEveryNthIter == 0
if !isShiftedAvgOnLastIter {
validatorsHeap := cmn.NewHeap()
vals.shiftByAvgAccum(validatorsHeap)
vals.shiftByAvgProposerPriority(validatorsHeap)
}
vals.Proposer = proposer
}
func (vals *ValidatorSet) incrementAccum(subAvg bool) *Validator {
func (vals *ValidatorSet) incrementProposerPriority(subAvg bool) *Validator {
for _, val := range vals.Validators {
// Check for overflow for sum.
val.Accum = safeAddClip(val.Accum, val.VotingPower)
val.ProposerPriority = safeAddClip(val.ProposerPriority, val.VotingPower)
}
validatorsHeap := cmn.NewHeap()
if subAvg { // shift by avg accum
vals.shiftByAvgAccum(validatorsHeap)
if subAvg { // shift by avg ProposerPriority
vals.shiftByAvgProposerPriority(validatorsHeap)
} else { // just update the heap
for _, val := range vals.Validators {
validatorsHeap.PushComparable(val, accumComparable{val})
validatorsHeap.PushComparable(val, proposerPriorityComparable{val})
}
}
// Decrement the validator with most accum:
// Decrement the validator with most ProposerPriority:
mostest := validatorsHeap.Peek().(*Validator)
// mind underflow
mostest.Accum = safeSubClip(mostest.Accum, vals.TotalVotingPower())
mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower())
return mostest
}
func (vals *ValidatorSet) computeAvgAccum() int64 {
func (vals *ValidatorSet) computeAvgProposerPriority() int64 {
n := int64(len(vals.Validators))
sum := big.NewInt(0)
for _, val := range vals.Validators {
sum.Add(sum, big.NewInt(val.Accum))
sum.Add(sum, big.NewInt(val.ProposerPriority))
}
avg := sum.Div(sum, big.NewInt(n))
if avg.IsInt64() {
return avg.Int64()
}
// this should never happen: each val.Accum is in bounds of int64
panic(fmt.Sprintf("Cannot represent avg accum as an int64 %v", avg))
// this should never happen: each val.ProposerPriority is in bounds of int64
panic(fmt.Sprintf("Cannot represent avg ProposerPriority as an int64 %v", avg))
}
func (vals *ValidatorSet) shiftByAvgAccum(validatorsHeap *cmn.Heap) {
avgAccum := vals.computeAvgAccum()
func (vals *ValidatorSet) shiftByAvgProposerPriority(validatorsHeap *cmn.Heap) {
avgProposerPriority := vals.computeAvgProposerPriority()
for _, val := range vals.Validators {
val.Accum = safeSubClip(val.Accum, avgAccum)
validatorsHeap.PushComparable(val, accumComparable{val})
val.ProposerPriority = safeSubClip(val.ProposerPriority, avgProposerPriority)
validatorsHeap.PushComparable(val, proposerPriorityComparable{val})
}
}
@ -143,7 +143,7 @@ func (vals *ValidatorSet) shiftByAvgAccum(validatorsHeap *cmn.Heap) {
func (vals *ValidatorSet) Copy() *ValidatorSet {
validators := make([]*Validator, len(vals.Validators))
for i, val := range vals.Validators {
// NOTE: must copy, since IncrementAccum updates in place.
// NOTE: must copy, since IncrementProposerPriority updates in place.
validators[i] = val.Copy()
}
return &ValidatorSet{
@ -225,7 +225,7 @@ func (vals *ValidatorSet) findProposer() *Validator {
var proposer *Validator
for _, val := range vals.Validators {
if proposer == nil || !bytes.Equal(val.Address, proposer.Address) {
proposer = proposer.CompareAccum(val)
proposer = proposer.CompareProposerPriority(val)
}
}
return proposer
@ -500,16 +500,16 @@ func (valz ValidatorsByAddress) Swap(i, j int) {
}
//-------------------------------------
// Use with Heap for sorting validators by accum
// Use with Heap for sorting validators by ProposerPriority
type accumComparable struct {
type proposerPriorityComparable struct {
*Validator
}
// We want to find the validator with the greatest accum.
func (ac accumComparable) Less(o interface{}) bool {
other := o.(accumComparable).Validator
larger := ac.CompareAccum(other)
// We want to find the validator with the greatest ProposerPriority.
func (ac proposerPriorityComparable) Less(o interface{}) bool {
other := o.(proposerPriorityComparable).Validator
larger := ac.CompareProposerPriority(other)
return bytes.Equal(larger.Address, ac.Address)
}


+ 32
- 32
types/validator_set_test.go View File

@ -18,12 +18,12 @@ import (
func TestValidatorSetBasic(t *testing.T) {
// empty or nil validator lists are allowed,
// but attempting to IncrementAccum on them will panic.
// but attempting to IncrementProposerPriority on them will panic.
vset := NewValidatorSet([]*Validator{})
assert.Panics(t, func() { vset.IncrementAccum(1) })
assert.Panics(t, func() { vset.IncrementProposerPriority(1) })
vset = NewValidatorSet(nil)
assert.Panics(t, func() { vset.IncrementAccum(1) })
assert.Panics(t, func() { vset.IncrementProposerPriority(1) })
assert.EqualValues(t, vset, vset.Copy())
assert.False(t, vset.HasAddress([]byte("some val")))
@ -58,7 +58,7 @@ func TestValidatorSetBasic(t *testing.T) {
assert.Equal(t, val.VotingPower, vset.TotalVotingPower())
assert.Equal(t, val, vset.GetProposer())
assert.NotNil(t, vset.Hash())
assert.NotPanics(t, func() { vset.IncrementAccum(1) })
assert.NotPanics(t, func() { vset.IncrementProposerPriority(1) })
// update
assert.False(t, vset.Update(randValidator_()))
@ -89,17 +89,17 @@ func TestCopy(t *testing.T) {
}
}
// Test that IncrementAccum requires positive times.
func TestIncrementAccumPositiveTimes(t *testing.T) {
// Test that IncrementProposerPriority requires positive times.
func TestIncrementProposerPriorityPositiveTimes(t *testing.T) {
vset := NewValidatorSet([]*Validator{
newValidator([]byte("foo"), 1000),
newValidator([]byte("bar"), 300),
newValidator([]byte("baz"), 330),
})
assert.Panics(t, func() { vset.IncrementAccum(-1) })
assert.Panics(t, func() { vset.IncrementAccum(0) })
vset.IncrementAccum(1)
assert.Panics(t, func() { vset.IncrementProposerPriority(-1) })
assert.Panics(t, func() { vset.IncrementProposerPriority(0) })
vset.IncrementProposerPriority(1)
}
func BenchmarkValidatorSetCopy(b *testing.B) {
@ -132,7 +132,7 @@ func TestProposerSelection1(t *testing.T) {
for i := 0; i < 99; i++ {
val := vset.GetProposer()
proposers = append(proposers, string(val.Address))
vset.IncrementAccum(1)
vset.IncrementProposerPriority(1)
}
expected := `foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo foo baz bar foo foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo`
if expected != strings.Join(proposers, " ") {
@ -155,18 +155,18 @@ func TestProposerSelection2(t *testing.T) {
if !bytes.Equal(prop.Address, valList[ii].Address) {
t.Fatalf("(%d): Expected %X. Got %X", i, valList[ii].Address, prop.Address)
}
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
}
// One validator has more than the others, but not enough to propose twice in a row
*val2 = *newValidator(addr2, 400)
vals = NewValidatorSet(valList)
// vals.IncrementAccum(1)
// vals.IncrementProposerPriority(1)
prop := vals.GetProposer()
if !bytes.Equal(prop.Address, addr2) {
t.Fatalf("Expected address with highest voting power to be first proposer. Got %X", prop.Address)
}
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
prop = vals.GetProposer()
if !bytes.Equal(prop.Address, addr0) {
t.Fatalf("Expected smallest address to be validator. Got %X", prop.Address)
@ -179,12 +179,12 @@ func TestProposerSelection2(t *testing.T) {
if !bytes.Equal(prop.Address, addr2) {
t.Fatalf("Expected address with highest voting power to be first proposer. Got %X", prop.Address)
}
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
prop = vals.GetProposer()
if !bytes.Equal(prop.Address, addr2) {
t.Fatalf("Expected address with highest voting power to be second proposer. Got %X", prop.Address)
}
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
prop = vals.GetProposer()
if !bytes.Equal(prop.Address, addr0) {
t.Fatalf("Expected smallest address to be validator. Got %X", prop.Address)
@ -200,7 +200,7 @@ func TestProposerSelection2(t *testing.T) {
prop := vals.GetProposer()
ii := prop.Address[19]
propCount[ii]++
vals.IncrementAccum(1)
vals.IncrementProposerPriority(1)
}
if propCount[0] != 40*N {
@ -225,12 +225,12 @@ func TestProposerSelection3(t *testing.T) {
proposerOrder := make([]*Validator, 4)
for i := 0; i < 4; i++ {
proposerOrder[i] = vset.GetProposer()
vset.IncrementAccum(1)
vset.IncrementProposerPriority(1)
}
// i for the loop
// j for the times
// we should go in order for ever, despite some IncrementAccums with times > 1
// we should go in order for ever, despite some IncrementProposerPriority with times > 1
var i, j int
for ; i < 10000; i++ {
got := vset.GetProposer().Address
@ -257,7 +257,7 @@ func TestProposerSelection3(t *testing.T) {
// sometimes its up to 5
times = (cmn.RandInt() % 4) + 1
}
vset.IncrementAccum(times)
vset.IncrementProposerPriority(times)
j += times
}
@ -275,7 +275,7 @@ func randPubKey() crypto.PubKey {
func randValidator_() *Validator {
val := NewValidator(randPubKey(), cmn.RandInt64())
val.Accum = cmn.RandInt64()
val.ProposerPriority = cmn.RandInt64() % MaxTotalVotingPower
return val
}
@ -306,33 +306,33 @@ func (valSet *ValidatorSet) fromBytes(b []byte) {
//-------------------------------------------------------------------
func TestValidatorSetTotalVotingPowerPanicsOnOverflow(t *testing.T) {
// NewValidatorSet calls IncrementAccum which calls TotalVotingPower()
// NewValidatorSet calls IncrementProposerPriority which calls TotalVotingPower()
// which should panic on overflows:
shouldPanic := func() {
NewValidatorSet([]*Validator{
{Address: []byte("a"), VotingPower: math.MaxInt64, Accum: 0},
{Address: []byte("b"), VotingPower: math.MaxInt64, Accum: 0},
{Address: []byte("c"), VotingPower: math.MaxInt64, Accum: 0},
{Address: []byte("a"), VotingPower: math.MaxInt64, ProposerPriority: 0},
{Address: []byte("b"), VotingPower: math.MaxInt64, ProposerPriority: 0},
{Address: []byte("c"), VotingPower: math.MaxInt64, ProposerPriority: 0},
})
}
assert.Panics(t, shouldPanic)
}
func TestAvgAccum(t *testing.T) {
// Create Validator set without calling IncrementAccum:
func TestAvgProposerPriority(t *testing.T) {
// Create Validator set without calling IncrementProposerPriority:
tcs := []struct {
vs ValidatorSet
want int64
}{
0: {ValidatorSet{Validators: []*Validator{{Accum: 0}, {Accum: 0}, {Accum: 0}}}, 0},
1: {ValidatorSet{Validators: []*Validator{{Accum: math.MaxInt64}, {Accum: 0}, {Accum: 0}}}, math.MaxInt64 / 3},
2: {ValidatorSet{Validators: []*Validator{{Accum: math.MaxInt64}, {Accum: 0}}}, math.MaxInt64 / 2},
3: {ValidatorSet{Validators: []*Validator{{Accum: math.MaxInt64}, {Accum: math.MaxInt64}}}, math.MaxInt64},
4: {ValidatorSet{Validators: []*Validator{{Accum: math.MinInt64}, {Accum: math.MinInt64}}}, math.MinInt64},
0: {ValidatorSet{Validators: []*Validator{{ProposerPriority: 0}, {ProposerPriority: 0}, {ProposerPriority: 0}}}, 0},
1: {ValidatorSet{Validators: []*Validator{{ProposerPriority: math.MaxInt64}, {ProposerPriority: 0}, {ProposerPriority: 0}}}, math.MaxInt64 / 3},
2: {ValidatorSet{Validators: []*Validator{{ProposerPriority: math.MaxInt64}, {ProposerPriority: 0}}}, math.MaxInt64 / 2},
3: {ValidatorSet{Validators: []*Validator{{ProposerPriority: math.MaxInt64}, {ProposerPriority: math.MaxInt64}}}, math.MaxInt64},
4: {ValidatorSet{Validators: []*Validator{{ProposerPriority: math.MinInt64}, {ProposerPriority: math.MinInt64}}}, math.MinInt64},
}
for i, tc := range tcs {
got := tc.vs.computeAvgAccum()
got := tc.vs.computeAvgProposerPriority()
assert.Equal(t, tc.want, got, "test case: %v", i)
}


Loading…
Cancel
Save