Skip to content

Commit

Permalink
Refactor interchain test (#133)
Browse files Browse the repository at this point in the history
* refactor: move custom interchaintest logic from notional repo

* refactor: refactor interchain test

---------

Co-authored-by: Tien Nguyen <[email protected]>
  • Loading branch information
minhngoc274 and tnv1 authored Jan 24, 2024
1 parent 7f8685b commit fb6075a
Show file tree
Hide file tree
Showing 11 changed files with 798 additions and 1,010 deletions.
322 changes: 322 additions & 0 deletions tests/interchaintest/chain_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package interchaintest

import (
"context"
"fmt"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/testutil"
"testing"

"github.com/strangelove-ventures/interchaintest/v7"
Expand Down Expand Up @@ -64,3 +68,321 @@ func TestStartFeeabs(t *testing.T) {
_ = ic.Close()
})
}

func SetupChain(t *testing.T, ctx context.Context) ([]ibc.Chain, []ibc.Wallet, []ibc.ChannelOutput) {
client, network := interchaintest.DockerSetup(t)

rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)

// Create chain factory with Feeabs and Gaia
numVals := 1
numFullNodes := 1
gasAdjustment := 2.0

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "feeabs",
ChainConfig: feeabsConfig,
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
{
Name: "gaia",
Version: "v12.0.0-rc0",
ChainConfig: ibc.ChainConfig{
GasPrices: "0.0uatom",
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
{
Name: "osmosis",
Version: "v17.0.0",
ChainConfig: ibc.ChainConfig{
GasPrices: "0.005uosmo",
EncodingConfig: osmosisEncoding(),
},
GasAdjustment: &gasAdjustment,
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

feeabs, gaia, osmosis := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain), chains[2].(*cosmos.CosmosChain)

r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
).Build(t, client, network)

ic := interchaintest.NewInterchain().
AddChain(feeabs).
AddChain(gaia).
AddChain(osmosis).
AddRelayer(r, "relayer").
AddLink(interchaintest.InterchainLink{
Chain1: feeabs,
Chain2: gaia,
Relayer: r,
Path: pathFeeabsGaia,
}).
AddLink(interchaintest.InterchainLink{
Chain1: feeabs,
Chain2: osmosis,
Relayer: r,
Path: pathFeeabsOsmosis,
}).
AddLink(interchaintest.InterchainLink{
Chain1: osmosis,
Chain2: gaia,
Relayer: r,
Path: pathOsmosisGaia,
})

require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),

SkipPathCreation: true,
}))
t.Cleanup(func() {
_ = ic.Close()
})

const userFunds = int64(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, feeabs, gaia, osmosis)

// rly feeabs-osmo
// Generate new path
err = r.GeneratePath(ctx, eRep, feeabs.Config().ChainID, osmosis.Config().ChainID, pathFeeabsOsmosis)
require.NoError(t, err)
// Create client
err = r.CreateClients(ctx, eRep, pathFeeabsOsmosis, ibc.DefaultClientOpts())
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, osmosis)
require.NoError(t, err)

// Create connection
err = r.CreateConnections(ctx, eRep, pathFeeabsOsmosis)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, osmosis)
require.NoError(t, err)
// Create channel
err = r.CreateChannel(ctx, eRep, pathFeeabsOsmosis, ibc.CreateChannelOptions{
SourcePortName: "transfer",
DestPortName: "transfer",
Order: ibc.Unordered,
Version: "ics20-1",
})
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, osmosis)
require.NoError(t, err)
var chanels []ibc.ChannelOutput
channsFeeabs, err := r.GetChannels(ctx, eRep, feeabs.Config().ChainID)
require.NoError(t, err)

channsOsmosis, err := r.GetChannels(ctx, eRep, osmosis.Config().ChainID)
require.NoError(t, err)

require.Len(t, channsFeeabs, 1)
require.Len(t, channsOsmosis, 1)

