mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-06 21:36:26 +00:00
consensus: fix TestSimulateValidatorsChange
* consensus: fix TestSimulateValidatorsChange * fix selfIndex calculation * exit from goroutine after 1 sec * reuse the function
This commit is contained in:
@@ -70,6 +70,7 @@ type validatorStub struct {
|
||||
Height int64
|
||||
Round int
|
||||
types.PrivValidator
|
||||
VotingPower int64
|
||||
}
|
||||
|
||||
var testMinPower int64 = 10
|
||||
@@ -78,6 +79,7 @@ func newValidatorStub(privValidator types.PrivValidator, valIndex int) *validato
|
||||
return &validatorStub{
|
||||
Index: valIndex,
|
||||
PrivValidator: privValidator,
|
||||
VotingPower: testMinPower,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,13 +140,13 @@ func incrementRound(vss ...*validatorStub) {
|
||||
}
|
||||
}
|
||||
|
||||
type ValidatorStubsByAddress []*validatorStub
|
||||
type ValidatorStubsByPower []*validatorStub
|
||||
|
||||
func (vss ValidatorStubsByAddress) Len() int {
|
||||
func (vss ValidatorStubsByPower) Len() int {
|
||||
return len(vss)
|
||||
}
|
||||
|
||||
func (vss ValidatorStubsByAddress) Less(i, j int) bool {
|
||||
func (vss ValidatorStubsByPower) Less(i, j int) bool {
|
||||
vssi, err := vss[i].GetPubKey()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -153,10 +155,14 @@ func (vss ValidatorStubsByAddress) Less(i, j int) bool {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return bytes.Compare(vssi.Address(), vssj.Address()) == -1
|
||||
|
||||
if vss[i].VotingPower == vss[j].VotingPower {
|
||||
return bytes.Compare(vssi.Address(), vssj.Address()) == -1
|
||||
}
|
||||
return vss[i].VotingPower > vss[j].VotingPower
|
||||
}
|
||||
|
||||
func (vss ValidatorStubsByAddress) Swap(i, j int) {
|
||||
func (vss ValidatorStubsByPower) Swap(i, j int) {
|
||||
it := vss[i]
|
||||
vss[i] = vss[j]
|
||||
vss[i].Index = i
|
||||
|
||||
@@ -333,6 +333,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
vss[i] = newValidatorStub(css[i].privValidator, i)
|
||||
}
|
||||
height, round := css[0].Height, css[0].Round
|
||||
|
||||
// start the machine
|
||||
startTestRound(css[0], height, round)
|
||||
incrementHeight(vss...)
|
||||
@@ -342,7 +343,9 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
|
||||
ensureNewRound(newRoundCh, height+1, 0)
|
||||
|
||||
//height 2
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// HEIGHT 2
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
height++
|
||||
incrementHeight(vss...)
|
||||
newValidatorPubKey1, err := css[nVals].privValidator.GetPubKey()
|
||||
@@ -368,7 +371,9 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
|
||||
ensureNewRound(newRoundCh, height+1, 0)
|
||||
|
||||
//height 3
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// HEIGHT 3
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
height++
|
||||
incrementHeight(vss...)
|
||||
updateValidatorPubKey1, err := css[nVals].privValidator.GetPubKey()
|
||||
@@ -394,7 +399,9 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
signAddVotes(css[0], types.PrecommitType, rs.ProposalBlock.Hash(), rs.ProposalBlockParts.Header(), vss[1:nVals]...)
|
||||
ensureNewRound(newRoundCh, height+1, 0)
|
||||
|
||||
//height 4
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// HEIGHT 4
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
height++
|
||||
incrementHeight(vss...)
|
||||
newValidatorPubKey2, err := css[nVals+1].privValidator.GetPubKey()
|
||||
@@ -414,21 +421,25 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
|
||||
newVss := make([]*validatorStub, nVals+1)
|
||||
copy(newVss, vss[:nVals+1])
|
||||
sort.Sort(ValidatorStubsByAddress(newVss))
|
||||
selfIndex := 0
|
||||
for i, vs := range newVss {
|
||||
vsPubKey, err := vs.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
sort.Sort(ValidatorStubsByPower(newVss))
|
||||
|
||||
css0PubKey, err := css[0].privValidator.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
valIndexFn := func(cssIdx int) int {
|
||||
for i, vs := range newVss {
|
||||
vsPubKey, err := vs.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
|
||||
if vsPubKey.Equals(css0PubKey) {
|
||||
selfIndex = i
|
||||
break
|
||||
cssPubKey, err := css[cssIdx].privValidator.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
|
||||
if vsPubKey.Equals(cssPubKey) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("validator css[%d] not found in newVss", cssIdx))
|
||||
}
|
||||
|
||||
selfIndex := valIndexFn(0)
|
||||
|
||||
proposal = types.NewProposal(vss[3].Height, round, -1, blockID)
|
||||
if err := vss[3].SignProposal(config.ChainID(), proposal); err != nil {
|
||||
t.Fatal("failed to sign bad proposal", err)
|
||||
@@ -454,9 +465,16 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
|
||||
ensureNewRound(newRoundCh, height+1, 0)
|
||||
|
||||
//height 5
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// HEIGHT 5
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
height++
|
||||
incrementHeight(vss...)
|
||||
// Reflect the changes to vss[nVals] at height 3 and resort newVss.
|
||||
newVssIdx := valIndexFn(nVals)
|
||||
newVss[newVssIdx].VotingPower = 25
|
||||
sort.Sort(ValidatorStubsByPower(newVss))
|
||||
selfIndex = valIndexFn(0)
|
||||
ensureNewProposal(proposalCh, height, round)
|
||||
rs = css[0].GetRoundState()
|
||||
for i := 0; i < nVals+1; i++ {
|
||||
@@ -467,7 +485,9 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
}
|
||||
ensureNewRound(newRoundCh, height+1, 0)
|
||||
|
||||
//height 6
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// HEIGHT 6
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
height++
|
||||
incrementHeight(vss...)
|
||||
removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
|
||||
@@ -478,19 +498,9 @@ func TestSimulateValidatorsChange(t *testing.T) {
|
||||
blockID = types.BlockID{Hash: propBlock.Hash(), PartsHeader: propBlockParts.Header()}
|
||||
newVss = make([]*validatorStub, nVals+3)
|
||||
copy(newVss, vss[:nVals+3])
|
||||
sort.Sort(ValidatorStubsByAddress(newVss))
|
||||
for i, vs := range newVss {
|
||||
vsKeyKey, err := vs.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
sort.Sort(ValidatorStubsByPower(newVss))
|
||||
|
||||
css0PubKey, err := css[0].privValidator.GetPubKey()
|
||||
require.NoError(t, err)
|
||||
|
||||
if vsKeyKey.Equals(css0PubKey) {
|
||||
selfIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
selfIndex = valIndexFn(0)
|
||||
proposal = types.NewProposal(vss[1].Height, round, -1, blockID)
|
||||
if err := vss[1].SignProposal(config.ChainID(), proposal); err != nil {
|
||||
t.Fatal("failed to sign bad proposal", err)
|
||||
|
||||
@@ -178,13 +178,17 @@ func TestEventBusPublishEventTxDuplicateKeys(t *testing.T) {
|
||||
done := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
msg := <-sub.Out()
|
||||
data := msg.Data().(EventDataTx)
|
||||
assert.Equal(t, int64(1), data.Height)
|
||||
assert.Equal(t, uint32(0), data.Index)
|
||||
assert.Equal(t, tx, data.Tx)
|
||||
assert.Equal(t, result, data.Result)
|
||||
close(done)
|
||||
select {
|
||||
case msg := <-sub.Out():
|
||||
data := msg.Data().(EventDataTx)
|
||||
assert.Equal(t, int64(1), data.Height)
|
||||
assert.Equal(t, uint32(0), data.Index)
|
||||
assert.Equal(t, tx, data.Tx)
|
||||
assert.Equal(t, result, data.Result)
|
||||
close(done)
|
||||
case <-time.After(1 * time.Second):
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
err = eventBus.PublishEventTx(EventDataTx{TxResult{
|
||||
|
||||
Reference in New Issue
Block a user