github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/29-fee/keeper/grpc_query_test.go (about) 1 package keeper_test 2 3 import ( 4 "fmt" 5 6 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 7 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 8 9 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/query" 10 "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/29-fee/types" 11 ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing" 12 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1" 13 ) 14 15 func (suite *KeeperTestSuite) TestQueryIncentivizedPackets() { 16 var ( 17 req *types.QueryIncentivizedPacketsRequest 18 expectedPackets []types.IdentifiedPacketFees 19 ) 20 21 testCases := []struct { 22 name string 23 malleate func() 24 expPass bool 25 }{ 26 { 27 "success", 28 func() { 29 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 30 31 fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) 32 packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), []string(nil)) 33 34 for i := 0; i < 3; i++ { 35 // escrow packet fees for three different packets 36 packetID := channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, uint64(i+1)) 37 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees([]types.PacketFee{packetFee})) 38 39 expectedPackets = append(expectedPackets, types.NewIdentifiedPacketFees(packetID, []types.PacketFee{packetFee})) 40 } 41 42 req = &types.QueryIncentivizedPacketsRequest{ 43 Pagination: &query.PageRequest{ 44 Limit: 5, 45 CountTotal: false, 46 }, 47 QueryHeight: 0, 48 } 49 }, 50 true, 51 }, 52 { 53 "empty pagination", 54 func() { 55 expectedPackets = nil 56 req = &types.QueryIncentivizedPacketsRequest{} 57 }, 58 true, 59 }, 60 } 61 62 for _, tc := range testCases { 63 suite.Run(tc.name, func() { 64 suite.SetupTest() // reset 65 66 tc.malleate() // malleate mutates test data 67 68 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 69 res, err := suite.queryClient.IncentivizedPackets(ctx, req) 70 71 if tc.expPass { 72 suite.Require().NoError(err) 73 suite.Require().NotNil(res) 74 suite.Require().Equal(expectedPackets, res.IncentivizedPackets) 75 } else { 76 suite.Require().Error(err) 77 } 78 }) 79 } 80 } 81 82 func (suite *KeeperTestSuite) TestQueryIncentivizedPacket() { 83 var req *types.QueryIncentivizedPacketRequest 84 85 testCases := []struct { 86 name string 87 malleate func() 88 expPass bool 89 }{ 90 { 91 "success", 92 func() {}, 93 true, 94 }, 95 { 96 "fees not found for packet id", 97 func() { 98 req = &types.QueryIncentivizedPacketRequest{ 99 PacketId: channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 100), 100 QueryHeight: 0, 101 } 102 }, 103 false, 104 }, 105 } 106 107 for _, tc := range testCases { 108 suite.Run(tc.name, func() { 109 suite.SetupTest() // reset 110 111 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 112 113 packetID := channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 1) 114 fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) 115 packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), []string(nil)) 116 117 packetFees := []types.PacketFee{packetFee, packetFee, packetFee} 118 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees)) 119 120 req = &types.QueryIncentivizedPacketRequest{ 121 PacketId: packetID, 122 QueryHeight: 0, 123 } 124 125 tc.malleate() // malleate mutates test data 126 127 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 128 res, err := suite.queryClient.IncentivizedPacket(ctx, req) 129 130 if tc.expPass { 131 suite.Require().NoError(err) 132 suite.Require().NotNil(res) 133 suite.Require().Equal(types.NewIdentifiedPacketFees(packetID, []types.PacketFee{packetFee, packetFee, packetFee}), res.IncentivizedPacket) 134 } else { 135 suite.Require().Error(err) 136 } 137 }) 138 } 139 } 140 141 func (suite *KeeperTestSuite) TestQueryIncentivizedPacketsForChannel() { 142 var ( 143 req *types.QueryIncentivizedPacketsForChannelRequest 144 expIdentifiedPacketFees []*types.IdentifiedPacketFees 145 ) 146 147 fee := types.Fee{ 148 AckFee: sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}}, 149 RecvFee: sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}}, 150 TimeoutFee: sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}}, 151 } 152 153 testCases := []struct { 154 msg string 155 malleate func() 156 expPass bool 157 }{ 158 { 159 "empty pagination", 160 func() { 161 expIdentifiedPacketFees = nil 162 req = &types.QueryIncentivizedPacketsForChannelRequest{} 163 }, 164 true, 165 }, 166 { 167 "success", 168 func() { 169 req = &types.QueryIncentivizedPacketsForChannelRequest{ 170 Pagination: &query.PageRequest{ 171 Limit: 5, 172 CountTotal: false, 173 }, 174 PortId: ibctesting.MockFeePort, 175 ChannelId: ibctesting.FirstChannelID, 176 QueryHeight: 0, 177 } 178 }, 179 true, 180 }, 181 { 182 "no packets for specified channel", 183 func() { 184 expIdentifiedPacketFees = nil 185 req = &types.QueryIncentivizedPacketsForChannelRequest{ 186 Pagination: &query.PageRequest{ 187 Limit: 5, 188 CountTotal: false, 189 }, 190 PortId: ibctesting.MockFeePort, 191 ChannelId: "channel-10", 192 QueryHeight: 0, 193 } 194 }, 195 true, 196 }, 197 } 198 199 for _, tc := range testCases { 200 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 201 suite.SetupTest() // reset 202 203 // setup 204 refundAcc := suite.chainA.SenderAccount().GetAddress() 205 packetFee := types.NewPacketFee(fee, refundAcc.String(), nil) 206 packetFees := types.NewPacketFees([]types.PacketFee{packetFee, packetFee, packetFee}) 207 208 identifiedFees1 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 1), packetFees.PacketFees) 209 identifiedFees2 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 2), packetFees.PacketFees) 210 identifiedFees3 := types.NewIdentifiedPacketFees(channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 3), packetFees.PacketFees) 211 212 expIdentifiedPacketFees = append(expIdentifiedPacketFees, &identifiedFees1, &identifiedFees2, &identifiedFees3) 213 214 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 215 for _, identifiedPacketFees := range expIdentifiedPacketFees { 216 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), identifiedPacketFees.PacketId, types.NewPacketFees(identifiedPacketFees.PacketFees)) 217 } 218 219 tc.malleate() 220 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 221 222 res, err := suite.queryClient.IncentivizedPacketsForChannel(ctx, req) 223 224 if tc.expPass { 225 suite.Require().NoError(err) 226 suite.Require().NotNil(res) 227 suite.Require().Equal(expIdentifiedPacketFees, res.IncentivizedPackets) 228 } else { 229 suite.Require().Error(err) 230 } 231 }) 232 } 233 } 234 235 func (suite *KeeperTestSuite) TestQueryTotalRecvFees() { 236 var req *types.QueryTotalRecvFeesRequest 237 238 testCases := []struct { 239 name string 240 malleate func() 241 expPass bool 242 }{ 243 { 244 "success", 245 func() {}, 246 true, 247 }, 248 { 249 "packet not found", 250 func() { 251 req.PacketId = channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 100) 252 }, 253 false, 254 }, 255 } 256 257 for _, tc := range testCases { 258 suite.Run(tc.name, func() { 259 suite.SetupTest() // reset 260 261 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 262 263 packetID := channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 1) 264 265 fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) 266 packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), []string(nil)) 267 268 packetFees := []types.PacketFee{packetFee, packetFee, packetFee} 269 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees)) 270 271 req = &types.QueryTotalRecvFeesRequest{ 272 PacketId: packetID, 273 } 274 275 tc.malleate() 276 277 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 278 res, err := suite.queryClient.TotalRecvFees(ctx, req) 279 280 if tc.expPass { 281 suite.Require().NoError(err) 282 suite.Require().NotNil(res) 283 284 // expected total is three times the default recv fee 285 expectedFees := defaultRecvFee.Add(defaultRecvFee...).Add(defaultRecvFee...) 286 suite.Require().Equal(expectedFees, res.RecvFees) 287 } else { 288 suite.Require().Error(err) 289 } 290 }) 291 } 292 } 293 294 func (suite *KeeperTestSuite) TestQueryTotalAckFees() { 295 var req *types.QueryTotalAckFeesRequest 296 297 testCases := []struct { 298 name string 299 malleate func() 300 expPass bool 301 }{ 302 { 303 "success", 304 func() {}, 305 true, 306 }, 307 { 308 "packet not found", 309 func() { 310 req.PacketId = channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 100) 311 }, 312 false, 313 }, 314 } 315 316 for _, tc := range testCases { 317 suite.Run(tc.name, func() { 318 suite.SetupTest() // reset 319 320 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 321 322 packetID := channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 1) 323 324 fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) 325 packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), []string(nil)) 326 327 packetFees := []types.PacketFee{packetFee, packetFee, packetFee} 328 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees)) 329 330 req = &types.QueryTotalAckFeesRequest{ 331 PacketId: packetID, 332 } 333 334 tc.malleate() 335 336 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 337 res, err := suite.queryClient.TotalAckFees(ctx, req) 338 339 if tc.expPass { 340 suite.Require().NoError(err) 341 suite.Require().NotNil(res) 342 343 // expected total is three times the default acknowledgement fee 344 expectedFees := defaultAckFee.Add(defaultAckFee...).Add(defaultAckFee...) 345 suite.Require().Equal(expectedFees, res.AckFees) 346 } else { 347 suite.Require().Error(err) 348 } 349 }) 350 } 351 } 352 353 func (suite *KeeperTestSuite) TestQueryTotalTimeoutFees() { 354 var req *types.QueryTotalTimeoutFeesRequest 355 356 testCases := []struct { 357 name string 358 malleate func() 359 expPass bool 360 }{ 361 { 362 "success", 363 func() {}, 364 true, 365 }, 366 { 367 "packet not found", 368 func() { 369 req.PacketId = channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 100) 370 }, 371 false, 372 }, 373 } 374 375 for _, tc := range testCases { 376 suite.Run(tc.name, func() { 377 suite.SetupTest() // reset 378 379 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID) 380 381 packetID := channeltypes.NewPacketId(ibctesting.MockFeePort, ibctesting.FirstChannelID, 1) 382 383 fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) 384 packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), []string(nil)) 385 386 packetFees := []types.PacketFee{packetFee, packetFee, packetFee} 387 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees)) 388 389 req = &types.QueryTotalTimeoutFeesRequest{ 390 PacketId: packetID, 391 } 392 393 tc.malleate() 394 395 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 396 res, err := suite.queryClient.TotalTimeoutFees(ctx, req) 397 398 if tc.expPass { 399 suite.Require().NoError(err) 400 suite.Require().NotNil(res) 401 402 // expected total is three times the default acknowledgement fee 403 expectedFees := defaultTimeoutFee.Add(defaultTimeoutFee...).Add(defaultTimeoutFee...) 404 suite.Require().Equal(expectedFees, res.TimeoutFees) 405 } else { 406 suite.Require().Error(err) 407 } 408 }) 409 } 410 } 411 412 func (suite *KeeperTestSuite) TestQueryPayee() { 413 var req *types.QueryPayeeRequest 414 415 testCases := []struct { 416 name string 417 malleate func() 418 expPass bool 419 }{ 420 { 421 "success", 422 func() {}, 423 true, 424 }, 425 { 426 "payee address not found: invalid channel", 427 func() { 428 req.ChannelId = "invalid-channel-id" 429 }, 430 false, 431 }, 432 { 433 "payee address not found: invalid relayer address", 434 func() { 435 req.Relayer = "invalid-addr" 436 }, 437 false, 438 }, 439 } 440 441 for _, tc := range testCases { 442 suite.Run(tc.name, func() { 443 suite.SetupTest() // reset 444 445 pk := secp256k1.GenPrivKey().PubKey() 446 expPayeeAddr := sdk.AccAddress(pk.Address()) 447 448 suite.chainA.GetSimApp().IBCFeeKeeper.SetPayeeAddress( 449 suite.chainA.GetContext(), 450 suite.chainA.SenderAccount().GetAddress().String(), 451 expPayeeAddr.String(), 452 suite.path.EndpointA.ChannelID, 453 ) 454 455 req = &types.QueryPayeeRequest{ 456 ChannelId: suite.path.EndpointA.ChannelID, 457 Relayer: suite.chainA.SenderAccount().GetAddress().String(), 458 } 459 460 tc.malleate() 461 462 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 463 res, err := suite.queryClient.Payee(ctx, req) 464 465 if tc.expPass { 466 suite.Require().NoError(err) 467 suite.Require().Equal(expPayeeAddr.String(), res.PayeeAddress) 468 } else { 469 suite.Require().Error(err) 470 } 471 }) 472 } 473 } 474 475 func (suite *KeeperTestSuite) TestQueryCounterpartyPayee() { 476 var req *types.QueryCounterpartyPayeeRequest 477 478 testCases := []struct { 479 name string 480 malleate func() 481 expPass bool 482 }{ 483 { 484 "success", 485 func() {}, 486 true, 487 }, 488 { 489 "counterparty address not found: invalid channel", 490 func() { 491 req.ChannelId = "invalid-channel-id" 492 }, 493 false, 494 }, 495 { 496 "counterparty address not found: invalid address", 497 func() { 498 req.Relayer = "invalid-addr" 499 }, 500 false, 501 }, 502 } 503 504 for _, tc := range testCases { 505 suite.Run(tc.name, func() { 506 suite.SetupTest() // reset 507 508 pk := secp256k1.GenPrivKey().PubKey() 509 expCounterpartyPayeeAddr := sdk.AccAddress(pk.Address()) 510 511 suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyPayeeAddress( 512 suite.chainA.GetContext(), 513 suite.chainA.SenderAccount().GetAddress().String(), 514 expCounterpartyPayeeAddr.String(), 515 suite.path.EndpointA.ChannelID, 516 ) 517 518 req = &types.QueryCounterpartyPayeeRequest{ 519 ChannelId: suite.path.EndpointA.ChannelID, 520 Relayer: suite.chainA.SenderAccount().GetAddress().String(), 521 } 522 523 tc.malleate() 524 525 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 526 res, err := suite.queryClient.CounterpartyPayee(ctx, req) 527 528 if tc.expPass { 529 suite.Require().NoError(err) 530 suite.Require().Equal(expCounterpartyPayeeAddr.String(), res.CounterpartyPayee) 531 } else { 532 suite.Require().Error(err) 533 } 534 }) 535 } 536 } 537 538 func (suite *KeeperTestSuite) TestQueryFeeEnabledChannels() { 539 var ( 540 req *types.QueryFeeEnabledChannelsRequest 541 expFeeEnabledChannels []types.FeeEnabledChannel 542 ) 543 544 testCases := []struct { 545 name string 546 malleate func() 547 expPass bool 548 }{ 549 { 550 "success", 551 func() {}, 552 true, 553 }, 554 { 555 "success: empty pagination", 556 func() { 557 req = &types.QueryFeeEnabledChannelsRequest{} 558 }, 559 true, 560 }, 561 { 562 "success: with multiple fee enabled channels", 563 func() { 564 suite.coordinator.Setup(suite.pathAToC) 565 566 expChannel := types.FeeEnabledChannel{ 567 PortId: suite.pathAToC.EndpointA.ChannelConfig.PortID, 568 ChannelId: suite.pathAToC.EndpointA.ChannelID, 569 } 570 571 expFeeEnabledChannels = append(expFeeEnabledChannels, expChannel) 572 }, 573 true, 574 }, 575 { 576 "success: pagination with multiple fee enabled channels", 577 func() { 578 // start at index 1, as channel-0 is already added to expFeeEnabledChannels below 579 for i := 1; i < 10; i++ { 580 channelID := channeltypes.FormatChannelIdentifier(uint64(i)) 581 suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, channelID) 582 583 expChannel := types.FeeEnabledChannel{ 584 PortId: ibctesting.MockFeePort, 585 ChannelId: channelID, 586 } 587 588 if i < 5 { // add only the first 5 channels, as our default pagination limit is 5 589 expFeeEnabledChannels = append(expFeeEnabledChannels, expChannel) 590 } 591 } 592 593 suite.chainA.Coordinator().CommitBlock(suite.chainA) 594 }, 595 true, 596 }, 597 { 598 "empty response", 599 func() { 600 suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeeEnabled(suite.chainA.GetContext(), suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID) 601 expFeeEnabledChannels = nil 602 603 suite.chainA.Coordinator().CommitBlock(suite.chainA) 604 }, 605 true, 606 }, 607 } 608 609 for _, tc := range testCases { 610 suite.Run(tc.name, func() { 611 suite.SetupTest() // reset 612 613 suite.coordinator.Setup(suite.path) 614 615 expChannel := types.FeeEnabledChannel{ 616 PortId: suite.path.EndpointA.ChannelConfig.PortID, 617 ChannelId: suite.path.EndpointA.ChannelID, 618 } 619 620 expFeeEnabledChannels = []types.FeeEnabledChannel{expChannel} 621 622 req = &types.QueryFeeEnabledChannelsRequest{ 623 Pagination: &query.PageRequest{ 624 Limit: 5, 625 CountTotal: false, 626 }, 627 QueryHeight: 0, 628 } 629 630 tc.malleate() 631 632 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 633 res, err := suite.queryClient.FeeEnabledChannels(ctx, req) 634 635 if tc.expPass { 636 suite.Require().NoError(err) 637 suite.Require().Equal(expFeeEnabledChannels, res.FeeEnabledChannels) 638 } else { 639 suite.Require().Error(err) 640 } 641 }) 642 } 643 } 644 645 func (suite *KeeperTestSuite) TestQueryFeeEnabledChannel() { 646 var req *types.QueryFeeEnabledChannelRequest 647 648 testCases := []struct { 649 name string 650 malleate func() 651 expPass bool 652 }{ 653 { 654 "success", 655 func() {}, 656 true, 657 }, 658 { 659 "fee not enabled on channel", 660 func() { 661 req.ChannelId = "invalid-channel-id" 662 req.PortId = "invalid-port-id" 663 }, 664 false, 665 }, 666 } 667 668 for _, tc := range testCases { 669 suite.Run(tc.name, func() { 670 suite.SetupTest() // reset 671 672 suite.coordinator.Setup(suite.path) 673 674 req = &types.QueryFeeEnabledChannelRequest{ 675 PortId: suite.path.EndpointA.ChannelConfig.PortID, 676 ChannelId: suite.path.EndpointA.ChannelID, 677 } 678 679 tc.malleate() 680 681 ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) 682 res, err := suite.queryClient.FeeEnabledChannel(ctx, req) 683 684 if tc.expPass { 685 suite.Require().NoError(err) 686 suite.Require().True(res.FeeEnabled) 687 } else { 688 suite.Require().False(res.FeeEnabled) 689 } 690 }) 691 } 692 }