channFeeabsOsmosis := channsFeeabs[0]
require.NotEmpty(t, channFeeabsOsmosis.ChannelID)
channOsmosisFeeabs := channsOsmosis[0]
require.NotEmpty(t, channOsmosisFeeabs.ChannelID)
// rly feeabs-gaia
// Generate new path
err = r.GeneratePath(ctx, eRep, feeabs.Config().ChainID, gaia.Config().ChainID, pathFeeabsGaia)
require.NoError(t, err)
// Create clients
err = r.CreateClients(ctx, eRep, pathFeeabsGaia, ibc.DefaultClientOpts())
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, gaia)
require.NoError(t, err)

// Create connection
err = r.CreateConnections(ctx, eRep, pathFeeabsGaia)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, gaia)
require.NoError(t, err)

// Create channel
err = r.CreateChannel(ctx, eRep, pathFeeabsGaia, ibc.CreateChannelOptions{
SourcePortName: "transfer",
DestPortName: "transfer",
Order: ibc.Unordered,
Version: "ics20-1",
})
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, feeabs, gaia)
require.NoError(t, err)

channsFeeabs, err = r.GetChannels(ctx, eRep, feeabs.Config().ChainID)
require.NoError(t, err)

channsGaia, err := r.GetChannels(ctx, eRep, gaia.Config().ChainID)
require.NoError(t, err)

require.Len(t, channsFeeabs, 2)
require.Len(t, channsGaia, 1)

var channFeeabsGaia ibc.ChannelOutput
for _, chann := range channsFeeabs {
if chann.ChannelID != channFeeabsOsmosis.ChannelID {
channFeeabsGaia = chann
}
}
require.NotEmpty(t, channFeeabsGaia.ChannelID)

channGaiaFeeabs := channsGaia[0]
require.NotEmpty(t, channGaiaFeeabs.ChannelID)
// rly osmo-gaia
// Generate new path
err = r.GeneratePath(ctx, eRep, osmosis.Config().ChainID, gaia.Config().ChainID, pathOsmosisGaia)
require.NoError(t, err)
// Create clients
err = r.CreateClients(ctx, eRep, pathOsmosisGaia, ibc.DefaultClientOpts())
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, osmosis, gaia)
require.NoError(t, err)
// Create connection
err = r.CreateConnections(ctx, eRep, pathOsmosisGaia)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, osmosis, gaia)
require.NoError(t, err)
// Create channel
err = r.CreateChannel(ctx, eRep, pathOsmosisGaia, ibc.CreateChannelOptions{
SourcePortName: "transfer",
DestPortName: "transfer",
Order: ibc.Unordered,
Version: "ics20-1",
})
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, osmosis, gaia)
require.NoError(t, err)

channsOsmosis, err = r.GetChannels(ctx, eRep, osmosis.Config().ChainID)
require.NoError(t, err)

channsGaia, err = r.GetChannels(ctx, eRep, gaia.Config().ChainID)
require.NoError(t, err)

require.Len(t, channsOsmosis, 2)
require.Len(t, channsGaia, 2)

var channOsmosisGaia ibc.ChannelOutput
var channGaiaOsmosis ibc.ChannelOutput

for _, chann := range channsOsmosis {
if chann.ChannelID != channOsmosisFeeabs.ChannelID {
channOsmosisGaia = chann
}
}
require.NotEmpty(t, channOsmosisGaia)

for _, chann := range channsGaia {
if chann.ChannelID != channGaiaFeeabs.ChannelID {
channGaiaOsmosis = chann
}
}
require.NotEmpty(t, channGaiaOsmosis)

fmt.Println("-----------------------------------")
fmt.Printf("channFeeabsOsmosis: %s - %s\n", channFeeabsOsmosis.ChannelID, channFeeabsOsmosis.Counterparty.ChannelID)
fmt.Printf("channOsmosisFeeabs: %s - %s\n", channOsmosisFeeabs.ChannelID, channOsmosisFeeabs.Counterparty.ChannelID)
fmt.Printf("channFeeabsGaia: %s - %s\n", channFeeabsGaia.ChannelID, channFeeabsGaia.Counterparty.ChannelID)
fmt.Printf("channGaiaFeeabs: %s - %s\n", channGaiaFeeabs.ChannelID, channGaiaFeeabs.Counterparty.ChannelID)
fmt.Printf("channOsmosisGaia: %s - %s\n", channOsmosisGaia.ChannelID, channOsmosisGaia.Counterparty.ChannelID)
fmt.Printf("channGaiaOsmosis: %s - %s\n", channGaiaOsmosis.ChannelID, channGaiaOsmosis.Counterparty.ChannelID)
fmt.Println("-----------------------------------")

