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.

164 lines
4.7 KiB

  1. package client_test
  2. import (
  3. "bytes"
  4. "context"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  12. "github.com/tendermint/tendermint/crypto/tmhash"
  13. tmrand "github.com/tendermint/tendermint/libs/rand"
  14. "github.com/tendermint/tendermint/privval"
  15. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  16. "github.com/tendermint/tendermint/rpc/client"
  17. rpctest "github.com/tendermint/tendermint/rpc/test"
  18. "github.com/tendermint/tendermint/types"
  19. )
  20. // For some reason the empty node used in tests has a time of
  21. // 2018-10-10 08:20:13.695936996 +0000 UTC
  22. // this is because the test genesis time is set here
  23. // so in order to validate evidence we need evidence to be the same time
  24. var defaultTestTime = time.Date(2018, 10, 10, 8, 20, 13, 695936996, time.UTC)
  25. func newEvidence(t *testing.T, val *privval.FilePV,
  26. vote *types.Vote, vote2 *types.Vote,
  27. chainID string) *types.DuplicateVoteEvidence {
  28. var err error
  29. v := vote.ToProto()
  30. v2 := vote2.ToProto()
  31. vote.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v))
  32. require.NoError(t, err)
  33. vote2.Signature, err = val.Key.PrivKey.Sign(types.VoteSignBytes(chainID, v2))
  34. require.NoError(t, err)
  35. validator := types.NewValidator(val.Key.PubKey, 10)
  36. valSet := types.NewValidatorSet([]*types.Validator{validator})
  37. return types.NewDuplicateVoteEvidence(vote, vote2, defaultTestTime, valSet)
  38. }
  39. func makeEvidences(
  40. t *testing.T,
  41. val *privval.FilePV,
  42. chainID string,
  43. ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
  44. vote := types.Vote{
  45. ValidatorAddress: val.Key.Address,
  46. ValidatorIndex: 0,
  47. Height: 1,
  48. Round: 0,
  49. Type: tmproto.PrevoteType,
  50. Timestamp: defaultTestTime,
  51. BlockID: types.BlockID{
  52. Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
  53. PartSetHeader: types.PartSetHeader{
  54. Total: 1000,
  55. Hash: tmhash.Sum([]byte("partset")),
  56. },
  57. },
  58. }
  59. vote2 := vote
  60. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  61. correct = newEvidence(t, val, &vote, &vote2, chainID)
  62. fakes = make([]*types.DuplicateVoteEvidence, 0)
  63. // different address
  64. {
  65. v := vote2
  66. v.ValidatorAddress = []byte("some_address")
  67. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  68. }
  69. // different height
  70. {
  71. v := vote2
  72. v.Height = vote.Height + 1
  73. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  74. }
  75. // different round
  76. {
  77. v := vote2
  78. v.Round = vote.Round + 1
  79. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  80. }
  81. // different type
  82. {
  83. v := vote2
  84. v.Type = tmproto.PrecommitType
  85. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  86. }
  87. // exactly same vote
  88. {
  89. v := vote
  90. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  91. }
  92. return correct, fakes
  93. }
  94. func TestBroadcastEvidence_DuplicateVoteEvidence(t *testing.T) {
  95. var (
  96. config = rpctest.GetConfig()
  97. chainID = config.ChainID()
  98. pv = privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  99. )
  100. for i, c := range GetClients() {
  101. correct, fakes := makeEvidences(t, pv, chainID)
  102. t.Logf("client %d", i)
  103. result, err := c.BroadcastEvidence(context.Background(), correct)
  104. require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
  105. assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
  106. status, err := c.Status(context.Background())
  107. require.NoError(t, err)
  108. err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  109. require.NoError(t, err)
  110. ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
  111. rawpub := ed25519pub.Bytes()
  112. result2, err := c.ABCIQuery(context.Background(), "/val", rawpub)
  113. require.NoError(t, err)
  114. qres := result2.Response
  115. require.True(t, qres.IsOK())
  116. var v abci.ValidatorUpdate
  117. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  118. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  119. pk, err := cryptoenc.PubKeyFromProto(v.PubKey)
  120. require.NoError(t, err)
  121. require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  122. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  123. for _, fake := range fakes {
  124. _, err := c.BroadcastEvidence(context.Background(), fake)
  125. require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
  126. }
  127. }
  128. }
  129. func TestBroadcastEmptyEvidence(t *testing.T) {
  130. for _, c := range GetClients() {
  131. _, err := c.BroadcastEvidence(context.Background(), nil)
  132. assert.Error(t, err)
  133. }
  134. }