Skip to content

Commit

Permalink
fix ICS test
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed May 10, 2024
1 parent 7540445 commit fc5de0c
Showing 1 changed file with 134 additions and 140 deletions.
274 changes: 134 additions & 140 deletions examples/ibc/ics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,169 +5,163 @@ import (
"fmt"
"strings"
"testing"
"time"

"cosmossdk.io/math"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcconntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
interchaintest "github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/relayer"
"github.com/strangelove-ventures/interchaintest/v7/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

var (
icsVersions = []string{"v3.1.0", "v3.3.0", "v4.0.0"}
vals = 1
fNodes = 0
providerChainID = "provider-1"
)

// This tests Cosmos Interchain Security, spinning up a provider and a single consumer chain.
// go test -timeout 3000s -run ^TestICS$ github.com/strangelove-ventures/interchaintest/v8/examples/ibc -v -test.short
func TestICS(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
ver := icsVersions[0]
t.Logf("Running in short mode, only testing the latest ICS version: %s", ver)
icsVersions = []string{ver}
}

relayers := []relayerImp{
for _, version := range icsVersions {
version := version
testName := "ics_" + strings.ReplaceAll(version, ".", "_")

t.Run(testName, func(t *testing.T) {
t.Parallel()
icsTest(t, version)
})
}

}

func icsTest(t *testing.T, version string) {
ctx := context.Background()

consumerBechPrefix := "cosmos"
if version == "v4.0.0" {
consumerBechPrefix = "consumer"
}

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
name: "Cosmos Relayer",
relayerImp: ibc.CosmosRly,
Name: "ics-provider", Version: version,
NumValidators: &vals, NumFullNodes: &fNodes,
ChainConfig: ibc.ChainConfig{GasAdjustment: 1.5, ChainID: providerChainID, TrustingPeriod: "336h"},
},
{
name: "Hermes",
relayerImp: ibc.Hermes,
Name: "ics-consumer", Version: version,
NumValidators: &vals, NumFullNodes: &fNodes,
ChainConfig: ibc.ChainConfig{GasAdjustment: 1.5, ChainID: "consumer-1", Bech32Prefix: consumerBechPrefix},
},
}
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
provider, consumer := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Relayer Factory
client, network := interchaintest.DockerSetup(t)
r := interchaintest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayer.StartupFlags("--block-history", "250"),
).Build(t, client, network)

// Prep Interchain
const ibcPath = "ics-path"
ic := interchaintest.NewInterchain().
AddChain(provider).
AddChain(consumer).
AddRelayer(r, "relayer").
AddProviderConsumerLink(interchaintest.ProviderConsumerLink{
Provider: provider,
Consumer: consumer,
Relayer: r,
Path: ibcPath,
})

icsVersions := []string{"v3.1.0", "v3.3.0", "v4.0.0"}
// Reporter/logs
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)

// Build interchain
err = ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: false,
})
require.NoError(t, err, "failed to build interchain")

// ------------------ ICS Setup ------------------

// Finish the ICS provider chain initialization.
// - Restarts the relayer to connect ics20-1 transfer channel
// - Delegates tokens to the provider to update consensus value
// - Flushes the IBC state to the consumer
err = provider.FinishICSProviderSetup(ctx, r, eRep, ibcPath)
require.NoError(t, err)

// ------------------ Test Begins ------------------

// Fund users
// NOTE: this has to be done after the provider delegation & IBC update to the consumer.
amt := math.NewInt(10_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", amt, consumer, provider)
consumerUser, providerUser := users[0], users[1]

t.Run("validate consumer action executed", func(t *testing.T) {
bal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), consumer.Config().Denom)
require.NoError(t, err)
require.EqualValues(t, amt, bal)
})

