Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
58 changes: 58 additions & 0 deletions modules/core/04-channel/v2/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

// InitGenesis sets the genesis state in the store.
func (k *Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
// set acks
for _, ack := range data.Acknowledgements {
k.SetPacketAcknowledgement(ctx, ack.ClientId, ack.Sequence, ack.Data)
}

// set commits
for _, commitment := range data.Commitments {
k.SetPacketCommitment(ctx, commitment.ClientId, commitment.Sequence, commitment.Data)
}

// set receipts
for _, receipt := range data.Receipts {
k.SetPacketReceipt(ctx, receipt.ClientId, receipt.Sequence)
}

// set send sequences
for _, seq := range data.SendSequences {
k.SetNextSequenceSend(ctx, seq.ClientId, seq.Sequence)
}
}

// ExportGenesis exports the current state to a genesis state.
func (k *Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
clientStates := k.ClientKeeper.GetAllGenesisClients(ctx)
gs := types.GenesisState{
Acknowledgements: make([]types.PacketState, 0),
Commitments: make([]types.PacketState, 0),
Receipts: make([]types.PacketState, 0),
SendSequences: make([]types.PacketSequence, 0),
}
for _, clientState := range clientStates {
acks := k.GetAllPacketAcknowledgementsForClient(ctx, clientState.ClientId)
gs.Acknowledgements = append(gs.Acknowledgements, acks...)

comms := k.GetAllPacketCommitmentsForClient(ctx, clientState.ClientId)
gs.Commitments = append(gs.Commitments, comms...)

receipts := k.GetAllPacketReceiptsForClient(ctx, clientState.ClientId)
gs.Receipts = append(gs.Receipts, receipts...)

seq, ok := k.GetNextSequenceSend(ctx, clientState.ClientId)
if ok {
gs.SendSequences = append(gs.SendSequences, types.NewPacketSequence(clientState.ClientId, seq))
}
}

return gs
}
55 changes: 55 additions & 0 deletions modules/core/04-channel/v2/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package keeper_test

import (
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
)

// TestInitExportGenesis tests the import and export flow for the channel v2 keeper.
func (suite *KeeperTestSuite) TestInitExportGenesis() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This testing setup is what I feel the least confident about. There was not a previous testing setup in the v1 module for genesis, so this is what I rolled with.

The intricacy is that the ibctesting suite already has initialized genesis for things like the client keeper and some actions have led to state existing. So for creating a valid genesis that will be exported properly, we have to use existing state to set it

path := ibctesting.NewPath(suite.chainA, suite.chainB)
path.SetupV2()

app := suite.chainA.App

emptyGenesis := types.DefaultGenesisState()

// create a valid genesis state that uses the client keepers existing client IDs
clientStates := app.GetIBCKeeper().ClientKeeper.GetAllGenesisClients(suite.chainA.GetContext())
validGs := types.DefaultGenesisState()
for i, clientState := range clientStates {
ack := types.NewPacketState(clientState.ClientId, uint64(i+1), []byte("ack"))
receipt := types.NewPacketState(clientState.ClientId, uint64(i+1), []byte{byte(0x2)})
commitment := types.NewPacketState(clientState.ClientId, uint64(i+1), []byte("commit_hash"))
seq := types.NewPacketSequence(clientState.ClientId, uint64(i+1))

validGs.Acknowledgements = append(validGs.Acknowledgements, ack)
validGs.Receipts = append(validGs.Receipts, receipt)
validGs.Commitments = append(validGs.Commitments, commitment)
validGs.SendSequences = append(validGs.SendSequences, seq)
emptyGenesis.SendSequences = append(emptyGenesis.SendSequences, seq)
}

tests := []struct {
name string
genState types.GenesisState
}{
{
name: "no modifications genesis",
genState: emptyGenesis,
},
{
name: "valid",
genState: validGs,
},
}

for _, tt := range tests {
suite.Run(tt.name, func() {
app.GetIBCKeeper().ChannelKeeperV2.InitGenesis(suite.chainA.GetContext(), tt.genState)

exported := app.GetIBCKeeper().ChannelKeeperV2.ExportGenesis(suite.chainA.GetContext())
suite.Require().Equal(tt.genState, exported)
})
}
}
59 changes: 58 additions & 1 deletion modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package keeper

import (
"bytes"
"context"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"

connectionkeeper "github.com/cosmos/ibc-go/v9/modules/core/03-connection/keeper"
Expand Down Expand Up @@ -50,7 +53,7 @@ func NewKeeper(
}

// Logger returns a module-specific logger.
func (Keeper) Logger(ctx context.Context) log.Logger {
func (*Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx) // TODO: https://github.com/cosmos/ibc-go/issues/5917
return sdkCtx.Logger().With("module", "x/"+exported.ModuleName+"/"+types.SubModuleName)
}
Expand Down Expand Up @@ -194,3 +197,57 @@ func (k *Keeper) DeleteAsyncPacket(ctx context.Context, clientID string, sequenc
panic(err)
}
}

