Skip to content

Commit

Permalink
feat: add migration for inflation-share-weight
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed May 22, 2024
1 parent 2b7e8d9 commit 6463063
Show file tree
Hide file tree
Showing 3 changed files with 1,938 additions and 0 deletions.
68 changes: 68 additions & 0 deletions app/upgrades/v1_5/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1_5
import (
"context"
"fmt"
"github.com/KYVENetwork/chain/app/upgrades/v1_5/v1_4_pool_types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"

"cosmossdk.io/math"

Expand Down Expand Up @@ -32,6 +34,10 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator,
return nil, err
}

if err := migrateInflationShareWeight(sdkCtx, poolKeeper, storeKeys, cdc); err != nil {
return nil, err
}

// TODO: migrate gov params

// TODO: migrate fundings
Expand Down Expand Up @@ -73,3 +79,65 @@ func migrateStorageCosts(sdkCtx sdk.Context, bundlesKeeper keeper.Keeper, poolKe
bundlesKeeper.SetParams(sdkCtx, newParams)
return nil
}

func migrateInflationShareWeight(sdkCtx sdk.Context, poolKeeper *poolkeeper.Keeper, storeKeys []storetypes.StoreKey, cdc codec.Codec) error {
var poolStoreKey storetypes.StoreKey
for _, k := range storeKeys {
if k.Name() == "pool" {
poolStoreKey = k
break
}
}
if poolStoreKey == nil {
return fmt.Errorf("store key not found: pool")
}

pools := v1_4_pool_types.GetAllPools(sdkCtx, poolStoreKey, cdc)
for _, pool := range pools {
var newPool pooltypes.Pool

var protocol *pooltypes.Protocol
if pool.Protocol != nil {
protocol = &pooltypes.Protocol{
Version: pool.Protocol.Version,
Binaries: pool.Protocol.Binaries,
LastUpgrade: pool.Protocol.LastUpgrade,
}
}
var upgradePlan *pooltypes.UpgradePlan
if pool.UpgradePlan != nil {
upgradePlan = &pooltypes.UpgradePlan{
Version: pool.UpgradePlan.Version,
Binaries: pool.UpgradePlan.Binaries,
ScheduledAt: pool.UpgradePlan.ScheduledAt,
Duration: pool.UpgradePlan.Duration,
}
}

newPool = pooltypes.Pool{
Id: pool.Id,
Name: pool.Name,
Runtime: pool.Runtime,
Logo: pool.Logo,
Config: pool.Config,
StartKey: pool.StartKey,
CurrentKey: pool.CurrentKey,
CurrentSummary: pool.CurrentSummary,
CurrentIndex: pool.CurrentIndex,
TotalBundles: pool.TotalBundles,
UploadInterval: pool.UploadInterval,
// Convert inflation share weight to new decimal type
InflationShareWeight: math.LegacyNewDec(int64(pool.InflationShareWeight)),
MinDelegation: pool.MinDelegation,
MaxBundleSize: pool.MaxBundleSize,
Disabled: pool.Disabled,
Protocol: protocol,
UpgradePlan: upgradePlan,
CurrentStorageProviderId: pool.CurrentStorageProviderId,
CurrentCompressionId: pool.CurrentCompressionId,
EndKey: pool.EndKey,
}
poolKeeper.SetPool(sdkCtx, newPool)
}
return nil
}
23 changes: 23 additions & 0 deletions app/upgrades/v1_5/v1_4_pool_types/getters_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1_4_pool_types

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetAllPools returns all pools
func GetAllPools(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec) (list []Pool) {
store := ctx.KVStore(storeKey)
iterator := storetypes.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val Pool
cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
Loading

0 comments on commit 6463063

Please sign in to comment.