Skip to content

Commit

Permalink
TestMintAndEscrowProcess: check more IBC transfer from chain A->B
Browse files Browse the repository at this point in the history
  • Loading branch information
vuong177 committed Jun 1, 2023
1 parent 9be381b commit 701bdbc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 97 deletions.
9 changes: 9 additions & 0 deletions app/ibctesting/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -567,6 +568,10 @@ func (chain *TestChain) AllBalances(acc sdk.AccAddress) sdk.Coins {
return chain.GetTestSupport().BankKeeper().GetAllBalances(chain.GetContext(), acc)
}

func (chain *TestChain) GetBankKeeper() bankkeeper.Keeper {
return chain.GetTestSupport().BankKeeper()
}

func (chain TestChain) GetTestSupport() *banksy.TestSupport {
return chain.App.(*TestingAppDecorator).TestSupport()
}
Expand All @@ -590,6 +595,10 @@ func (a TestingAppDecorator) GetStakingKeeper() ibctestingtypes.StakingKeeper {
return a.TestSupport().StakingKeeper()
}

func (a TestingAppDecorator) GetBankKeeper() bankkeeper.Keeper {
return a.TestSupport().BankKeeper()
}

func (a TestingAppDecorator) GetIBCKeeper() *ibckeeper.Keeper {
return a.TestSupport().IBCKeeper()
}
Expand Down
2 changes: 1 addition & 1 deletion app/ibctesting/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ var (
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
// mint.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(
[]govclient.ProposalHandler{
Expand Down
229 changes: 133 additions & 96 deletions x/transfermiddleware/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
customibctesting "github.com/notional-labs/banksy/v2/app/ibctesting"

"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -90,6 +91,7 @@ func (suite *TransferMiddlewareTestSuite) TestOnrecvPacket() {
fmt.Println("expBalance", expBalance)
fmt.Println("gotBalance", gotBalance)
suite.Require().Equal(expBalance, gotBalance)
suite.Require().NoError(err)
})
}
}
Expand Down Expand Up @@ -153,9 +155,7 @@ func (suite *TransferMiddlewareTestSuite) TestSendPacket() {
suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointA.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainB.PendingSendPackets))

suite.Require().NoError(err)
// and when relay to chain B and handle Ack on chain A
err = suite.coordinator.RelayAndAckPendingPacketsReverse(path)
suite.Require().NoError(err)
Expand Down Expand Up @@ -227,6 +227,7 @@ func (suite *TransferMiddlewareTestSuite) TestTimeOutPacket() {
suite.Require().Equal(originalChainABalance.Sub(expChainABalanceDiff), newChainABalance)

// and dest chain balance contains voucher
suite.Require().NoError(err)
expBalance := originalChainBBalance.Add(expChainBBalanceDiff)
gotBalance := suite.chainB.AllBalances(suite.chainB.SenderAccount.GetAddress())
suite.Require().Equal(expBalance, gotBalance)
Expand Down Expand Up @@ -262,102 +263,138 @@ func (suite *TransferMiddlewareTestSuite) TestMintAndBurnProcessWhenLaunchChain(
timeoutHeight = clienttypes.NewHeight(1, 110)
path *customibctesting.Path
expDenom = "ibc/C053D637CCA2A2BA030E2C5EE1B28A16F71CCB0E45E8BE52766DC1B241B77878"
transferAmountFromChainBToChainA = sdk.NewInt(1000000000)
transferAmountFromChainBToChainA = sdk.NewInt(100000000000000)
transferAmountFromChainAToChainB = sdk.NewInt(1000000000000)

// pathBtoC = NewTransferPath(suite.chainB, suite.chainC)
)

testCases := []struct {
name string
}{
{
"Test Mint",
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
// When setup chainB(Composable already have 10^19 stake in test account (genesis))
path = NewTransferPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)

senderABalance := suite.chainB.Balance(suite.chainB.SenderAccount.GetAddress(), "stake")

// Send coin from picasso (chainA) to escrow address
escrowAddress := ibctransfertypes.GetEscrowAddress(ibctransfertypes.PortID, path.EndpointB.ChannelID)
msg := ibctransfertypes.NewMsgTransfer(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
senderABalance,
suite.chainA.SenderAccount.GetAddress().String(),
escrowAddress.String(),
timeoutHeight,
0,
"",
)
_, err := suite.chainA.SendMsgs(msg)
suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointB.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))

// and when relay to chain B and handle Ack on chain A
err = suite.coordinator.RelayAndAckPendingPackets(path)
suite.Require().NoError(err)

// then
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))
suite.Run("Test mint and escrow process", func() {
suite.SetupTest()
// When setup chainB(Composable already have 10^19 stake in test account (genesis))
path = NewTransferPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)

chainBSupply := suite.chainB.Balance(suite.chainB.SenderAccount.GetAddress(), "stake")
// Send coin from (chainA) to escrow address in chain B
escrowAddress := ibctransfertypes.GetEscrowAddress(ibctransfertypes.PortID, path.EndpointB.ChannelID)
msg := ibctransfertypes.NewMsgTransfer(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
chainBSupply,
suite.chainA.SenderAccount.GetAddress().String(),
escrowAddress.String(),
timeoutHeight,
0,
"",
)
_, err := suite.chainA.SendMsgs(msg)
suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointB.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))

// and when relay to chain B and handle Ack on chain A
err = suite.coordinator.RelayAndAckPendingPackets(path)
suite.Require().NoError(err)

// then
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))

// Check escrow address have ibcPICA tokens
balance := suite.chainB.AllBalances(escrowAddress)
expBalance := sdk.NewCoins(sdk.NewCoin(expDenom, chainBSupply.Amount))
suite.Require().Equal(expBalance, balance)

