Skip to content

Commit

Permalink
fix use v47 genesis for chain upgrade test & abstract
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Aug 1, 2023
1 parent b52df27 commit 251d268
Showing 1 changed file with 61 additions and 85 deletions.
146 changes: 61 additions & 85 deletions interchaintest/chain_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"testing"
"time"

cosmosproto "github.com/cosmos/gogoproto/proto"
"github.com/docker/docker/client"
"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/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

const (
Expand All @@ -35,60 +38,11 @@ func CosmosChainUpgradeTest(t *testing.T, chainName, initialVersion, upgradeBran

t.Log(chainName, initialVersion, upgradeBranchVersion, upgradeRepo, upgradeName)

// v45 genesis params
genesisKVs := []cosmos.GenesisKV{
{
Key: "app_state.gov.voting_params.voting_period",
Value: VotingPeriod,
},
{
Key: "app_state.gov.deposit_params.max_deposit_period",
Value: MaxDepositPeriod,
},
{
Key: "app_state.gov.deposit_params.min_deposit.0.denom",
Value: Denom,
},
}

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: chainName,
ChainName: chainName,
Version: initialVersion,
ChainConfig: ibc.ChainConfig{
Images: []ibc.DockerImage{
{
Repository: JunoE2ERepo,
Version: initialVersion,
UidGid: JunoImage.UidGid,
},
},
GasPrices: fmt.Sprintf("0%s", Denom),
ModifyGenesis: cosmos.ModifyGenesis(genesisKVs),
UsingNewGenesisCommand: true, // SDK v47
},
},
})

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

numVals, numNodes := 4, 4
chains := CreateThisBranchChain(t, numVals, numNodes)
chain := chains[0].(*cosmos.CosmosChain)

ic := interchaintest.NewInterchain().
AddChain(chain)

ctx := context.Background()
client, network := interchaintest.DockerSetup(t)

err = ic.Build(ctx, nil, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
})
require.NoError(t, err)
ic, ctx, client, _ := BuildInitialChain(t, chains)

t.Cleanup(func() {
_ = ic.Close()
Expand All @@ -103,42 +57,18 @@ func CosmosChainUpgradeTest(t *testing.T, chainName, initialVersion, upgradeBran
require.NoError(t, err, "error fetching height before submit upgrade proposal")

haltHeight := height + haltHeightDelta
proposalID := SubmitUpgradeProposal(t, ctx, chain, chainUser, upgradeName, haltHeight)

proposal := cosmos.SoftwareUpgradeProposal{
Deposit: "500000000" + chain.Config().Denom, // greater than min deposit
Title: "Chain Upgrade 1",
Name: upgradeName,
Description: "First chain software upgrade",
Height: haltHeight,
}

upgradeTx, err := chain.UpgradeProposal(ctx, chainUser.KeyName(), proposal)
require.NoError(t, err, "error submitting software upgrade proposal tx")
ValidatorVoting(t, ctx, chain, proposalID, height, haltHeight)

err = chain.VoteOnProposalAllValidators(ctx, upgradeTx.ProposalID, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

_, err = cosmos.PollForProposalStatus(ctx, chain, height, height+haltHeightDelta, upgradeTx.ProposalID, cosmos.ProposalStatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height before upgrade")

// this should timeout due to chain halt at upgrade height.
_ = testutil.WaitForBlocks(timeoutCtx, int(haltHeight-height), chain)
UpgradeNodes(t, ctx, chain, client, haltHeight, upgradeRepo, upgradeBranchVersion)

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height after chain should have halted")

// make sure that chain is halted
require.Equal(t, haltHeight, height, "height is not equal to halt height")
}

func UpgradeNodes(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, client *client.Client, haltHeight uint64, upgradeRepo, upgradeBranchVersion string) {
// bring down nodes to prepare for upgrade
t.Log("stopping node(s)")
err = chain.StopAllNodes(ctx)
err := chain.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")

// upgrade version on all nodes
Expand All @@ -152,14 +82,60 @@ func CosmosChainUpgradeTest(t *testing.T, chainName, initialVersion, upgradeBran
err = chain.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")

timeoutCtx, timeoutCtxCancel = context.WithTimeout(ctx, time.Second*60)
timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*60)
defer timeoutCtxCancel()

err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), chain)
require.NoError(t, err, "chain did not produce blocks after upgrade")

height, err = chain.Height(ctx)
height, err := chain.Height(ctx)
require.NoError(t, err, "error fetching height after upgrade")

require.GreaterOrEqual(t, height, haltHeight+blocksAfterUpgrade, "height did not increment enough after upgrade")
}

func ValidatorVoting(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, proposalID string, height uint64, haltHeight uint64) {
err := chain.VoteOnProposalAllValidators(ctx, proposalID, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

_, err = cosmos.PollForProposalStatus(ctx, chain, height, height+haltHeightDelta, proposalID, cosmos.ProposalStatusPassed)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height before upgrade")

// this should timeout due to chain halt at upgrade height.
_ = testutil.WaitForBlocks(timeoutCtx, int(haltHeight-height), chain)

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height after chain should have halted")

// make sure that chain is halted
require.Equal(t, haltHeight, height, "height is not equal to halt height")
}

func SubmitUpgradeProposal(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, upgradeName string, haltHeight uint64) string {
// TODO Return proposal id
upgradeMsg := []cosmosproto.Message{
&upgradetypes.MsgSoftwareUpgrade{
// gGov Module account
Authority: "juno10d07y265gmmuvt4z0w9aw880jnsr700jvss730",
Plan: upgradetypes.Plan{
Name: upgradeName,
Height: int64(haltHeight),
},
},
}

proposal, err := chain.BuildProposal(upgradeMsg, "Chain Upgrade 1", "Summary desc", "ipfs://CID", fmt.Sprintf(`500000000%s`, chain.Config().Denom))
require.NoError(t, err, "error building proposal")

txProp, err := chain.SubmitProposal(ctx, user.KeyName(), proposal)
t.Log("txProp", txProp)
require.NoError(t, err, "error submitting proposal")

return txProp.ProposalID
}

0 comments on commit 251d268

Please sign in to comment.