// GetAllPacketCommitmentsForClient returns all stored PacketCommitments objects for a specified
// client ID.
func (k *Keeper) GetAllPacketCommitmentsForClient(ctx context.Context, clientID string) []types.PacketState {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
storePrefix := hostv2.PacketCommitmentPrefixKey(clientID)
iterator := storetypes.KVStorePrefixIterator(store, storePrefix)

var commitments []types.PacketState
for ; iterator.Valid(); iterator.Next() {
sequenceBz := bytes.TrimPrefix(iterator.Key(), storePrefix)
sequence := sdk.BigEndianToUint64(sequenceBz)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if this byte slice is more than 64 bytes? Should never happen but perhaps we should add a defensive check

state := types.NewPacketState(clientID, sequence, iterator.Value())

commitments = append(commitments, state)
}
return commitments
}

// GetAllPacketAcknowledgementsForClient returns all stored PacketAcknowledgements objects for a specified
// client ID.
func (k *Keeper) GetAllPacketAcknowledgementsForClient(ctx context.Context, clientID string) []types.PacketState {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
storePrefix := hostv2.PacketAcknowledgementPrefixKey(clientID)
iterator := storetypes.KVStorePrefixIterator(store, storePrefix)

var acknowledgements []types.PacketState
for ; iterator.Valid(); iterator.Next() {
sequenceBz := bytes.TrimPrefix(iterator.Key(), storePrefix)
sequence := sdk.BigEndianToUint64(sequenceBz)
state := types.NewPacketState(clientID, sequence, iterator.Value())

acknowledgements = append(acknowledgements, state)
}
return acknowledgements
}

// GetAllPacketReceiptsForClient returns all stored PacketReceipts objects for a specified
// client ID.
func (k *Keeper) GetAllPacketReceiptsForClient(ctx context.Context, clientID string) []types.PacketState {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
storePrefix := hostv2.PacketReceiptPrefixKey(clientID)
iterator := storetypes.KVStorePrefixIterator(store, storePrefix)

var receipts []types.PacketState
for ; iterator.Valid(); iterator.Next() {
sequenceBz := bytes.TrimPrefix(iterator.Key(), storePrefix)
sequence := sdk.BigEndianToUint64(sequenceBz)
state := types.NewPacketState(clientID, sequence, iterator.Value())

receipts = append(receipts, state)
}
return receipts
}
16 changes: 15 additions & 1 deletion modules/core/04-channel/v2/module.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package client

import (
"context"

"github.com/spf13/cobra"

sdk "github.com/cosmos/cosmos-sdk/types"

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

// Name returns the IBC channel/v2 name
func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
k.InitGenesis(sdkCtx, gs)
}

func ExportGenesis(ctx context.Context, k *keeper.Keeper) types.GenesisState {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return k.ExportGenesis(sdkCtx)
}

func Name() string {
return types.SubModuleName
}
Expand Down
2 changes: 2 additions & 0 deletions modules/core/04-channel/v2/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ type ClientKeeper interface {
GetClientConsensusState(ctx context.Context, clientID string, height exported.Height) (exported.ConsensusState, bool)
// GetClientCounterparty returns the counterpartyInfo given a clientID
GetClientCounterparty(ctx context.Context, clientID string) (clienttypes.CounterpartyInfo, bool)
// GetAllGenesisClients returns all the clients in state with their client ids returned as IdentifiedClientState
GetAllGenesisClients(ctx context.Context) clienttypes.IdentifiedClientStates
}
14 changes: 10 additions & 4 deletions modules/core/24-host/v2/packet_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
PacketCommitmentBasePrefix = byte(1)
PacketReceiptBasePrefix = byte(2)
PacketAcknowledgementBasePrefix = byte(3)
)

// PacketCommitmentPrefixKey returns the store key prefix under which packet commitments for a particular channel are stored.
// channelID must be a generated identifier, not provided externally so key collisions are not possible.
func PacketCommitmentPrefixKey(channelID string) []byte {
return append([]byte(channelID), byte(1))
return append([]byte(channelID), PacketCommitmentBasePrefix)
}

// PacketCommitmentKey returns the store key of under which a packet commitment is stored.
// channelID must be a generated identifier, not provided externally so key collisions are not possible.
func PacketCommitmentKey(channelID string, sequence uint64) []byte {
return append(PacketCommitmentPrefixKey((channelID)), sdk.Uint64ToBigEndian(sequence)...)
return append(PacketCommitmentPrefixKey(channelID), sdk.Uint64ToBigEndian(sequence)...)
}

// PacketReceiptPrefixKey returns the store key prefix under which packet receipts for a particular channel are stored.
// channelID must be a generated identifier, not provided externally so key collisions are not possible.
func PacketReceiptPrefixKey(channelID string) []byte {
return append([]byte(channelID), byte(2))
return append([]byte(channelID), PacketReceiptBasePrefix)
}

