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.

104 lines
4.4 KiB

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