Skip to content

Commit

Permalink
fix(x/warden): non-determinism in v3 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pitasi committed Aug 9, 2024
1 parent 67ca982 commit 5a8cd6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* (x/act) When an Action is approved, the new approval was not recorded in the database
* (x/warden) Fixed a bug where UpdateKey could be used to set RuleID to a non-existent Rule
* (x/warden) Fixed a bug where Keychains fees could be set to negative or invalid amounts
* (x/warden) Fixed a bug in v3 migrations where some insertions in the database were performed in a non-deterministic order. From our tests this didn't lead to any problem, in this particular case, but we want to play it safe.

### Misc

Expand Down
26 changes: 23 additions & 3 deletions warden/x/warden/migrations/v3/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package v3

import (
"bytes"
"sort"

"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"cosmossdk.io/math"
Expand Down Expand Up @@ -178,10 +181,27 @@ func migrateSpacesByOwner(ctx sdk.Context, ns NewStore, ownersMap map[string][]u
return err
}

spacesByOwner := make([]struct {
Owner sdk.AccAddress
Spaces []uint64
}, 0, len(ownersMap))
for owner, spaceIds := range ownersMap {
for _, spaceId := range spaceIds {
ownerAddr := sdk.MustAccAddressFromBech32(owner)
if err := ns.SpacesByOwner.Set(ctx, collections.Join(ownerAddr, spaceId)); err != nil {
ownerAddr := sdk.MustAccAddressFromBech32(owner)
spacesByOwner = append(spacesByOwner, struct {
Owner sdk.AccAddress
Spaces []uint64
}{
Owner: ownerAddr,
Spaces: spaceIds,
})
}
sort.Slice(spacesByOwner, func(i, j int) bool {
return bytes.Compare(spacesByOwner[i].Owner, spacesByOwner[j].Owner) < 0
})

for _, os := range spacesByOwner {
for _, spaceId := range os.Spaces {
if err := ns.SpacesByOwner.Set(ctx, collections.Join(os.Owner, spaceId)); err != nil {
return err
}
}
Expand Down

0 comments on commit 5a8cd6e

Please sign in to comment.