github.com/Finschia/finschia-sdk@v0.48.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  		tc := tc
   150  		s.Run(tc.name, func() {
   151  			grpcRes, err := s.queryClient.GetLatestValidatorSet(context.Background(), tc.req)
   152  			if tc.expErr {
   153  				s.Require().Error(err)
   154  				s.Require().Contains(err.Error(), tc.expErrMsg)
   155  			} else {
   156  				s.Require().NoError(err)
   157  				s.Require().Len(grpcRes.Validators, len(vals))
   158  				s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
   159  				content, ok := grpcRes.Validators[0].PubKey.GetCachedValue().(cryptotypes.PubKey)
   160  				s.Require().Equal(true, ok)
   161  				s.Require().Equal(content, vals[0].PubKey)
   162  			}
   163  		})
   164  	}
   165  }
   166  
   167  func (s IntegrationTestSuite) TestLatestValidatorSet_GRPCGateway() {
   168  	vals := s.network.Validators
   169  	testCases := []struct {
   170  		name      string
   171  		url       string
   172  		expErr    bool
   173  		expErrMsg string
   174  	}{
   175  		{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest", vals[0].APIAddress), false, ""},
   176  		{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress), true, "strconv.ParseUint"},
   177  		{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2", vals[0].APIAddress), false, ""},
   178  	}
   179  	for _, tc := range testCases {
   180  		tc := tc
   181  		s.Run(tc.name, func() {
   182  			res, err := rest.GetRequest(tc.url)
   183  			s.Require().NoError(err)
   184  			if tc.expErr {
   185  				s.Require().Contains(string(res), tc.expErrMsg)
   186  			} else {
   187  				var result tmservice.GetLatestValidatorSetResponse
   188  				err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
   189  				s.Require().NoError(err)
   190  				s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
   191  				anyPub, err := codectypes.NewAnyWithValue(vals[0].PubKey)
   192  				s.Require().NoError(err)
   193  				s.Require().Equal(result.Validators[0].PubKey, anyPub)
   194  			}
   195  		})
   196  	}
   197  }
   198  
   199  func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPC() {
   200  	vals := s.network.Validators
   201  	testCases := []struct {
   202  		name      string
   203  		req       *tmservice.GetValidatorSetByHeightRequest
   204  		expErr    bool
   205  		expErrMsg string
   206  	}{
   207  		{"nil request", nil, true, "request cannot be nil"},
   208  		{"empty request", &tmservice.GetValidatorSetByHeightRequest{}, true, "height must be greater than 0"},
   209  		{"no pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1}, false, ""},
   210  		{"with pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1, Pagination: &qtypes.PageRequest{Offset: 0, Limit: 1}}, false, ""},
   211  	}
   212  	for _, tc := range testCases {
   213  		tc := tc
   214  		s.Run(tc.name, func() {
   215  			grpcRes, err := s.queryClient.GetValidatorSetByHeight(context.Background(), tc.req)
   216  			if tc.expErr {
   217  				s.Require().Error(err)
   218  				s.Require().Contains(err.Error(), tc.expErrMsg)
   219  			} else {
   220  				s.Require().NoError(err)
   221  				s.Require().Len(grpcRes.Validators, len(vals))
   222  				s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
   223  			}
   224  		})
   225  	}
   226  }
   227  
   228  func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPCGateway() {
   229  	vals := s.network.Validators
   230  	testCases := []struct {
   231  		name      string
   232  		url       string
   233  		expErr    bool
   234  		expErrMsg string
   235  	}{
   236  		{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, -1), true, "height must be greater than 0"},
   237  		{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, 1), false, ""},
   238  		{"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"},
   239  		{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=0&pagination.limit=2", vals[0].APIAddress, 1), false, ""},
   240  	}
   241  	for _, tc := range testCases {
   242  		tc := tc
   243  		s.Run(tc.name, func() {
   244  			res, err := rest.GetRequest(tc.url)
   245  			s.Require().NoError(err)
   246  			if tc.expErr {
   247  				s.Require().Contains(string(res), tc.expErrMsg)
   248  			} else {
   249  				var result tmservice.GetValidatorSetByHeightResponse
   250  				err = vals[0].ClientCtx.Codec.UnmarshalJSON(res, &result)
   251  				s.Require().NoError(err)
   252  				s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
   253  			}
   254  		})
   255  	}
   256  }
   257  
   258  func TestIntegrationTestSuite(t *testing.T) {
   259  	suite.Run(t, new(IntegrationTestSuite))
   260  }