Skip to content

Commit

Permalink
feat: remove inactive subscriptions for account during upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 20, 2024
1 parent 7681f97 commit 5d94de3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"

subscriptionkeeper "github.com/sentinel-official/hub/v12/x/subscription/keeper"
subscriptiontypes "github.com/sentinel-official/hub/v12/x/subscription/types"
)

const (
Expand Down Expand Up @@ -132,6 +135,34 @@ func UpgradeHandler(
ibcClientParams.AllowedClients = append(ibcClientParams.AllowedClients, exported.Localhost)
keepers.IBCKeeper.ClientKeeper.SetParams(ctx, ibcClientParams)

if err := deleteInactiveSubscriptionsForAccounts(ctx, keepers.VPNKeeper.Subscription); err != nil {
return nil, err
}

return newVM, nil
}
}

func deleteInactiveSubscriptionsForAccounts(ctx sdk.Context, k subscriptionkeeper.Keeper) error {
var (
store = k.Store(ctx)
iter = sdk.KVStorePrefixIterator(store, subscriptiontypes.SubscriptionForAccountKeyPrefix)
)

defer iter.Close()

for ; iter.Valid(); iter.Next() {
var (
accAddr = subscriptiontypes.AccAddrFromSubscriptionForAccountKey(iter.Key())
id = subscriptiontypes.IDFromSubscriptionForAccountKey(iter.Key())
)

if _, found := k.GetSubscription(ctx, id); found {
continue
}

k.DeleteSubscriptionForAccount(ctx, accAddr, id)
}

return nil
}
11 changes: 11 additions & 0 deletions x/subscription/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func PayoutForAccountByNodeKey(accAddr sdk.AccAddress, nodeAddr hubtypes.NodeAdd
return append(GetPayoutForAccountByNodeKeyPrefix(accAddr, nodeAddr), sdk.Uint64ToBigEndian(id)...)
}

func AccAddrFromSubscriptionForAccountKey(key []byte) sdk.AccAddress {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)

addrLen := int(key[1])
if len(key) != 10+addrLen {
panic(fmt.Errorf("invalid key length %d; expected %d", len(key), 10+addrLen))
}

return key[2 : 2+addrLen]
}

func IDFromSubscriptionForAccountKey(key []byte) uint64 {
// prefix (1 byte) | addrLen (1 byte) | addr (addrLen bytes) | id (8 bytes)

Expand Down
19 changes: 19 additions & 0 deletions x/subscription/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ func TestIDFromPayoutForNodeKey(t *testing.T) {
}
}

func TestAccAddrFromSubscriptionForAccountKey(t *testing.T) {
var (
addr []byte
key []byte
)

for i := 1; i <= 256; i += 64 {
addr = make([]byte, i)
_, _ = rand.Read(addr)

key = SubscriptionForAccountKey(addr, uint64(i))
require.Equal(
t,
sdk.AccAddress(addr),
AccAddrFromSubscriptionForAccountKey(key),
)
}
}

func TestIDFromSubscriptionForAccountKey(t *testing.T) {
var (
addr []byte
Expand Down

0 comments on commit 5d94de3

Please sign in to comment.