Skip to content

Commit

Permalink
ci: interchaintest for IBC and jailing with consumer chains.
Browse files Browse the repository at this point in the history
  • Loading branch information
fastfadingviolets committed Aug 15, 2024
1 parent 78daca4 commit 7a2b7ce
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
44 changes: 44 additions & 0 deletions tests/interchain/chainsuite/chain_ics.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,47 @@ func (p *Chain) CheckCCV(ctx context.Context, consumer *Chain, relayer *Relayer,
func relayerICSPathFor(chainA, chainB *Chain) string {
return fmt.Sprintf("ics-%s-%s", chainA.Config().ChainID, chainB.Config().ChainID)
}

func (p *Chain) IsValoperJailed(ctx context.Context, valoper string) (bool, error) {
out, _, err := p.Validators[0].ExecQuery(ctx, "staking", "validator", valoper)
if err != nil {
return false, err
}
if gjson.GetBytes(out, "jailed").Exists() {
return gjson.GetBytes(out, "jailed").Bool(), nil
}
return gjson.GetBytes(out, "validator.jailed").Bool(), nil
}

func (p *Chain) IsValidatorJailedForConsumerDowntime(ctx context.Context, relayer Relayer, consumer *Chain, validatorIdx int) (jailed bool, err error) {
if err = consumer.Validators[validatorIdx].StopContainer(ctx); err != nil {
return
}
defer func() {
err = consumer.Validators[validatorIdx].StartContainer(ctx)
}()
channel, err := relayer.GetChannelWithPort(ctx, consumer, p, "consumer")
if err != nil {
return
}
if err = testutil.WaitForBlocks(ctx, SlashingWindowConsumer+1, consumer); err != nil {
return
}
rs := relayer.Exec(ctx, GetRelayerExecReporter(ctx), []string{
"hermes", "clear", "packets", "--port", "consumer", "--channel", channel.ChannelID,
"--chain", consumer.Config().ChainID,
}, nil)
if rs.Err != nil {
return false, rs.Err
}
tCtx, tCancel := context.WithTimeout(ctx, 30*CommitTimeout)
defer tCancel()
for tCtx.Err() == nil {
jailed, err = p.IsValoperJailed(ctx, p.ValidatorWallets[validatorIdx].ValoperAddress)
if err != nil || jailed {
return
}
time.Sleep(CommitTimeout)
}
return false, nil
}
70 changes: 70 additions & 0 deletions tests/interchain/chainsuite/ibc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package chainsuite

import (
"context"
"fmt"
"time"

"cosmossdk.io/math"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
)

func SendSimpleIBCTx(ctx context.Context, chainA *Chain, chainB *Chain, relayer *Relayer) error {
addr1 := chainA.ValidatorWallets[0].Address
addr2 := chainB.ValidatorWallets[0].Address

senderTxChannel, err := relayer.GetTransferChannel(ctx, chainA, chainB)
if err != nil {
return err
}

srcDenomTrace := transfertypes.ParseDenomTrace(transfertypes.GetPrefixedDenom("transfer", senderTxChannel.Counterparty.ChannelID, chainA.Config().Denom))
dstIbcDenom := srcDenomTrace.IBCDenom()

initial1, err := chainA.GetBalance(ctx, addr1, chainA.Config().Denom)
if err != nil {
return err
}
initial2, err := chainB.GetBalance(ctx, addr2, dstIbcDenom)
if err != nil {
return err
}

amountToSend := math.NewInt(1_000_000)
_, err = chainA.Validators[0].SendIBCTransfer(ctx, senderTxChannel.ChannelID, chainA.ValidatorWallets[0].Moniker, ibc.WalletAmount{
Denom: chainA.Config().Denom,
Amount: amountToSend,
Address: addr2,
}, ibc.TransferOptions{})
if err != nil {
return err
}
tCtx, tCancel := context.WithTimeout(ctx, 30*CommitTimeout)
defer tCancel()

for tCtx.Err() == nil {
time.Sleep(CommitTimeout)
var final1, final2 math.Int
final1, err = chainA.GetBalance(ctx, addr1, chainA.Config().Denom)
if err != nil {
continue
}
final2, err = chainB.GetBalance(ctx, addr2, dstIbcDenom)
if err != nil {
continue
}

if !initial2.Add(amountToSend).Equal(final2) {
err = fmt.Errorf("destination balance not updated; expected %s, got %s", initial2.Add(amountToSend), final2)
continue
}
if !final1.LTE(initial1.Sub(amountToSend)) {
err = fmt.Errorf("source balance not updated; expected <= %s, got %s", initial1.Sub(amountToSend), final1)
continue
}
break
}

return err
}
21 changes: 20 additions & 1 deletion tests/interchain/consumer_launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *ConsumerLaunchSuite) TestChainLaunch() {
Version: s.OtherChainVersion,
ShouldCopyProviderKey: s.ShouldCopyProviderKey,
Denom: chainsuite.Ucon,
TopN: 95,
TopN: 94,
}
consumer, err := s.Chain.AddConsumerChain(s.GetContext(), s.Relayer, cfg)
s.Require().NoError(err)
Expand All @@ -44,10 +44,27 @@ func (s *ConsumerLaunchSuite) TestChainLaunch() {

err = s.Chain.CheckCCV(s.GetContext(), consumer, s.Relayer, 1_000_000, 0, 1)
s.Require().NoError(err)
s.Require().NoError(chainsuite.SendSimpleIBCTx(s.GetContext(), s.Chain, consumer, s.Relayer))

jailed, err := s.Chain.IsValidatorJailedForConsumerDowntime(s.GetContext(), *s.Relayer, consumer, 1)
s.Require().NoError(err)
s.Require().True(jailed, "validator 1 should be jailed for downtime")
jailed, err = s.Chain.IsValidatorJailedForConsumerDowntime(s.GetContext(), *s.Relayer, consumer, 5)
s.Require().NoError(err)
s.Require().False(jailed, "validator 5 should not be jailed for downtime")

consumer2, err := s.Chain.AddConsumerChain(s.GetContext(), s.Relayer, cfg)
s.Require().NoError(err)
err = s.Chain.CheckCCV(s.GetContext(), consumer2, s.Relayer, 1_000_000, 0, 1)
s.Require().NoError(err)
s.Require().NoError(chainsuite.SendSimpleIBCTx(s.GetContext(), s.Chain, consumer2, s.Relayer))

jailed, err = s.Chain.IsValidatorJailedForConsumerDowntime(s.GetContext(), *s.Relayer, consumer2, 1)
s.Require().NoError(err)
s.Require().True(jailed, "validator 1 should be jailed for downtime")
jailed, err = s.Chain.IsValidatorJailedForConsumerDowntime(s.GetContext(), *s.Relayer, consumer2, 5)
s.Require().NoError(err)
s.Require().False(jailed, "validator 5 should not be jailed for downtime")
}

