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.

98 lines
4.5 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. BlockID: types.BlockID{Hash: tmhash.Sum([]byte("blockID_hash")), PartSetHeader: types.PartSetHeader{Total: 1000000, Hash: tmhash.Sum([]byte("blockID_part_set_header_hash"))}},
  24. Timestamp: stamp,
  25. ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
  26. ValidatorIndex: 56789,
  27. Extension: []byte("app_data_signed"),
  28. }
  29. }
  30. func exampleProposal() *types.Proposal {
  31. return &types.Proposal{
  32. Type: tmproto.SignedMsgType(1),
  33. Height: 3,
  34. Round: 2,
  35. Timestamp: stamp,
  36. POLRound: 2,
  37. Signature: []byte("it's a signature"),
  38. BlockID: types.BlockID{
  39. Hash: tmhash.Sum([]byte("blockID_hash")),
  40. PartSetHeader: types.PartSetHeader{
  41. Total: 1000000,
  42. Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
  43. },
  44. },
  45. }
  46. }
  47. func TestPrivvalVectors(t *testing.T) {
  48. pk := ed25519.GenPrivKeyFromSecret([]byte("it's a secret")).PubKey()
  49. ppk, err := encoding.PubKeyToProto(pk)
  50. require.NoError(t, err)
  51. // Generate a simple vote
  52. vote := exampleVote()
  53. votepb := vote.ToProto()
  54. // Generate a simple proposal
  55. proposal := exampleProposal()
  56. proposalpb := proposal.ToProto()
  57. // Create a Reuseable remote error
  58. remoteError := &privproto.RemoteSignerError{Code: 1, Description: "it's a error"}
  59. testCases := []struct {
  60. testName string
  61. msg proto.Message
  62. expBytes string
  63. }{
  64. {"ping request", &privproto.PingRequest{}, "3a00"},
  65. {"ping response", &privproto.PingResponse{}, "4200"},
  66. {"pubKey request", &privproto.PubKeyRequest{}, "0a00"},
  67. {"pubKey response", &privproto.PubKeyResponse{PubKey: ppk, Error: nil}, "12240a220a20556a436f1218d30942efe798420f51dc9b6a311b929c578257457d05c5fcf230"},
  68. {"pubKey response with error", &privproto.PubKeyResponse{PubKey: cryptoproto.PublicKey{}, Error: remoteError}, "12140a0012100801120c697427732061206572726f72"},
  69. {"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1a88010a8501080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb034a0f6170705f646174615f7369676e6564"},
  70. {"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "2288010a8501080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb034a0f6170705f646174615f7369676e6564"},
  71. {"Vote Response with error", &privproto.SignedVoteResponse{Vote: tmproto.Vote{}, Error: remoteError}, "22250a11220212002a0b088092b8c398feffffff0112100801120c697427732061206572726f72"},
  72. {"Proposal Request", &privproto.SignProposalRequest{Proposal: proposalpb}, "2a700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
  73. {"Proposal Response", &privproto.SignedProposalResponse{Proposal: *proposalpb, Error: nil}, "32700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
  74. {"Proposal Response with error", &privproto.SignedProposalResponse{Proposal: tmproto.Proposal{}, Error: remoteError}, "32250a112a021200320b088092b8c398feffffff0112100801120c697427732061206572726f72"},
  75. }
  76. for _, tc := range testCases {
  77. tc := tc
  78. pm := mustWrapMsg(tc.msg)
  79. bz, err := pm.Marshal()
  80. require.NoError(t, err, tc.testName)
  81. require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
  82. }
  83. }