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.

73 lines
2.0 KiB

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