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.

107 lines
4.7 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 privval
  2. import (
  3. "encoding/hex"
  4. "testing"
  5. "time"
  6. "github.com/gogo/protobuf/proto"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. "github.com/tendermint/tendermint/crypto/encoding"
  11. "github.com/tendermint/tendermint/crypto/tmhash"
  12. cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
  13. privproto "github.com/tendermint/tendermint/proto/tendermint/privval"
  14. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. var stamp = time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC)
  18. func exampleVote() *types.Vote {
  19. return &types.Vote{
  20. Type: tmproto.SignedMsgType(1),
  21. Height: 3,
  22. Round: 2,
  23. Timestamp: stamp,
  24. BlockID: types.BlockID{
  25. Hash: tmhash.Sum([]byte("blockID_hash")),
  26. PartSetHeader: types.PartSetHeader{
  27. Total: 1000000,
  28. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  29. },
  30. },
  31. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  32. ValidatorIndex: 56789,
  33. VoteExtension: types.VoteExtension{
  34. AppDataToSign: []byte("app_data_signed"),
  35. AppDataSelfAuthenticating: []byte("app_data_self_authenticating"),
  36. },
  37. }
  38. }
  39. func exampleProposal() *types.Proposal {
  40. return &types.Proposal{
  41. Type: tmproto.SignedMsgType(1),
  42. Height: 3,
  43. Round: 2,
  44. Timestamp: stamp,
  45. POLRound: 2,
  46. Signature: []byte("it's a signature"),
  47. BlockID: types.BlockID{
  48. Hash: tmhash.Sum([]byte("blockID_hash")),
  49. PartSetHeader: types.PartSetHeader{
  50. Total: 1000000,
  51. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  52. },
  53. },
  54. }
  55. }
  56. func TestPrivvalVectors(t *testing.T) {
  57. pk := ed25519.GenPrivKeyFromSecret([]byte("it's a secret")).PubKey()
  58. ppk, err := encoding.PubKeyToProto(pk)
  59. require.NoError(t, err)
  60. // Generate a simple vote
  61. vote := exampleVote()
  62. votepb := vote.ToProto()
  63. // Generate a simple proposal
  64. proposal := exampleProposal()
  65. proposalpb := proposal.ToProto()
  66. // Create a Reuseable remote error
  67. remoteError := &privproto.RemoteSignerError{Code: 1, Description: "it's a error"}
  68. testCases := []struct {
  69. testName string
  70. msg proto.Message
  71. expBytes string
  72. }{
  73. {"ping request", &privproto.PingRequest{}, "3a00"},
  74. {"ping response", &privproto.PingResponse{}, "4200"},
  75. {"pubKey request", &privproto.PubKeyRequest{}, "0a00"},
  76. {"pubKey response", &privproto.PubKeyResponse{PubKey: ppk, Error: nil}, "12240a220a20556a436f1218d30942efe798420f51dc9b6a311b929c578257457d05c5fcf230"},
  77. {"pubKey response with error", &privproto.PubKeyResponse{PubKey: cryptoproto.PublicKey{}, Error: remoteError}, "12140a0012100801120c697427732061206572726f72"},
  78. {"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1aa8010aa501080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb034a2f0a0f6170705f646174615f7369676e6564121c6170705f646174615f73656c665f61757468656e7469636174696e67"},
  79. {"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "22a8010aa501080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb034a2f0a0f6170705f646174615f7369676e6564121c6170705f646174615f73656c665f61757468656e7469636174696e67"},
  80. {"Vote Response with error", &privproto.SignedVoteResponse{Vote: tmproto.Vote{}, Error: remoteError}, "22250a11220212002a0b088092b8c398feffffff0112100801120c697427732061206572726f72"},
  81. {"Proposal Request", &privproto.SignProposalRequest{Proposal: proposalpb}, "2a700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
  82. {"Proposal Response", &privproto.SignedProposalResponse{Proposal: *proposalpb, Error: nil}, "32700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
  83. {"Proposal Response with error", &privproto.SignedProposalResponse{Proposal: tmproto.Proposal{}, Error: remoteError}, "32250a112a021200320b088092b8c398feffffff0112100801120c697427732061206572726f72"},
  84. }
  85. for _, tc := range testCases {
  86. tc := tc
  87. pm := mustWrapMsg(tc.msg)
  88. bz, err := pm.Marshal()
  89. require.NoError(t, err, tc.testName)
  90. require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
  91. }
  92. }