Skip to content

Commit

Permalink
add redemption assertions with relayer multiple simultaneous ordered …
Browse files Browse the repository at this point in the history
…channel packets fix
  • Loading branch information
agouin committed Sep 19, 2022
1 parent 027c16d commit 69b59fd
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 25 deletions.
48 changes: 48 additions & 0 deletions examples/stride/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package stride_test

import (
"context"
"encoding/json"
"fmt"

"github.com/icza/dyno"
"github.com/strangelove-ventures/ibctest/v3/chain/cosmos"
"github.com/strangelove-ventures/ibctest/v3/ibc"
"github.com/strangelove-ventures/ibctest/v3/test"
)

const (
Expand Down Expand Up @@ -91,6 +94,21 @@ type DepositRecordWrapper struct {
DepositRecord []DepositRecord `json:"DepositRecord"`
}

type UserRedemptionRecordWrapper struct {
UserRedemptionRecord []UserRedemptionRecord `json:"UserRedemptionRecord"`
}

type UserRedemptionRecord struct {
ID string `json:"id"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Amount string `json:"amount"`
Denom string `json:"denom"`
HostZoneID string `json:"hostZoneId"`
EpochNumber string `json:"epochNumber"`
ClaimIsPending bool `json:"claimIsPending"`
}

func ModifyGenesisStride() func(ibc.ChainConfig, []byte) ([]byte, error) {
return func(cfg ibc.ChainConfig, genbz []byte) ([]byte, error) {
g := make(map[string]interface{})
Expand Down Expand Up @@ -159,3 +177,33 @@ func ModifyGenesisStrideCounterparty() func(ibc.ChainConfig, []byte) ([]byte, er
return out, nil
}
}

// PollForHostZoneStakedBalance polls until the host zone for the given chainID has a non-zero staked balance
func PollForHostZoneStakedBalance(ctx context.Context, chain *cosmos.CosmosChain, startHeight, maxHeight uint64, chainID string) (HostZoneWrapper, error) {
var zero HostZoneWrapper
doPoll := func(ctx context.Context, height uint64) (any, error) {
stdout, _, err := chain.FullNodes[0].ExecQuery(ctx,
"stakeibc", "show-host-zone", chainID,
)
if err != nil {
return zero, err
}
hostZone := new(HostZoneWrapper)
err = json.Unmarshal(stdout, &hostZone)
if err != nil {
return zero, err
}

if hostZone.HostZone.StakedBal == "0" {
return zero, fmt.Errorf("host zone for %s has zero staked balance", chainID)
}

return *hostZone, nil
}
bp := test.BlockPoller{CurrentHeight: chain.Height, PollFunc: doPoll}
p, err := bp.DoPoll(ctx, startHeight, maxHeight)
if err != nil {
return zero, err
}
return p.(HostZoneWrapper), nil
}
114 changes: 89 additions & 25 deletions examples/stride/stride_redemptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package stride_test
import (
"context"
"encoding/json"
"fmt"
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -81,8 +81,8 @@ func TestStrideRedemptions(t *testing.T) {
r := ibctest.NewBuiltinRelayerFactory(
ibc.CosmosRly,
zaptest.NewLogger(t),
relayer.ImagePull(false),
relayer.CustomDockerImage("relayer", "local", "100:1000"),
//relayer.ImagePull(false),
relayer.CustomDockerImage("ghcr.io/cosmos/relayer", "andrew-client_icq", "100:1000"),
relayer.StartupFlags("-p", "events"),
).Build(t, client, network)

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestStrideRedemptions(t *testing.T) {
strideUser, gaiaUser := users[0], users[1]

strideFullNode := stride.FullNodes[0]
gaiaFullNode := gaia.FullNodes[0]
//gaiaFullNode := gaia.FullNodes[0]

// Wait a few blocks for user accounts to be created on chain.
err = test.WaitForBlocks(ctx, 2, stride, gaia)
Expand Down Expand Up @@ -249,44 +249,108 @@ func TestStrideRedemptions(t *testing.T) {
err = json.Unmarshal(stdout, &gaiaHostZone)
require.NoError(t, err)

strideUserIBCAtomBefore, err := stride.GetBalance(ctx, strideAddr, atomIBCDenom)
require.NoError(t, err)

strideUserSTAtomBefore, err := stride.GetBalance(ctx, strideAddr, "st"+gaiaCfg.Denom)
require.NoError(t, err)

// Liquid stake some atom
_, err = strideFullNode.ExecTx(ctx, strideUser.KeyName,
"stakeibc", "liquid-stake",
"10000", gaiaCfg.Denom,
)
require.NoError(t, err)

err = test.WaitForBlocks(ctx, 10, stride, gaia)
require.NoError(t, err)

strideUserIBCAtomAfter, err := stride.GetBalance(ctx, strideAddr, atomIBCDenom)
require.NoError(t, err)

strideUserSTAtomAfter, err := stride.GetBalance(ctx, strideAddr, "st"+gaiaCfg.Denom)
require.NoError(t, err)

require.Equal(t, strideUserIBCAtomBefore-10000, strideUserIBCAtomAfter)
require.Equal(t, strideUserSTAtomBefore+10000, strideUserSTAtomAfter)

// wait for delegation
for {
err = test.WaitForBlocks(ctx, 1, stride, gaia)
require.NoError(t, err)

stdout, _, err := strideFullNode.ExecQuery(ctx,
"stakeibc", "show-host-zone", gaiaCfg.ChainID,
)
require.NoError(t, err)
err = json.Unmarshal(stdout, &gaiaHostZone)
require.NoError(t, err)

if gaiaHostZone.HostZone.StakedBal != "0" {
break
}
}
height, err := stride.Height(ctx)
require.NoError(t, err)

_, err = PollForHostZoneStakedBalance(ctx, stride, height, height+70, gaiaCfg.ChainID)
require.NoError(t, err)

redemptionAmount := int64(5)

// Redeem
_, err = strideFullNode.ExecTx(ctx, strideUser.KeyName,
"stakeibc", "redeem-stake",
"5000", gaiaCfg.ChainID, gaiaUser.Address,
strconv.FormatInt(redemptionAmount, 10), gaiaCfg.ChainID, gaiaAddress,
)
require.NoError(t, err)

err = test.WaitForBlocks(ctx, 100, stride, gaia)
// err = test.WaitForBlocks(ctx, 20, stride, gaia)
// require.NoError(t, err)

// Check that tokens were transfered to the redemption account
redemptionAddress := gaiaHostZone.HostZone.RedemptionAccount.Address
redemptionBalance, err := gaiaFullNode.Chain.GetBalance(ctx, redemptionAddress, "uatom")
// redemptionBalance, err := gaiaFullNode.Chain.GetBalance(ctx, hz.HostZone.RedemptionAccount.Address, gaiaCfg.Denom)
// require.NoError(t, err)
// require.Greater(t, redemptionBalance, int64(0))

// fmt.Printf("redemption balance: %d\n", redemptionBalance)

stdout, _, err = strideFullNode.ExecQuery(ctx,
"records", "list-user-redemption-record",
)
require.NoError(t, err)

var userRedemptionRecords UserRedemptionRecordWrapper
err = json.Unmarshal(stdout, &userRedemptionRecords)
require.NoError(t, err)
require.Len(t, userRedemptionRecords.UserRedemptionRecord, 1)
redemptionRecord := userRedemptionRecords.UserRedemptionRecord[0]
require.Equal(t, redemptionRecord.HostZoneID, gaiaCfg.ChainID)
require.False(t, redemptionRecord.ClaimIsPending)
require.Equal(t, redemptionRecord.Amount, strconv.FormatInt(redemptionAmount, 10))
require.Equal(t, redemptionRecord.Denom, gaiaCfg.Denom)
require.Equal(t, redemptionRecord.Sender, strideAddr)
require.Equal(t, redemptionRecord.Receiver, gaiaAddress)

// wait for unbondings to process
err = test.WaitForBlocks(ctx, 100, stride, gaia)
require.NoError(t, err)

balanceBeforeClaim, err := gaia.GetBalance(ctx, gaiaAddress, gaiaCfg.Denom)
require.NoError(t, err)

_, err = strideFullNode.ExecTx(ctx, strideUser.KeyName,
"stakeibc", "claim-undelegated-tokens", gaiaCfg.ChainID,
redemptionRecord.EpochNumber, strideAddr,
)
require.NoError(t, err)

stdout, _, err = strideFullNode.ExecQuery(ctx,
"records", "list-user-redemption-record",
)
require.NoError(t, err)

err = json.Unmarshal(stdout, &userRedemptionRecords)
require.NoError(t, err)
require.Len(t, userRedemptionRecords.UserRedemptionRecord, 1)
redemptionRecord = userRedemptionRecords.UserRedemptionRecord[0]
require.Equal(t, redemptionRecord.HostZoneID, gaiaCfg.ChainID)
require.True(t, redemptionRecord.ClaimIsPending)
require.Equal(t, redemptionRecord.Amount, strconv.FormatInt(redemptionAmount, 10))
require.Equal(t, redemptionRecord.Denom, gaiaCfg.Denom)
require.Equal(t, redemptionRecord.Sender, strideAddr)
require.Equal(t, redemptionRecord.Receiver, gaiaAddress)

fmt.Println("BALANCE:", redemptionBalance)
err = test.WaitForBlocks(ctx, 100, stride, gaia)
require.NoError(t, err)

balanceAfterClaim, err := gaia.GetBalance(ctx, gaiaAddress, gaiaCfg.Denom)
require.NoError(t, err)

require.True(t, redemptionBalance > 0)
require.Equal(t, balanceBeforeClaim+redemptionAmount, balanceAfterClaim)
}

0 comments on commit 69b59fd

Please sign in to comment.