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.

165 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. )
  99. pv, err := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  100. require.NoError(t, err)
  101. for i, c := range GetClients() {
  102. correct, fakes := makeEvidences(t, pv, chainID)
  103. t.Logf("client %d", i)
  104. result, err := c.BroadcastEvidence(context.Background(), correct)
  105. require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
  106. assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
  107. status, err := c.Status(context.Background())
  108. require.NoError(t, err)
  109. err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  110. require.NoError(t, err)
  111. ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
  112. rawpub := ed25519pub.Bytes()
  113. result2, err := c.ABCIQuery(context.Background(), "/val", rawpub)
  114. require.NoError(t, err)
  115. qres := result2.Response
  116. require.True(t, qres.IsOK())
  117. var v abci.ValidatorUpdate
  118. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  119. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  120. pk, err := cryptoenc.PubKeyFromProto(v.PubKey)
  121. require.NoError(t, err)
  122. require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  123. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  124. for _, fake := range fakes {
  125. _, err := c.BroadcastEvidence(context.Background(), fake)
  126. require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
  127. }
  128. }
  129. }
  130. func TestBroadcastEmptyEvidence(t *testing.T) {
  131. for _, c := range GetClients() {
  132. _, err := c.BroadcastEvidence(context.Background(), nil)
  133. assert.Error(t, err)
  134. }
  135. }