-
Notifications
You must be signed in to change notification settings - Fork 75
switch to collections in the ibchooks module. #2768
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
Open
nagarajdivine
wants to merge
1
commit into
main
Choose a base branch
from
feat/collections-ibchooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 @@ | ||
| * Switch to collections in the ibchooks module [#2492](https://github.com/provenance-io/provenance/issues/2492). |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "cosmossdk.io/collections" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/provenance-io/provenance/x/ibchooks/types" | ||
| ) | ||
|
|
||
| // Migrator handles in-place store migrations for the x/ibchooks module. | ||
| type Migrator struct { | ||
| keeper Keeper | ||
| } | ||
|
|
||
| // NewMigrator returns a Migrator for the x/ibchooks module. | ||
| func NewMigrator(k Keeper) Migrator { | ||
| return Migrator{keeper: k} | ||
| } | ||
|
|
||
| // Migrate1to2 re-keys legacy raw-string packet entries into the new collections: | ||
| // - "channel::seq" -> PacketCallbacks[(channel, seq)] (prefix 0x02) | ||
| // - "channel::seq::ack" -> PacketAckActors[(channel, seq)] (prefix 0x03) | ||
| // | ||
| // Params (0x01) is already byte-identical under the new collections.Item and needs no migration. | ||
| // Legacy entries are ephemeral, so there are usually few (only in-flight packets) at upgrade time. | ||
| func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
| store := m.keeper.storeService.OpenKVStore(ctx) | ||
| it, err := store.Iterator(nil, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| type entry struct{ key, val []byte } | ||
| var legacy []entry | ||
| for ; it.Valid(); it.Next() { | ||
| key := it.Key() | ||
| if len(key) == 1 && key[0] == types.ParamsKeyBz[0] { | ||
| continue | ||
| } | ||
| if len(key) > 0 && (key[0] == types.PacketCallbackKeyBz[0] || key[0] == types.PacketAckKeyBz[0]) { | ||
| continue | ||
| } | ||
| legacy = append(legacy, | ||
| entry{key: append([]byte(nil), key...), val: append([]byte(nil), it.Value()...)}) | ||
| } | ||
| if cerr := it.Close(); cerr != nil { | ||
| return cerr | ||
| } | ||
|
|
||
| for _, e := range legacy { | ||
| parts := strings.Split(string(e.key), "::") | ||
| switch { | ||
| case len(parts) == 2: | ||
| seq, perr := strconv.ParseUint(parts[1], 10, 64) | ||
| if perr != nil { | ||
| continue | ||
| } | ||
| if err := m.keeper.packetCallbacks.Set(ctx, collections.Join(parts[0], seq), string(e.val)); err != nil { | ||
| return err | ||
| } | ||
| case len(parts) == 3 && parts[2] == "ack": | ||
| seq, perr := strconv.ParseUint(parts[1], 10, 64) | ||
| if perr != nil { | ||
| continue | ||
| } | ||
| if err := m.keeper.packetAckActors.Set(ctx, collections.Join(parts[0], seq), e.val); err != nil { | ||
| return err | ||
| } | ||
| default: | ||
| continue | ||
| } | ||
| if err := store.Delete(e.key); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| ctx.Logger().Info("ibchooks 1->2 migration complete: re-keyed legacy packet entries", "count", len(legacy)) | ||
| return nil | ||
| } |
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,25 +1,30 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "cosmossdk.io/collections" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
|
||
| "github.com/provenance-io/provenance/x/ibchooks/types" | ||
| ) | ||
|
|
||
| // GetParams returns the total set of the module's parameters. | ||
| func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| bz := store.Get(types.IbcHooksParamStoreKey) | ||
| if bz == nil { | ||
| return types.DefaultParams() | ||
| params, err := k.params.Get(ctx) | ||
| if err != nil { | ||
| if errors.Is(err, collections.ErrNotFound) { | ||
| return types.DefaultParams() | ||
| } | ||
| panic(err) | ||
| } | ||
| k.cdc.MustUnmarshal(bz, ¶ms) | ||
| return params | ||
| } | ||
|
|
||
| // SetParams sets the module's parameters with the provided parameters. | ||
| func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { | ||
| store := ctx.KVStore(k.storeKey) | ||
| bz := k.cdc.MustMarshal(¶ms) | ||
| store.Set(types.IbcHooksParamStoreKey, bz) | ||
| if err := k.params.Set(ctx, params); err != nil { | ||
| panic(err) | ||
| } | ||
| } |
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.
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.
🩺 Stability & Availability | 🟠 Major
🧩 Analysis chain
🌐 Web query:
In cosmossdk.io/collections v1.4.0, does Map.Get return collections.ErrNotFound for a missing key, and what other errors can it return (e.g. value decoding errors)?💡 Result:
In cosmossdk.io/collections v1.4.0, the Map.Get method does return collections.ErrNotFound when a requested key is missing from the underlying storage [1][2]. Beyond ErrNotFound, Map.Get can return other errors, most notably those related to encoding and decoding [1]. Because the collections framework relies on value codecs to deserialize data retrieved from the store, any failure during this deserialization process will result in an error, typically categorized under collections.ErrEncoding (which is aliased to the SDK's codec encoding error) [1]. Additionally, because Map.Get interacts with the underlying KVStore, it can propagate errors returned by the storage layer itself (e.g., database access issues). Developers frequently use errors.Is(err, collections.ErrNotFound) to check for missing keys specifically, allowing them to handle such cases—such as by returning a default value—differently than actual system or data integrity errors [2].
Citations:
🏁 Script executed:
Repository: provenance-io/provenance
Length of output: 970
🏁 Script executed:
rg --type go "func.*GetParams.*Returns" -A 20Repository: provenance-io/provenance
Length of output: 162
🏁 Script executed:
rg --type go "func.*GetParams" -A 15Repository: provenance-io/provenance
Length of output: 162
🏁 Script executed:
rg --type go "GetParams" -n -B 2 -A 15Repository: provenance-io/provenance
Length of output: 162
🏁 Script executed:
Repository: provenance-io/provenance
Length of output: 311
🏁 Script executed:
Repository: provenance-io/provenance
Length of output: 1363
🏁 Script executed:
Repository: provenance-io/provenance
Length of output: 162
🏁 Script executed:
Repository: provenance-io/provenance
Length of output: 162
Avoid swallowing non-
ErrNotFounderrors from collectionsGet.GetPacketCallbackandGetPacketAckActorreturn empty results on any error fromGet. Incosmossdk.io/collections,Map.Getreturnscollections.ErrNotFoundonly for missing keys, while other errors (e.g., decoding failures, storage corruption) propagate distinct values. Handling all errors identically masks data integrity issues, potentially dropping in-flight callbacks or actors instead of surfacing the failure.Match the standard pattern: return empty only when the key is missing, and propagate or panic on actual errors.
🛠 Proposed alignment for GetPacketCallback
func (k Keeper) GetPacketCallback(ctx sdk.Context, channel string, packetSequence uint64) string { v, err := k.packetCallbacks.Get(ctx, collections.Join(channel, packetSequence)) if err != nil { - return "" + if errors.Is(err, collections.ErrNotFound) { + return "" + } + panic(err) } return v }🛠 Proposed alignment for GetPacketAckActor
func (k Keeper) GetPacketAckActor(ctx sdk.Context, channel string, packetSequence uint64) (string, string) { rawData, err := k.packetAckActors.Get(ctx, collections.Join(channel, packetSequence)) if err != nil { - return "", "" + if errors.Is(err, collections.ErrNotFound) { + return "", "" + } + panic(err) } // ... rest of function📝 Committable suggestion
🤖 Prompt for AI Agents