Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msg server function and tests for MsgScheduleIBCClientUpgrade #4442

Merged
merged 35 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f9ce4fd
add protos and keeper function placeholder
charleenfei Aug 22, 2023
5242278
add keeper functions/tests, msgs and tests
charleenfei Aug 23, 2023
f499db4
fix linter
charleenfei Aug 23, 2023
59514fe
bring back msg_server placeholders
charleenfei Aug 23, 2023
55eb017
add msg_server function and tests
charleenfei Aug 23, 2023
a4922e7
fix linter
charleenfei Aug 23, 2023
8c4ca90
Merge branch 'charly/issue#3673-add-message-and-rpc-handler-to-upgrad…
charleenfei Aug 23, 2023
70a431f
update msg name, make changes in testing
charleenfei Aug 24, 2023
22125d3
Merge branch 'charly/issue#3673-add-message-and-rpc-handler-to-upgrad…
charleenfei Aug 24, 2023
314c81d
fix linter, update to new msg name
charleenfei Aug 24, 2023
b2eee55
update test bool, method name
charleenfei Aug 28, 2023
2c94f5f
capitalisation
charleenfei Aug 28, 2023
6d7407b
Merge branch 'charly/issue#3673-add-message-and-rpc-handler-to-upgrad…
charleenfei Aug 28, 2023
5b4aa3c
update test bool
charleenfei Aug 28, 2023
5462ba6
update testcase
charleenfei Aug 28, 2023
f23b9b4
Merge branch 'charly/issue#3673-add-message-and-rpc-handler-to-upgrad…
charleenfei Aug 28, 2023
776a7ce
added check that message implements sdk.Msg interface
Aug 29, 2023
efa5e43
update authority -> signers
charleenfei Aug 29, 2023
dbbf6b9
Merge branch 'feat/govv1' into charly/issue#3673-add-message-and-rpc-…
charleenfei Aug 29, 2023
eb99d94
merge
charleenfei Aug 29, 2023
77a2720
fix merge conflicts
charleenfei Aug 29, 2023
9744005
Merge branch 'charly/issue#3673-add-message-and-rpc-handler-to-upgrad…
charleenfei Aug 29, 2023
7868ed2
update authority -> signer
charleenfei Aug 29, 2023
d5d5dcb
add tests back in
charleenfei Aug 29, 2023
c418302
fix merge shenanigans
charleenfei Aug 29, 2023
4cad828
add check for tm client type
charleenfei Aug 29, 2023
89da39e
msgs
charleenfei Aug 29, 2023
3de8fd5
Merge branch 'feat/govv1' into charly/issue#3673-msg-server
charleenfei Aug 29, 2023
c92a408
move params tests back to reduce diffs
charleenfei Aug 29, 2023
f4cd006
merge shenanigans
charleenfei Aug 29, 2023
49655cc
update tests per comments
charleenfei Aug 29, 2023
0505634
linter fix
charleenfei Aug 29, 2023
c4c033d
Merge branch 'feat/govv1' into charly/issue#3673-msg-server
charleenfei Aug 30, 2023
0125cdc
go mod tidy
charleenfei Aug 30, 2023
d7ff43a
pr comments
charleenfei Aug 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test got removed in the last pr merge process.

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 (
expError error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: I like defining the expError on the testCases, it makes it more explicit what we expect to occur and avoids potential mishaps of not setting an expected error

msg *clienttypes.MsgIBCSoftwareUpgrade
)
testCases := []struct {
name string
malleate func()
}{
{
"success: valid authority and client upgrade",
func() {},
},
{
"failure: invalid authority address",
func() {
msg.Signer = suite.chainA.SenderAccount.GetAddress().String()
expError = ibcerrors.ErrUnauthorized
},
},
{
"failure: invalid clientState",
func() {
msg.UpgradedClientState = nil
expError = clienttypes.ErrInvalidClientType
},
},
{
"failure: failed to schedule client upgrade",
func() {
msg.Plan.Height = 0
expError = 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 := ibctm.NewClientState(suite.chainA.ChainID, ibctesting.DefaultTrustLevel, ibctesting.TrustingPeriod+100, ibctesting.UnbondingPeriod, ibctesting.MaxClockDrift, clienttypes.NewHeight(1, 10), commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also likely do:

clientState := path.EndpointB.GetClientState()
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 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside expPass we can also check that there exists an upgrade plan in the x/upgrade store and upgraded client state in the upgradePath

suite.Require().Equal(clientState.ZeroCustomFields(), upgradedClientState)
} else {
suite.Require().True(errors.Is(err, expError))
}
})
}
}

// TestUpdateConnectionParams tests the UpdateConnectionParams rpc handler
Expand Down