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

feat: add UpdateChannelPermissions tx for crosschain module #247

Merged
merged 1 commit into from
Jul 18, 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
612 changes: 594 additions & 18 deletions api/cosmos/crosschain/v1/crosschain.pulsar.go

Large diffs are not rendered by default.

1,107 changes: 1,074 additions & 33 deletions api/cosmos/crosschain/v1/tx.pulsar.go

Large diffs are not rendered by default.

43 changes: 42 additions & 1 deletion api/cosmos/crosschain/v1/tx_grpc.pb.go

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

10 changes: 10 additions & 0 deletions proto/cosmos/crosschain/v1/crosschain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ message Params {
(gogoproto.nullable) = false
];
}

// ChannelPermission defines the fields of the channel permission
message ChannelPermission {
// destination chain id
uint32 dest_chain_id = 1;
// channel id
uint32 channel_id = 2;
// permission status, 1 for allow, 0 for forbidden
uint32 permission = 3;
}
19 changes: 19 additions & 0 deletions proto/cosmos/crosschain/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ service Msg {
//
// Since: cosmos-sdk 0.47
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);

// UpdateChannelPermissions defines a governance operation for updating the channel permissions.
// The authority is defined in the keeper.
rpc UpdateChannelPermissions(MsgUpdateChannelPermissions) returns (MsgUpdateChannelPermissionsResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand All @@ -37,3 +41,18 @@ message MsgUpdateParams {
// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}

// MsgUpdateChannelPermissions is the Msg/MsgUpdateChannelPermissions request type.
message MsgUpdateChannelPermissions {
option (cosmos.msg.v1.signer) = "authority";

// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// channel_permissions defines the channel permissions to update
repeated ChannelPermission channel_permissions = 2;
}

// MsgUpdateChannelPermissionsResponse defines the response structure for executing a
// MsgUpdateChannelPermissions message.
message MsgUpdateChannelPermissionsResponse {}
24 changes: 24 additions & 0 deletions x/crosschain/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) error {
return nil
}

// UpdatePermissions updates the permission of channels
func (k Keeper) UpdatePermissions(ctx sdk.Context, permissions []*types.ChannelPermission) error {
for _, permission := range permissions {
if !k.IsDestChainSupported(sdk.ChainID(permission.DestChainId)) {
return fmt.Errorf("dest chain %d is not supported", permission.DestChainId)
}
if !k.IsChannelSupported(sdk.ChannelID(permission.ChannelId)) {
return fmt.Errorf("channel %d is not supported", permission.ChannelId)
}
if sdk.ChannelPermission(permission.Permission) != sdk.ChannelAllow && sdk.ChannelPermission(permission.Permission) != sdk.ChannelForbidden {
return fmt.Errorf("permission %d is not supported", permission.Permission)
}

k.SetChannelSendPermission(ctx, sdk.ChainID(permission.DestChainId), sdk.ChannelID(permission.ChannelId), sdk.ChannelPermission(permission.Permission))
}
return nil
}

// CreateRawIBCPackageWithFee creates a cross chain package with given cross chain fee
func (k Keeper) CreateRawIBCPackageWithFee(ctx sdk.Context, destChainId sdk.ChainID, channelID sdk.ChannelID,
packageType sdk.CrossChainPackageType, packageLoad []byte, relayerFee, ackRelayerFee *big.Int,
Expand Down Expand Up @@ -160,6 +178,12 @@ func (k Keeper) IsDestChainSupported(chainID sdk.ChainID) bool {
return chainID == k.cfg.destBscChainId
}

// IsChannelSupported returns the support status of a channel
func (k Keeper) IsChannelSupported(channelId sdk.ChannelID) bool {
_, ok := k.cfg.channelIDToName[channelId]
return ok
}

// SetChannelSendPermission sets the channel send permission
func (k Keeper) SetChannelSendPermission(ctx sdk.Context, destChainID sdk.ChainID, channelID sdk.ChannelID, permission sdk.ChannelPermission) {
kvStore := ctx.KVStore(k.storeKey)
Expand Down
20 changes: 20 additions & 0 deletions x/crosschain/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ func (s *TestSuite) TestSetChannelSendPermission() {
permission := s.crossChainKeeper.GetChannelSendPermission(s.ctx, sdk.ChainID(1), sdk.ChannelID(1))
s.Require().EqualValues(sdk.ChannelAllow, permission)
}

func (s *TestSuite) TestUpdateChannelPermission() {
s.crossChainKeeper.RegisterChannel("test", 1, &testutil2.MockCrossChainApplication{})
s.crossChainKeeper.SetDestChainID(1)

s.crossChainKeeper.SetChannelSendPermission(s.ctx, sdk.ChainID(1), sdk.ChannelID(1), sdk.ChannelAllow)

permissions := []*types.ChannelPermission{
&types.ChannelPermission{
DestChainId: 1,
ChannelId: 1,
Permission: 0,
},
}
err := s.crossChainKeeper.UpdatePermissions(s.ctx, permissions)
s.Require().NoError(err)

permission := s.crossChainKeeper.GetChannelSendPermission(s.ctx, sdk.ChainID(1), sdk.ChannelID(1))
s.Require().EqualValues(sdk.ChannelForbidden, permission)
}
13 changes: 13 additions & 0 deletions x/crosschain/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam

return &types.MsgUpdateParamsResponse{}, nil
}

func (k msgServer) UpdateChannelPermissions(goCtx context.Context, req *types.MsgUpdateChannelPermissions) (*types.MsgUpdateChannelPermissionsResponse, error) {
if k.GetAuthority() != req.Authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.GetAuthority(), req.Authority)
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.UpdatePermissions(ctx, req.ChannelPermissions); err != nil {
return nil, err
}

return &types.MsgUpdateChannelPermissionsResponse{}, nil
}
5 changes: 4 additions & 1 deletion x/crosschain/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.NewQueryCmd()
}

func (am AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {}
func (am AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}

// AppModule implements an application module for the distribution module.
type AppModule struct {
Expand Down Expand Up @@ -117,6 +119,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {}
// RegisterServices registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

Expand Down
1 change: 1 addition & 0 deletions x/crosschain/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgUpdateParams{},
&MsgUpdateChannelPermissions{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
Loading