github.com/Finschia/finschia-sdk@v0.48.1/x/distribution/client/testutil/grpc.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 6 "github.com/gogo/protobuf/proto" 7 "github.com/stretchr/testify/suite" 8 9 "github.com/Finschia/finschia-sdk/testutil" 10 "github.com/Finschia/finschia-sdk/testutil/network" 11 "github.com/Finschia/finschia-sdk/testutil/rest" 12 sdk "github.com/Finschia/finschia-sdk/types" 13 grpctypes "github.com/Finschia/finschia-sdk/types/grpc" 14 "github.com/Finschia/finschia-sdk/types/query" 15 "github.com/Finschia/finschia-sdk/x/distribution/types" 16 ) 17 18 type GRPCQueryTestSuite struct { 19 suite.Suite 20 21 cfg network.Config 22 network *network.Network 23 } 24 25 func (s *GRPCQueryTestSuite) SetupSuite() { 26 s.T().Log("setting up integration test suite") 27 28 cfg := network.DefaultConfig() 29 cfg.NumValidators = 1 30 s.cfg = cfg 31 32 s.network = network.New(s.T(), s.cfg) 33 34 _, err := s.network.WaitForHeight(1) 35 s.Require().NoError(err) 36 } 37 38 func (s *GRPCQueryTestSuite) TestQueryParamsGRPC() { 39 val := s.network.Validators[0] 40 baseURL := val.APIAddress 41 42 testCases := []struct { 43 name string 44 url string 45 respType proto.Message 46 expected proto.Message 47 }{ 48 { 49 "gRPC request params", 50 fmt.Sprintf("%s/cosmos/distribution/v1beta1/params", baseURL), 51 &types.QueryParamsResponse{}, 52 &types.QueryParamsResponse{ 53 Params: types.DefaultParams(), 54 }, 55 }, 56 } 57 58 for _, tc := range testCases { 59 tc := tc 60 resp, err := rest.GetRequest(tc.url) 61 s.Run(tc.name, func() { 62 s.Require().NoError(err) 63 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 64 s.Require().Equal(tc.expected, tc.respType) 65 }) 66 } 67 } 68 69 func (s *GRPCQueryTestSuite) TestQueryOutstandingRewardsGRPC() { 70 val := s.network.Validators[0] 71 baseURL := val.APIAddress 72 73 rewards, err := sdk.ParseDecCoins("19.6stake") 74 s.Require().NoError(err) 75 76 testCases := []struct { 77 name string 78 url string 79 headers map[string]string 80 expErr bool 81 respType proto.Message 82 expected proto.Message 83 }{ 84 { 85 "gRPC request params with wrong validator address", 86 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, "wrongAddress"), 87 map[string]string{}, 88 true, 89 &types.QueryValidatorOutstandingRewardsResponse{}, 90 &types.QueryValidatorOutstandingRewardsResponse{}, 91 }, 92 { 93 "gRPC request params valid address", 94 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/outstanding_rewards", baseURL, val.ValAddress.String()), 95 map[string]string{ 96 grpctypes.GRPCBlockHeightHeader: "2", 97 }, 98 false, 99 &types.QueryValidatorOutstandingRewardsResponse{}, 100 &types.QueryValidatorOutstandingRewardsResponse{ 101 Rewards: types.ValidatorOutstandingRewards{ 102 Rewards: rewards, 103 }, 104 }, 105 }, 106 } 107 108 for _, tc := range testCases { 109 tc := tc 110 resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) 111 s.Run(tc.name, func() { 112 if tc.expErr { 113 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 114 } else { 115 s.Require().NoError(err) 116 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 117 s.Require().Equal(tc.expected.String(), tc.respType.String()) 118 } 119 }) 120 } 121 } 122 123 func (s *GRPCQueryTestSuite) TestQueryValidatorCommissionGRPC() { 124 val := s.network.Validators[0] 125 baseURL := val.APIAddress 126 127 commission, err := sdk.ParseDecCoins("9.8stake") 128 s.Require().NoError(err) 129 130 testCases := []struct { 131 name string 132 url string 133 headers map[string]string 134 expErr bool 135 respType proto.Message 136 expected proto.Message 137 }{ 138 { 139 "gRPC request params with wrong validator address", 140 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/commission", baseURL, "wrongAddress"), 141 map[string]string{}, 142 true, 143 &types.QueryValidatorCommissionResponse{}, 144 &types.QueryValidatorCommissionResponse{}, 145 }, 146 { 147 "gRPC request params valid address", 148 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/commission", baseURL, val.ValAddress.String()), 149 map[string]string{ 150 grpctypes.GRPCBlockHeightHeader: "2", 151 }, 152 false, 153 &types.QueryValidatorCommissionResponse{}, 154 &types.QueryValidatorCommissionResponse{ 155 Commission: types.ValidatorAccumulatedCommission{ 156 Commission: commission, 157 }, 158 }, 159 }, 160 } 161 162 for _, tc := range testCases { 163 tc := tc 164 resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) 165 s.Run(tc.name, func() { 166 if tc.expErr { 167 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 168 } else { 169 s.Require().NoError(err) 170 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 171 s.Require().Equal(tc.expected.String(), tc.respType.String()) 172 } 173 }) 174 } 175 } 176 177 func (s *GRPCQueryTestSuite) TestQuerySlashesGRPC() { 178 val := s.network.Validators[0] 179 baseURL := val.APIAddress 180 181 testCases := []struct { 182 name string 183 url string 184 expErr bool 185 respType proto.Message 186 expected proto.Message 187 }{ 188 { 189 "invalid validator address", 190 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes", baseURL, ""), 191 true, 192 &types.QueryValidatorSlashesResponse{}, 193 nil, 194 }, 195 { 196 "invalid start height", 197 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "-1", "3"), 198 true, 199 &types.QueryValidatorSlashesResponse{}, 200 nil, 201 }, 202 { 203 "invalid start height", 204 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "1", "-3"), 205 true, 206 &types.QueryValidatorSlashesResponse{}, 207 nil, 208 }, 209 { 210 "valid request get slashes", 211 fmt.Sprintf("%s/cosmos/distribution/v1beta1/validators/%s/slashes?starting_height=%s&ending_height=%s", baseURL, val.ValAddress.String(), "1", "3"), 212 false, 213 &types.QueryValidatorSlashesResponse{}, 214 &types.QueryValidatorSlashesResponse{ 215 Pagination: &query.PageResponse{}, 216 }, 217 }, 218 } 219 220 for _, tc := range testCases { 221 tc := tc 222 resp, err := rest.GetRequest(tc.url) 223 224 s.Run(tc.name, func() { 225 if tc.expErr { 226 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 227 } else { 228 s.Require().NoError(err) 229 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 230 s.Require().Equal(tc.expected.String(), tc.respType.String()) 231 } 232 }) 233 } 234 } 235 236 func (s *GRPCQueryTestSuite) TestQueryDelegatorRewardsGRPC() { 237 val := s.network.Validators[0] 238 baseURL := val.APIAddress 239 240 rewards, err := sdk.ParseDecCoins("9.8stake") 241 s.Require().NoError(err) 242 243 testCases := []struct { 244 name string 245 url string 246 headers map[string]string 247 expErr bool 248 respType proto.Message 249 expected proto.Message 250 }{ 251 { 252 "wrong delegator address", 253 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards", baseURL, "wrongDelegatorAddress"), 254 map[string]string{}, 255 true, 256 &types.QueryDelegationTotalRewardsResponse{}, 257 nil, 258 }, 259 { 260 "valid request", 261 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards", baseURL, val.Address.String()), 262 map[string]string{ 263 grpctypes.GRPCBlockHeightHeader: "2", 264 }, 265 false, 266 &types.QueryDelegationTotalRewardsResponse{}, 267 &types.QueryDelegationTotalRewardsResponse{ 268 Rewards: []types.DelegationDelegatorReward{ 269 types.NewDelegationDelegatorReward(val.ValAddress, rewards), 270 }, 271 Total: rewards, 272 }, 273 }, 274 { 275 "wrong validator address(specific validator rewards)", 276 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards/%s", baseURL, val.Address.String(), "wrongValAddress"), 277 map[string]string{}, 278 true, 279 &types.QueryDelegationTotalRewardsResponse{}, 280 nil, 281 }, 282 { 283 "valid request(specific validator rewards)", 284 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/rewards/%s", baseURL, val.Address.String(), val.ValAddress.String()), 285 map[string]string{ 286 grpctypes.GRPCBlockHeightHeader: "2", 287 }, 288 false, 289 &types.QueryDelegationRewardsResponse{}, 290 &types.QueryDelegationRewardsResponse{ 291 Rewards: rewards, 292 }, 293 }, 294 } 295 296 for _, tc := range testCases { 297 tc := tc 298 resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) 299 300 s.Run(tc.name, func() { 301 if tc.expErr { 302 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 303 } else { 304 s.Require().NoError(err) 305 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 306 s.Require().Equal(tc.expected.String(), tc.respType.String()) 307 } 308 }) 309 } 310 } 311 312 func (s *GRPCQueryTestSuite) TestQueryDelegatorValidatorsGRPC() { 313 val := s.network.Validators[0] 314 baseURL := val.APIAddress 315 316 testCases := []struct { 317 name string 318 url string 319 expErr bool 320 respType proto.Message 321 expected proto.Message 322 }{ 323 { 324 "empty delegator address", 325 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, ""), 326 true, 327 &types.QueryDelegatorValidatorsResponse{}, 328 nil, 329 }, 330 { 331 "wrong delegator address", 332 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, "wrongDelegatorAddress"), 333 true, 334 &types.QueryDelegatorValidatorsResponse{}, 335 nil, 336 }, 337 { 338 "valid request", 339 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/validators", baseURL, val.Address.String()), 340 false, 341 &types.QueryDelegatorValidatorsResponse{}, 342 &types.QueryDelegatorValidatorsResponse{ 343 Validators: []string{val.ValAddress.String()}, 344 }, 345 }, 346 } 347 348 for _, tc := range testCases { 349 tc := tc 350 resp, err := rest.GetRequest(tc.url) 351 352 s.Run(tc.name, func() { 353 if tc.expErr { 354 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 355 } else { 356 s.Require().NoError(err) 357 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 358 s.Require().Equal(tc.expected.String(), tc.respType.String()) 359 } 360 }) 361 } 362 } 363 364 func (s *GRPCQueryTestSuite) TestQueryWithdrawAddressGRPC() { 365 val := s.network.Validators[0] 366 baseURL := val.APIAddress 367 368 testCases := []struct { 369 name string 370 url string 371 expErr bool 372 respType proto.Message 373 expected proto.Message 374 }{ 375 { 376 "empty delegator address", 377 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, ""), 378 true, 379 &types.QueryDelegatorWithdrawAddressResponse{}, 380 nil, 381 }, 382 { 383 "wrong delegator address", 384 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, "wrongDelegatorAddress"), 385 true, 386 &types.QueryDelegatorWithdrawAddressResponse{}, 387 nil, 388 }, 389 { 390 "valid request", 391 fmt.Sprintf("%s/cosmos/distribution/v1beta1/delegators/%s/withdraw_address", baseURL, val.Address.String()), 392 false, 393 &types.QueryDelegatorWithdrawAddressResponse{}, 394 &types.QueryDelegatorWithdrawAddressResponse{ 395 WithdrawAddress: val.Address.String(), 396 }, 397 }, 398 } 399 400 for _, tc := range testCases { 401 tc := tc 402 resp, err := rest.GetRequest(tc.url) 403 404 s.Run(tc.name, func() { 405 if tc.expErr { 406 s.Require().Error(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 407 } else { 408 s.Require().NoError(err) 409 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 410 s.Require().Equal(tc.expected.String(), tc.respType.String()) 411 } 412 }) 413 } 414 } 415 416 func (s *GRPCQueryTestSuite) TestQueryValidatorCommunityPoolGRPC() { 417 val := s.network.Validators[0] 418 baseURL := val.APIAddress 419 420 communityPool, err := sdk.ParseDecCoins("0.4stake") 421 s.Require().NoError(err) 422 423 testCases := []struct { 424 name string 425 url string 426 headers map[string]string 427 expErr bool 428 respType proto.Message 429 expected proto.Message 430 }{ 431 { 432 "gRPC request params with wrong validator address", 433 fmt.Sprintf("%s/cosmos/distribution/v1beta1/community_pool", baseURL), 434 map[string]string{ 435 grpctypes.GRPCBlockHeightHeader: "2", 436 }, 437 false, 438 &types.QueryCommunityPoolResponse{}, 439 &types.QueryCommunityPoolResponse{ 440 Pool: communityPool, 441 }, 442 }, 443 } 444 445 for _, tc := range testCases { 446 tc := tc 447 resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers) 448 449 s.Run(tc.name, func() { 450 if tc.expErr { 451 s.Require().Error(err) 452 } else { 453 s.Require().NoError(err) 454 s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(resp, tc.respType)) 455 s.Require().Equal(tc.expected.String(), tc.respType.String()) 456 } 457 }) 458 } 459 }