github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/29-fee/keeper/keeper_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	types2 "github.com/fibonacci-chain/fbc/libs/tendermint/types"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/baseapp"
    10  
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing"
    13  	ibcmock "github.com/fibonacci-chain/fbc/libs/ibc-go/testing/mock"
    14  
    15  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/29-fee/types"
    16  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    17  	"github.com/stretchr/testify/suite"
    18  )
    19  
    20  var (
    21  	defaultRecvFee       = sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultIbcWei, Amount: sdk.NewInt(100)}}
    22  	defaultAckFee        = sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultIbcWei, Amount: sdk.NewInt(200)}}
    23  	defaultTimeoutFee    = sdk.CoinAdapters{sdk.CoinAdapter{Denom: sdk.DefaultIbcWei, Amount: sdk.NewInt(300)}}
    24  	invalidCoins         = sdk.CoinAdapters{sdk.CoinAdapter{Denom: "invalidDenom", Amount: sdk.NewInt(100)}}
    25  	invalidCoinsNotExist = sdk.CoinAdapters{sdk.CoinAdapter{Denom: "stake", Amount: sdk.NewInt(100)}}
    26  )
    27  
    28  type KeeperTestSuite struct {
    29  	suite.Suite
    30  
    31  	coordinator *ibctesting.Coordinator
    32  
    33  	// testing chains used for convenience and readability
    34  	chainA ibctesting.TestChainI
    35  	chainB ibctesting.TestChainI
    36  	chainC ibctesting.TestChainI
    37  
    38  	path     *ibctesting.Path
    39  	pathAToC *ibctesting.Path
    40  
    41  	queryClient types.QueryClient
    42  }
    43  
    44  func (suite *KeeperTestSuite) SetupTest() {
    45  	suite.coordinator = ibctesting.NewCoordinator(suite.T(), 3)
    46  	types2.UnittestOnlySetMilestoneVenus1Height(-1)
    47  	types2.UnittestOnlySetMilestoneVenus4Height(-1)
    48  	suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0))
    49  	suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1))
    50  	suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2))
    51  
    52  	path := ibctesting.NewPath(suite.chainA, suite.chainB)
    53  	mockFeeVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version}))
    54  	path.EndpointA.ChannelConfig.Version = mockFeeVersion
    55  	path.EndpointB.ChannelConfig.Version = mockFeeVersion
    56  	path.EndpointA.ChannelConfig.PortID = ibctesting.MockFeePort
    57  	path.EndpointB.ChannelConfig.PortID = ibctesting.MockFeePort
    58  	suite.path = path
    59  
    60  	path = ibctesting.NewPath(suite.chainA, suite.chainC)
    61  	path.EndpointA.ChannelConfig.Version = mockFeeVersion
    62  	path.EndpointB.ChannelConfig.Version = mockFeeVersion
    63  	path.EndpointA.ChannelConfig.PortID = ibctesting.MockFeePort
    64  	path.EndpointB.ChannelConfig.PortID = ibctesting.MockFeePort
    65  	suite.pathAToC = path
    66  
    67  	queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), suite.chainA.GetSimApp().CodecProxy.GetProtocMarshal().InterfaceRegistry())
    68  	types.RegisterQueryServer(queryHelper, suite.chainA.GetSimApp().IBCFeeKeeper)
    69  	suite.queryClient = types.NewQueryClient(queryHelper)
    70  }
    71  
    72  func TestKeeperTestSuite(t *testing.T) {
    73  	suite.Run(t, new(KeeperTestSuite))
    74  }
    75  
    76  // helper function
    77  func lockFeeModule(chain ibctesting.TestChainI) {
    78  	ctx := chain.GetContext()
    79  	storeKey := chain.GetSimApp().GetKey(types.ModuleName)
    80  	store := ctx.KVStore(storeKey)
    81  	store.Set(types.KeyLocked(), []byte{1})
    82  }
    83  
    84  func (suite *KeeperTestSuite) TestEscrowAccountHasBalance() {
    85  	fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
    86  
    87  	suite.Require().False(suite.chainA.GetSimApp().IBCFeeKeeper.EscrowAccountHasBalance(suite.chainA.GetContext(), fee.Total()))
    88  
    89  	// set fee in escrow account
    90  	err := suite.chainA.GetSimApp().SupplyKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), suite.chainA.SenderAccount().GetAddress(), types.ModuleName, fee.Total().ToCoins())
    91  	suite.Require().Nil(err)
    92  
    93  	suite.Require().True(suite.chainA.GetSimApp().IBCFeeKeeper.EscrowAccountHasBalance(suite.chainA.GetContext(), fee.Total()))
    94  
    95  	// increase ack fee
    96  	fee.AckFee = fee.AckFee.Add(defaultAckFee...)
    97  	suite.Require().False(suite.chainA.GetSimApp().IBCFeeKeeper.EscrowAccountHasBalance(suite.chainA.GetContext(), fee.Total()))
    98  }
    99  
   100  func (suite *KeeperTestSuite) TestGetSetPayeeAddress() {
   101  	suite.coordinator.Setup(suite.path)
   102  
   103  	payeeAddr, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetPayeeAddress(suite.chainA.GetContext(), suite.chainA.SenderAccount().GetAddress().String(), suite.path.EndpointA.ChannelID)
   104  	suite.Require().False(found)
   105  	suite.Require().Empty(payeeAddr)
   106  
   107  	suite.chainA.GetSimApp().IBCFeeKeeper.SetPayeeAddress(
   108  		suite.chainA.GetContext(),
   109  		suite.chainA.SenderAccounts()[0].SenderAccount.GetAddress().String(),
   110  		suite.chainA.SenderAccounts()[1].SenderAccount.GetAddress().String(),
   111  		suite.path.EndpointA.ChannelID,
   112  	)
   113  
   114  	payeeAddr, found = suite.chainA.GetSimApp().IBCFeeKeeper.GetPayeeAddress(suite.chainA.GetContext(), suite.chainA.SenderAccounts()[0].SenderAccount.GetAddress().String(), suite.path.EndpointA.ChannelID)
   115  	suite.Require().True(found)
   116  	suite.Require().Equal(suite.chainA.SenderAccounts()[1].SenderAccount.GetAddress().String(), payeeAddr)
   117  }
   118  
   119  func (suite *KeeperTestSuite) TestFeesInEscrow() {
   120  	suite.coordinator.Setup(suite.path)
   121  
   122  	// escrow five fees for packet sequence 1
   123  	packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
   124  	fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
   125  
   126  	packetFee := types.NewPacketFee(fee, suite.chainA.SenderAccount().GetAddress().String(), nil)
   127  	packetFees := []types.PacketFee{packetFee, packetFee, packetFee, packetFee, packetFee}
   128  
   129  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees))
   130  
   131  	// retrieve the fees in escrow and assert the length of PacketFees
   132  	feesInEscrow, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeesInEscrow(suite.chainA.GetContext(), packetID)
   133  	suite.Require().True(found)
   134  	suite.Require().Len(feesInEscrow.PacketFees, 5, fmt.Sprintf("expected length 5, but got %d", len(feesInEscrow.PacketFees)))
   135  
   136  	// delete fees for packet sequence 1
   137  	suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeesInEscrow(suite.chainA.GetContext(), packetID)
   138  	hasFeesInEscrow := suite.chainA.GetSimApp().IBCFeeKeeper.HasFeesInEscrow(suite.chainA.GetContext(), packetID)
   139  	suite.Require().False(hasFeesInEscrow)
   140  }
   141  
   142  func (suite *KeeperTestSuite) TestIsLocked() {
   143  	ctx := suite.chainA.GetContext()
   144  	suite.Require().False(suite.chainA.GetSimApp().IBCFeeKeeper.IsLocked(ctx))
   145  
   146  	lockFeeModule(suite.chainA)
   147  
   148  	suite.Require().True(suite.chainA.GetSimApp().IBCFeeKeeper.IsLocked(ctx))
   149  }
   150  
   151  func (suite *KeeperTestSuite) TestGetIdentifiedPacketFeesForChannel() {
   152  	suite.coordinator.Setup(suite.path)
   153  
   154  	// escrow a fee
   155  	refundAcc := suite.chainA.SenderAccount().GetAddress()
   156  	packetID1 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
   157  	packetID2 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 2)
   158  	packetID5 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 51)
   159  
   160  	fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
   161  
   162  	// escrow the packet fee
   163  	packetFee := types.NewPacketFee(fee, refundAcc.String(), []string{})
   164  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID1, types.NewPacketFees([]types.PacketFee{packetFee}))
   165  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID2, types.NewPacketFees([]types.PacketFee{packetFee}))
   166  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID5, types.NewPacketFees([]types.PacketFee{packetFee}))
   167  
   168  	// set fees in escrow for packetIDs on different channel
   169  	diffChannel := "channel-1"
   170  	diffPacketID1 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, diffChannel, 1)
   171  	diffPacketID2 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, diffChannel, 2)
   172  	diffPacketID5 := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, diffChannel, 5)
   173  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), diffPacketID1, types.NewPacketFees([]types.PacketFee{packetFee}))
   174  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), diffPacketID2, types.NewPacketFees([]types.PacketFee{packetFee}))
   175  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), diffPacketID5, types.NewPacketFees([]types.PacketFee{packetFee}))
   176  
   177  	expectedFees := []types.IdentifiedPacketFees{
   178  		{
   179  			PacketId: packetID1,
   180  			PacketFees: []types.PacketFee{
   181  				{
   182  					Fee:           fee,
   183  					RefundAddress: refundAcc.String(),
   184  					Relayers:      nil,
   185  				},
   186  			},
   187  		},
   188  		{
   189  			PacketId: packetID2,
   190  			PacketFees: []types.PacketFee{
   191  				{
   192  					Fee:           fee,
   193  					RefundAddress: refundAcc.String(),
   194  					Relayers:      nil,
   195  				},
   196  			},
   197  		},
   198  		{
   199  			PacketId: packetID5,
   200  			PacketFees: []types.PacketFee{
   201  				{
   202  					Fee:           fee,
   203  					RefundAddress: refundAcc.String(),
   204  					Relayers:      nil,
   205  				},
   206  			},
   207  		},
   208  	}
   209  
   210  	identifiedFees := suite.chainA.GetSimApp().IBCFeeKeeper.GetIdentifiedPacketFeesForChannel(suite.chainA.GetContext(), suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID)
   211  	suite.Require().Len(identifiedFees, len(expectedFees))
   212  	suite.Require().Equal(identifiedFees, expectedFees)
   213  }
   214  
   215  func (suite *KeeperTestSuite) TestGetAllIdentifiedPacketFees() {
   216  	suite.coordinator.Setup(suite.path)
   217  
   218  	// escrow a fee
   219  	refundAcc := suite.chainA.SenderAccount().GetAddress()
   220  	packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
   221  	fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
   222  
   223  	// escrow the packet fee
   224  	packetFee := types.NewPacketFee(fee, refundAcc.String(), []string{})
   225  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees([]types.PacketFee{packetFee}))
   226  
   227  	expectedFees := []types.IdentifiedPacketFees{
   228  		{
   229  			PacketId: packetID,
   230  			PacketFees: []types.PacketFee{
   231  				{
   232  					Fee:           fee,
   233  					RefundAddress: refundAcc.String(),
   234  					Relayers:      nil,
   235  				},
   236  			},
   237  		},
   238  	}
   239  
   240  	identifiedFees := suite.chainA.GetSimApp().IBCFeeKeeper.GetAllIdentifiedPacketFees(suite.chainA.GetContext())
   241  	suite.Require().Len(identifiedFees, len(expectedFees))
   242  	suite.Require().Equal(identifiedFees, expectedFees)
   243  }
   244  
   245  func (suite *KeeperTestSuite) TestGetAllFeeEnabledChannels() {
   246  	validPortId := "ibcmoduleport"
   247  	// set two channels enabled
   248  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), ibctesting.MockFeePort, ibctesting.FirstChannelID)
   249  	suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), validPortId, ibctesting.FirstChannelID)
   250  
   251  	expectedCh := []types.FeeEnabledChannel{
   252  		{
   253  			PortId:    validPortId,
   254  			ChannelId: ibctesting.FirstChannelID,
   255  		},
   256  		{
   257  			PortId:    ibctesting.MockFeePort,
   258  			ChannelId: ibctesting.FirstChannelID,
   259  		},
   260  	}
   261  
   262  	ch := suite.chainA.GetSimApp().IBCFeeKeeper.GetAllFeeEnabledChannels(suite.chainA.GetContext())
   263  	suite.Require().Len(ch, len(expectedCh))
   264  	suite.Require().Equal(ch, expectedCh)
   265  }
   266  
   267  func (suite *KeeperTestSuite) TestGetAllPayees() {
   268  	var expectedPayees []types.RegisteredPayee
   269  
   270  	for i := 0; i < 3; i++ {
   271  		suite.chainA.GetSimApp().IBCFeeKeeper.SetPayeeAddress(
   272  			suite.chainA.GetContext(),
   273  			suite.chainA.SenderAccounts()[i].SenderAccount.GetAddress().String(),
   274  			suite.chainB.SenderAccounts()[i].SenderAccount.GetAddress().String(),
   275  			ibctesting.FirstChannelID,
   276  		)
   277  
   278  		registeredPayee := types.RegisteredPayee{
   279  			Relayer:   suite.chainA.SenderAccounts()[i].SenderAccount.GetAddress().String(),
   280  			Payee:     suite.chainB.SenderAccounts()[i].SenderAccount.GetAddress().String(),
   281  			ChannelId: ibctesting.FirstChannelID,
   282  		}
   283  
   284  		expectedPayees = append(expectedPayees, registeredPayee)
   285  	}
   286  
   287  	registeredPayees := suite.chainA.GetSimApp().IBCFeeKeeper.GetAllPayees(suite.chainA.GetContext())
   288  	suite.Require().Len(registeredPayees, len(expectedPayees))
   289  	suite.Require().ElementsMatch(expectedPayees, registeredPayees)
   290  }
   291  
   292  func (suite *KeeperTestSuite) TestGetAllCounterpartyPayees() {
   293  	relayerAddr := suite.chainA.SenderAccount().GetAddress().String()
   294  	counterpartyPayee := suite.chainB.SenderAccount().GetAddress().String()
   295  
   296  	suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyPayeeAddress(suite.chainA.GetContext(), relayerAddr, counterpartyPayee, ibctesting.FirstChannelID)
   297  
   298  	expectedCounterpartyPayee := []types.RegisteredCounterpartyPayee{
   299  		{
   300  			Relayer:           relayerAddr,
   301  			CounterpartyPayee: counterpartyPayee,
   302  			ChannelId:         ibctesting.FirstChannelID,
   303  		},
   304  	}
   305  
   306  	counterpartyPayeeAddr := suite.chainA.GetSimApp().IBCFeeKeeper.GetAllCounterpartyPayees(suite.chainA.GetContext())
   307  	suite.Require().Len(counterpartyPayeeAddr, len(expectedCounterpartyPayee))
   308  	suite.Require().Equal(counterpartyPayeeAddr, expectedCounterpartyPayee)
   309  }