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.

93 lines
2.4 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
  1. package types
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/config"
  7. "github.com/tendermint/tendermint/crypto/tmhash"
  8. "github.com/tendermint/tendermint/internal/test/factory"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. tmtime "github.com/tendermint/tendermint/libs/time"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. func TestPeerCatchupRounds(t *testing.T) {
  15. cfg, err := config.ResetTestRoot(t.TempDir(), "consensus_height_vote_set_test")
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. ctx, cancel := context.WithCancel(context.Background())
  20. defer cancel()
  21. valSet, privVals := factory.ValidatorSet(ctx, t, 10, 1)
  22. chainID := cfg.ChainID()
  23. hvs := NewHeightVoteSet(chainID, 1, valSet)
  24. vote999_0 := makeVoteHR(ctx, t, 1, 0, 999, privVals, chainID)
  25. added, err := hvs.AddVote(vote999_0, "peer1")
  26. if !added || err != nil {
  27. t.Error("Expected to successfully add vote from peer", added, err)
  28. }
  29. vote1000_0 := makeVoteHR(ctx, t, 1, 0, 1000, privVals, chainID)
  30. added, err = hvs.AddVote(vote1000_0, "peer1")
  31. if !added || err != nil {
  32. t.Error("Expected to successfully add vote from peer", added, err)
  33. }
  34. vote1001_0 := makeVoteHR(ctx, t, 1, 0, 1001, privVals, chainID)
  35. added, err = hvs.AddVote(vote1001_0, "peer1")
  36. if err != ErrGotVoteFromUnwantedRound {
  37. t.Errorf("expected GotVoteFromUnwantedRoundError, but got %v", err)
  38. }
  39. if added {
  40. t.Error("Expected to *not* add vote from peer, too many catchup rounds.")
  41. }
  42. added, err = hvs.AddVote(vote1001_0, "peer2")
  43. if !added || err != nil {
  44. t.Error("Expected to successfully add vote from another peer")
  45. }
  46. }
  47. func makeVoteHR(
  48. ctx context.Context,
  49. t *testing.T,
  50. height int64,
  51. valIndex, round int32,
  52. privVals []types.PrivValidator,
  53. chainID string,
  54. ) *types.Vote {
  55. t.Helper()
  56. privVal := privVals[valIndex]
  57. pubKey, err := privVal.GetPubKey(ctx)
  58. require.NoError(t, err)
  59. randBytes := tmrand.Bytes(tmhash.Size)
  60. vote := &types.Vote{
  61. ValidatorAddress: pubKey.Address(),
  62. ValidatorIndex: valIndex,
  63. Height: height,
  64. Round: round,
  65. Timestamp: tmtime.Now(),
  66. Type: tmproto.PrecommitType,
  67. BlockID: types.BlockID{Hash: randBytes, PartSetHeader: types.PartSetHeader{}},
  68. }
  69. v := vote.ToProto()
  70. err = privVal.SignVote(ctx, chainID, v)
  71. require.NoError(t, err, "Error signing vote")
  72. vote.Signature = v.Signature
  73. return vote
  74. }