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.

168 lines
4.1 KiB

  1. package grpc_test
  2. import (
  3. "context"
  4. "net"
  5. "testing"
  6. "time"
  7. grpc "google.golang.org/grpc"
  8. "google.golang.org/grpc/test/bufconn"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/tendermint/tendermint/crypto"
  12. "github.com/tendermint/tendermint/crypto/tmhash"
  13. "github.com/tendermint/tendermint/libs/log"
  14. tmrand "github.com/tendermint/tendermint/libs/rand"
  15. tmgrpc "github.com/tendermint/tendermint/privval/grpc"
  16. privvalproto "github.com/tendermint/tendermint/proto/tendermint/privval"
  17. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  18. "github.com/tendermint/tendermint/types"
  19. )
  20. const chainID = "chain-id"
  21. func dialer(pv types.PrivValidator, logger log.Logger) (*grpc.Server, func(context.Context, string) (net.Conn, error)) {
  22. listener := bufconn.Listen(1024 * 1024)
  23. server := grpc.NewServer()
  24. s := tmgrpc.NewSignerServer(chainID, pv, logger)
  25. privvalproto.RegisterPrivValidatorAPIServer(server, s)
  26. go func() {
  27. if err := server.Serve(listener); err != nil {
  28. panic(err)
  29. }
  30. }()
  31. return server, func(context.Context, string) (net.Conn, error) {
  32. return listener.Dial()
  33. }
  34. }
  35. func TestSignerClient_GetPubKey(t *testing.T) {
  36. ctx := context.Background()
  37. mockPV := types.NewMockPV()
  38. logger := log.TestingLogger()
  39. srv, dialer := dialer(mockPV, logger)
  40. defer srv.Stop()
  41. conn, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithContextDialer(dialer))
  42. if err != nil {
  43. panic(err)
  44. }
  45. defer conn.Close()
  46. client, err := tmgrpc.NewSignerClient(conn, chainID, logger)
  47. require.NoError(t, err)
  48. pk, err := client.GetPubKey()
  49. require.NoError(t, err)
  50. assert.Equal(t, mockPV.PrivKey.PubKey(), pk)
  51. }
  52. func TestSignerClient_SignVote(t *testing.T) {
  53. ctx := context.Background()
  54. mockPV := types.NewMockPV()
  55. logger := log.TestingLogger()
  56. srv, dialer := dialer(mockPV, logger)
  57. defer srv.Stop()
  58. conn, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithContextDialer(dialer))
  59. if err != nil {
  60. panic(err)
  61. }
  62. defer conn.Close()
  63. client, err := tmgrpc.NewSignerClient(conn, chainID, logger)
  64. require.NoError(t, err)
  65. ts := time.Now()
  66. hash := tmrand.Bytes(tmhash.Size)
  67. valAddr := tmrand.Bytes(crypto.AddressSize)
  68. want := &types.Vote{
  69. Type: tmproto.PrecommitType,
  70. Height: 1,
  71. Round: 2,
  72. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  73. Timestamp: ts,
  74. ValidatorAddress: valAddr,
  75. ValidatorIndex: 1,
  76. }
  77. have := &types.Vote{
  78. Type: tmproto.PrecommitType,
  79. Height: 1,
  80. Round: 2,
  81. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  82. Timestamp: ts,
  83. ValidatorAddress: valAddr,
  84. ValidatorIndex: 1,
  85. }
  86. pbHave := have.ToProto()
  87. err = client.SignVote(chainID, pbHave)
  88. require.NoError(t, err)
  89. pbWant := want.ToProto()
  90. require.NoError(t, mockPV.SignVote(chainID, pbWant))
  91. assert.Equal(t, pbWant.Signature, pbHave.Signature)
  92. }
  93. func TestSignerClient_SignProposal(t *testing.T) {
  94. ctx := context.Background()
  95. mockPV := types.NewMockPV()
  96. logger := log.TestingLogger()
  97. srv, dialer := dialer(mockPV, logger)
  98. defer srv.Stop()
  99. conn, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithContextDialer(dialer))
  100. if err != nil {
  101. panic(err)
  102. }
  103. defer conn.Close()
  104. client, err := tmgrpc.NewSignerClient(conn, chainID, logger)
  105. require.NoError(t, err)
  106. ts := time.Now()
  107. hash := tmrand.Bytes(tmhash.Size)
  108. have := &types.Proposal{
  109. Type: tmproto.ProposalType,
  110. Height: 1,
  111. Round: 2,
  112. POLRound: 2,
  113. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  114. Timestamp: ts,
  115. }
  116. want := &types.Proposal{
  117. Type: tmproto.ProposalType,
  118. Height: 1,
  119. Round: 2,
  120. POLRound: 2,
  121. BlockID: types.BlockID{Hash: hash, PartSetHeader: types.PartSetHeader{Hash: hash, Total: 2}},
  122. Timestamp: ts,
  123. }
  124. pbHave := have.ToProto()
  125. err = client.SignProposal(chainID, pbHave)
  126. require.NoError(t, err)
  127. pbWant := want.ToProto()
  128. require.NoError(t, mockPV.SignProposal(chainID, pbWant))
  129. assert.Equal(t, pbWant.Signature, pbHave.Signature)
  130. }