From 6a6e9235cf9ed455140d1b0d88ff9aa017f31bf1 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Tue, 22 Mar 2022 09:50:26 +0000 Subject: [PATCH] fix: fixed the client id migration to deleting leftover keys --- x/profiles/legacy/v4/keeper.go | 15 +++++++++++++++ x/profiles/legacy/v4/store.go | 16 ++++++++++++++-- x/profiles/legacy/v4/store_test.go | 1 + 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/x/profiles/legacy/v4/keeper.go b/x/profiles/legacy/v4/keeper.go index 061fdba6f0..3e9ec69e58 100644 --- a/x/profiles/legacy/v4/keeper.go +++ b/x/profiles/legacy/v4/keeper.go @@ -179,3 +179,18 @@ func (k Keeper) IterateApplicationLinks(ctx sdk.Context, fn func(index int64, ap index++ } } + +func (k Keeper) IterateApplicationLinkClientIDKeys(ctx sdk.Context, fn func(index int64, key []byte, value []byte) (stop bool)) { + store := ctx.KVStore(k.storeKey) + + clientIDsStore := prefix.NewStore(store, ApplicationLinkClientIDPrefix) + iterator := clientIDsStore.Iterator(nil, nil) + defer iterator.Close() + + var stop = false + var index = int64(0) + for ; iterator.Valid() && !stop; iterator.Next() { + stop = fn(index, append(ApplicationLinkClientIDPrefix, iterator.Key()...), iterator.Value()) + index++ + } +} diff --git a/x/profiles/legacy/v4/store.go b/x/profiles/legacy/v4/store.go index b920465a61..424a67a9a2 100644 --- a/x/profiles/legacy/v4/store.go +++ b/x/profiles/legacy/v4/store.go @@ -118,17 +118,29 @@ func migrateDTagTransferRequests(ctx sdk.Context, k Keeper, storeKey sdk.StoreKe } func migrateApplicationLinks(ctx sdk.Context, k Keeper, storeKey sdk.StoreKey, cdc codec.BinaryCodec) { + store := ctx.KVStore(storeKey) + var applicationLinks []types.ApplicationLink k.IterateApplicationLinks(ctx, func(index int64, applicationLink types.ApplicationLink) (stop bool) { applicationLinks = append(applicationLinks, applicationLink) return false }) - store := ctx.KVStore(storeKey) + var clientIDKeys [][]byte + k.IterateApplicationLinkClientIDKeys(ctx, func(index int64, key []byte, value []byte) (stop bool) { + clientIDKeys = append(clientIDKeys, key) + return false + }) + + // Delete all the client ID keys to make sure we remove leftover ones + // The new client ID keys will be set when storing the application links anyway later + for _, key := range clientIDKeys { + store.Delete(key) + } + for i, link := range applicationLinks { // Delete the old keys store.Delete(UserApplicationLinkKey(link.User, link.Data.Application, link.Data.Username)) - store.Delete(ApplicationLinkClientIDKey(link.OracleRequest.ClientID)) // Store the link with the new key linkKey := types.UserApplicationLinkKey(link.User, link.Data.Application, link.Data.Username) diff --git a/x/profiles/legacy/v4/store_test.go b/x/profiles/legacy/v4/store_test.go index be1b22b5ba..37469ba57b 100644 --- a/x/profiles/legacy/v4/store_test.go +++ b/x/profiles/legacy/v4/store_test.go @@ -239,6 +239,7 @@ func TestMigrateStore(t *testing.T) { }, check: func(ctx sdk.Context) { kvStore := ctx.KVStore(keys[types.StoreKey]) + require.False(t, kvStore.Has(v4.ApplicationLinkClientIDKey("client_id"))) require.False(t, kvStore.Has(profilestypes.ApplicationLinkClientIDKey("client_id"))) }, },