Skip to content

Commit

Permalink
Add proto message, implement sdk.Msg for Recover Client. (#4494)
Browse files Browse the repository at this point in the history
* Add proto message for Recover client, implement sdk.Message interface.

* Update modules/core/02-client/types/msgs.go

Co-authored-by: Damian Nolan <[email protected]>

* Apply suggestions from code review

Co-authored-by: Charly <[email protected]>

* Remove gogoproto false for cmp, lint, move ibctesting address inline.

---------

Co-authored-by: Damian Nolan <[email protected]>
Co-authored-by: Charly <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2023
1 parent 85821bd commit 51417ee
Show file tree
Hide file tree
Showing 6 changed files with 605 additions and 43 deletions.
1 change: 1 addition & 0 deletions modules/core/02-client/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&MsgUpgradeClient{},
&MsgSubmitMisbehaviour{},
&MsgUpdateParams{},
&MsgRecoverClient{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
32 changes: 32 additions & 0 deletions modules/core/02-client/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
_ sdk.Msg = (*MsgSubmitMisbehaviour)(nil)
_ sdk.Msg = (*MsgUpgradeClient)(nil)
_ sdk.Msg = (*MsgUpdateParams)(nil)
_ sdk.Msg = (*MsgRecoverClient)(nil)

_ codectypes.UnpackInterfacesMessage = (*MsgCreateClient)(nil)
_ codectypes.UnpackInterfacesMessage = (*MsgUpdateClient)(nil)
Expand Down Expand Up @@ -283,3 +284,34 @@ func (msg *MsgUpdateParams) ValidateBasic() error {
}
return msg.Params.Validate()
}

// NewMsgRecoverClient creates a new MsgRecoverClient instance
func NewMsgRecoverClient(signer, subjectClientID, substituteClientID string) *MsgRecoverClient {
return &MsgRecoverClient{
Signer: signer,
SubjectClientId: subjectClientID,
SubstituteClientId: substituteClientID,
}
}

// GetSigners returns the expected signers for a MsgRecoverClient message.
func (msg *MsgRecoverClient) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}

// ValidateBasic performs basic checks on a MsgRecoverClient.
func (msg *MsgRecoverClient) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Signer); err != nil {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}

if err := host.ClientIdentifierValidator(msg.SubjectClientId); err != nil {
return err
}

return host.ClientIdentifierValidator(msg.SubstituteClientId)
}
85 changes: 85 additions & 0 deletions modules/core/02-client/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/cosmos/ibc-go/v7/modules/core/02-client/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"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
Expand Down Expand Up @@ -678,3 +680,86 @@ func TestMsgUpdateParamsGetSigners(t *testing.T) {
}
}
}

// TestMsgRecoverClientValidateBasic tests ValidateBasic for MsgRecoverClient
func (suite *TypesTestSuite) TestMsgRecoverClientValidateBasic() {
var msg *types.MsgRecoverClient

testCases := []struct {
name string
malleate func()
expError error
}{
{
"success: valid signer and client identifiers",
func() {},
nil,
},
{
"failure: invalid signer address",
func() {
msg.Signer = "invalid"
},
ibcerrors.ErrInvalidAddress,
},
{
"failure: invalid subject client ID",
func() {
msg.SubjectClientId = ""
},
host.ErrInvalidID,
},
{
"failure: invalid substitute client ID",
func() {
msg.SubjectClientId = ""
},
host.ErrInvalidID,
},
}

for _, tc := range testCases {
msg = types.NewMsgRecoverClient(
ibctesting.TestAccAddress,
ibctesting.FirstClientID,
ibctesting.FirstClientID,
)

tc.malleate()

err := msg.ValidateBasic()
expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err, "valid case %s failed", tc.name)
} else {
suite.Require().Error(err, "invalid case %s passed", tc.name)
suite.Require().ErrorIs(err, tc.expError, "invalid case %s passed", tc.name)
}
}
}

// TestMsgRecoverClientGetSigners tests GetSigners for MsgRecoverClient
func TestMsgRecoverClientGetSigners(t *testing.T) {
testCases := []struct {
name string
address sdk.AccAddress
expPass bool
}{
{"success: valid address", sdk.AccAddress(ibctesting.TestAccAddress), true},
{"failure: nil address", nil, false},
}

for _, tc := range testCases {
// Leave subject client ID and substitute client ID as empty strings
msg := types.MsgRecoverClient{
Signer: tc.address.String(),
}
if tc.expPass {
require.Equal(t, []sdk.AccAddress{tc.address}, msg.GetSigners())
} else {
require.Panics(t, func() {
msg.GetSigners()
})
}
}
}
Loading

0 comments on commit 51417ee

Please sign in to comment.