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

Add proto message, implement sdk.Msg for Recover Client. #4494

Merged
merged 4 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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