Skip to content

Commit

Permalink
check channel is already taken or not at channel creation (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored May 31, 2024
1 parent 7fad17b commit 9c8b993
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/hook/bridge_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package hook

import (
"context"
"errors"

"cosmossdk.io/collections"
"cosmossdk.io/core/address"
sdk "github.com/cosmos/cosmos-sdk/types"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
permtypes "github.com/initia-labs/initia/x/ibc/perm/types"
)

var _ ophosttypes.BridgeHook = BridgeHook{}
Expand All @@ -26,6 +29,7 @@ type ChannelKeeper interface {
type PermKeeper interface {
HasPermission(ctx context.Context, portID, channelID string, relayer sdk.AccAddress) (bool, error)
SetPermissionedRelayer(ctx context.Context, portID, channelID string, relayer sdk.AccAddress) error
GetPermissionedRelayer(ctx context.Context, portID, channelID string) (sdk.AccAddress, error)
}

func NewBridgeHook(channelKeeper ChannelKeeper, permKeeper PermKeeper, ac address.Codec) BridgeHook {
Expand Down Expand Up @@ -56,6 +60,13 @@ func (h BridgeHook) BridgeCreated(
return channeltypes.ErrChannelExists.Wrap("cannot register permissioned relayer for the channel in use")
}

// check if the channel has a permissioned relayer
if _, err := h.IBCPermKeeper.GetPermissionedRelayer(ctx, portID, channelID); err == nil {
return permtypes.ErrAlreadyTaken.Wrap("failed to claim permissioned relayer")
} else if !errors.Is(err, collections.ErrNotFound) {
return err
}

// register challenger as channel relayer
if err = h.IBCPermKeeper.SetPermissionedRelayer(sdkCtx, portID, channelID, challenger); err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions app/hook/bridge_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"cosmossdk.io/collections"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
Expand All @@ -18,6 +19,7 @@ import (

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
"github.com/initia-labs/initia/app/hook"
permtypes "github.com/initia-labs/initia/x/ibc/perm/types"
)

var _ hook.ChannelKeeper = MockChannelKeeper{}
Expand Down Expand Up @@ -45,6 +47,14 @@ func (k MockPermKeeper) SetPermissionedRelayer(ctx context.Context, portID, chan
return nil
}

func (k MockPermKeeper) GetPermissionedRelayer(ctx context.Context, portID, channelID string) (sdk.AccAddress, error) {
if _, ok := k.perms[portID+"/"+channelID]; !ok {
return nil, collections.ErrNotFound
}

return k.perms[portID+"/"+channelID], nil
}

func setup() (context.Context, hook.BridgeHook) {
h := hook.NewBridgeHook(MockChannelKeeper{
sequenceIDs: map[string]uint64{
Expand Down Expand Up @@ -99,6 +109,13 @@ func Test_BridgeHook_BridgeCreated(t *testing.T) {
})
require.NoError(t, err)

// can't create bridge with already taken channel
err = h.BridgeCreated(ctx, 2, ophosttypes.BridgeConfig{
Challenger: addr.String(),
Metadata: metadata,
})
require.ErrorIs(t, err, permtypes.ErrAlreadyTaken)

// cannot take non-1 sequence channel
err = h.BridgeCreated(ctx, 1, ophosttypes.BridgeConfig{
Challenger: addr.String(),
Expand Down
10 changes: 10 additions & 0 deletions x/ibc/perm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ func (k Keeper) SetPermissionedRelayer(ctx context.Context, portID, channelID st
return k.PermissionedRelayers.Set(ctx, collections.Join(portID, channelID), relayer)
}

// GetPermissionedRelayer returns the permissioned relayer for the channel.
func (k Keeper) GetPermissionedRelayer(ctx context.Context, portID, channelID string) (sdk.AccAddress, error) {
relayer, err := k.PermissionedRelayers.Get(ctx, collections.Join(portID, channelID))
if err != nil {
return nil, err
}

return sdk.AccAddress(relayer), nil
}

// HasPermission checks if the relayer has permission to relay packets on the channel.
func (k Keeper) HasPermission(ctx context.Context, portID, channelID string, relayer sdk.AccAddress) (bool, error) {
permRelayer, err := k.PermissionedRelayers.Get(ctx, collections.Join(portID, channelID))
Expand Down
11 changes: 11 additions & 0 deletions x/ibc/perm/types/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

import (
errorsmod "cosmossdk.io/errors"
)

// IBC Perm Errors
var (
// ErrAlreadyTaken raised if the channel relayer is already taken
ErrAlreadyTaken = errorsmod.Register(ModuleName, 2, "already taken")
)

0 comments on commit 9c8b993

Please sign in to comment.