Skip to content

Commit

Permalink
feat: storage-cost per storage-provider (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 authored May 2, 2024
1 parent 7fdb184 commit 3f81aca
Show file tree
Hide file tree
Showing 21 changed files with 1,061 additions and 128 deletions.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ func New(
v1_5.CreateUpgradeHandler(
app.ModuleManager,
app.Configurator(),
app.appCodec,
app.GetStoreKeys(),
app.BundlesKeeper,
app.PoolKeeper,
),
)

Expand Down
49 changes: 47 additions & 2 deletions app/upgrades/v1_5/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import (
"context"
"fmt"

"cosmossdk.io/math"

storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/KYVENetwork/chain/app/upgrades/v1_5/v1_4_types"
"github.com/KYVENetwork/chain/x/bundles/keeper"
bundlestypes "github.com/KYVENetwork/chain/x/bundles/types"
poolkeeper "github.com/KYVENetwork/chain/x/pool/keeper"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)
Expand All @@ -14,11 +22,48 @@ const (
UpgradeName = "v1.5.0"
)

func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator) upgradetypes.UpgradeHandler {
func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, cdc codec.Codec, storeKeys []storetypes.StoreKey, bundlesKeeper keeper.Keeper, poolKeeper *poolkeeper.Keeper) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
logger := sdk.UnwrapSDKContext(ctx).Logger().With("upgrade", UpgradeName)
sdkCtx := sdk.UnwrapSDKContext(ctx)
logger := sdkCtx.Logger().With("upgrade", UpgradeName)
logger.Info(fmt.Sprintf("performing upgrade %v", UpgradeName))

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

// TODO: migrate gov params

return mm.RunMigrations(ctx, configurator, fromVM)
}
}

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

// Copy storage cost from old params to new params
// The storage cost of all storage providers will be the same after this migration
oldParams := v1_4_types.GetParams(sdkCtx, bundlesStoreKey, cdc)
newParams := bundlestypes.Params{
UploadTimeout: oldParams.UploadTimeout,
StorageCosts: []bundlestypes.StorageCost{
// TODO: define value for storage provider id 1 and 2
{StorageProviderId: 1, Cost: math.LegacyMustNewDecFromStr("0.00")},
{StorageProviderId: 2, Cost: math.LegacyMustNewDecFromStr("0.00")},
},
NetworkFee: oldParams.NetworkFee,
MaxPoints: oldParams.MaxPoints,
}

bundlesKeeper.SetParams(sdkCtx, newParams)
return nil
}
21 changes: 21 additions & 0 deletions app/upgrades/v1_5/v1_4_types/getters_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package v1_4_types

import (
storeTypes "cosmossdk.io/store/types"
"github.com/KYVENetwork/chain/x/bundles/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetParams returns the current x/bundles module parameters.
func GetParams(ctx sdk.Context, storeKey storeTypes.StoreKey, cdc codec.Codec) (params Params) {
store := ctx.KVStore(storeKey)

bz := store.Get(types.ParamsPrefix)
if bz == nil {
return params
}

cdc.MustUnmarshal(bz, &params)
return params
}
Loading

0 comments on commit 3f81aca

Please sign in to comment.