You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "testing"
  5. cfg "github.com/tendermint/tendermint/config"
  6. "github.com/tendermint/tendermint/types"
  7. tmtime "github.com/tendermint/tendermint/types/time"
  8. )
  9. var config *cfg.Config // NOTE: must be reset for each _test.go file
  10. func init() {
  11. config = cfg.ResetTestRoot("consensus_height_vote_set_test")
  12. }
  13. func TestPeerCatchupRounds(t *testing.T) {
  14. valSet, privVals := types.RandValidatorSet(10, 1)
  15. hvs := NewHeightVoteSet(config.ChainID(), 1, valSet)
  16. vote999_0 := makeVoteHR(t, 1, 999, privVals, 0)
  17. added, err := hvs.AddVote(vote999_0, "peer1")
  18. if !added || err != nil {
  19. t.Error("Expected to successfully add vote from peer", added, err)
  20. }
  21. vote1000_0 := makeVoteHR(t, 1, 1000, privVals, 0)
  22. added, err = hvs.AddVote(vote1000_0, "peer1")
  23. if !added || err != nil {
  24. t.Error("Expected to successfully add vote from peer", added, err)
  25. }
  26. vote1001_0 := makeVoteHR(t, 1, 1001, privVals, 0)
  27. added, err = hvs.AddVote(vote1001_0, "peer1")
  28. if err != GotVoteFromUnwantedRoundError {
  29. t.Errorf("Expected GotVoteFromUnwantedRoundError, but got %v", err)
  30. }
  31. if added {
  32. t.Error("Expected to *not* add vote from peer, too many catchup rounds.")
  33. }
  34. added, err = hvs.AddVote(vote1001_0, "peer2")
  35. if !added || err != nil {
  36. t.Error("Expected to successfully add vote from another peer")
  37. }
  38. }
  39. func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivValidator, valIndex int) *types.Vote {
  40. privVal := privVals[valIndex]
  41. addr := privVal.GetPubKey().Address()
  42. vote := &types.Vote{
  43. ValidatorAddress: addr,
  44. ValidatorIndex: valIndex,
  45. Height: height,
  46. Round: round,
  47. Timestamp: tmtime.Now(),
  48. Type: types.PrecommitType,
  49. BlockID: types.BlockID{[]byte("fakehash"), types.PartSetHeader{}},
  50. }
  51. chainID := config.ChainID()
  52. err := privVal.SignVote(chainID, vote)
  53. if err != nil {
  54. panic(fmt.Sprintf("Error signing vote: %v", err))
  55. return nil
  56. }
  57. return vote
  58. }