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.

188 lines
5.3 KiB

  1. package grpc_test
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. "github.com/tendermint/tendermint/libs/log"
  11. tmrand "github.com/tendermint/tendermint/libs/rand"
  12. tmgrpc "github.com/tendermint/tendermint/privval/grpc"
  13. privvalproto "github.com/tendermint/tendermint/proto/tendermint/privval"
  14. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. const ChainID = "123"
  18. func TestGetPubKey(t *testing.T) {
  19. testCases := []struct {
  20. name string
  21. pv types.PrivValidator
  22. err bool
  23. }{
  24. {name: "valid", pv: types.NewMockPV(), err: false},
  25. {name: "error on pubkey", pv: types.NewErroringMockPV(), err: true},
  26. }
  27. for _, tc := range testCases {
  28. tc := tc
  29. t.Run(tc.name, func(t *testing.T) {
  30. s := tmgrpc.NewSignerServer(ChainID, tc.pv, log.TestingLogger())
  31. req := &privvalproto.PubKeyRequest{ChainId: ChainID}
  32. resp, err := s.GetPubKey(context.Background(), req)
  33. if tc.err {
  34. require.Error(t, err)
  35. } else {
  36. pk, err := tc.pv.GetPubKey(context.Background())
  37. require.NoError(t, err)
  38. assert.Equal(t, resp.PubKey.GetEd25519(), pk.Bytes())
  39. }
  40. })
  41. }
  42. }
  43. func TestSignVote(t *testing.T) {
  44. ts := time.Now()
  45. hash := tmrand.Bytes(tmhash.Size)
  46. valAddr := tmrand.Bytes(crypto.AddressSize)
  47. testCases := []struct {
  48. name string
  49. pv types.PrivValidator
  50. have, want *types.Vote
  51. err bool
  52. }{
  53. {name: "valid", pv: types.NewMockPV(), have: &types.Vote{
  54. Type: tmproto.PrecommitType,
  55. Height: 1,
  56. Round: 2,
  57. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  58. Timestamp: ts,
  59. ValidatorAddress: valAddr,
  60. ValidatorIndex: 1,
  61. }, want: &types.Vote{
  62. Type: tmproto.PrecommitType,
  63. Height: 1,
  64. Round: 2,
  65. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  66. Timestamp: ts,
  67. ValidatorAddress: valAddr,
  68. ValidatorIndex: 1,
  69. },
  70. err: false},
  71. {name: "invalid vote", pv: types.NewErroringMockPV(), have: &types.Vote{
  72. Type: tmproto.PrecommitType,
  73. Height: 1,
  74. Round: 2,
  75. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  76. Timestamp: ts,
  77. ValidatorAddress: valAddr,
  78. ValidatorIndex: 1,
  79. Signature: []byte("signed"),
  80. }, want: &types.Vote{
  81. Type: tmproto.PrecommitType,
  82. Height: 1,
  83. Round: 2,
  84. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  85. Timestamp: ts,
  86. ValidatorAddress: valAddr,
  87. ValidatorIndex: 1,
  88. Signature: []byte("signed"),
  89. },
  90. err: true},
  91. }
  92. for _, tc := range testCases {
  93. tc := tc
  94. t.Run(tc.name, func(t *testing.T) {
  95. s := tmgrpc.NewSignerServer(ChainID, tc.pv, log.TestingLogger())
  96. req := &privvalproto.SignVoteRequest{ChainId: ChainID, Vote: tc.have.ToProto()}
  97. resp, err := s.SignVote(context.Background(), req)
  98. if tc.err {
  99. require.Error(t, err)
  100. } else {
  101. pbVote := tc.want.ToProto()
  102. require.NoError(t, tc.pv.SignVote(context.Background(), ChainID, pbVote))
  103. assert.Equal(t, pbVote.Signature, resp.Vote.Signature)
  104. }
  105. })
  106. }
  107. }
  108. func TestSignProposal(t *testing.T) {
  109. ts := time.Now()
  110. hash := tmrand.Bytes(tmhash.Size)
  111. testCases := []struct {
  112. name string
  113. pv types.PrivValidator
  114. have, want *types.Proposal
  115. err bool
  116. }{
  117. {name: "valid", pv: types.NewMockPV(), have: &types.Proposal{
  118. Type: tmproto.ProposalType,
  119. Height: 1,
  120. Round: 2,
  121. POLRound: 2,
  122. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  123. Timestamp: ts,
  124. }, want: &types.Proposal{
  125. Type: tmproto.ProposalType,
  126. Height: 1,
  127. Round: 2,
  128. POLRound: 2,
  129. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  130. Timestamp: ts,
  131. },
  132. err: false},
  133. {name: "invalid proposal", pv: types.NewErroringMockPV(), have: &types.Proposal{
  134. Type: tmproto.ProposalType,
  135. Height: 1,
  136. Round: 2,
  137. POLRound: 2,
  138. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  139. Timestamp: ts,
  140. Signature: []byte("signed"),
  141. }, want: &types.Proposal{
  142. Type: tmproto.ProposalType,
  143. Height: 1,
  144. Round: 2,
  145. POLRound: 2,
  146. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  147. Timestamp: ts,
  148. Signature: []byte("signed"),
  149. },
  150. err: true},
  151. }
  152. for _, tc := range testCases {
  153. tc := tc
  154. t.Run(tc.name, func(t *testing.T) {
  155. s := tmgrpc.NewSignerServer(ChainID, tc.pv, log.TestingLogger())
  156. req := &privvalproto.SignProposalRequest{ChainId: ChainID, Proposal: tc.have.ToProto()}
  157. resp, err := s.SignProposal(context.Background(), req)
  158. if tc.err {
  159. require.Error(t, err)
  160. } else {
  161. pbProposal := tc.want.ToProto()
  162. require.NoError(t, tc.pv.SignProposal(context.Background(), ChainID, pbProposal))
  163. assert.Equal(t, pbProposal.Signature, resp.Proposal.Signature)
  164. }
  165. })
  166. }
  167. }