Browse Source

types: valSet LastProposer->Proposer and Proposer()->GetProposer()

pull/426/head
Ethan Buchman 8 years ago
parent
commit
b1cd677711
4 changed files with 32 additions and 32 deletions
  1. +4
    -4
      consensus/state.go
  2. +3
    -3
      consensus/state_test.go
  3. +14
    -14
      types/validator_set.go
  4. +11
    -11
      types/validator_set_test.go

+ 4
- 4
consensus/state.go View File

@ -806,10 +806,10 @@ func (cs *ConsensusState) enterPropose(height int, round int) {
return
}
if !bytes.Equal(cs.Validators.Proposer().Address, cs.privValidator.GetAddress()) {
log.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.Proposer().Address, "privValidator", cs.privValidator)
if !bytes.Equal(cs.Validators.GetProposer().Address, cs.privValidator.GetAddress()) {
log.Info("enterPropose: Not our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
} else {
log.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.Proposer().Address, "privValidator", cs.privValidator)
log.Info("enterPropose: Our turn to propose", "proposer", cs.Validators.GetProposer().Address, "privValidator", cs.privValidator)
cs.decideProposal(height, round)
}
@ -1283,7 +1283,7 @@ func (cs *ConsensusState) defaultSetProposal(proposal *types.Proposal) error {
}
// Verify signature
if !cs.Validators.Proposer().PubKey.VerifyBytes(types.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
if !cs.Validators.GetProposer().PubKey.VerifyBytes(types.SignBytes(cs.state.ChainID, proposal), proposal.Signature) {
return ErrInvalidProposalSignature
}


+ 3
- 3
consensus/state_test.go View File

@ -65,7 +65,7 @@ func TestProposerSelection0(t *testing.T) {
<-newRoundCh
// lets commit a block and ensure proposer for the next height is correct
prop := cs1.GetRoundState().Validators.Proposer()
prop := cs1.GetRoundState().Validators.GetProposer()
if !bytes.Equal(prop.Address, cs1.privValidator.GetAddress()) {
t.Fatalf("expected proposer to be validator %d. Got %X", 0, prop.Address)
}
@ -79,7 +79,7 @@ func TestProposerSelection0(t *testing.T) {
// wait for new round so next validator is set
<-newRoundCh
prop = cs1.GetRoundState().Validators.Proposer()
prop = cs1.GetRoundState().Validators.GetProposer()
if !bytes.Equal(prop.Address, vss[1].Address) {
panic(Fmt("expected proposer to be validator %d. Got %X", 1, prop.Address))
}
@ -100,7 +100,7 @@ func TestProposerSelection2(t *testing.T) {
// everyone just votes nil. we get a new proposer each round
for i := 0; i < len(vss); i++ {
prop := cs1.GetRoundState().Validators.Proposer()
prop := cs1.GetRoundState().Validators.GetProposer()
if !bytes.Equal(prop.Address, vss[(i+2)%len(vss)].Address) {
panic(Fmt("expected proposer to be validator %d. Got %X", (i+2)%len(vss), prop.Address))
}


+ 14
- 14
types/validator_set.go View File

@ -16,15 +16,15 @@ import (
// 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
// the designated .Proposer() of a set changes every round,
// the designated .GetProposer() of a set changes every round,
// upon calling .IncrementAccum().
// NOTE: Not goroutine-safe.
// NOTE: All get/set to validators should copy the value for safety.
// TODO: consider validator Accum overflow
// TODO: move valset into an iavl tree where key is 'blockbonded|pubkey'
type ValidatorSet struct {
Validators []*Validator // NOTE: persisted via reflect, must be exported.
LastProposer *Validator
Validators []*Validator // NOTE: persisted via reflect, must be exported.
Proposer *Validator
// cached (unexported)
totalVotingPower int64
@ -61,7 +61,7 @@ func (valSet *ValidatorSet) IncrementAccum(times int) {
for i := 0; i < times; i++ {
mostest := validatorsHeap.Peek().(*Validator)
if i == times-1 {
valSet.LastProposer = mostest
valSet.Proposer = mostest
}
mostest.Accum -= int64(valSet.TotalVotingPower())
validatorsHeap.Update(mostest, accumComparable{mostest})
@ -76,7 +76,7 @@ func (valSet *ValidatorSet) Copy() *ValidatorSet {
}
return &ValidatorSet{
Validators: validators,
LastProposer: valSet.LastProposer,
Proposer: valSet.Proposer,
totalVotingPower: valSet.totalVotingPower,
}
}
@ -117,14 +117,14 @@ func (valSet *ValidatorSet) TotalVotingPower() int64 {
return valSet.totalVotingPower
}
func (valSet *ValidatorSet) Proposer() (proposer *Validator) {
func (valSet *ValidatorSet) GetProposer() (proposer *Validator) {
if len(valSet.Validators) == 0 {
return nil
}
if valSet.LastProposer == nil {
valSet.LastProposer = valSet.findProposer()
if valSet.Proposer == nil {
valSet.Proposer = valSet.findProposer()
}
return valSet.LastProposer.Copy()
return valSet.Proposer.Copy()
}
func (valSet *ValidatorSet) findProposer() *Validator {
@ -156,7 +156,7 @@ func (valSet *ValidatorSet) Add(val *Validator) (added bool) {
if idx == len(valSet.Validators) {
valSet.Validators = append(valSet.Validators, val)
// Invalidate cache
valSet.LastProposer = nil
valSet.Proposer = nil
valSet.totalVotingPower = 0
return true
} else if bytes.Compare(valSet.Validators[idx].Address, val.Address) == 0 {
@ -168,7 +168,7 @@ func (valSet *ValidatorSet) Add(val *Validator) (added bool) {
copy(newValidators[idx+1:], valSet.Validators[idx:])
valSet.Validators = newValidators
// Invalidate cache
valSet.LastProposer = nil
valSet.Proposer = nil
valSet.totalVotingPower = 0
return true
}
@ -181,7 +181,7 @@ func (valSet *ValidatorSet) Update(val *Validator) (updated bool) {
} else {
valSet.Validators[index] = val.Copy()
// Invalidate cache
valSet.LastProposer = nil
valSet.Proposer = nil
valSet.totalVotingPower = 0
return true
}
@ -201,7 +201,7 @@ func (valSet *ValidatorSet) Remove(address []byte) (val *Validator, removed bool
}
valSet.Validators = newValidators
// Invalidate cache
valSet.LastProposer = nil
valSet.Proposer = nil
valSet.totalVotingPower = 0
return removedVal, true
}
@ -325,7 +325,7 @@ func (valSet *ValidatorSet) StringIndented(indent string) string {
%s Validators:
%s %v
%s}`,
indent, valSet.Proposer().String(),
indent, valSet.GetProposer().String(),
indent,
indent, strings.Join(valStrings, "\n"+indent+" "),
indent)


+ 11
- 11
types/validator_set_test.go View File

@ -52,7 +52,7 @@ func TestProposerSelection1(t *testing.T) {
})
proposers := []string{}
for i := 0; i < 99; i++ {
val := vset.Proposer()
val := vset.GetProposer()
proposers = append(proposers, string(val.Address))
vset.IncrementAccum(1)
}
@ -77,7 +77,7 @@ func TestProposerSelection2(t *testing.T) {
vals := NewValidatorSet(valList)
for i := 0; i < len(valList)*5; i++ {
ii := (i) % len(valList)
prop := vals.Proposer()
prop := vals.GetProposer()
if !bytes.Equal(prop.Address, valList[ii].Address) {
t.Fatalf("(%d): Expected %X. Got %X", i, valList[ii].Address, prop.Address)
}
@ -88,12 +88,12 @@ func TestProposerSelection2(t *testing.T) {
*val2 = *newValidator(addr2, 400)
vals = NewValidatorSet(valList)
// vals.IncrementAccum(1)
prop := vals.Proposer()
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)
prop = vals.Proposer()
prop = vals.GetProposer()
if !bytes.Equal(prop.Address, addr0) {
t.Fatalf("Expected smallest address to be validator. Got %X", prop.Address)
}
@ -101,17 +101,17 @@ func TestProposerSelection2(t *testing.T) {
// One validator has more than the others, and enough to be proposer twice in a row
*val2 = *newValidator(addr2, 401)
vals = NewValidatorSet(valList)
prop = vals.Proposer()
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)
prop = vals.Proposer()
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)
prop = vals.Proposer()
prop = vals.GetProposer()
if !bytes.Equal(prop.Address, addr0) {
t.Fatalf("Expected smallest address to be validator. Got %X", prop.Address)
}
@ -123,7 +123,7 @@ func TestProposerSelection2(t *testing.T) {
vals = NewValidatorSet(valList)
N := 1
for i := 0; i < 120*N; i++ {
prop := vals.Proposer()
prop := vals.GetProposer()
ii := prop.Address[19]
propCount[ii] += 1
vals.IncrementAccum(1)
@ -150,7 +150,7 @@ func TestProposerSelection3(t *testing.T) {
proposerOrder := make([]*Validator, 4)
for i := 0; i < 4; i++ {
proposerOrder[i] = vset.Proposer()
proposerOrder[i] = vset.GetProposer()
vset.IncrementAccum(1)
}
@ -159,7 +159,7 @@ func TestProposerSelection3(t *testing.T) {
// we should go in order for ever, despite some IncrementAccums with times > 1
var i, j int
for ; i < 10000; i++ {
got := vset.Proposer().Address
got := vset.GetProposer().Address
expected := proposerOrder[j%4].Address
if !bytes.Equal(got, expected) {
t.Fatalf(cmn.Fmt("vset.Proposer (%X) does not match expected proposer (%X) for (%d, %d)", got, expected, i, j))
@ -169,7 +169,7 @@ func TestProposerSelection3(t *testing.T) {
b := vset.ToBytes()
vset.FromBytes(b)
computed := vset.Proposer() // findProposer()
computed := vset.GetProposer() // findGetProposer()
if i != 0 {
if !bytes.Equal(got, computed.Address) {
t.Fatalf(cmn.Fmt("vset.Proposer (%X) does not match computed proposer (%X) for (%d, %d)", got, computed.Address, i, j))


Loading…
Cancel
Save