Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Oct 4, 2023
1 parent cd1668e commit 8b16deb
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 104 deletions.
62 changes: 12 additions & 50 deletions app/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
package app

import (
icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"

"github.com/cosmos/cosmos-sdk/baseapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcconnectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

// UpgradeName defines the on-chain upgrade name for the sample SimApp upgrade
Expand All @@ -31,7 +20,7 @@ import (
// NOTE: This upgrade defines a reference implementation of what an upgrade
// could look like when an application is migrating from Cosmos SDK version
// v0.46.x to v0.47.x.
const UpgradeName = "my chain upgrade"
const UpgradeName = "v0.43.x"

func (app WasmApp) RegisterUpgradeHandlers() {
// Set param key table for params module migration
Expand All @@ -40,32 +29,16 @@ func (app WasmApp) RegisterUpgradeHandlers() {

var keyTable paramstypes.KeyTable
switch subspace.Name() {
case authtypes.ModuleName:
keyTable = authtypes.ParamKeyTable() //nolint:staticcheck
case banktypes.ModuleName:
keyTable = banktypes.ParamKeyTable() //nolint:staticcheck
case stakingtypes.ModuleName:
keyTable = stakingtypes.ParamKeyTable()
case minttypes.ModuleName:
keyTable = minttypes.ParamKeyTable() //nolint:staticcheck
case distrtypes.ModuleName:
keyTable = distrtypes.ParamKeyTable() //nolint:staticcheck
case slashingtypes.ModuleName:
keyTable = slashingtypes.ParamKeyTable() //nolint:staticcheck
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable() //nolint:staticcheck
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable() //nolint:staticcheck
// ibc types
// ibc types
case ibcexported.ModuleName:
keyTable = ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
case ibctransfertypes.ModuleName:
keyTable = ibctransfertypes.ParamKeyTable()
case icahosttypes.SubModuleName:
keyTable = icahosttypes.ParamKeyTable()
case icacontrollertypes.SubModuleName:
keyTable = icacontrollertypes.ParamKeyTable()
// wasm
case wasmtypes.ModuleName:
keyTable = wasmtypes.ParamKeyTable() //nolint:staticcheck
default:
continue
}
Expand All @@ -75,17 +48,9 @@ func (app WasmApp) RegisterUpgradeHandlers() {
}
}

baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())

app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Migrate Tendermint consensus parameters from x/params module to a dedicated x/consensus module.
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)

// Note: this migration is optional,
// You can include x/gov proposal migration documented in [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md)

return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
)
Expand All @@ -97,10 +62,7 @@ func (app WasmApp) RegisterUpgradeHandlers() {

if upgradeInfo.Name == UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{
Added: []string{
consensustypes.ModuleName,
crisistypes.ModuleName,
},
Added: []string{},
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
Expand Down
9 changes: 6 additions & 3 deletions tests/system/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ func TestBasicWasm(t *testing.T) {
t.Cleanup(cleanupFn)

t.Log("Instantiate wasm code")
initMsg := fmt.Sprintf(`{"verifier":%q, "beneficiary":%q}`, randomBech32Addr(), randomBech32Addr())
verifierAddr := randomBech32Addr()
initMsg := fmt.Sprintf(`{"verifier":%q, "beneficiary":%q}`, verifierAddr, randomBech32Addr())
newContractAddr := cli.WasmInstantiate(codeID, initMsg, "--admin="+defaultSrcAddr, "--label=label1", "--from="+defaultSrcAddr)
assert.Equal(t, expContractAddr, newContractAddr)
assert.Len(t, done(), 1)
require.Equal(t, expContractAddr, newContractAddr)
require.Len(t, done(), 1)
gotRsp := cli.QuerySmart(newContractAddr, `{"verifier":{}}`)
require.Equal(t, fmt.Sprintf(`{"data":{"verifier":"%s"}}`, verifierAddr), gotRsp)

t.Log("Update Instantiate Config")
qResult = cli.CustomQuery("q", "wasm", "code-info", fmt.Sprint(codeID))
Expand Down
61 changes: 51 additions & 10 deletions tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@ package system
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"golang.org/x/exp/slices"

"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"golang.org/x/exp/slices"
)

type (
Expand All @@ -42,6 +43,7 @@ type WasmdCli struct {
awaitNextBlock awaitNextBlock
expTXCommitted bool
execBinary string
nodesCount int
}

// NewWasmdCLI constructor
Expand All @@ -52,6 +54,7 @@ func NewWasmdCLI(t *testing.T, sut *SystemUnderTest, verbose bool) *WasmdCli {
sut.rpcAddr,
sut.chainID,
sut.AwaitNextBlock,
sut.nodesCount,
filepath.Join(WorkDir, sut.outputDir),
"1"+sdk.DefaultBondDenom,
verbose,
Expand All @@ -67,6 +70,7 @@ func NewWasmdCLIx(
nodeAddress string,
chainID string,
awaiter awaitNextBlock,
nodesCount int,
homeDir string,
fees string,
debug bool,
Expand All @@ -84,6 +88,7 @@ func NewWasmdCLIx(
homeDir: homeDir,
Debug: debug,
awaitNextBlock: awaiter,
nodesCount: nodesCount,
fees: fees,
assertErrorFn: assertErrorFn,
expTXCommitted: expTXCommitted,
Expand All @@ -105,6 +110,7 @@ func (c WasmdCli) WithRunErrorMatcher(f RunErrorAssert) WasmdCli {
c.nodeAddress,
c.chainID,
c.awaitNextBlock,
c.nodesCount,
c.homeDir,
c.fees,
c.Debug,
Expand All @@ -120,6 +126,7 @@ func (c WasmdCli) WithNodeAddress(nodeAddr string) WasmdCli {
nodeAddr,
c.chainID,
c.awaitNextBlock,
c.nodesCount,
c.homeDir,
c.fees,
c.Debug,
Expand All @@ -135,6 +142,7 @@ func (c WasmdCli) WithAssertTXUncommitted() WasmdCli {
c.nodeAddress,
c.chainID,
c.awaitNextBlock,
c.nodesCount,
c.homeDir,
c.fees,
c.Debug,
Expand Down Expand Up @@ -216,7 +224,7 @@ func (c WasmdCli) runWithInput(args []string, input io.Reader) (output string, o
return cmd.CombinedOutput()
}()
ok = c.assertErrorFn(c.t, gotErr, string(gotOut))
return string(gotOut), ok
return strings.TrimSpace(string(gotOut)), ok
}

func (c WasmdCli) withQueryFlags(args ...string) []string {
Expand Down Expand Up @@ -302,7 +310,7 @@ func (c WasmdCli) FundAddress(destAddr, amount string) string {
// WasmStore uploads a wasm contract to the chain. Returns code id
func (c WasmdCli) WasmStore(file string, args ...string) int {
if len(args) == 0 {
args = []string{"--from=" + defaultSrcAddr, "--gas=2500000"}
args = []string{"--from=" + defaultSrcAddr, "--gas=2500000", "--fees=3stake"}
}
cmd := append([]string{"tx", "wasm", "store", file}, args...)
rsp := c.CustomCommand(cmd...)
Expand Down Expand Up @@ -368,8 +376,8 @@ func (c WasmdCli) GetTendermintValidatorSet() rpc.ResultValidatorsOutput {
return res
}

// IsInTendermintValset returns true when the given pub key is in the current active tendermint validator set
func (c WasmdCli) IsInTendermintValset(valPubKey cryptotypes.PubKey) (rpc.ResultValidatorsOutput, bool) {
// IsInCometBftValset returns true when the given pub key is in the current active tendermint validator set
func (c WasmdCli) IsInCometBftValset(valPubKey cryptotypes.PubKey) (rpc.ResultValidatorsOutput, bool) {
valResult := c.GetTendermintValidatorSet()
var found bool
for _, v := range valResult.Validators {
Expand All @@ -381,6 +389,39 @@ func (c WasmdCli) IsInTendermintValset(valPubKey cryptotypes.PubKey) (rpc.Result
return valResult, found
}

// SubmitUpgradeGovProposal submit a chain upgrade gov v1 proposal

// SubmitGovProposal submit a gov v1 proposal
func (c WasmdCli) SubmitGovProposal(proposalJson string, args ...string) string {
if len(args) == 0 {
args = []string{"--from=" + defaultSrcAddr}
}

pathToProposal := filepath.Join(c.t.TempDir(), "proposal.json")
err := os.WriteFile(pathToProposal, []byte(proposalJson), os.FileMode(0o744))
require.NoError(c.t, err)
c.t.Log("Submit upgrade proposal")
return c.CustomCommand(append([]string{"tx", "gov", "submit-proposal", pathToProposal}, args...)...)
}

// SubmitAndVoteGovProposal submit proposal, let all validators vote yes and return proposal id
func (c WasmdCli) SubmitAndVoteGovProposal(proposalJson string, args ...string) string {
rsp := c.SubmitGovProposal(proposalJson, args...)
RequireTxSuccess(c.t, rsp)
raw := c.CustomQuery("q", "gov", "proposals", "--depositor", c.GetKeyAddr(defaultSrcAddr))
proposals := gjson.Get(raw, "proposals.#.id").Array()
require.NotEmpty(c.t, proposals, raw)
ourProposalID := proposals[len(proposals)-1].String() // last is ours
for i := 0; i < c.nodesCount; i++ {
go func(i int) { // do parallel
c.t.Logf("Voting: validator %d\n", i)
rsp = c.CustomCommand("tx", "gov", "vote", ourProposalID, "yes", "--from", c.GetKeyAddr(fmt.Sprintf("node%d", i)))
RequireTxSuccess(c.t, rsp)
}(i)
}
return ourProposalID
}

// RequireTxSuccess require the received response to contain the success code
func RequireTxSuccess(t *testing.T, got string) {
t.Helper()
Expand Down
44 changes: 31 additions & 13 deletions tests/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,23 @@ func isLogNoise(text string) bool {
return false
}

// AwaitUpgradeInfo blocks util an upgrade info file is persisted to disk
func (s *SystemUnderTest) AwaitUpgradeInfo(t *testing.T) {
var found bool
for !found {
s.withEachNodeHome(func(i int, home string) {
_, err := os.Stat(filepath.Join(s.nodePath(0), "data", "upgrade-info.json"))
switch {
case err == nil:
found = true
case !os.IsNotExist(err):
t.Fatalf(err.Error())
}
})
time.Sleep(s.blockTime)
}
}

func (s *SystemUnderTest) AwaitChainStopped() {
for s.anyNodeRunning() {
time.Sleep(s.blockTime)
Expand Down Expand Up @@ -289,23 +306,24 @@ func (s *SystemUnderTest) StopChain() {
s.cleanupFn = nil
// send SIGTERM
s.withEachPid(func(p *os.Process) {
if err := p.Signal(syscall.SIGTERM); err != nil {
s.Logf("failed to stop node with pid %d: %s\n", p.Pid, err)
}
go func() {
if err := p.Signal(syscall.SIGTERM); err != nil {
s.Logf("failed to stop node with pid %d: %s\n", p.Pid, err)
}
}()
})
// give some final time to shut down
s.withEachPid(func(p *os.Process) {
time.Sleep(200 * time.Millisecond)
})
// goodbye
s.withEachPid(func(p *os.Process) {
s.Logf("killing node %d\n", p.Pid)
if err := p.Kill(); err != nil {
s.Logf("failed to kill node with pid %d: %s\n", p.Pid, err)
}
})
for s.anyNodeRunning() {
time.Sleep(100 * time.Millisecond)
for ; s.anyNodeRunning(); time.Sleep(100 * time.Millisecond) {
s.withEachPid(func(p *os.Process) {
s.Logf("killing node %d\n", p.Pid)
if err := p.Kill(); err != nil {
s.Logf("failed to kill node with pid %d: %s\n", p.Pid, err)
}
})
}
s.ChainStarted = false
}
Expand Down Expand Up @@ -547,13 +565,13 @@ func (s *SystemUnderTest) startNodesAsync(t *testing.T, xargs ...string) {
s.Logf("Node started: %d\n", pid)

// cleanup when stopped
go func() {
go func(pid int) {
_ = cmd.Wait() // blocks until shutdown
s.pidsLock.Lock()
delete(s.pids, pid)
s.pidsLock.Unlock()
s.Logf("Node stopped: %d\n", pid)
}()
}(pid)
})
}

Expand Down
Loading

0 comments on commit 8b16deb

Please sign in to comment.