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.

127 lines
3.1 KiB

  1. package client_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. tmrand "github.com/tendermint/tendermint/libs/rand"
  9. "github.com/tendermint/tendermint/privval"
  10. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  11. "github.com/tendermint/tendermint/rpc/client"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. // For some reason the empty node used in tests has a time of
  15. // 2018-10-10 08:20:13.695936996 +0000 UTC
  16. // this is because the test genesis time is set here
  17. // so in order to validate evidence we need evidence to be the same time
  18. var defaultTestTime = time.Date(2018, 10, 10, 8, 20, 13, 695936996, time.UTC)
  19. func newEvidence(t *testing.T, val *privval.FilePV,
  20. vote *types.Vote, vote2 *types.Vote,
  21. chainID string) *types.DuplicateVoteEvidence {
  22. t.Helper()
  23. var err error
  24. v := vote.ToProto()
  25. v2 := vote2.ToProto()
  26. vote.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v))
  27. require.NoError(t, err)
  28. vote2.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v2))
  29. require.NoError(t, err)
  30. validator := types.NewValidator(val.Key.PubKey, 10)
  31. valSet := types.NewValidatorSet([]*types.Validator{validator})
  32. ev, err := types.NewDuplicateVoteEvidence(vote, vote2, defaultTestTime, valSet)
  33. require.NoError(t, err)
  34. return ev
  35. }
  36. func makeEvidences(
  37. t *testing.T,
  38. val *privval.FilePV,
  39. chainID string,
  40. ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
  41. vote := types.Vote{
  42. ValidatorAddress: val.Key.Address,
  43. ValidatorIndex: 0,
  44. Height: 1,
  45. Round: 0,
  46. Type: tmproto.PrevoteType,
  47. Timestamp: defaultTestTime,
  48. BlockID: types.BlockID{
  49. Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
  50. PartSetHeader: types.PartSetHeader{
  51. Total: 1000,
  52. Hash: tmhash.Sum([]byte("partset")),
  53. },
  54. },
  55. }
  56. vote2 := vote
  57. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  58. correct = newEvidence(t, val, &vote, &vote2, chainID)
  59. fakes = make([]*types.DuplicateVoteEvidence, 0)
  60. // different address
  61. {
  62. v := vote2
  63. v.ValidatorAddress = []byte("some_address")
  64. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  65. }
  66. // different height
  67. {
  68. v := vote2
  69. v.Height = vote.Height + 1
  70. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  71. }
  72. // different round
  73. {
  74. v := vote2
  75. v.Round = vote.Round + 1
  76. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  77. }
  78. // different type
  79. {
  80. v := vote2
  81. v.Type = tmproto.PrecommitType
  82. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  83. }
  84. // exactly same vote
  85. {
  86. v := vote
  87. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  88. }
  89. return correct, fakes
  90. }
  91. func waitForBlock(ctx context.Context, t *testing.T, c client.Client, height int64) {
  92. timer := time.NewTimer(0 * time.Millisecond)
  93. defer timer.Stop()
  94. for {
  95. select {
  96. case <-ctx.Done():
  97. return
  98. case <-timer.C:
  99. status, err := c.Status(ctx)
  100. require.NoError(t, err)
  101. if status.SyncInfo.LatestBlockHeight >= height {
  102. return
  103. }
  104. timer.Reset(200 * time.Millisecond)
  105. }
  106. }
  107. }