// Add parachain token info
chainBtransMiddleware := suite.chainB.TransferMiddleware()
err = chainBtransMiddleware.AddParachainIBCInfo(suite.chainB.GetContext(), expDenom, path.EndpointB.ChannelID, sdk.DefaultBondDenom)
suite.Require().NoError(err)

// send coin from B to A
msg = ibctransfertypes.NewMsgTransfer(
path.EndpointB.ChannelConfig.PortID,
path.EndpointB.ChannelID,
sdk.NewCoin("stake", transferAmountFromChainBToChainA),
suite.chainB.SenderAccount.GetAddress().String(),
suite.chainA.SenderAccount.GetAddress().String(),
timeoutHeight,
0,
"",
)
_, err = suite.chainB.SendMsgs(msg)
suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointA.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainB.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))

// and when relay to chain A and handle Ack on chain B
err = suite.coordinator.RelayAndAckPendingPacketsReverse(path)
suite.Require().NoError(err)

// then
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))

// check balances in sender address in chain B
balance = suite.chainB.AllBalances(suite.chainB.SenderAccount.GetAddress())
expBalance = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, chainBSupply.Amount.Sub(transferAmountFromChainBToChainA)))
suite.Require().Equal(expBalance, balance)

// check balances in escrow address in chain B
balance = suite.chainB.AllBalances(escrowAddress)
expBalance = sdk.NewCoins(sdk.NewCoin(expDenom, chainBSupply.Amount.Sub(transferAmountFromChainBToChainA)))
suite.Require().Equal(expBalance, balance)

// receiver in chain A receive exactly native token that transferd from chain B
balance = suite.chainA.AllBalances(suite.chainA.SenderAccount.GetAddress())
expBalance = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, transferAmountFromChainBToChainA))
suite.Require().Equal(expBalance, balance)

// Continue send coin from (chainA) to escrow address in chain B
msg = ibctransfertypes.NewMsgTransfer(
path.EndpointA.ChannelConfig.PortID,
path.EndpointA.ChannelID,
sdk.NewCoin("stake", transferAmountFromChainAToChainB),
suite.chainA.SenderAccount.GetAddress().String(),
suite.chainB.SenderAccount.GetAddress().String(),
timeoutHeight,
0,
"",
)

_, err = suite.chainA.SendMsgs(msg)

suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointB.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))

// and when relay to chain B and handle Ack on chain A
err = suite.coordinator.RelayAndAckPendingPackets(path)
suite.Require().NoError(err)

// then
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))

// check new escrow address: newbalances := chainBSupply - transferAmountFromChainBToChainA (first IBC transfer from chain B -> A) + transferAmountFromChainAToChainB (second IBC transfer from chain A -> B)
balance = suite.chainB.AllBalances(escrowAddress)
expBalance = sdk.NewCoins(sdk.NewCoin(expDenom, chainBSupply.Amount.Add(transferAmountFromChainAToChainB).Sub(transferAmountFromChainBToChainA)))
suite.Require().Equal(expBalance, balance)

// check new chain B supply: newbalances := chainBSupply - transferAmountFromChainBToChainA (first IBC transfer from chain B -> A) + transferAmountFromChainAToChainB (second IBC transfer from chain A -> B)
balance = suite.chainB.AllBalances(suite.chainB.SenderAccount.GetAddress())
expBalance = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, chainBSupply.Amount.Sub(transferAmountFromChainBToChainA).Add(transferAmountFromChainAToChainB)))
suite.Require().Equal(expBalance, balance)

})

balance := suite.chainB.AllBalances(escrowAddress)
expBalance := sdk.NewCoins(sdk.NewCoin(expDenom, senderABalance.Amount))
suite.Require().Equal(expBalance, balance)

// Add parachain token info
chainBtransMiddleware := suite.chainB.TransferMiddleware()
err = chainBtransMiddleware.AddParachainIBCInfo(suite.chainB.GetContext(), expDenom, path.EndpointB.ChannelID, sdk.DefaultBondDenom)
suite.Require().NoError(err)

// send coin from B to A
msg = ibctransfertypes.NewMsgTransfer(
path.EndpointB.ChannelConfig.PortID,
path.EndpointB.ChannelID,
sdk.NewCoin("stake", transferAmountFromChainBToChainA),
suite.chainB.SenderAccount.GetAddress().String(),
suite.chainA.SenderAccount.GetAddress().String(),
timeoutHeight,
0,
"",
)
_, err = suite.chainB.SendMsgs(msg)
suite.Require().NoError(err)
suite.Require().NoError(err, path.EndpointA.UpdateClient())

// then
suite.Require().Equal(1, len(suite.chainB.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))

// and when relay to chain A and handle Ack on chain B
err = suite.coordinator.RelayAndAckPendingPacketsReverse(path)
suite.Require().NoError(err)

// then
suite.Require().Equal(0, len(suite.chainB.PendingSendPackets))
suite.Require().Equal(0, len(suite.chainA.PendingSendPackets))

balance = suite.chainB.AllBalances(suite.chainB.SenderAccount.GetAddress())
expBalance = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, senderABalance.Amount.Sub(transferAmountFromChainBToChainA)))
suite.Require().Equal(expBalance, balance)

balance = suite.chainB.AllBalances(escrowAddress)
expBalance = sdk.NewCoins(sdk.NewCoin(expDenom, senderABalance.Amount.Sub(transferAmountFromChainBToChainA)))
suite.Require().Equal(expBalance, balance)

balance = suite.chainA.AllBalances(suite.chainA.SenderAccount.GetAddress())
expBalance = sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, transferAmountFromChainBToChainA))
suite.Require().Equal(expBalance, balance)
})
}
}

0 comments on commit 701bdbc

Please sign in to comment.