-
Notifications
You must be signed in to change notification settings - Fork 751
feat: channel-v2 genesis #7933
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
Merged
Merged
feat: channel-v2 genesis #7933
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b22155e
add
6459667
fix and wire to core
fb1c3db
sequence check and cleanup
71d127e
Merge branch 'main' into feat/channel-v2-genesis
3bb7753
have more than one client in state on tests
AdityaSripal aecd1a2
refactor
8e13af6
lint fix
4a3cd5c
Merge branch 'main' into feat/channel-v2-genesis
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
| path := ibctesting.NewPath(suite.chainA, suite.chainB) | ||
| path.SetupV2() | ||
aljo242 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
|
|
@@ -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) | ||
| } | ||
|
|
@@ -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) | ||
|
||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
ibctestingsuite already has initialized genesis for things like the client keeper and some actions have led to state existing. So for creating avalid genesisthat will be exported properly, we have to use existing state to set it