topology-foundation / topchain-node

MIT License
2 stars 7 forks source link

feat(subscription): add secondary mapping for provider #13

Closed sfroment closed 1 month ago

sfroment commented 1 month ago

Hey 👋 ,

This PR aim to fix #8. I wasn't sure wherever or not a subscription could be set twice this is why I go with a map to be sure all subscription are unique. If this isn't the case we could go with a slice and it will make the subscription retrieval even easier. Also would you like the SetSubscription to be divided like so:

func (k Keeper) SetSubscription(ctx sdk.Context, subscription types.Subscription) {
    k.setSubscription(ctx, subscription)
    k.setProviderSubscription(ctx, subscription)
}

func (k Keeper) setSubscription(ctx sdk.Context, subscription types.Subscription) {
    storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
    store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.SubscriptionKeyPrefix))

    appendedValue := k.cdc.MustMarshal(&subscription)
    store.Set([]byte(subscription.Id), appendedValue)
}

func (k Keeper) setProviderSubscription(ctx sdk.Context, subscription types.Subscription) {
    storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
    store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.ProviderSubscriptionKeyPrefix))

    providerSubscriptionsBytes := store.Get([]byte(subscription.Provider))
    var providerSubscriptions types.SubscriptionMap
    if providerSubscriptionsBytes != nil {
        k.cdc.MustUnmarshal(providerSubscriptionsBytes, &providerSubscriptions)
    } else {
        providerSubscriptions = types.SubscriptionMap{Subscriptions: map[string]types.Subscription{}}
    }
    providerSubscriptions.Subscriptions[subscription.Id] = subscription
    mapValue := k.cdc.MustMarshal(&providerSubscriptions)
    store.Set([]byte(subscription.Provider), mapValue)
}

Or like so:

func (k Keeper) SetSubscription(ctx sdk.Context, subscription types.Subscription) {
    storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
    k.setSubscription(storeAdapter, subscription)
    k.setProviderSubscription(storeAdapter, subscription)
}

func (k Keeper) setSubscription(storeAdapter storetypes.KVStore, subscription types.Subscription) {
    store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.SubscriptionKeyPrefix))

    appendedValue := k.cdc.MustMarshal(&subscription)
    store.Set([]byte(subscription.Id), appendedValue)
}

func (k Keeper) setProviderSubscription(storeAdapter storetypes.KVStore, subscription types.Subscription) {
    store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.ProviderSubscriptionKeyPrefix))

    providerSubscriptionsBytes := store.Get([]byte(subscription.Provider))
    var providerSubscriptions types.SubscriptionMap
    if providerSubscriptionsBytes != nil {
        k.cdc.MustUnmarshal(providerSubscriptionsBytes, &providerSubscriptions)
    } else {
        providerSubscriptions = types.SubscriptionMap{Subscriptions: map[string]types.Subscription{}}
    }
    providerSubscriptions.Subscriptions[subscription.Id] = subscription
    mapValue := k.cdc.MustMarshal(&providerSubscriptions)
    store.Set([]byte(subscription.Provider), mapValue)
}

Let me know thanks.

sfroment commented 1 month ago

@JanLewDev adapted, shall I had a function to get the store name so we don't do a mistake when wanting to use this store name ?

JanLewDev commented 1 month ago

@JanLewDev adapted, shall I had a function to get the store name so we don't do a mistake when wanting to use this store name ?

sure, will make it cleaner

sfroment commented 1 month ago

helper function added hopefully where you were wanting it, otherwise in the keeper.go, nonetheless this function is more a util one ?

JanLewDev commented 1 month ago

After this lgtm