Skip to content

Commit

Permalink
fix: fixed the client id migration to deleting leftover keys
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Mar 22, 2022
1 parent a10355b commit 6a6e923
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions x/profiles/legacy/v4/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}
16 changes: 14 additions & 2 deletions x/profiles/legacy/v4/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions x/profiles/legacy/v4/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
},
},
Expand Down

0 comments on commit 6a6e923

Please sign in to comment.