/
migrations.go
238 lines (199 loc) · 8.12 KB
/
migrations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package keeper
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
v2 "github.com/lavanet/lava/x/subscription/migrations/v2"
v5 "github.com/lavanet/lava/x/subscription/migrations/v5"
v6 "github.com/lavanet/lava/x/subscription/migrations/v6"
v8 "github.com/lavanet/lava/x/subscription/migrations/v8"
"github.com/lavanet/lava/x/subscription/types"
)
type Migrator struct {
keeper Keeper
}
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}
// Migrate2to3 implements store migration from v2 to v3:
// - Convert subscription store to fixation store and use timers
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate: subscriptions")
keeper := m.keeper
store := prefix.NewStore(
ctx.KVStore(keeper.storeKey),
v2.KeyPrefix(v2.SubscriptionKeyPrefix),
)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var sub_V2 v2.Subscription
keeper.cdc.MustUnmarshal(iterator.Value(), &sub_V2)
utils.LavaFormatDebug("migrate:",
utils.Attribute{Key: "subscription", Value: sub_V2.Consumer})
sub_V3 := types.Subscription{
Creator: sub_V2.Creator,
Consumer: sub_V2.Consumer,
Block: sub_V2.Block,
PlanIndex: sub_V2.PlanIndex,
PlanBlock: sub_V2.PlanBlock,
DurationTotal: sub_V2.DurationTotal,
DurationLeft: sub_V2.DurationLeft,
MonthExpiryTime: sub_V2.MonthExpiryTime,
MonthCuTotal: sub_V2.MonthCuTotal,
MonthCuLeft: sub_V2.MonthCuLeft,
}
// each subscription entry in V2 store should have an entry in V3 store
err := keeper.subsFS.AppendEntry(ctx, sub_V3.Consumer, sub_V3.Block, &sub_V3)
if err != nil {
return fmt.Errorf("%w: subscriptions %s", err, sub_V3.Consumer)
}
// if the subscription has expired, then delete the entry from V3 store to induce
// stale-period state (use the block of last expiry as the block for deletion).
// otherwise, the subscription is alive, but the current month may have expired
// between since the upgrade proposal took effect (and until now); if indeed so,
// then invoke advanceMonth() since the current block is the (month) expiry block.
// otherwise, set the timer for the monthly expiry as already was set in V2.
if sub_V3.DurationLeft > 0 {
expiry := sub_V2.MonthExpiryTime
if expiry <= uint64(ctx.BlockTime().UTC().Unix()) {
utils.LavaFormatDebug(" subscription live, month expired",
utils.Attribute{Key: "expiry", Value: time.Unix(int64(expiry), 0)},
utils.Attribute{Key: "blockTime", Value: ctx.BlockTime().UTC()},
)
keeper.advanceMonth(ctx, []byte(sub_V3.Consumer))
} else {
utils.LavaFormatDebug(" subscription live, future expiry",
utils.Attribute{Key: "expiry", Value: time.Unix(int64(expiry), 0)},
utils.Attribute{Key: "blockTime", Value: ctx.BlockTime().UTC()},
)
keeper.subsTS.AddTimerByBlockTime(ctx, expiry, []byte(sub_V3.Consumer), []byte{})
}
} else {
utils.LavaFormatDebug(" subscription deleted",
utils.Attribute{Key: "block", Value: sub_V2.PrevExpiryBlock})
keeper.subsFS.DelEntry(ctx, sub_V3.Consumer, sub_V2.PrevExpiryBlock)
}
store.Delete(iterator.Key())
}
return nil
}
// Migrate3to4 implements store migration from v3 to v4:
// -- trigger fixation migration (v4->v5), initialize IsLatest field
func (m Migrator) Migrate3to4(ctx sdk.Context) error {
// This migration used to call a deprecated fixationstore function called MigrateVersionAndPrefix
return nil
}
// Migrate4to5 implements store migration from v4 to v5:
// -- rename the DurationTotal field to DurationBought
// -- introduce two new fields: DurationTotal (with new meaning) and cluster
// -- assign the subscription's cluster
func (m Migrator) Migrate4to5(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate 4->5: subscriptions")
keeper := m.keeper
indices := keeper.subsFS.AllEntryIndicesFilter(ctx, "", nil)
for _, ind := range indices {
blocks := keeper.subsFS.GetAllEntryVersions(ctx, ind)
for _, block := range blocks {
var sub_V5 v5.Subscription
keeper.subsFS.ReadEntry(ctx, ind, block, &sub_V5)
utils.LavaFormatDebug("migrate:",
utils.Attribute{Key: "subscription", Value: sub_V5.Consumer})
sub_V5.Cluster = v5.GetClusterKey(sub_V5)
keeper.subsFS.ModifyEntry(ctx, ind, block, &sub_V5)
}
}
return nil
}
// Migrate5to6 implements store migration from v5 to v6:
// -- find old subscriptions and trigger advance month to make them expire
func (m Migrator) Migrate5to6(ctx sdk.Context) error {
indices := m.keeper.GetAllSubscriptionsIndices(ctx)
currentTime := ctx.BlockTime().UTC().Unix()
for _, ind := range indices {
sub, found := m.keeper.GetSubscription(ctx, ind)
if !found {
utils.LavaFormatError("cannot migrate sub", fmt.Errorf("sub not found"),
utils.Attribute{Key: "sub", Value: sub},
)
}
if sub.MonthExpiryTime < uint64(currentTime) {
m.keeper.advanceMonth(ctx, []byte(ind))
}
}
return nil
}
// Migrate6to7 implements store migration from v6 to v7:
// -- if subscription's auto_renewal = true, set auto_renewal_next_plan to the current's subscription plan
func (m Migrator) Migrate6to7(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate 6->7: subscriptions")
for _, index := range m.keeper.subsFS.GetAllEntryIndices(ctx) {
for _, block := range m.keeper.subsFS.GetAllEntryVersions(ctx, index) {
var subscriptionV6 v6.Subscription
var subscriptionV7 types.Subscription
foundOld := m.keeper.subsFS.FindEntry(ctx, index, block, &subscriptionV6)
foundNew := m.keeper.subsFS.FindEntry(ctx, index, block, &subscriptionV7)
if !foundOld || !foundNew {
utils.LavaFormatError("cannot migrate sub", fmt.Errorf("sub not found"),
utils.Attribute{Key: "index", Value: index},
utils.Attribute{Key: "block", Value: block},
)
continue
}
if subscriptionV6.AutoRenewal {
subscriptionV7.AutoRenewalNextPlan = subscriptionV7.PlanIndex
} else {
subscriptionV7.AutoRenewalNextPlan = types.AUTO_RENEWAL_PLAN_NONE
}
m.keeper.subsFS.ModifyEntry(ctx, index, block, &subscriptionV7)
}
}
return nil
}
// Migrate7to8 implements store migration from v7 to v8:
// init new credit field
func (m Migrator) Migrate7to8(ctx sdk.Context) error {
utils.LavaFormatDebug("migrate 7->8: subscriptions")
for _, index := range m.keeper.subsFS.GetAllEntryIndices(ctx) {
for _, block := range m.keeper.subsFS.GetAllEntryVersions(ctx, index) {
// read current subscription from fixation to new subscription struct
var s8 v8.Subscription
m.keeper.subsFS.ReadEntry(ctx, index, block, &s8)
// calculate sub's credit
plan, found := m.keeper.plansKeeper.FindPlan(ctx, s8.PlanIndex, s8.PlanBlock)
if !found {
utils.LavaFormatError("cannot migrate sub", fmt.Errorf("sub's plan not found"),
utils.Attribute{Key: "consumer", Value: index},
utils.Attribute{Key: "sub_block", Value: block},
utils.Attribute{Key: "plan", Value: s8.PlanIndex},
utils.Attribute{Key: "plan_block", Value: s8.PlanBlock},
)
continue
}
creditAmount := plan.Price.Amount.MulRaw(int64(s8.DurationLeft))
credit := sdk.NewCoin(m.keeper.stakingKeeper.BondDenom(ctx), creditAmount)
// calculate future sub's credit
if s8.FutureSubscription != nil {
futurePlan, found := m.keeper.plansKeeper.FindPlan(ctx, s8.FutureSubscription.PlanIndex, s8.FutureSubscription.PlanBlock)
if !found {
utils.LavaFormatError("cannot migrate sub", fmt.Errorf("sub's future plan not found"),
utils.Attribute{Key: "consumer", Value: index},
utils.Attribute{Key: "sub_block", Value: block},
utils.Attribute{Key: "plan", Value: s8.PlanIndex},
utils.Attribute{Key: "plan_block", Value: s8.PlanBlock},
)
continue
}
futureCreditAmount := futurePlan.Price.Amount.MulRaw(int64(s8.FutureSubscription.DurationBought))
futureCredit := sdk.NewCoin(m.keeper.stakingKeeper.BondDenom(ctx), futureCreditAmount)
s8.FutureSubscription.Credit = futureCredit
}
s8.Credit = credit
// modify sub entry
m.keeper.subsFS.ModifyEntry(ctx, index, block, &s8)
}
}
return nil
}