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.

87 lines
2.3 KiB

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