t.Run("provider -> consumer IBC transfer", func(t *testing.T) {
providerChannelInfo, err := r.GetChannels(ctx, eRep, provider.Config().ChainID)
require.NoError(t, err)

channelID, err := getTransferChannel(providerChannelInfo)
require.NoError(t, err, providerChannelInfo)

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

consumerChannelID, err := getTransferChannel(consumerChannelInfo)
require.NoError(t, err, consumerChannelInfo)

dstAddress := consumerUser.FormattedAddress()
sendAmt := math.NewInt(7)
transfer := ibc.WalletAmount{
Address: dstAddress,
Denom: provider.Config().Denom,
Amount: sendAmt,
}

for _, rly := range relayers {
rly := rly
testname := rly.name
t.Run(testname, func(t *testing.T) {
// We paralellize the relayers, but not the versions. That would be too many tests running at once, and things can become unstable.
t.Parallel()
for _, providerVersion := range icsVersions {
providerVersion := providerVersion
for _, consumerVersion := range icsVersions {
consumerVersion := consumerVersion
testname := fmt.Sprintf("provider%s-consumer%s", providerVersion, consumerVersion)
testname = strings.ReplaceAll(testname, ".", "")
t.Run(testname, func(t *testing.T) {
fullNodes := 0
validators := 1
ctx := context.Background()
var consumerBechPrefix string
if consumerVersion == "v4.0.0" {
consumerBechPrefix = "consumer"
}

// Chain Factory
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{Name: "ics-provider", Version: providerVersion, NumValidators: &validators, NumFullNodes: &fullNodes, ChainConfig: ibc.ChainConfig{GasAdjustment: 1.5}},
{Name: "ics-consumer", Version: consumerVersion, NumValidators: &validators, NumFullNodes: &fullNodes, ChainConfig: ibc.ChainConfig{Bech32Prefix: consumerBechPrefix}},
})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)
provider, consumer := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Relayer Factory
client, network := interchaintest.DockerSetup(t)

r := interchaintest.NewBuiltinRelayerFactory(
rly.relayerImp,
zaptest.NewLogger(t),
).Build(t, client, network)

// Prep Interchain
const ibcPath = "ics-path"
ic := interchaintest.NewInterchain().
AddChain(provider).
AddChain(consumer).
AddRelayer(r, "relayer").
AddProviderConsumerLink(interchaintest.ProviderConsumerLink{
Provider: provider,
Consumer: consumer,
Relayer: r,
Path: ibcPath,
})

// Log location
f, err := interchaintest.CreateLogFile(fmt.Sprintf("%d.json", time.Now().Unix()))
require.NoError(t, err)
// Reporter/logs
rep := testreporter.NewReporter(f)
eRep := rep.RelayerExecReporter(t)

// Build interchain
err = ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,

SkipPathCreation: false,
})
require.NoError(t, err, "failed to build interchain")

// ------------------ ICS Setup ------------------

// Finish the ICS provider chain initialization.
// - Restarts the relayer to connect ics20-1 transfer channel
// - Delegates tokens to the provider to update consensus value
// - Flushes the IBC state to the consumer
err = provider.FinishICSProviderSetup(ctx, r, eRep, ibcPath)
require.NoError(t, err)

// ------------------ Test Begins ------------------

// Fund users
// NOTE: this has to be done after the provider delegation & IBC update to the consumer.
amt := math.NewInt(10_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", amt, consumer, provider)
consumerUser, providerUser := users[0], users[1]

t.Run("validate consumer action executed", func(t *testing.T) {
bal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), consumer.Config().Denom)
require.NoError(t, err)
require.EqualValues(t, amt, bal)
})

t.Run("provider -> consumer IBC transfer", func(t *testing.T) {
providerChannelInfo, err := r.GetChannels(ctx, eRep, provider.Config().ChainID)
require.NoError(t, err)

channelID, err := getTransferChannel(providerChannelInfo)
require.NoError(t, err, providerChannelInfo)

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

consumerChannelID, err := getTransferChannel(consumerChannelInfo)
require.NoError(t, err, consumerChannelInfo)

dstAddress := consumerUser.FormattedAddress()
sendAmt := math.NewInt(7)
transfer := ibc.WalletAmount{
Address: dstAddress,
Denom: provider.Config().Denom,
Amount: sendAmt,
}

tx, err := provider.SendIBCTransfer(ctx, channelID, providerUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

require.NoError(t, r.Flush(ctx, eRep, ibcPath, channelID))

srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", consumerChannelID, provider.Config().Denom))
dstIbcDenom := srcDenomTrace.IBCDenom()

consumerBal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), dstIbcDenom)
require.NoError(t, err)
require.EqualValues(t, sendAmt, consumerBal)
})
})
}
}
})
}
tx, err := provider.SendIBCTransfer(ctx, channelID, providerUser.KeyName(), transfer, ibc.TransferOptions{})
require.NoError(t, err)
require.NoError(t, tx.Validate())

require.NoError(t, r.Flush(ctx, eRep, ibcPath, channelID))

srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", consumerChannelID, provider.Config().Denom))
dstIbcDenom := srcDenomTrace.IBCDenom()

consumerBal, err := consumer.BankQueryBalance(ctx, consumerUser.FormattedAddress(), dstIbcDenom)
require.NoError(t, err)
require.EqualValues(t, sendAmt, consumerBal)
})
}

func getTransferChannel(channels []ibc.ChannelOutput) (string, error) {
Expand Down

0 comments on commit fc5de0c

Please sign in to comment.