2017-10-10 01:39:21 -07:00
|
|
|
package types
|
2015-12-31 15:02:38 -08:00
|
|
|
|
|
|
|
import (
|
2016-07-11 18:10:05 -07:00
|
|
|
"testing"
|
2017-12-11 09:42:58 -08:00
|
|
|
"time"
|
2016-07-11 18:10:05 -07:00
|
|
|
|
2017-10-10 01:39:21 -07:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2015-12-31 15:02:38 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2017-10-04 13:40:45 -07:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2015-12-31 15:02:38 -08:00
|
|
|
)
|
|
|
|
|
2017-10-10 01:39:21 -07:00
|
|
|
var config *cfg.Config // NOTE: must be reset for each _test.go file
|
|
|
|
|
2016-03-06 15:05:50 -08:00
|
|
|
func init() {
|
2017-10-10 01:39:21 -07:00
|
|
|
config = cfg.ResetTestRoot("consensus_height_vote_set_test")
|
2016-03-06 15:05:50 -08:00
|
|
|
}
|
|
|
|
|
2015-12-31 15:02:38 -08:00
|
|
|
func TestPeerCatchupRounds(t *testing.T) {
|
|
|
|
valSet, privVals := types.RandValidatorSet(10, 1)
|
|
|
|
|
2017-12-10 17:43:58 -08:00
|
|
|
hvs := NewHeightVoteSet(config.ChainID(), 1, valSet)
|
2015-12-31 15:02:38 -08:00
|
|
|
|
2016-07-01 14:47:31 -07:00
|
|
|
vote999_0 := makeVoteHR(t, 1, 999, privVals, 0)
|
|
|
|
added, err := hvs.AddVote(vote999_0, "peer1")
|
2015-12-31 15:02:38 -08:00
|
|
|
if !added || err != nil {
|
|
|
|
t.Error("Expected to successfully add vote from peer", added, err)
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:47:31 -07:00
|
|
|
vote1000_0 := makeVoteHR(t, 1, 1000, privVals, 0)
|
|
|
|
added, err = hvs.AddVote(vote1000_0, "peer1")
|
2016-09-16 09:20:07 -07:00
|
|
|
if !added || err != nil {
|
|
|
|
t.Error("Expected to successfully add vote from peer", added, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vote1001_0 := makeVoteHR(t, 1, 1001, privVals, 0)
|
|
|
|
added, err = hvs.AddVote(vote1001_0, "peer1")
|
2018-03-05 03:35:50 -08:00
|
|
|
if err != GotVoteFromUnwantedRoundError {
|
|
|
|
t.Errorf("Expected GotVoteFromUnwantedRoundError, but got %v", err)
|
2017-05-29 20:11:40 -07:00
|
|
|
}
|
2015-12-31 15:02:38 -08:00
|
|
|
if added {
|
|
|
|
t.Error("Expected to *not* add vote from peer, too many catchup rounds.")
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:20:07 -07:00
|
|
|
added, err = hvs.AddVote(vote1001_0, "peer2")
|
2015-12-31 15:02:38 -08:00
|
|
|
if !added || err != nil {
|
|
|
|
t.Error("Expected to successfully add vote from another peer")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-03 07:03:08 -07:00
|
|
|
func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivValidator, valIndex int) *types.Vote {
|
2016-07-01 14:47:31 -07:00
|
|
|
privVal := privVals[valIndex]
|
2015-12-31 15:02:38 -08:00
|
|
|
vote := &types.Vote{
|
2017-09-21 13:32:02 -07:00
|
|
|
ValidatorAddress: privVal.GetAddress(),
|
2016-07-01 14:47:31 -07:00
|
|
|
ValidatorIndex: valIndex,
|
|
|
|
Height: height,
|
|
|
|
Round: round,
|
2017-12-11 09:42:58 -08:00
|
|
|
Timestamp: time.Now().UTC(),
|
2016-07-01 14:47:31 -07:00
|
|
|
Type: types.VoteTypePrecommit,
|
2016-08-16 14:59:19 -07:00
|
|
|
BlockID: types.BlockID{[]byte("fakehash"), types.PartSetHeader{}},
|
2015-12-31 15:02:38 -08:00
|
|
|
}
|
2017-12-10 17:43:58 -08:00
|
|
|
chainID := config.ChainID()
|
2015-12-31 15:02:38 -08:00
|
|
|
err := privVal.SignVote(chainID, vote)
|
|
|
|
if err != nil {
|
2017-10-04 13:40:45 -07:00
|
|
|
panic(cmn.Fmt("Error signing vote: %v", err))
|
2015-12-31 15:02:38 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return vote
|
|
|
|
}
|