-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented migration methods for all the types and indexes
- Loading branch information
1 parent
1f56f13
commit 3b4974f
Showing
23 changed files
with
851 additions
and
421 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
package migrations | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/x/deposit/types/v1" | ||
) | ||
|
||
type DepositKeeper interface { | ||
SetDeposit(ctx sdk.Context, deposit v1.Deposit) | ||
Store(ctx sdk.Context) sdk.KVStore | ||
} |
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,46 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/x/deposit/types" | ||
"github.com/sentinel-official/hub/v12/x/deposit/types/v1" | ||
) | ||
|
||
type Migrator struct { | ||
cdc codec.BinaryCodec | ||
deposit DepositKeeper | ||
} | ||
|
||
func NewMigrator(cdc codec.BinaryCodec, deposit DepositKeeper) Migrator { | ||
return Migrator{ | ||
cdc: cdc, | ||
deposit: deposit, | ||
} | ||
} | ||
|
||
func (k *Migrator) Migrate(ctx sdk.Context) error { | ||
if err := k.resetDeposits(ctx); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (k *Migrator) resetDeposits(ctx sdk.Context) error { | ||
store := prefix.NewStore(k.deposit.Store(ctx), types.DepositKeyPrefix) | ||
|
||
it := store.Iterator(nil, nil) | ||
defer it.Close() | ||
|
||
for ; it.Valid(); it.Next() { | ||
var item v1.Deposit | ||
k.cdc.MustUnmarshal(it.Value(), &item) | ||
|
||
k.deposit.SetDeposit(ctx, item) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package migrations | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/x/lease/types/v1" | ||
) | ||
|
||
type LeaseKeeper interface { | ||
SetParams(ctx sdk.Context, params v1.Params) | ||
} |
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,37 @@ | ||
package migrations | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/x/lease/types/v1" | ||
) | ||
|
||
type Migrator struct { | ||
cdc codec.BinaryCodec | ||
lease LeaseKeeper | ||
} | ||
|
||
func NewMigrator(cdc codec.BinaryCodec, lease LeaseKeeper) Migrator { | ||
return Migrator{ | ||
cdc: cdc, | ||
lease: lease, | ||
} | ||
} | ||
|
||
func (k *Migrator) Migrate(ctx sdk.Context) error { | ||
k.setParams(ctx) | ||
|
||
return nil | ||
} | ||
|
||
func (k *Migrator) setParams(ctx sdk.Context) { | ||
params := v1.Params{ | ||
MaxLeaseHours: 720, | ||
MinLeaseHours: 1, | ||
StakingShare: math.LegacyMustNewDecFromStr("0.2"), | ||
} | ||
|
||
k.lease.SetParams(ctx, params) | ||
} |
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
package migrations | ||
|
||
import ( | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/types" | ||
"github.com/sentinel-official/hub/v12/x/node/types/v3" | ||
) | ||
|
||
type NodeKeeper interface { | ||
SetNode(ctx sdk.Context, node v3.Node) | ||
SetNodeForInactiveAt(ctx sdk.Context, time time.Time, addr types.NodeAddress) | ||
SetNodeForPlan(ctx sdk.Context, id uint64, addr types.NodeAddress) | ||
SetParams(ctx sdk.Context, params v3.Params) | ||
Store(ctx sdk.Context) sdk.KVStore | ||
} |
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,121 @@ | ||
package migrations | ||
|
||
import ( | ||
"time" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/sentinel-official/hub/v12/types" | ||
"github.com/sentinel-official/hub/v12/types/v1" | ||
"github.com/sentinel-official/hub/v12/x/node/types/v2" | ||
"github.com/sentinel-official/hub/v12/x/node/types/v3" | ||
) | ||
|
||
type Migrator struct { | ||
cdc codec.BinaryCodec | ||
node NodeKeeper | ||
} | ||
|
||
func NewMigrator(cdc codec.BinaryCodec, node NodeKeeper) Migrator { | ||
return Migrator{ | ||
cdc: cdc, | ||
node: node, | ||
} | ||
} | ||
|
||
func (k *Migrator) Migrate(ctx sdk.Context) error { | ||
k.setParams(ctx) | ||
k.migrateNodes(ctx) | ||
|
||
nodeForInactiveAtKeys := k.deleteKeys(ctx, []byte{0x11}) | ||
nodeForPlanKeys := k.deleteKeys(ctx, []byte{0x12}) | ||
|
||
k.setNodeForPlanKeys(ctx, nodeForPlanKeys...) | ||
k.setNodeForInactiveAtKeys(ctx, nodeForInactiveAtKeys...) | ||
|
||
return nil | ||
} | ||
|
||
func (k *Migrator) deleteKeys(ctx sdk.Context, keyPrefix []byte) (keys [][]byte) { | ||
store := prefix.NewStore(k.node.Store(ctx), keyPrefix) | ||
|
||
it := store.Iterator(nil, nil) | ||
defer it.Close() | ||
|
||
for ; it.Valid(); it.Next() { | ||
store.Delete(it.Key()) | ||
|
||
keys = append(keys, it.Key()) | ||
} | ||
|
||
return keys | ||
} | ||
|
||
func (k *Migrator) setParams(ctx sdk.Context) { | ||
params := v3.Params{ | ||
Deposit: sdk.NewInt64Coin("udvpn", 0), | ||
ActiveDuration: 1 * time.Hour, | ||
MinGigabytePrices: []v1.Price{}, // TODO: set min gigabyte prices | ||
MinHourlyPrices: []v1.Price{}, // TODO: set min hourly prices | ||
MaxSessionGigabytes: 1_000_000, | ||
MinSessionGigabytes: 1, | ||
MaxSessionHours: 720, | ||
MinSessionHours: 1, | ||
StakingShare: math.LegacyMustNewDecFromStr("0.2"), | ||
} | ||
|
||
k.node.SetParams(ctx, params) | ||
} | ||
|
||
func (k *Migrator) migrateNodes(ctx sdk.Context) { | ||
store := prefix.NewStore(k.node.Store(ctx), []byte{0x10}) | ||
|
||
it := store.Iterator(nil, nil) | ||
defer it.Close() | ||
|
||
for ; it.Valid(); it.Next() { | ||
store.Delete(it.Key()) | ||
|
||
var item v2.Node | ||
k.cdc.MustUnmarshal(it.Value(), &item) | ||
|
||
node := v3.Node{ | ||
Address: item.Address, | ||
GigabytePrices: v1.NewPricesFromCoins(item.GigabytePrices...), | ||
HourlyPrices: v1.NewPricesFromCoins(item.HourlyPrices...), | ||
RemoteURL: item.RemoteURL, | ||
InactiveAt: item.InactiveAt, | ||
Status: item.Status, | ||
StatusAt: item.StatusAt, | ||
} | ||
|
||
k.node.SetNode(ctx, node) | ||
} | ||
} | ||
|
||
func (k *Migrator) setNodeForPlanKeys(ctx sdk.Context, keys ...[]byte) { | ||
for _, key := range keys { | ||
id := sdk.BigEndianToUint64(key[:8]) | ||
addrLen := int(key[8]) | ||
addr := types.NodeAddress(key[9 : 9+addrLen]) | ||
|
||
k.node.SetNodeForPlan(ctx, id, addr) | ||
} | ||
} | ||
|
||
func (k *Migrator) setNodeForInactiveAtKeys(ctx sdk.Context, keys ...[]byte) { | ||
for _, key := range keys { | ||
inactiveAt, err := sdk.ParseTimeBytes(key[:29]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
addrLen := int(key[29]) | ||
addr := types.NodeAddress(key[30 : 30+addrLen]) | ||
|
||
k.node.SetNodeForInactiveAt(ctx, inactiveAt, addr) | ||
} | ||
} |
Oops, something went wrong.