diff --git a/CHANGELOG.md b/CHANGELOG.md index a2102cbd5..dd51d2cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/warden/x/warden/migrations/v3/store.go b/warden/x/warden/migrations/v3/store.go index 534efeb43..eb5908bef 100644 --- a/warden/x/warden/migrations/v3/store.go +++ b/warden/x/warden/migrations/v3/store.go @@ -1,6 +1,9 @@ package v3 import ( + "bytes" + "sort" + "cosmossdk.io/collections" "cosmossdk.io/core/store" "cosmossdk.io/math" @@ -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 } }