Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review from composable team #141

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
fmt.Println("expBalance", expBalance)
fmt.Println("gotBalance", gotBalance)
suite.Require().Equal(expBalance, gotBalance)
suite.Require().NoError(err)
})
}
}
Expand Down Expand Up @@ -152,8 +154,6 @@
_, 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))

// and when relay to chain B and handle Ack on chain A
Expand Down Expand Up @@ -227,6 +227,7 @@
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 @@ -256,108 +257,144 @@
suite.Run(t, new(TransferMiddlewareTestSuite))
}

func (suite *TransferMiddlewareTestSuite) TestMintAndBurnProcessWhenLaunchChain() {
func (suite *TransferMiddlewareTestSuite) TestMintAndEscrowProcessWhenLaunchChain() {
var (
// when transfer via sdk transfer from A (module) -> B (contract)
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))

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)
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 transferred 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 sender account 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)

Check failure on line 397 in x/transfermiddleware/relay_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
})

Check failure on line 399 in x/transfermiddleware/relay_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
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)
})
}
}
Loading