From bc71e38bade95859804177bf5f24f54e687a001e Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sat, 4 Jul 2015 15:12:00 -0700 Subject: [PATCH] proposer selection fix --- state/validator_set.go | 2 +- state/validator_set_test.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/state/validator_set.go b/state/validator_set.go index fad0413d1..27f1c29da 100644 --- a/state/validator_set.go +++ b/state/validator_set.go @@ -298,5 +298,5 @@ type accumComparable int64 // We want to find the validator with the greatest accum. func (ac accumComparable) Less(o interface{}) bool { - return int64(ac) < int64(o.(accumComparable)) + return int64(ac) > int64(o.(accumComparable)) } diff --git a/state/validator_set_test.go b/state/validator_set_test.go index dc74d4841..23f83f583 100644 --- a/state/validator_set_test.go +++ b/state/validator_set_test.go @@ -5,7 +5,7 @@ import ( . "github.com/tendermint/tendermint/common" "bytes" - "fmt" + "strings" "testing" ) @@ -43,12 +43,39 @@ func TestCopy(t *testing.T) { } func TestProposerSelection(t *testing.T) { - vset := randValidatorSet(10) + vset := NewValidatorSet([]*Validator{ + &Validator{ + Address: []byte("foo"), + PubKey: account.PubKeyEd25519(RandBytes(64)), + BondHeight: RandInt(), + VotingPower: 1000, + Accum: 0, + }, + &Validator{ + Address: []byte("bar"), + PubKey: account.PubKeyEd25519(RandBytes(64)), + BondHeight: RandInt(), + VotingPower: 300, + Accum: 0, + }, + &Validator{ + Address: []byte("baz"), + PubKey: account.PubKeyEd25519(RandBytes(64)), + BondHeight: RandInt(), + VotingPower: 330, + Accum: 0, + }, + }) + proposers := []string{} for i := 0; i < 100; i++ { val := vset.Proposer() - fmt.Printf("Proposer: %v\n", val) + proposers = append(proposers, string(val.Address)) vset.IncrementAccum(1) } + expected := `bar 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, " ") { + t.Errorf("Expected sequence of proposers was\n%v\nbut got \n%v", expected, strings.Join(proposers, " ")) + } } func BenchmarkValidatorSetCopy(b *testing.B) {