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.

124 lines
2.9 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. func newEvidence(t *testing.T, val *privval.FilePV,
  15. vote *types.Vote, vote2 *types.Vote,
  16. chainID string,
  17. timestamp time.Time,
  18. ) *types.DuplicateVoteEvidence {
  19. t.Helper()
  20. var err error
  21. v := vote.ToProto()
  22. v2 := vote2.ToProto()
  23. vote.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v))
  24. require.NoError(t, err)
  25. vote2.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v2))
  26. require.NoError(t, err)
  27. validator := types.NewValidator(val.Key.PubKey, 10)
  28. valSet := types.NewValidatorSet([]*types.Validator{validator})
  29. ev, err := types.NewDuplicateVoteEvidence(vote, vote2, timestamp, valSet)
  30. require.NoError(t, err)
  31. return ev
  32. }
  33. func makeEvidences(
  34. t *testing.T,
  35. val *privval.FilePV,
  36. chainID string,
  37. timestamp time.Time,
  38. ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
  39. vote := types.Vote{
  40. ValidatorAddress: val.Key.Address,
  41. ValidatorIndex: 0,
  42. Height: 1,
  43. Round: 0,
  44. Type: tmproto.PrevoteType,
  45. Timestamp: timestamp,
  46. BlockID: types.BlockID{
  47. Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
  48. PartSetHeader: types.PartSetHeader{
  49. Total: 1000,
  50. Hash: tmhash.Sum([]byte("partset")),
  51. },
  52. },
  53. }
  54. vote2 := vote
  55. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  56. correct = newEvidence(t, val, &vote, &vote2, chainID, timestamp)
  57. fakes = make([]*types.DuplicateVoteEvidence, 0)
  58. // different address
  59. {
  60. v := vote2
  61. v.ValidatorAddress = []byte("some_address")
  62. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID, timestamp))
  63. }
  64. // different height
  65. {
  66. v := vote2
  67. v.Height = vote.Height + 1
  68. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID, timestamp))
  69. }
  70. // different round
  71. {
  72. v := vote2
  73. v.Round = vote.Round + 1
  74. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID, timestamp))
  75. }
  76. // different type
  77. {
  78. v := vote2
  79. v.Type = tmproto.PrecommitType
  80. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID, timestamp))
  81. }
  82. // exactly same vote
  83. {
  84. v := vote
  85. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID, timestamp))
  86. }
  87. return correct, fakes
  88. }
  89. func waitForBlock(ctx context.Context, t *testing.T, c client.Client, height int64) {
  90. timer := time.NewTimer(0 * time.Millisecond)
  91. defer timer.Stop()
  92. for {
  93. select {
  94. case <-ctx.Done():
  95. return
  96. case <-timer.C:
  97. status, err := c.Status(ctx)
  98. require.NoError(t, err)
  99. if status.SyncInfo.LatestBlockHeight >= height {
  100. return
  101. }
  102. timer.Reset(200 * time.Millisecond)
  103. }
  104. }
  105. }