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

chore: use separate store paths for channels/creators #7696

Merged
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
7 changes: 7 additions & 0 deletions modules/core/04-channel/v2/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ package keeper
import (
"context"

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

// ChannelStore is a wrapper around channelStore to allow its usage during testing.
func (k Keeper) ChannelStore(ctx context.Context) storetypes.KVStore {
return k.channelStore(ctx)
}

func (k *Keeper) SendPacketTest(
ctx context.Context,
sourceChannel string,
Expand Down
28 changes: 17 additions & 11 deletions modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmentv2types "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
hostv2 "github.com/cosmos/ibc-go/v9/modules/core/24-host/v2"
"github.com/cosmos/ibc-go/v9/modules/core/api"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
Expand Down Expand Up @@ -55,21 +54,28 @@ func (Keeper) Logger(ctx context.Context) log.Logger {
return sdkCtx.Logger().With("module", "x/"+exported.ModuleName+"/"+types.SubModuleName)
}

func (k Keeper) ChannelStore(ctx context.Context, channelID string) storetypes.KVStore {
channelPrefix := []byte(fmt.Sprintf("%s/%s/", host.KeyChannelPrefix, channelID))
// channelStore returns the KV store under which channels are stored.
func (k Keeper) channelStore(ctx context.Context) storetypes.KVStore {
channelPrefix := []byte(fmt.Sprintf("%s/", types.ChannelPrefix))
return prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), channelPrefix)
}

// creatorStore returns the KV store under which creators are stored.
func (k Keeper) creatorStore(ctx context.Context) storetypes.KVStore {
creatorPrefix := []byte(fmt.Sprintf("%s/", types.CreatorPrefix))
return prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), creatorPrefix)
}

// SetChannel sets the Channel for a given channel identifier.
func (k *Keeper) SetChannel(ctx context.Context, channelID string, channel types.Channel) {
bz := k.cdc.MustMarshal(&channel)
k.ChannelStore(ctx, channelID).Set([]byte(types.ChannelKey), bz)
k.channelStore(ctx).Set([]byte(channelID), bz)
}

// GetChannel gets the Channel for a given channel identifier.
func (k *Keeper) GetChannel(ctx context.Context, channelID string) (types.Channel, bool) {
store := k.ChannelStore(ctx, channelID)
bz := store.Get([]byte(types.ChannelKey))
store := k.channelStore(ctx)
bz := store.Get([]byte(channelID))
if len(bz) == 0 {
return types.Channel{}, false
}
Expand All @@ -81,13 +87,13 @@ func (k *Keeper) GetChannel(ctx context.Context, channelID string) (types.Channe

// HasChannel returns true if a Channel exists for a given channel identifier, otherwise false.
func (k *Keeper) HasChannel(ctx context.Context, channelID string) bool {
store := k.ChannelStore(ctx, channelID)
return store.Has([]byte(types.ChannelKey))
store := k.channelStore(ctx)
return store.Has([]byte(channelID))
}

// GetCreator returns the creator of the channel.
func (k *Keeper) GetCreator(ctx context.Context, channelID string) (string, bool) {
bz := k.ChannelStore(ctx, channelID).Get([]byte(types.CreatorKey))
bz := k.creatorStore(ctx).Get([]byte(channelID))
if len(bz) == 0 {
return "", false
}
Expand All @@ -97,12 +103,12 @@ func (k *Keeper) GetCreator(ctx context.Context, channelID string) (string, bool

// SetCreator sets the creator of the channel.
func (k *Keeper) SetCreator(ctx context.Context, channelID, creator string) {
k.ChannelStore(ctx, channelID).Set([]byte(types.CreatorKey), []byte(creator))
k.creatorStore(ctx).Set([]byte(channelID), []byte(creator))
}

// DeleteCreator deletes the creator associated with the channel.
func (k *Keeper) DeleteCreator(ctx context.Context, channelID string) {
k.ChannelStore(ctx, channelID).Delete([]byte(types.CreatorKey))
k.creatorStore(ctx).Delete([]byte(channelID))
}

// GetPacketReceipt returns the packet receipt from the packet receipt path based on the channelID and sequence.
Expand Down
2 changes: 1 addition & 1 deletion modules/core/04-channel/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (suite *KeeperTestSuite) TestRegisterCounterparty() {
"failure: channel must already exist",
func() {
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.DeleteCreator(suite.chainA.GetContext(), path.EndpointA.ChannelID)
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.ChannelStore(suite.chainA.GetContext(), path.EndpointA.ChannelID).Delete([]byte(types.ChannelKey))
suite.chainA.App.GetIBCKeeper().ChannelKeeperV2.ChannelStore(suite.chainA.GetContext()).Delete([]byte(path.EndpointA.ChannelID))
},
types.ErrChannelNotFound,
},
Expand Down
16 changes: 8 additions & 8 deletions modules/core/04-channel/v2/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const (
// SubModuleName defines the channelv2 module name.
SubModuleName = "channelv2"

// ChannelKey is the key used to store channels in the channel store.
// the channel key is imported from types instead of host because
// the channel key is not a part of the ics-24 host specification
ChannelKey = "channel"
// ChannelPrefix is the prefix under which all v2 channels are stored.
// It is imported from types since it is not part of the ics-24 host
// specification.
ChannelPrefix = "channels"

// CreatorKey is the key used to store the channel creator in the channel store
// the creator key is imported from types instead of host because
// the creator key is not a part of the ics-24 host specification
CreatorKey = "creator"
// CreatorPrefix is the prefix under which all v2 channel creators are stored.
// It is imported from types since it is not part of the ics-24 host
// specification.
CreatorPrefix = "creators"
)
Loading