Skip to content

Commit 2ae41e8

Browse files
committed
feat: run post migration script in plan to delete invalid keys
1 parent 8854bb3 commit 2ae41e8

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

x/plan/migrations/expected.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ import (
77
"github.com/sentinel-official/sentinelhub/v12/x/plan/types/v3"
88
)
99

10+
type LeaseKeeper interface {
11+
HasAnyLeaseForNodeByProvider(ctx sdk.Context, nodeAddr types.NodeAddress, provAddr types.ProvAddress) bool
12+
}
13+
1014
type NodeKeeper interface {
15+
DeleteNodeForPlan(ctx sdk.Context, id uint64, addr types.NodeAddress)
1116
Store(ctx sdk.Context) sdk.KVStore
1217
}
1318

1419
type PlanKeeper interface {
20+
DeletePlanForNodeByProvider(ctx sdk.Context, nodeAddr types.NodeAddress, provAddr types.ProvAddress, id uint64)
1521
GetPlan(ctx sdk.Context, id uint64) (v3.Plan, bool)
1622
SetPlan(ctx sdk.Context, plan v3.Plan)
1723
SetPlanForNodeByProvider(ctx sdk.Context, nodeAddr types.NodeAddress, provAddr types.ProvAddress, id uint64)

x/plan/migrations/migrator.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package migrations
22

33
import (
4+
"encoding/binary"
45
"fmt"
56

67
"github.com/cosmos/cosmos-sdk/codec"
@@ -9,22 +10,25 @@ import (
910

1011
base "github.com/sentinel-official/sentinelhub/v12/types"
1112
"github.com/sentinel-official/sentinelhub/v12/types/v1"
12-
"github.com/sentinel-official/sentinelhub/v12/x/node/types"
13+
nodetypes "github.com/sentinel-official/sentinelhub/v12/x/node/types"
14+
"github.com/sentinel-official/sentinelhub/v12/x/plan/types"
1315
"github.com/sentinel-official/sentinelhub/v12/x/plan/types/v2"
1416
"github.com/sentinel-official/sentinelhub/v12/x/plan/types/v3"
1517
)
1618

1719
type Migrator struct {
18-
cdc codec.BinaryCodec
19-
node NodeKeeper
20-
plan PlanKeeper
20+
cdc codec.BinaryCodec
21+
lease LeaseKeeper
22+
node NodeKeeper
23+
plan PlanKeeper
2124
}
2225

23-
func NewMigrator(cdc codec.BinaryCodec, node NodeKeeper, plan PlanKeeper) Migrator {
26+
func NewMigrator(cdc codec.BinaryCodec, lease LeaseKeeper, node NodeKeeper, plan PlanKeeper) Migrator {
2427
return Migrator{
25-
cdc: cdc,
26-
node: node,
27-
plan: plan,
28+
cdc: cdc,
29+
lease: lease,
30+
node: node,
31+
plan: plan,
2832
}
2933
}
3034

@@ -87,7 +91,7 @@ func (k *Migrator) migratePlans(ctx sdk.Context) {
8791
}
8892

8993
func (k *Migrator) setPlanForNodeByProviderKeys(ctx sdk.Context) {
90-
store := prefix.NewStore(k.node.Store(ctx), types.NodeForPlanKeyPrefix)
94+
store := prefix.NewStore(k.node.Store(ctx), nodetypes.NodeForPlanKeyPrefix)
9195

9296
it := store.Iterator(nil, nil)
9397
defer it.Close()
@@ -121,3 +125,26 @@ func (k *Migrator) setPlanForProviderKeys(ctx sdk.Context, keys ...[]byte) {
121125
k.plan.SetPlanForProvider(ctx, addr, id)
122126
}
123127
}
128+
129+
func (k *Migrator) PostMigrate(ctx sdk.Context) error {
130+
store := prefix.NewStore(k.plan.Store(ctx), types.PlanForNodeKeyPrefix)
131+
132+
it := store.Iterator(nil, nil)
133+
defer it.Close()
134+
135+
for ; it.Valid(); it.Next() {
136+
key := it.Key()
137+
nodeAddrLen := int(key[0])
138+
nodeAddr := base.NodeAddress(key[1 : 1+nodeAddrLen])
139+
provAddrLen := int(key[1+nodeAddrLen])
140+
provAddr := base.ProvAddress(key[1+nodeAddrLen+1 : 1+nodeAddrLen+1+provAddrLen])
141+
id := binary.BigEndian.Uint64(key[1+nodeAddrLen+1+provAddrLen:])
142+
143+
if !k.lease.HasAnyLeaseForNodeByProvider(ctx, nodeAddr, provAddr) {
144+
k.node.DeleteNodeForPlan(ctx, id, nodeAddr)
145+
k.plan.DeletePlanForNodeByProvider(ctx, nodeAddr, provAddr, id)
146+
}
147+
}
148+
149+
return nil
150+
}

x/subscription/migrations/migrator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
160160
if !item.Status.Equal(v1base.StatusActive) {
161161
refund = true
162162
}
163+
163164
if k.lease.HasAnyLeaseForNodeByProvider(ctx, nodeAddr, provAddr) {
164165
refund = true
165166
}
@@ -168,6 +169,7 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
168169
if !found {
169170
panic(fmt.Errorf("node %s does not exist", nodeAddr))
170171
}
172+
171173
if !node.Status.Equal(v1base.StatusActive) {
172174
refund = true
173175
}
@@ -176,6 +178,7 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
176178
if !found {
177179
panic(fmt.Errorf("provider %s does not exist", provAddr))
178180
}
181+
179182
if !provider.Status.Equal(v1base.StatusActive) {
180183
refund = true
181184
}
@@ -253,6 +256,17 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
253256
k.subscription.SetSubscriptionForPlan(ctx, subscription.PlanID, subscription.ID)
254257
k.subscription.SetSubscriptionForInactiveAt(ctx, subscription.InactiveAt, subscription.ID)
255258
k.subscription.SetSubscriptionForRenewalAt(ctx, subscription.RenewalAt(), subscription.ID)
259+
260+
k.subscription.IterateAllocationsForSubscription(ctx, item.ID, func(_ int, item v2.Allocation) bool {
261+
accAddr, err := sdk.AccAddressFromBech32(item.Address)
262+
if err != nil {
263+
panic(err)
264+
}
265+
266+
k.subscription.SetSubscriptionForAccount(ctx, accAddr, subscription.ID)
267+
268+
return false
269+
})
256270
}
257271
}
258272
}

x/vpn/migrations/migrator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewMigrator(cdc codec.BinaryCodec, k keeper.Keeper) Migrator {
3030
lease: lease.NewMigrator(cdc, &k.Lease),
3131
provider: provider.NewMigrator(cdc, &k.Provider),
3232
node: node.NewMigrator(cdc, &k.Node),
33-
plan: plan.NewMigrator(cdc, &k.Node, &k.Plan),
33+
plan: plan.NewMigrator(cdc, &k.Lease, &k.Node, &k.Plan),
3434
subscription: subscription.NewMigrator(
3535
cdc, &k.Deposit, &k.Lease, &k.Node, &k.Plan, &k.Provider, &k.Subscription,
3636
),
@@ -67,5 +67,9 @@ func (k *Migrator) Migrate(ctx sdk.Context) error {
6767
return err
6868
}
6969

70+
if err := k.plan.PostMigrate(ctx); err != nil {
71+
return err
72+
}
73+
7074
return nil
7175
}

0 commit comments

Comments
 (0)