-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added migration methods for plan store
- Loading branch information
1 parent
25d568e
commit 81ac8d2
Showing
8 changed files
with
187 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package keeper | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/x/plan/types" | ||
) | ||
|
||
type Migrator struct { | ||
k Keeper | ||
} | ||
|
||
func NewMigrator(k Keeper) Migrator { | ||
return Migrator{k: k} | ||
} | ||
|
||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||
store := m.k.Store(ctx) | ||
|
||
if err := migrateActivePlanForProviderKeys(store); err != nil { | ||
return err | ||
} | ||
if err := migrateInactivePlanForProviderKeys(store); err != nil { | ||
return err | ||
} | ||
if err := migrateNodeForPlanKeys(store); err != nil { | ||
return err | ||
} | ||
if err := migrateCountForNodeByProviderKeys(store); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func migrateActivePlanForProviderKeys(parent sdk.KVStore) error { | ||
child := prefix.NewStore(parent, types.ActivePlanForProviderKeyPrefix) | ||
|
||
iterator := child.Iterator(nil, nil) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
var ( | ||
addr = iterator.Key()[:20] | ||
id = sdk.BigEndianToUint64(iterator.Key()[20:]) | ||
) | ||
|
||
key := types.ActivePlanForProviderKey(addr, id) | ||
|
||
parent.Set(key, iterator.Value()) | ||
child.Delete(iterator.Key()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func migrateInactivePlanForProviderKeys(parent sdk.KVStore) error { | ||
child := prefix.NewStore(parent, types.InactivePlanForProviderKeyPrefix) | ||
|
||
iterator := child.Iterator(nil, nil) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
var ( | ||
addr = iterator.Key()[:20] | ||
id = sdk.BigEndianToUint64(iterator.Key()[20:]) | ||
) | ||
|
||
key := types.InactivePlanForProviderKey(addr, id) | ||
|
||
parent.Set(key, iterator.Value()) | ||
child.Delete(iterator.Key()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func migrateNodeForPlanKeys(parent sdk.KVStore) error { | ||
child := prefix.NewStore(parent, types.NodeForPlanKeyPrefix) | ||
|
||
iterator := child.Iterator(nil, nil) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
var ( | ||
id = sdk.BigEndianToUint64(iterator.Key()[:8]) | ||
addr = iterator.Key()[8:] | ||
) | ||
|
||
key := types.NodeForPlanKey(id, addr) | ||
|
||
parent.Set(key, iterator.Value()) | ||
child.Delete(iterator.Key()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func migrateCountForNodeByProviderKeys(parent sdk.KVStore) error { | ||
child := prefix.NewStore(parent, types.CountForNodeByProviderKeyPrefix) | ||
|
||
iterator := child.Iterator(nil, nil) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
var ( | ||
provider = iterator.Key()[:20] | ||
node = iterator.Key()[20:] | ||
) | ||
|
||
key := types.CountForNodeByProviderKey(provider, node) | ||
|
||
parent.Set(key, iterator.Value()) | ||
child.Delete(iterator.Key()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.