func TestICS40ChainLaunch(t *testing.T) {
Expand Down Expand Up @@ -122,6 +139,8 @@ func (s *MainnetConsumerChainsSuite) TestMainnetConsumerChainsAfterUpgrade() {

s.Require().NoError(s.Chain.CheckCCV(s.GetContext(), neutron, s.Relayer, 1_000_000, 0, 1))
s.Require().NoError(s.Chain.CheckCCV(s.GetContext(), stride, s.Relayer, 1_000_000, 0, 1))
s.Require().NoError(chainsuite.SendSimpleIBCTx(s.GetContext(), s.Chain, neutron, s.Relayer))
s.Require().NoError(chainsuite.SendSimpleIBCTx(s.GetContext(), s.Chain, stride, s.Relayer))
}

func TestMainnetConsumerChainsAfterUpgrade(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/interchain/cosmwasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path"
"testing"

"github.com/cosmos/gaia/v19/tests/interchain/chainsuite"
"github.com/cosmos/gaia/v20/tests/interchain/chainsuite"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/stretchr/testify/suite"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/interchain/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"github.com/cosmos/gaia/v19/tests/interchain/chainsuite"
"github.com/cosmos/gaia/v20/tests/interchain/chainsuite"
"github.com/stretchr/testify/suite"
"golang.org/x/mod/semver"
)
Expand Down

0 comments on commit 7a2b7ce

Please sign in to comment.