// PacketReceiptKey returns the store key of under which a packet receipt is stored.
Expand All @@ -33,7 +39,7 @@ func PacketReceiptKey(channelID string, sequence uint64) []byte {
// PacketAcknowledgementPrefixKey returns the store key prefix under which packet acknowledgements for a particular channel are stored.
// channelID must be a generated identifier, not provided externally so key collisions are not possible.
func PacketAcknowledgementPrefixKey(channelID string) []byte {
return append([]byte(channelID), byte(3))
return append([]byte(channelID), PacketAcknowledgementBasePrefix)
}

// PacketAcknowledgementKey returns the store key of under which a packet acknowledgement is stored.
Expand Down
3 changes: 3 additions & 0 deletions modules/core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
client "github.com/cosmos/ibc-go/v9/modules/core/02-client"
connection "github.com/cosmos/ibc-go/v9/modules/core/03-connection"
channel "github.com/cosmos/ibc-go/v9/modules/core/04-channel"
channelv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2"
"github.com/cosmos/ibc-go/v9/modules/core/keeper"
"github.com/cosmos/ibc-go/v9/modules/core/types"
)
Expand All @@ -18,6 +19,7 @@ func InitGenesis(ctx context.Context, k keeper.Keeper, gs *types.GenesisState) e
}
connection.InitGenesis(ctx, k.ConnectionKeeper, gs.ConnectionGenesis)
channel.InitGenesis(ctx, k.ChannelKeeper, gs.ChannelGenesis)
channelv2.InitGenesis(ctx, k.ChannelKeeperV2, gs.ChannelV2Genesis)
return nil
}

Expand All @@ -31,5 +33,6 @@ func ExportGenesis(ctx context.Context, k keeper.Keeper) (*types.GenesisState, e
ClientGenesis: gs,
ConnectionGenesis: connection.ExportGenesis(ctx, k.ConnectionKeeper),
ChannelGenesis: channel.ExportGenesis(ctx, k.ChannelKeeper),
ChannelV2Genesis: channelv2.ExportGenesis(ctx, k.ChannelKeeperV2),
}, nil
}
46 changes: 46 additions & 0 deletions modules/core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v9/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
channelv2types "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
commitmenttypes "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
"github.com/cosmos/ibc-go/v9/modules/core/types"
Expand Down Expand Up @@ -145,6 +146,20 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
0,
channeltypes.Params{UpgradeTimeout: channeltypes.DefaultTimeout},
),
ChannelV2Genesis: channelv2types.NewGenesisState(
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel2, 1, []byte("ack")),
},
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel2, 1, []byte("")),
},
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel1, 1, []byte("commit_hash")),
},
[]channelv2types.PacketSequence{
channelv2types.NewPacketSequence(channel1, 1),
},
),
},
expError: nil,
},
Expand Down Expand Up @@ -172,6 +187,7 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
2,
),
ConnectionGenesis: connectiontypes.DefaultGenesisState(),
ChannelV2Genesis: channelv2types.DefaultGenesisState(),
},
expError: errors.New("genesis metadata key cannot be empty"),
},
Expand All @@ -189,6 +205,7 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
0,
connectiontypes.Params{},
),
ChannelV2Genesis: channelv2types.DefaultGenesisState(),
},
expError: errors.New("invalid connection"),
},
Expand All @@ -202,6 +219,21 @@ func (suite *IBCTestSuite) TestValidateGenesis() {
channeltypes.NewPacketState("(portID)", channel1, 1, []byte("ack")),
},
},
ChannelV2Genesis: channelv2types.DefaultGenesisState(),
},
expError: errors.New("invalid acknowledgement"),
},
{
name: "invalid channel v2 genesis",
genState: &types.GenesisState{
ClientGenesis: clienttypes.DefaultGenesisState(),
ConnectionGenesis: connectiontypes.DefaultGenesisState(),
ChannelGenesis: channeltypes.DefaultGenesisState(),
ChannelV2Genesis: channelv2types.GenesisState{
Acknowledgements: []channelv2types.PacketState{
channelv2types.NewPacketState(channel1, 1, nil),
},
},
},
expError: errors.New("invalid acknowledgement"),
},
Expand Down Expand Up @@ -305,6 +337,20 @@ func (suite *IBCTestSuite) TestInitGenesis() {
0,
channeltypes.Params{UpgradeTimeout: channeltypes.DefaultTimeout},
),
ChannelV2Genesis: channelv2types.NewGenesisState(
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel2, 1, []byte("ack")),
},
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel2, 1, []byte("")),
},
[]channelv2types.PacketState{
channelv2types.NewPacketState(channel1, 1, []byte("commit_hash")),
},
[]channelv2types.PacketSequence{
channelv2types.NewPacketSequence(channel1, 1),
},
),
},
},
}
Expand Down
Loading
Loading