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.

200 lines
5.5 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. ctx, cancel := context.WithCancel(context.Background())
  31. defer cancel()
  32. logger := log.NewTestingLogger(t)
  33. s := tmgrpc.NewSignerServer(ChainID, tc.pv, logger)
  34. req := &privvalproto.PubKeyRequest{ChainId: ChainID}
  35. resp, err := s.GetPubKey(ctx, req)
  36. if tc.err {
  37. require.Error(t, err)
  38. } else {
  39. pk, err := tc.pv.GetPubKey(ctx)
  40. require.NoError(t, err)
  41. assert.Equal(t, resp.PubKey.GetEd25519(), pk.Bytes())
  42. }
  43. })
  44. }
  45. }
  46. func TestSignVote(t *testing.T) {
  47. ts := time.Now()
  48. hash := tmrand.Bytes(tmhash.Size)
  49. valAddr := tmrand.Bytes(crypto.AddressSize)
  50. testCases := []struct {
  51. name string
  52. pv types.PrivValidator
  53. have, want *types.Vote
  54. err bool
  55. }{
  56. {name: "valid", pv: types.NewMockPV(), have: &types.Vote{
  57. Type: tmproto.PrecommitType,
  58. Height: 1,
  59. Round: 2,
  60. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  61. Timestamp: ts,
  62. ValidatorAddress: valAddr,
  63. ValidatorIndex: 1,
  64. }, want: &types.Vote{
  65. Type: tmproto.PrecommitType,
  66. Height: 1,
  67. Round: 2,
  68. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  69. Timestamp: ts,
  70. ValidatorAddress: valAddr,
  71. ValidatorIndex: 1,
  72. },
  73. err: false},
  74. {name: "invalid vote", pv: types.NewErroringMockPV(), have: &types.Vote{
  75. Type: tmproto.PrecommitType,
  76. Height: 1,
  77. Round: 2,
  78. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  79. Timestamp: ts,
  80. ValidatorAddress: valAddr,
  81. ValidatorIndex: 1,
  82. Signature: []byte("signed"),
  83. }, want: &types.Vote{
  84. Type: tmproto.PrecommitType,
  85. Height: 1,
  86. Round: 2,
  87. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  88. Timestamp: ts,
  89. ValidatorAddress: valAddr,
  90. ValidatorIndex: 1,
  91. Signature: []byte("signed"),
  92. },
  93. err: true},
  94. }
  95. for _, tc := range testCases {
  96. tc := tc
  97. t.Run(tc.name, func(t *testing.T) {
  98. ctx, cancel := context.WithCancel(context.Background())
  99. defer cancel()
  100. logger := log.NewTestingLogger(t)
  101. s := tmgrpc.NewSignerServer(ChainID, tc.pv, logger)
  102. req := &privvalproto.SignVoteRequest{ChainId: ChainID, Vote: tc.have.ToProto()}
  103. resp, err := s.SignVote(ctx, req)
  104. if tc.err {
  105. require.Error(t, err)
  106. } else {
  107. pbVote := tc.want.ToProto()
  108. require.NoError(t, tc.pv.SignVote(ctx, ChainID, pbVote))
  109. assert.Equal(t, pbVote.Signature, resp.Vote.Signature)
  110. }
  111. })
  112. }
  113. }
  114. func TestSignProposal(t *testing.T) {
  115. ts := time.Now()
  116. hash := tmrand.Bytes(tmhash.Size)
  117. testCases := []struct {
  118. name string
  119. pv types.PrivValidator
  120. have, want *types.Proposal
  121. err bool
  122. }{
  123. {name: "valid", pv: types.NewMockPV(), have: &types.Proposal{
  124. Type: tmproto.ProposalType,
  125. Height: 1,
  126. Round: 2,
  127. POLRound: 2,
  128. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  129. Timestamp: ts,
  130. }, want: &types.Proposal{
  131. Type: tmproto.ProposalType,
  132. Height: 1,
  133. Round: 2,
  134. POLRound: 2,
  135. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  136. Timestamp: ts,
  137. },
  138. err: false},
  139. {name: "invalid proposal", pv: types.NewErroringMockPV(), have: &types.Proposal{
  140. Type: tmproto.ProposalType,
  141. Height: 1,
  142. Round: 2,
  143. POLRound: 2,
  144. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  145. Timestamp: ts,
  146. Signature: []byte("signed"),
  147. }, want: &types.Proposal{
  148. Type: tmproto.ProposalType,
  149. Height: 1,
  150. Round: 2,
  151. POLRound: 2,
  152. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  153. Timestamp: ts,
  154. Signature: []byte("signed"),
  155. },
  156. err: true},
  157. }
  158. for _, tc := range testCases {
  159. tc := tc
  160. t.Run(tc.name, func(t *testing.T) {
  161. ctx, cancel := context.WithCancel(context.Background())
  162. defer cancel()
  163. logger := log.NewTestingLogger(t)
  164. s := tmgrpc.NewSignerServer(ChainID, tc.pv, logger)
  165. req := &privvalproto.SignProposalRequest{ChainId: ChainID, Proposal: tc.have.ToProto()}
  166. resp, err := s.SignProposal(ctx, req)
  167. if tc.err {
  168. require.Error(t, err)
  169. } else {
  170. pbProposal := tc.want.ToProto()
  171. require.NoError(t, tc.pv.SignProposal(ctx, ChainID, pbProposal))
  172. assert.Equal(t, pbProposal.Signature, resp.Proposal.Signature)
  173. }
  174. })
  175. }
  176. }