Skip to content

Commit

Permalink
msg server function and tests for MsgScheduleIBCClientUpgrade (#4442)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei authored Aug 30, 2023
1 parent e30e1bd commit d9f6200
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 4 deletions.
73 changes: 73 additions & 0 deletions modules/core/02-client/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/stretchr/testify/require"
testifysuite "github.com/stretchr/testify/suite"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand Down Expand Up @@ -849,3 +851,74 @@ func (suite *TypesTestSuite) TestMsgIBCSoftwareUpgrade_GetSigners() {
}
}
}

// TestMsgIBCSoftwareUpgrade_ValidateBasic tests ValidateBasic for MsgIBCSoftwareUpgrade
func (suite *TypesTestSuite) TestMsgIBCSoftwareUpgrade_ValidateBasic() {
var (
signer string
plan upgradetypes.Plan
anyClient *codectypes.Any
)
testCases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"failure: invalid authority address",
func() {
signer = "invalid"
},
ibcerrors.ErrInvalidAddress,
},
{
"failure: error unpacking client state",
func() {
anyClient = &codectypes.Any{}
},
ibcerrors.ErrUnpackAny,
},
{
"failure: error validating upgrade plan, height is not greater than zero",
func() {
plan.Height = 0
},
sdkerrors.ErrInvalidRequest,
},
}

for _, tc := range testCases {
signer = ibctesting.TestAccAddress
plan = upgradetypes.Plan{
Name: "upgrade IBC clients",
Height: 1000,
}
upgradedClientState := ibctm.NewClientState(suite.chainA.ChainID, ibctesting.DefaultTrustLevel, ibctesting.TrustingPeriod, ibctesting.UnbondingPeriod, ibctesting.MaxClockDrift, clientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)
var err error
anyClient, err = types.PackClientState(upgradedClientState)
suite.Require().NoError(err)

tc.malleate()

msg := types.MsgIBCSoftwareUpgrade{
plan,
anyClient,
signer,
}

err = msg.ValidateBasic()
expPass := tc.expError == nil

if expPass {
suite.Require().NoError(err)
}
if tc.expError != nil {
suite.Require().True(errors.Is(err, tc.expError))
}
}
}
19 changes: 16 additions & 3 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,22 @@ func (k Keeper) UpdateClientParams(goCtx context.Context, msg *clienttypes.MsgUp
}

// IBCSoftwareUpgrade defines a rpc handler method for MsgIBCSoftwareUpgrade.
func (Keeper) IBCSoftwareUpgrade(goCtx context.Context, msg *clienttypes.MsgIBCSoftwareUpgrade) (*clienttypes.MsgIBCSoftwareUpgradeResponse, error) {
// TODO
return nil, nil
func (k Keeper) IBCSoftwareUpgrade(goCtx context.Context, msg *clienttypes.MsgIBCSoftwareUpgrade) (*clienttypes.MsgIBCSoftwareUpgradeResponse, error) {
if k.GetAuthority() != msg.Signer {
return nil, errorsmod.Wrapf(ibcerrors.ErrUnauthorized, "expected %s, got %s", k.GetAuthority(), msg.Signer)
}

ctx := sdk.UnwrapSDKContext(goCtx)
upgradedClientState, err := clienttypes.UnpackClientState(msg.UpgradedClientState)
if err != nil {
return nil, errorsmod.Wrapf(clienttypes.ErrInvalidClientType, "cannot unpack client state: %s", err)
}

if err = k.ClientKeeper.ScheduleIBCSoftwareUpgrade(ctx, msg.Plan, upgradedClientState); err != nil {
return nil, errorsmod.Wrapf(err, "cannot schedule IBC client upgrade")
}

return &clienttypes.MsgIBCSoftwareUpgradeResponse{}, nil
}

// UpdateConnectionParams defines a rpc handler method for MsgUpdateParams for the 03-connection submodule.
Expand Down
85 changes: 84 additions & 1 deletion modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package keeper_test

import (
"errors"

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

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v7/modules/core/errors"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/cosmos/ibc-go/v7/modules/core/keeper"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
Expand Down Expand Up @@ -827,7 +831,86 @@ func (suite *KeeperTestSuite) TestUpdateClientParams() {
}

// TestIBCSoftwareUpgrade tests the IBCSoftwareUpgrade rpc handler
func (*KeeperTestSuite) TestIBCSoftwareUpgrade() {
func (suite *KeeperTestSuite) TestIBCSoftwareUpgrade() {
var msg *clienttypes.MsgIBCSoftwareUpgrade
testCases := []struct {
name string
malleate func()
expError error
}{
{
"success: valid authority and client upgrade",
func() {},
nil,
},
{
"failure: invalid authority address",
func() {
msg.Signer = suite.chainA.SenderAccount.GetAddress().String()
},
ibcerrors.ErrUnauthorized,
},
{
"failure: invalid clientState",
func() {
msg.UpgradedClientState = nil
},
clienttypes.ErrInvalidClientType,
},
{
"failure: failed to schedule client upgrade",
func() {
msg.Plan.Height = 0
},
sdkerrors.ErrInvalidRequest,
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.SetupClients(path)
validAuthority := suite.chainA.App.GetIBCKeeper().GetAuthority()
plan := upgradetypes.Plan{
Name: "upgrade IBC clients",
Height: 1000,
}
// update trusting period
clientState := path.EndpointB.GetClientState()
clientState.(*ibctm.ClientState).TrustingPeriod += 100

var err error
msg, err = clienttypes.NewMsgIBCSoftwareUpgrade(
validAuthority,
plan,
clientState,
)

suite.Require().NoError(err)

tc.malleate()

_, err = keeper.Keeper.IBCSoftwareUpgrade(*suite.chainA.App.GetIBCKeeper(), suite.chainA.GetContext(), msg)

if tc.expError == nil {
suite.Require().NoError(err)
// upgrade plan is stored
storedPlan, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradePlan(suite.chainA.GetContext())
suite.Require().True(found)
suite.Require().Equal(plan, storedPlan)

// upgraded client state is stored
bz, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), plan.Height)
suite.Require().True(found)
upgradedClientState, err := clienttypes.UnmarshalClientState(suite.chainA.App.AppCodec(), bz)
suite.Require().NoError(err)
suite.Require().Equal(clientState.ZeroCustomFields(), upgradedClientState)
} else {
suite.Require().True(errors.Is(err, tc.expError))
}
})
}
}

// TestUpdateConnectionParams tests the UpdateConnectionParams rpc handler
Expand Down

0 comments on commit d9f6200

Please sign in to comment.