Skip to content

Commit

Permalink
refactor: x/upgrade audit changes (#14188)
Browse files Browse the repository at this point in the history
  • Loading branch information
likhita-809 authored Dec 8, 2022
1 parent f3be418 commit bd59987
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 27 deletions.
4 changes: 2 additions & 2 deletions api/cosmos/upgrade/v1beta1/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions api/cosmos/upgrade/v1beta1/upgrade.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proto/cosmos/upgrade/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ service Msg {
//
// Since: cosmos-sdk 0.46
rpc SoftwareUpgrade(MsgSoftwareUpgrade) returns (MsgSoftwareUpgradeResponse);

// CancelUpgrade is a governance operation for cancelling a previously
// approvid software upgrade.
// approved software upgrade.
//
// Since: cosmos-sdk 0.46
rpc CancelUpgrade(MsgCancelUpgrade) returns (MsgCancelUpgradeResponse);
Expand Down
9 changes: 8 additions & 1 deletion proto/cosmos/upgrade/v1beta1/upgrade.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ message Plan {
[deprecated = true, (gogoproto.stdtime) = true, (gogoproto.nullable) = false, (amino.dont_omitempty) = true];

// The height at which the upgrade must be performed.
// Only used if Time is not set.
int64 height = 3;

// Any application specific upgrade info to be included on-chain
Expand All @@ -54,8 +53,13 @@ message SoftwareUpgradeProposal {
option (amino.name) = "cosmos-sdk/SoftwareUpgradeProposal";
option (gogoproto.equal) = true;

// title of the proposal
string title = 1;

// description of the proposal
string description = 2;

// plan of the proposal
Plan plan = 3 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

Expand All @@ -69,7 +73,10 @@ message CancelSoftwareUpgradeProposal {
option (amino.name) = "cosmos-sdk/CancelSoftwareUpgradeProposal";
option (gogoproto.equal) = true;

// title of the proposal
string title = 1;

// description of the proposal
string description = 2;
}

Expand Down
2 changes: 1 addition & 1 deletion x/upgrade/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

// GetQueryCmd returns the parent command for all x/upgrade CLi query commands.
// GetQueryCmd returns the parent command for all x/upgrade CLI query commands.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Expand Down
176 changes: 176 additions & 0 deletions x/upgrade/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package cli_test

import (
"context"
"fmt"
"io"
"testing"

"github.com/stretchr/testify/require"
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradecli "github.com/cosmos/cosmos-sdk/x/upgrade/client/cli"
)

func TestGetCurrentPlanCmd(t *testing.T) {
encCfg := testutilmod.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
baseCtx := client.Context{}.
WithKeyring(kr).
WithTxConfig(encCfg.TxConfig).
WithCodec(encCfg.Codec).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[--output=json]`,
},
{
name: "text output",
args: []string{fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[--output=text]`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd := upgradecli.GetCurrentPlanCmd()
cmd.SetOut(io.Discard)
require.NotNil(t, cmd)

cmd.SetContext(ctx)
cmd.SetArgs(tc.args)

require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))

require.Contains(t, fmt.Sprint(cmd), "plan [] [] get upgrade plan (if one exists)")
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
})
}
}

func TestGetAppliedPlanCmd(t *testing.T) {
encCfg := testutilmod.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
baseCtx := client.Context{}.
WithKeyring(kr).
WithTxConfig(encCfg.TxConfig).
WithCodec(encCfg.Codec).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

testCases := []struct {
name string
args []string
expCmdOutput string
}{
{
name: "json output",
args: []string{"test-upgrade", fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `[test-upgrade --output=json]`,
},
{
name: "text output",
args: []string{"test-upgrade", fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `[test-upgrade --output=text]`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd := upgradecli.GetAppliedPlanCmd()
cmd.SetOut(io.Discard)
require.NotNil(t, cmd)

cmd.SetContext(ctx)
cmd.SetArgs(tc.args)

require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))

require.Contains(t, fmt.Sprint(cmd), "applied [upgrade-name] [] [] block header for height at which a completed upgrade was applied")
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
})
}
}

func TestGetModuleVersionsCmd(t *testing.T) {
encCfg := testutilmod.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
baseCtx := client.Context{}.
WithKeyring(kr).
WithTxConfig(encCfg.TxConfig).
WithCodec(encCfg.Codec).
WithClient(clitestutil.MockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

testCases := []struct {
msg string
args []string
expCmdOutput string
}{
{
msg: "test full query with json output",
args: []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
expCmdOutput: `--height=1 --output=json`,
},
{
msg: "test full query with text output",
args: []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)},
expCmdOutput: `--height=1 --output=text`,
},
{
msg: "test single module",
args: []string{"bank", fmt.Sprintf("--%s=1", flags.FlagHeight)},
expCmdOutput: `bank --height=1`,
},
{
msg: "test non-existent module",
args: []string{"abcdefg", fmt.Sprintf("--%s=1", flags.FlagHeight)},
expCmdOutput: `abcdefg --height=1`,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.msg, func(t *testing.T) {
ctx := svrcmd.CreateExecuteContext(context.Background())

cmd := upgradecli.GetModuleVersionsCmd()
cmd.SetOut(io.Discard)
require.NotNil(t, cmd)

cmd.SetContext(ctx)
cmd.SetArgs(tc.args)

require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))

require.Contains(t, fmt.Sprint(cmd), "module_versions [optional module_name] [] [] get the list of module versions")
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOutput)
})
}
}
7 changes: 4 additions & 3 deletions x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (k *Keeper) SetVersionSetter(vs xp.ProtocolVersionSetter) {
k.versionSetter = vs
}

// GetVersionSetter gets the protocol version field of baseapp
func (k *Keeper) GetVersionSetter() xp.ProtocolVersionSetter {
return k.versionSetter
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func (k Keeper) GetModuleVersions(ctx sdk.Context) []*types.ModuleVersion {
return mv
}

// gets the version for a given module, and returns true if it exists, false otherwise
// getModuleVersion gets the version for a given module, and returns true if it exists, false otherwise
func (k Keeper) getModuleVersion(ctx sdk.Context, name string) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
it := sdk.KVStorePrefixIterator(store, []byte{types.VersionMapByte})
Expand Down Expand Up @@ -223,15 +224,15 @@ func (k Keeper) GetUpgradedClient(ctx sdk.Context, height int64) ([]byte, bool)
return bz, true
}

// SetUpgradedConsensusState set the expected upgraded consensus state for the next version of this chain
// SetUpgradedConsensusState sets the expected upgraded consensus state for the next version of this chain
// using the last height committed on this chain.
func (k Keeper) SetUpgradedConsensusState(ctx sdk.Context, planHeight int64, bz []byte) error {
store := ctx.KVStore(k.storeKey)
store.Set(types.UpgradedConsStateKey(planHeight), bz)
return nil
}

// GetUpgradedConsensusState set the expected upgraded consensus state for the next version of this chain
// GetUpgradedConsensusState gets the expected upgraded consensus state for the next version of this chain
func (k Keeper) GetUpgradedConsensusState(ctx sdk.Context, lastHeight int64) ([]byte, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.UpgradedConsStateKey(lastHeight))
Expand Down
Loading

0 comments on commit bd59987

Please sign in to comment.