@@ -20,19 +20,21 @@ type Migrator struct {
2020 cdc codec.BinaryCodec
2121 deposit DepositKeeper
2222 lease LeaseKeeper
23+ node NodeKeeper
2324 plan PlanKeeper
2425 provider ProviderKeeper
2526 subscription SubscriptionKeeper
2627}
2728
2829func NewMigrator (
29- cdc codec.BinaryCodec , deposit DepositKeeper , lease LeaseKeeper , plan PlanKeeper , provider ProviderKeeper ,
30- subscription SubscriptionKeeper ,
30+ cdc codec.BinaryCodec , deposit DepositKeeper , lease LeaseKeeper , node NodeKeeper , plan PlanKeeper ,
31+ provider ProviderKeeper , subscription SubscriptionKeeper ,
3132) Migrator {
3233 return Migrator {
3334 cdc : cdc ,
3435 deposit : deposit ,
3536 lease : lease ,
37+ node : node ,
3638 plan : plan ,
3739 provider : provider ,
3840 subscription : subscription ,
@@ -104,8 +106,6 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
104106 it := store .Iterator (nil , nil )
105107 defer it .Close ()
106108
107- leaseCount := uint64 (0 )
108-
109109 for ; it .Valid (); it .Next () {
110110 store .Delete (it .Key ())
111111
@@ -139,7 +139,9 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
139139
140140 if item .Hours != 0 {
141141 payout := k .getPayout (ctx , item .ID )
142- if ok := k .provider .HasProvider (ctx , accAddr .Bytes ()); ! ok {
142+ provAddr := types .ProvAddress (accAddr .Bytes ())
143+
144+ if ! k .provider .HasProvider (ctx , provAddr ) {
143145 refund := sdk .NewCoin (payout .Price .Denom , payout .Price .Amount .MulRaw (payout .Hours ))
144146
145147 if ! refund .IsZero () {
@@ -148,18 +150,49 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
148150 }
149151 }
150152 } else {
153+ nodeAddr , err := types .NodeAddressFromBech32 (item .NodeAddress )
154+ if err != nil {
155+ panic (err )
156+ }
157+
158+ refund := false
159+
151160 if ! item .Status .Equal (v1base .StatusActive ) {
152- refund := sdk .NewCoin (payout .Price .Denom , payout .Price .Amount .MulRaw (payout .Hours ))
161+ refund = true
162+ }
163+ if k .lease .HasAnyLeaseForNodeByProvider (ctx , nodeAddr , provAddr ) {
164+ refund = true
165+ }
166+
167+ node , found := k .node .GetNode (ctx , nodeAddr )
168+ if ! found {
169+ panic (fmt .Errorf ("node %s does not exist" , nodeAddr ))
170+ }
171+ if ! node .Status .Equal (v1base .StatusActive ) {
172+ refund = true
173+ }
153174
154- if ! refund .IsZero () {
155- if err := k .deposit .SubtractDeposit (ctx , accAddr , sdk .NewCoins (refund )); err != nil {
175+ provider , found := k .provider .GetProvider (ctx , provAddr )
176+ if ! found {
177+ panic (fmt .Errorf ("provider %s does not exist" , provAddr ))
178+ }
179+ if ! provider .Status .Equal (v1base .StatusActive ) {
180+ refund = true
181+ }
182+
183+ if refund {
184+ amount := sdk .NewCoin (payout .Price .Denom , payout .Price .Amount .MulRaw (payout .Hours ))
185+
186+ if ! amount .IsZero () {
187+ if err := k .deposit .SubtractDeposit (ctx , accAddr , sdk .NewCoins (amount )); err != nil {
156188 panic (err )
157189 }
158190 }
159191 } else {
192+ count := k .lease .GetLeaseCount (ctx )
160193 lease := v1.Lease {
161- ID : item . ID ,
162- ProvAddress : types . ProvAddress ( accAddr . Bytes ()) .String (),
194+ ID : count + 1 ,
195+ ProvAddress : provAddr .String (),
163196 NodeAddress : item .NodeAddress ,
164197 Price : v1base .NewPriceFromCoin (payout .Price ),
165198 Hours : item .Hours - payout .Hours ,
@@ -168,20 +201,7 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
168201 StartAt : item .StatusAt ,
169202 }
170203
171- if item .ID > leaseCount {
172- leaseCount = item .ID
173- }
174-
175- nodeAddr , err := types .NodeAddressFromBech32 (lease .NodeAddress )
176- if err != nil {
177- panic (err )
178- }
179-
180- provAddr , err := types .ProvAddressFromBech32 (lease .ProvAddress )
181- if err != nil {
182- panic (err )
183- }
184-
204+ k .lease .SetLeaseCount (ctx , count + 1 )
185205 k .lease .SetLease (ctx , lease )
186206 k .lease .SetLeaseForNodeByProvider (ctx , nodeAddr , provAddr , lease .ID )
187207 k .lease .SetLeaseForProvider (ctx , provAddr , lease .ID )
@@ -236,6 +256,4 @@ func (k *Migrator) migrateSubscriptions(ctx sdk.Context) {
236256 }
237257 }
238258 }
239-
240- k .lease .SetLeaseCount (ctx , leaseCount )
241259}
0 commit comments