Skip to content

Commit

Permalink
local-ic: call FinishICSProviderSetup on setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed May 7, 2024
1 parent ab88cf5 commit ef9f595
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
45 changes: 30 additions & 15 deletions chain/cosmos/ics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path"
"strconv"
"testing"
"time"

sdkmath "cosmossdk.io/math"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"golang.org/x/mod/semver"
"golang.org/x/sync/errgroup"
Expand All @@ -40,43 +38,60 @@ const (
//
// 2. Get the first provider validator, and delegate 1,000,000denom to it. This triggers a CometBFT power increase of 1.
// 3. Flush the pending ICS packets to the consumer chain.
func (c *CosmosChain) FinishICSProviderSetup(t *testing.T, ctx context.Context, r ibc.Relayer, eRep *testreporter.RelayerExecReporter, ibcPath string) {
func (c *CosmosChain) FinishICSProviderSetup(ctx context.Context, r ibc.Relayer, eRep *testreporter.RelayerExecReporter, ibcPath string) error {
// Restart the relayer to finish IBC transfer connection w/ ics20-1 link
require.NoError(t, r.StopRelayer(ctx, eRep))
require.NoError(t, r.StartRelayer(ctx, eRep, ibcPath))
if err := r.StopRelayer(ctx, eRep); err != nil {
return fmt.Errorf("failed to stop relayer: %w", err)
}
if err := r.StartRelayer(ctx, eRep); err != nil {
return fmt.Errorf("failed to start relayer: %w", err)
}

// perform provider delegation to complete provider<>consumer channel connection
stakingVals, err := c.StakingQueryValidators(ctx, stakingttypes.BondStatusBonded)
require.NoError(t, err)
if err != nil {
return fmt.Errorf("failed to query validators: %w", err)
}

providerVal := stakingVals[0]

beforeDel, err := c.StakingQueryDelegationsTo(ctx, providerVal.OperatorAddress)
require.NoError(t, err)
if err != nil {
return fmt.Errorf("failed to query delegations to validator: %w", err)
}

err = c.GetNode().StakingDelegate(ctx, "validator", providerVal.OperatorAddress, fmt.Sprintf("1000000%s", c.Config().Denom))
require.NoError(t, err, "failed to delegate to validator")
if err != nil {
return fmt.Errorf("failed to delegate to validator: %w", err)
}

afterDel, err := c.StakingQueryDelegationsTo(ctx, providerVal.OperatorAddress)
require.NoError(t, err)
require.Greater(t, afterDel[0].Balance.Amount.Int64(), beforeDel[0].Balance.Amount.Int64())
if err != nil {
return fmt.Errorf("failed to query delegations to validator: %w", err)
}

if afterDel[0].Balance.Amount.LT(beforeDel[0].Balance.Amount) {
return fmt.Errorf("delegation failed: %w", err)
}

// Flush now pending ICS update packet -> consumer
c.FlushPendingICSPackets(t, ctx, r, eRep, ibcPath)
return c.FlushPendingICSPackets(ctx, r, eRep, ibcPath)
}

// FlushPendingICSPackets flushes the pending ICS packets to the consumer chain from the "provider" port.
func (c *CosmosChain) FlushPendingICSPackets(t *testing.T, ctx context.Context, r ibc.Relayer, eRep *testreporter.RelayerExecReporter, ibcPath string) {
func (c *CosmosChain) FlushPendingICSPackets(ctx context.Context, r ibc.Relayer, eRep *testreporter.RelayerExecReporter, ibcPath string) error {
channels, err := r.GetChannels(ctx, eRep, c.cfg.ChainID)
require.NoError(t, err)
if err != nil {
return fmt.Errorf("failed to get channels: %w", err)
}

ICSChannel := ""
for _, channel := range channels {
if channel.PortID == "provider" {
ICSChannel = channel.ChannelID
}
}
require.NoError(t, r.Flush(ctx, eRep, ibcPath, ICSChannel))

return r.Flush(ctx, eRep, ibcPath, ICSChannel)
}

// Bootstraps the provider chain and starts it from genesis
Expand Down
4 changes: 3 additions & 1 deletion examples/ibc/ics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
)

// 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() {
ver := icsVersions[0]
Expand Down Expand Up @@ -112,7 +113,8 @@ func icsTest(t *testing.T, version string) {
// - 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
provider.FinishICSProviderSetup(t, ctx, r, eRep, ibcPath)
err = provider.FinishICSProviderSetup(ctx, r, eRep, ibcPath)
require.NoError(t, err)

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

Expand Down
16 changes: 16 additions & 0 deletions local-interchain/interchain/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {
}

// Add Interchain Security chain pairs together
icsProviderPaths := make(map[string]ibc.Chain)
if len(icsPair) > 0 {
for provider, consumers := range icsPair {
var p, c ibc.Chain
Expand All @@ -152,6 +153,8 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {

logger.Info("Adding ICS pair", zap.String("provider", p.Config().ChainID), zap.String("consumer", c.Config().ChainID), zap.String("path", pathName))

icsProviderPaths[pathName] = p

ic = ic.AddProviderConsumerLink(interchaintest.ProviderConsumerLink{
Provider: p,
Consumer: c,
Expand Down Expand Up @@ -197,6 +200,19 @@ func StartChain(installDir, chainCfgFile string, ac *types.AppStartConfig) {
}
}

// ICS provider setup
if len(icsProviderPaths) > 0 {
logger.Info("ICS provider setup", zap.Any("icsProviderPaths", icsProviderPaths))

for ibcPath, chain := range icsProviderPaths {
if provider, ok := chain.(*cosmos.CosmosChain); ok {
if err := provider.FinishICSProviderSetup(ctx, relayer, eRep, ibcPath); err != nil {
log.Fatal("FinishICSProviderSetup", err)
}
}
}
}

// Starts a non blocking REST server to take action on the chain.
go func() {
cosmosChains := map[string]*cosmos.CosmosChain{}
Expand Down

0 comments on commit ef9f595

Please sign in to comment.