// Start the relayer on both paths
err = r.StartRelayer(ctx, eRep, pathFeeabsGaia, pathFeeabsOsmosis, pathOsmosisGaia)
require.NoError(t, err)

t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occurred while stopping the relayer: %s", err)
}
},
)
chanels = append(chanels, channFeeabsOsmosis, channOsmosisFeeabs, channFeeabsGaia, channGaiaFeeabs, channOsmosisGaia, channGaiaOsmosis)
feeabsUser, gaiaUser, osmosisUser := users[0], users[1], users[2]

// Send Gaia uatom to Osmosis
gaiaHeight, err := gaia.Height(ctx)
require.NoError(t, err)
dstAddress := sdktypes.MustBech32ifyAddressBytes(osmosis.Config().Bech32Prefix, osmosisUser.Address())
transfer := ibc.WalletAmount{
Address: dstAddress,
Denom: gaia.Config().Denom,
Amount: amountToSend,
}

tx, err := gaia.SendIBCTransfer(ctx, channGaiaOsmosis.ChannelID, gaiaUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

_, err = testutil.PollForAck(ctx, gaia, gaiaHeight, gaiaHeight+30, tx.Packet)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 1, feeabs, gaia, osmosis)
require.NoError(t, err)

// Send Feeabs stake to Osmosis
feeabsHeight, err := feeabs.Height(ctx)
require.NoError(t, err)
dstAddress = sdktypes.MustBech32ifyAddressBytes(osmosis.Config().Bech32Prefix, osmosisUser.Address())
transfer = ibc.WalletAmount{
Address: dstAddress,
Denom: feeabs.Config().Denom,
Amount: amountToSend,
}

tx, err = feeabs.SendIBCTransfer(ctx, channFeeabsOsmosis.ChannelID, feeabsUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

_, err = testutil.PollForAck(ctx, feeabs, feeabsHeight, feeabsHeight+30, tx.Packet)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 1, feeabs, gaia, osmosis)
require.NoError(t, err)

// Send Gaia uatom to Feeabs
gaiaHeight, err = gaia.Height(ctx)
require.NoError(t, err)
dstAddress = sdktypes.MustBech32ifyAddressBytes(feeabs.Config().Bech32Prefix, feeabsUser.Address())
transfer = ibc.WalletAmount{
Address: dstAddress,
Denom: gaia.Config().Denom,
Amount: amountToSend,
}

tx, err = gaia.SendIBCTransfer(ctx, channGaiaFeeabs.ChannelID, gaiaUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

_, err = testutil.PollForAck(ctx, gaia, gaiaHeight, gaiaHeight+30, tx.Packet)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 1, feeabs, gaia, osmosis)
require.NoError(t, err)

return chains, users, chanels
}
33 changes: 33 additions & 0 deletions tests/interchaintest/feeabs/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package feeabs

import (
"encoding/base64"
abcitypes "github.com/cometbft/cometbft/abci/types"
)

func AttributeValue(events []abcitypes.Event, eventType, attrKey string) (string, bool) {
for _, event := range events {
if event.Type != eventType {
continue
}
for _, attr := range event.Attributes {
if attr.Key == attrKey {
return attr.Value, true
}

// tendermint < v0.37-alpha returns base64 encoded strings in events.
key, err := base64.StdEncoding.DecodeString(attr.Key)
if err != nil {
continue
}
if string(key) == attrKey {
value, err := base64.StdEncoding.DecodeString(attr.Value)
if err != nil {
continue
}
return string(value), true
}
}
}
return "", false
}
Loading

0 comments on commit fb6075a

Please sign in to comment.