Skip to content

Commit

Permalink
refactor(tests): use multidenom in OnRec_ReceiverIsNotSource test fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
DimitrisJim committed Jul 4, 2024
1 parent 96afd56 commit 34a6e28
Showing 1 changed file with 58 additions and 47 deletions.
105 changes: 58 additions & 47 deletions modules/apps/transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,8 @@ func (suite *KeeperTestSuite) TestSendTransfer() {
// We do not expect escrowed amounts in error cases.
expEscrowAmounts = []sdkmath.Int{sdkmath.ZeroInt(), sdkmath.ZeroInt()}
}

// check total amount in escrow of sent token denom on sending chain
for i, coin := range coins {
amount := suite.chainA.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(suite.chainA.GetContext(), coin.GetDenom())
suite.Require().Equal(expEscrowAmounts[i], amount.Amount)
}
// Assert amounts escrowed are as expected.
suite.assertEscrowEqual(suite.chainA, coins, expEscrowAmounts)
})
}
}
Expand Down Expand Up @@ -329,10 +325,9 @@ func (suite *KeeperTestSuite) TestSendTransferSetsTotalEscrowAmountForSourceIBCT
// for testing invalid cases.
func (suite *KeeperTestSuite) TestOnRecvPacket_ReceiverIsNotSource() {
var (
amount sdkmath.Int
receiver string
memo string
expEscrowAmount sdkmath.Int // total amount in escrow for denom on receiving chain
coins sdk.Coins
receiver string
memo string
)

testCases := []struct {
Expand All @@ -355,7 +350,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket_ReceiverIsNotSource() {
{
"failure: mint zero coin",
func() {
amount = sdkmath.ZeroInt()
coins = []sdk.Coin{sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.ZeroInt())}
},
types.ErrInvalidAmount,
},
Expand Down Expand Up @@ -396,59 +391,50 @@ func (suite *KeeperTestSuite) TestOnRecvPacket_ReceiverIsNotSource() {

receiver = suite.chainB.SenderAccount.GetAddress().String() // must be explicitly changed in malleate
memo = "" // can be explicitly changed in malleate
amount = sdkmath.NewInt(100) // must be explicitly changed in malleate
expEscrowAmount = sdkmath.ZeroInt() // total amount in escrow of voucher denom on receiving chain

// denom trace of tokens received on chain B and the associated expected metadata
denomOnB := types.NewDenom(sdk.DefaultBondDenom, types.NewHop(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID))
expDenomMetadataOnB := banktypes.Metadata{
Description: fmt.Sprintf("IBC token from %s", denomOnB.Path()),
DenomUnits: []*banktypes.DenomUnit{
{
Denom: denomOnB.Base,
Exponent: 0,
},
},
Base: denomOnB.IBCDenom(),
Display: denomOnB.Path(),
Name: fmt.Sprintf("%s IBC token", denomOnB.Path()),
Symbol: strings.ToUpper(denomOnB.Base),
}
coins = ibctesting.TestCoins

// send coin from chainA to chainB
coin := sdk.NewCoin(sdk.DefaultBondDenom, amount)
transferMsg := types.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.NewCoins(coin), suite.chainA.SenderAccount.GetAddress().String(), receiver, clienttypes.NewHeight(1, 110), 0, memo, emptyForwarding)
// send coins from chainA to chainB
transferMsg := types.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coins, suite.chainA.SenderAccount.GetAddress().String(), receiver, clienttypes.NewHeight(1, 110), 0, memo, emptyForwarding)
_, err := suite.chainA.SendMsgs(transferMsg)
suite.Require().NoError(err) // message committed

tc.malleate()

seq := uint64(1)
data := types.NewFungibleTokenPacketDataV2(
[]types.Token{
{
Denom: types.NewDenom(sdk.DefaultBondDenom, []types.Hop{}...),
Amount: amount.String(),
},
}, suite.chainA.SenderAccount.GetAddress().String(), receiver, memo, emptyForwardingPacketData)
packet := channeltypes.NewPacket(data.GetBytes(), seq, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(1, 100), 0)
var tokens []types.Token
for _, coin := range coins {
tokens = append(tokens, types.Token{Denom: types.NewDenom(coin.Denom), Amount: coin.Amount.String()})
}
data := types.NewFungibleTokenPacketDataV2(tokens, suite.chainA.SenderAccount.GetAddress().String(), receiver, memo, emptyForwardingPacketData)
packet := channeltypes.NewPacket(data.GetBytes(), uint64(1), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(1, 100), 0)

err = suite.chainB.GetSimApp().TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, data)

// check total amount in escrow of received token denom on receiving chain
totalEscrow := suite.chainB.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(suite.chainB.GetContext(), sdk.DefaultBondDenom)
suite.Require().Equal(expEscrowAmount, totalEscrow.Amount)
var denoms []types.Denom
for _, coin := range coins {
denoms = append(denoms, types.NewDenom(coin.Denom, types.NewHop(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)))
}

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)

denomMetadata, found := suite.chainB.GetSimApp().BankKeeper.GetDenomMetaData(suite.chainB.GetContext(), denomOnB.IBCDenom())
suite.Require().True(found)
suite.Require().Equal(expDenomMetadataOnB, denomMetadata)
// Check denom metadata for of tokens received on chain B.
for _, denom := range denoms {
actualMetadata, found := suite.chainB.GetSimApp().BankKeeper.GetDenomMetaData(suite.chainB.GetContext(), denom.IBCDenom())

suite.Require().True(found)
suite.Require().Equal(metadataFromDenom(denom), actualMetadata)
}
} else {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expError)

// Check denom metadata absence for cases where recv fails.
for _, denom := range denoms {
_, found := suite.chainB.GetSimApp().BankKeeper.GetDenomMetaData(suite.chainB.GetContext(), denom.IBCDenom())

suite.Require().False(found)
}
}
})
}
Expand Down Expand Up @@ -1402,3 +1388,28 @@ func (suite *KeeperTestSuite) TestCreatePacketDataBytesFromVersion() {
})
}
}

// metadataFromDenom creates a banktypes.Metadata from a given types.Denom
func metadataFromDenom(denom types.Denom) banktypes.Metadata {
return banktypes.Metadata{
Description: fmt.Sprintf("IBC token from %s", denom.Path()),
DenomUnits: []*banktypes.DenomUnit{
{
Denom: denom.Base,
Exponent: 0,
},
},
Base: denom.IBCDenom(),
Display: denom.Path(),
Name: fmt.Sprintf("%s IBC token", denom.Path()),
Symbol: strings.ToUpper(denom.Base),
}
}

// assertEscrowEqual asserts that the amounts escrowed for each of the coins on chain matches the expectedAmounts
func (suite *KeeperTestSuite) assertEscrowEqual(chain *ibctesting.TestChain, coins sdk.Coins, expectedAmounts []sdkmath.Int) {
for i, coin := range coins {
amount := chain.GetSimApp().TransferKeeper.GetTotalEscrowForDenom(chain.GetContext(), coin.GetDenom())
suite.Require().Equal(expectedAmounts[i], amount.Amount)
}
}

0 comments on commit 34a6e28

Please sign in to comment.