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.

161 lines
4.6 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. return types.NewDuplicateVoteEvidence(vote, vote2)
  36. }
  37. func makeEvidences(
  38. t *testing.T,
  39. val *privval.FilePV,
  40. chainID string,
  41. ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
  42. vote := types.Vote{
  43. ValidatorAddress: val.Key.Address,
  44. ValidatorIndex: 0,
  45. Height: 1,
  46. Round: 0,
  47. Type: tmproto.PrevoteType,
  48. Timestamp: defaultTestTime,
  49. BlockID: types.BlockID{
  50. Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
  51. PartSetHeader: types.PartSetHeader{
  52. Total: 1000,
  53. Hash: tmhash.Sum([]byte("partset")),
  54. },
  55. },
  56. }
  57. vote2 := vote
  58. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  59. correct = newEvidence(t, val, &vote, &vote2, chainID)
  60. fakes = make([]*types.DuplicateVoteEvidence, 0)
  61. // different address
  62. {
  63. v := vote2
  64. v.ValidatorAddress = []byte("some_address")
  65. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  66. }
  67. // different height
  68. {
  69. v := vote2
  70. v.Height = vote.Height + 1
  71. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  72. }
  73. // different round
  74. {
  75. v := vote2
  76. v.Round = vote.Round + 1
  77. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  78. }
  79. // different type
  80. {
  81. v := vote2
  82. v.Type = tmproto.PrecommitType
  83. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  84. }
  85. // exactly same vote
  86. {
  87. v := vote
  88. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  89. }
  90. return correct, fakes
  91. }
  92. func TestBroadcastEvidence_DuplicateVoteEvidence(t *testing.T) {
  93. var (
  94. config = rpctest.GetConfig()
  95. chainID = config.ChainID()
  96. pv = privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile())
  97. )
  98. for i, c := range GetClients() {
  99. correct, fakes := makeEvidences(t, pv, chainID)
  100. t.Logf("client %d", i)
  101. result, err := c.BroadcastEvidence(context.Background(), correct)
  102. require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
  103. assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
  104. status, err := c.Status(context.Background())
  105. require.NoError(t, err)
  106. err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  107. require.NoError(t, err)
  108. ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
  109. rawpub := ed25519pub.Bytes()
  110. result2, err := c.ABCIQuery(context.Background(), "/val", rawpub)
  111. require.NoError(t, err)
  112. qres := result2.Response
  113. require.True(t, qres.IsOK())
  114. var v abci.ValidatorUpdate
  115. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  116. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  117. pk, err := cryptoenc.PubKeyFromProto(v.PubKey)
  118. require.NoError(t, err)
  119. require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  120. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  121. for _, fake := range fakes {
  122. _, err := c.BroadcastEvidence(context.Background(), fake)
  123. require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
  124. }
  125. }
  126. }
  127. func TestBroadcastEmptyEvidence(t *testing.T) {
  128. for _, c := range GetClients() {
  129. _, err := c.BroadcastEvidence(context.Background(), nil)
  130. assert.Error(t, err)
  131. }
  132. }