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.

178 lines
4.4 KiB

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