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.

190 lines
5.2 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  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. "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. "github.com/tendermint/tendermint/types"
  18. )
  19. // For some reason the empty node used in tests has a time of
  20. // 2018-10-10 08:20:13.695936996 +0000 UTC
  21. // this is because the test genesis time is set here
  22. // so in order to validate evidence we need evidence to be the same time
  23. var defaultTestTime = time.Date(2018, 10, 10, 8, 20, 13, 695936996, time.UTC)
  24. func newEvidence(t *testing.T, val *privval.FilePV,
  25. vote *types.Vote, vote2 *types.Vote,
  26. chainID string) *types.DuplicateVoteEvidence {
  27. t.Helper()
  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. ev, err := types.NewDuplicateVoteEvidence(vote, vote2, defaultTestTime, valSet)
  38. require.NoError(t, err)
  39. return ev
  40. }
  41. func makeEvidences(
  42. t *testing.T,
  43. val *privval.FilePV,
  44. chainID string,
  45. ) (correct *types.DuplicateVoteEvidence, fakes []*types.DuplicateVoteEvidence) {
  46. vote := types.Vote{
  47. ValidatorAddress: val.Key.Address,
  48. ValidatorIndex: 0,
  49. Height: 1,
  50. Round: 0,
  51. Type: tmproto.PrevoteType,
  52. Timestamp: defaultTestTime,
  53. BlockID: types.BlockID{
  54. Hash: tmhash.Sum(tmrand.Bytes(tmhash.Size)),
  55. PartSetHeader: types.PartSetHeader{
  56. Total: 1000,
  57. Hash: tmhash.Sum([]byte("partset")),
  58. },
  59. },
  60. }
  61. vote2 := vote
  62. vote2.BlockID.Hash = tmhash.Sum([]byte("blockhash2"))
  63. correct = newEvidence(t, val, &vote, &vote2, chainID)
  64. fakes = make([]*types.DuplicateVoteEvidence, 0)
  65. // different address
  66. {
  67. v := vote2
  68. v.ValidatorAddress = []byte("some_address")
  69. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  70. }
  71. // different height
  72. {
  73. v := vote2
  74. v.Height = vote.Height + 1
  75. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  76. }
  77. // different round
  78. {
  79. v := vote2
  80. v.Round = vote.Round + 1
  81. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  82. }
  83. // different type
  84. {
  85. v := vote2
  86. v.Type = tmproto.PrecommitType
  87. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  88. }
  89. // exactly same vote
  90. {
  91. v := vote
  92. fakes = append(fakes, newEvidence(t, val, &vote, &v, chainID))
  93. }
  94. return correct, fakes
  95. }
  96. func TestBroadcastEvidence_DuplicateVoteEvidence(t *testing.T) {
  97. ctx, cancel := context.WithCancel(context.Background())
  98. defer cancel()
  99. n, config := NodeSuite(t)
  100. chainID := config.ChainID()
  101. pv, err := privval.LoadOrGenFilePV(config.PrivValidator.KeyFile(), config.PrivValidator.StateFile())
  102. require.NoError(t, err)
  103. for i, c := range GetClients(t, n, config) {
  104. correct, fakes := makeEvidences(t, pv, chainID)
  105. t.Logf("client %d", i)
  106. // make sure that the node has produced enough blocks
  107. waitForBlock(ctx, t, c, 2)
  108. result, err := c.BroadcastEvidence(ctx, correct)
  109. require.NoError(t, err, "BroadcastEvidence(%s) failed", correct)
  110. assert.Equal(t, correct.Hash(), result.Hash, "expected result hash to match evidence hash")
  111. status, err := c.Status(ctx)
  112. require.NoError(t, err)
  113. err = client.WaitForHeight(c, status.SyncInfo.LatestBlockHeight+2, nil)
  114. require.NoError(t, err)
  115. ed25519pub := pv.Key.PubKey.(ed25519.PubKey)
  116. rawpub := ed25519pub.Bytes()
  117. result2, err := c.ABCIQuery(ctx, "/val", rawpub)
  118. require.NoError(t, err)
  119. qres := result2.Response
  120. require.True(t, qres.IsOK())
  121. var v abci.ValidatorUpdate
  122. err = abci.ReadMessage(bytes.NewReader(qres.Value), &v)
  123. require.NoError(t, err, "Error reading query result, value %v", qres.Value)
  124. pk, err := encoding.PubKeyFromProto(v.PubKey)
  125. require.NoError(t, err)
  126. require.EqualValues(t, rawpub, pk, "Stored PubKey not equal with expected, value %v", string(qres.Value))
  127. require.Equal(t, int64(9), v.Power, "Stored Power not equal with expected, value %v", string(qres.Value))
  128. for _, fake := range fakes {
  129. _, err := c.BroadcastEvidence(ctx, fake)
  130. require.Error(t, err, "BroadcastEvidence(%s) succeeded, but the evidence was fake", fake)
  131. }
  132. }
  133. }
  134. func TestBroadcastEmptyEvidence(t *testing.T) {
  135. n, conf := NodeSuite(t)
  136. for _, c := range GetClients(t, n, conf) {
  137. _, err := c.BroadcastEvidence(context.Background(), nil)
  138. assert.Error(t, err)
  139. }
  140. }
  141. func waitForBlock(ctx context.Context, t *testing.T, c client.Client, height int64) {
  142. timer := time.NewTimer(0 * time.Millisecond)
  143. defer timer.Stop()
  144. for {
  145. select {
  146. case <-ctx.Done():
  147. return
  148. case <-timer.C:
  149. status, err := c.Status(ctx)
  150. require.NoError(t, err)
  151. if status.SyncInfo.LatestBlockHeight >= height {
  152. return
  153. }
  154. timer.Reset(200 * time.Millisecond)
  155. }
  156. }
  157. }