github.com/Finschia/finschia-sdk@v0.49.1/client/grpc/tmservice/service_test.go (about)

     1  package tmservice_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/Finschia/finschia-sdk/client/grpc/tmservice"
    11  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    12  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
    13  	"github.com/Finschia/finschia-sdk/testutil/network"
    14  	"github.com/Finschia/finschia-sdk/testutil/rest"
    15  	qtypes "github.com/Finschia/finschia-sdk/types/query"
    16  	"github.com/Finschia/finschia-sdk/version"
    17  )
    18  
    19  type IntegrationTestSuite struct {
    20  	suite.Suite
    21  
    22  	cfg     network.Config
    23  	network *network.Network
    24  
    25  	queryClient tmservice.ServiceClient
    26  }
    27  
    28  func (s *IntegrationTestSuite) SetupSuite() {
    29  	s.T().Log("setting up integration test suite")
    30  
    31  	cfg := network.DefaultConfig()
    32  	cfg.NumValidators = 1
    33  
    34  	s.cfg = cfg
    35  	s.network = network.New(s.T(), cfg)
    36  
    37  	s.Require().NotNil(s.network)
    38  
    39  	_, err := s.network.WaitForHeight(1)
    40  	s.Require().NoError(err)
    41  
    42  	s.queryClient = tmservice.NewServiceClient(s.network.Validators[0].ClientCtx)
    43  }
    44  
    45  func (s *IntegrationTestSuite) TearDownSuite() {
    46  	s.T().Log("tearing down integration test suite")
    47  	s.network.Cleanup()
    48  }
    49  
    50  func (s *IntegrationTestSuite) TestQueryNodeInfo() {
    51  	val := s.network.Validators[0]
    52  
    53  	res, err := s.queryClient.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
    54  	s.Require().NoError(err)
    55  	s.Require().Equal(res.ApplicationVersion.AppName, version.NewInfo().AppName)
    56  
    57  	restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/node_info", val.APIAddress))
    58  	s.Require().NoError(err)
    59  	var getInfoRes tmservice.GetNodeInfoResponse
    60  	s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &getInfoRes))
    61  	s.Require().Equal(getInfoRes.ApplicationVersion.AppName, version.NewInfo().AppName)
    62  }
    63  
    64  func (s *IntegrationTestSuite) TestQuerySyncing() {
    65  	val := s.network.Validators[0]
    66  
    67  	_, err := s.queryClient.GetSyncing(context.Background(), &tmservice.GetSyncingRequest{})
    68  	s.Require().NoError(err)
    69  
    70  	restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/syncing", val.APIAddress))
    71  	s.Require().NoError(err)
    72  	var syncingRes tmservice.GetSyncingResponse
    73  	s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &syncingRes))
    74  }
    75  
    76  func (s *IntegrationTestSuite) TestQueryLatestBlock() {
    77  	val := s.network.Validators[0]
    78  	_, err := s.queryClient.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{})
    79  	s.Require().NoError(err)
    80  
    81  	restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/latest", val.APIAddress))
    82  	s.Require().NoError(err)
    83  	var blockInfoRes tmservice.GetLatestBlockResponse
    84  	s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
    85  }
    86  
    87  func (s *IntegrationTestSuite) TestQueryBlockByHeight() {
    88  	val := s.network.Validators[0]
    89  	_, err := s.queryClient.GetBlockByHeight(context.Background(), &tmservice.GetBlockByHeightRequest{Height: 1})
    90  	s.Require().NoError(err)
    91  
    92  	restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/%d", val.APIAddress, 1))
    93  	s.Require().NoError(err)
    94  	var blockInfoRes tmservice.GetBlockByHeightResponse
    95  	s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &blockInfoRes))
    96  
    97  	block := blockInfoRes.GetBlock()
    98  	s.Require().Equal(int64(1), block.Header.Height)
    99  }
   100  
   101  func (s *IntegrationTestSuite) TestQueryLatestValidatorSet() {
   102  	val := s.network.Validators[0]
   103  
   104  	// nil pagination
   105  	res, err := s.queryClient.GetLatestValidatorSet(context.Background(), &tmservice.GetLatestValidatorSetRequest{
   106  		Pagination: nil,
   107  	})
   108  	s.Require().NoError(err)
   109  	s.Require().Equal(1, len(res.Validators))
   110  	content, ok := res.Validators[0].PubKey.GetCachedValue().(cryptotypes.PubKey)
   111  	s.Require().Equal(true, ok)
   112  	s.Require().Equal(content, val.PubKey)
   113  
   114  	// with pagination
   115  	_, err = s.queryClient.GetLatestValidatorSet(context.Background(), &tmservice.GetLatestValidatorSetRequest{Pagination: &qtypes.PageRequest{
   116  		Offset: 0,
   117  		Limit:  10,
   118  	}})
   119  	s.Require().NoError(err)
   120  
   121  	// rest request without pagination
   122  	_, err = rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest", val.APIAddress))
   123  	s.Require().NoError(err)
   124  
   125  	// rest request with pagination
   126  	restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=%d&pagination.limit=%d", val.APIAddress, 0, 1))
   127  	s.Require().NoError(err)
   128  	var validatorSetRes tmservice.GetLatestValidatorSetResponse
   129  	s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(restRes, &validatorSetRes))
   130  	s.Require().Equal(1, len(validatorSetRes.Validators))
   131  	anyPub, err := codectypes.NewAnyWithValue(val.PubKey)
   132  	s.Require().NoError(err)
   133  	s.Require().Equal(validatorSetRes.Validators[0].PubKey, anyPub)
   134  }
   135  
   136  func (s *IntegrationTestSuite) TestLatestValidatorSet_GRPC() {
   137  	vals := s.network.Validators
   138  	testCases := []struct {
   139  		name      string
   140  		req       *tmservice.GetLatestValidatorSetRequest
   141  		expErr    bool
   142  		expErrMsg string
   143  	}{
   144  		{"nil request", nil, true, "cannot be nil"},
   145  		{"no pagination", &tmservice.GetLatestValidatorSetRequest{}, false, ""},
   146  		{"with pagination", &tmservice.GetLatestValidatorSetRequest{Pagination: &qtypes.PageRequest{Offset: 0, Limit: uint64(len(vals))}}, false, ""},
   147  	}
   148  	for _, tc := range testCases {
   149  		s.Run(tc.name, func() {
   150  			grpcRes, err := s.queryClient.GetLatestValidatorSet(context.Background(), tc.req)
   151  			if tc.expErr {
   152  				s.Require().Error(err)
   153  				s.Require().Contains(err.Error(), tc.expErrMsg)
   154  			} else {
   155  				s.Require().NoError(err)
   156  				s.Require().Len(grpcRes.Validators, len(vals))
   157  				s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
   158  				content, ok := grpcRes.Validators[0].PubKey.GetCachedValue().(cryptotypes.PubKey)
   159  				s.Require().Equal(true, ok)
   160  				s.Require().Equal(content, vals[0].PubKey)
   161  			}
   162  		})
   163  	}
   164  }
   165  
   166  func (s *IntegrationTestSuite) TestLatestValidatorSet_GRPCGateway() {
   167  	vals := s.network.Validators
   168  	testCases := []struct {
   169  		name      string
   170  		url       string
   171  		expErr    bool
   172  		expErrMsg string
   173  	}{
   174  		{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest", vals[0].APIAddress), false, ""},
   175  		{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress), true, "strconv.ParseUint"},
   176  		{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2", vals[0].APIAddress), false, ""},
   177  	}
   178  	for _, tc := range testCases {
   179  		s.Run(tc.name, func() {
   180  			res, err := rest.GetRequest(tc.url)
   181  			s.Require().NoError(err)
   182  			if tc.expErr {
   183  				s.Require().Contains(string(res), tc.expErrMsg)
   184  			} else {
   185  				var result tmservice.GetLatestValidatorSetResponse
   186  				err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
   187  				s.Require().NoError(err)
   188  				s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
   189  				anyPub, err := codectypes.NewAnyWithValue(vals[0].PubKey)
   190  				s.Require().NoError(err)
   191  				s.Require().Equal(result.Validators[0].PubKey, anyPub)
   192  			}
   193  		})
   194  	}
   195  }
   196  
   197  func (s *IntegrationTestSuite) TestValidatorSetByHeight_GRPC() {
   198  	vals := s.network.Validators
   199  	testCases := []struct {
   200  		name      string
   201  		req       *tmservice.GetValidatorSetByHeightRequest
   202  		expErr    bool
   203  		expErrMsg string
   204  	}{
   205  		{"nil request", nil, true, "request cannot be nil"},
   206  		{"empty request", &tmservice.GetValidatorSetByHeightRequest{}, true, "height must be greater than 0"},
   207  		{"no pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1}, false, ""},
   208  		{"with pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1, Pagination: &qtypes.PageRequest{Offset: 0, Limit: 1}}, false, ""},
   209  	}
   210  	for _, tc := range testCases {
   211  		s.Run(tc.name, func() {
   212  			grpcRes, err := s.queryClient.GetValidatorSetByHeight(context.Background(), tc.req)
   213  			if tc.expErr {
   214  				s.Require().Error(err)
   215  				s.Require().Contains(err.Error(), tc.expErrMsg)
   216  			} else {
   217  				s.Require().NoError(err)
   218  				s.Require().Len(grpcRes.Validators, len(vals))
   219  				s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
   220  			}
   221  		})
   222  	}
   223  }
   224  
   225  func (s *IntegrationTestSuite) TestValidatorSetByHeight_GRPCGateway() {
   226  	vals := s.network.Validators
   227  	testCases := []struct {
   228  		name      string
   229  		url       string
   230  		expErr    bool
   231  		expErrMsg string
   232  	}{
   233  		{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, -1), true, "height must be greater than 0"},
   234  		{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, 1), false, ""},
   235  		{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress, 1), true, "strconv.ParseUint"},
   236  		{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=0&pagination.limit=2", vals[0].APIAddress, 1), false, ""},
   237  	}
   238  	for _, tc := range testCases {
   239  		s.Run(tc.name, func() {
   240  			res, err := rest.GetRequest(tc.url)
   241  			s.Require().NoError(err)
   242  			if tc.expErr {
   243  				s.Require().Contains(string(res), tc.expErrMsg)
   244  			} else {
   245  				var result tmservice.GetValidatorSetByHeightResponse
   246  				err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
   247  				s.Require().NoError(err)
   248  				s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
   249  			}
   250  		})
   251  	}
   252  }
   253  
   254  func TestIntegrationTestSuite(t *testing.T) {
   255  	suite.Run(t, new(IntegrationTestSuite))
   256  }