Skip to content

Commit

Permalink
Rapha/funders module migration (#130)
Browse files Browse the repository at this point in the history
Co-authored-by: mbreithecker <[email protected]>
  • Loading branch information
shifty11 and mbreithecker authored Oct 26, 2023
1 parent b143cea commit 85f7e52
Show file tree
Hide file tree
Showing 13 changed files with 2,399 additions and 90 deletions.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,10 @@ func NewKYVEApp(
*app.GovKeeper,
*app.IBCKeeper,
app.ParamsKeeper,
app.PoolKeeper,
app.FundersKeeper,
app.BankKeeper,
app.AccountKeeper,
),
)

Expand Down
163 changes: 156 additions & 7 deletions app/upgrades/v1_4/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package v1_4

import (
"errors"

"github.com/KYVENetwork/chain/app/upgrades/v1_4/v1_3_types"
"github.com/KYVENetwork/chain/util"
fundersKeeper "github.com/KYVENetwork/chain/x/funders/keeper"
fundersTypes "github.com/KYVENetwork/chain/x/funders/types"
globalTypes "github.com/KYVENetwork/chain/x/global/types"
poolKeeper "github.com/KYVENetwork/chain/x/pool/keeper"
poolTypes "github.com/KYVENetwork/chain/x/pool/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authKeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisisTypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distributionTypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand All @@ -17,18 +28,12 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
ibcTmMigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"

// Consensus
consensusKeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
// Global
globalKeeper "github.com/KYVENetwork/chain/x/global/keeper"
// Governance
consensusKeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
govKeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
// IBC Core
ibcKeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

// Params
paramsKeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
// Upgrade
upgradeTypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

Expand All @@ -43,6 +48,10 @@ func CreateUpgradeHandler(
govKeeper govKeeper.Keeper,
ibcKeeper ibcKeeper.Keeper,
paramsKeeper paramsKeeper.Keeper,
poolKeeper poolKeeper.Keeper,
fundersKeeper fundersKeeper.Keeper,
bankKeeper bankKeeper.Keeper,
accountKeeper authKeeper.AccountKeeper,
) upgradeTypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradeTypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", UpgradeName)
Expand Down Expand Up @@ -103,6 +112,20 @@ func CreateUpgradeHandler(
return vm, err
}

// Migrate funders.
err = migrateFundersAndPools(ctx, cdc, poolKeeper, fundersKeeper, bankKeeper, accountKeeper)
if err != nil {
return vm, err
}

// Set min gas for funder creation in global module
globalParams := globalKeeper.GetParams(ctx)
globalParams.GasAdjustments = append(globalParams.GasAdjustments, globalTypes.GasAdjustment{
Type: "/kyve.funders.v1beta1.MsgCreateFunder",
Amount: 50_000_000,
})
globalKeeper.SetParams(ctx, globalParams)

return vm, nil
}
}
Expand All @@ -121,3 +144,129 @@ func migrateInitialDepositRatio(

return govKeeper.SetParams(ctx, params)
}

type FundingMigration struct {
PoolId uint64
Amount uint64
}

type FunderMigration struct {
Address string
Fundings []FundingMigration
}

// migrateFunders migrates funders from x/pool to x/funders and creates funding states for pools.
func migrateFundersAndPools(
ctx sdk.Context,
cdc codec.BinaryCodec,
poolKeeper poolKeeper.Keeper,
fundersKeeper fundersKeeper.Keeper,
bankKeeper bankKeeper.Keeper,
accountKeeper authKeeper.AccountKeeper,
) error {
pools, err := v1_3_types.GetAllPools(ctx, poolKeeper, cdc)
if err != nil {
return err
}

toBeCreatedFunders := make(map[string]*FunderMigration)
amountToBeTransferred := uint64(0)

// Get all funders and their funding from pools.
for _, oldPool := range pools {
checkTotalFunds := uint64(0)
for _, funder := range oldPool.Funders {
if funder.Amount > 0 {
_, ok := toBeCreatedFunders[funder.Address]
if ok {
toBeCreatedFunders[funder.Address].Fundings = append(toBeCreatedFunders[funder.Address].Fundings, FundingMigration{PoolId: oldPool.Id, Amount: funder.Amount})
} else {
toBeCreatedFunders[funder.Address] = &FunderMigration{
Address: funder.Address,
Fundings: []FundingMigration{{PoolId: oldPool.Id, Amount: funder.Amount}},
}
}
checkTotalFunds += funder.Amount
}
}
if checkTotalFunds != oldPool.TotalFunds {
return errors.New("total funds is not equal to the sum of all funders amount")
}
amountToBeTransferred += oldPool.TotalFunds

// Create funding state for pool.
fundersKeeper.SetFundingState(ctx, &fundersTypes.FundingState{
PoolId: oldPool.Id,
ActiveFunderAddresses: []string{},
TotalAmount: oldPool.TotalFunds,
})

poolKeeper.SetPool(ctx, poolTypes.Pool{
Id: oldPool.Id,
Name: oldPool.Name,
Runtime: oldPool.Runtime,
Logo: oldPool.Logo,
Config: oldPool.Config,
StartKey: oldPool.StartKey,
CurrentKey: oldPool.CurrentKey,
CurrentSummary: oldPool.CurrentSummary,
CurrentIndex: oldPool.CurrentIndex,
TotalBundles: oldPool.TotalBundles,
UploadInterval: oldPool.UploadInterval,
InflationShareWeight: oldPool.OperatingCost,
MinDelegation: oldPool.MinDelegation,
MaxBundleSize: oldPool.MaxBundleSize,
Disabled: oldPool.Disabled,
Protocol: &poolTypes.Protocol{
Version: oldPool.Protocol.Version,
Binaries: oldPool.Protocol.Binaries,
LastUpgrade: oldPool.Protocol.LastUpgrade,
},
UpgradePlan: &poolTypes.UpgradePlan{
Version: oldPool.UpgradePlan.Version,
Binaries: oldPool.UpgradePlan.Binaries,
ScheduledAt: oldPool.UpgradePlan.ScheduledAt,
Duration: oldPool.UpgradePlan.Duration,
},
CurrentStorageProviderId: oldPool.CurrentStorageProviderId,
CurrentCompressionId: oldPool.CurrentCompressionId,
})
}

// Create new funders and fundings.
for _, funder := range toBeCreatedFunders {
fundersKeeper.SetFunder(ctx, &fundersTypes.Funder{
Address: funder.Address,
Moniker: funder.Address,
Identity: "",
Website: "",
Contact: "",
Description: "",
})
for _, funding := range funder.Fundings {
fundersKeeper.SetFunding(ctx, &fundersTypes.Funding{
FunderAddress: funder.Address,
PoolId: funding.PoolId,
Amount: funding.Amount,
AmountPerBundle: fundersTypes.DefaultMinFundingAmountPerBundle,
// Previous funders will not be considered, as there is no way to calculate this on chain.
// Although almost all funding was only provided by the Foundation itself.
TotalFunded: 0,
})
}
}

// Check if pool module balance is equal to the sum of all pools total funds.
poolModule := accountKeeper.GetModuleAddress(poolTypes.ModuleName)
balance := bankKeeper.GetBalance(ctx, poolModule, globalTypes.Denom)
if balance.Amount.Uint64() != amountToBeTransferred {
return errors.New("pool module balance is not equal to the sum of all pools total funds")
}

// Transfer funds from pools to funders.
if err := util.TransferFromModuleToModule(bankKeeper, ctx, poolTypes.ModuleName, fundersTypes.ModuleName, amountToBeTransferred); err != nil {
return err
}

return nil
}
29 changes: 29 additions & 0 deletions app/upgrades/v1_4/v1_3_types/getters_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v1_3_types

import (
poolKeeper "github.com/KYVENetwork/chain/x/pool/keeper"
"github.com/KYVENetwork/chain/x/pool/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetAllPools returns all pools
func GetAllPools(ctx sdk.Context, poolKeeper poolKeeper.Keeper, cdc codec.BinaryCodec) (list []Pool, err error) {
store := prefix.NewStore(ctx.KVStore(poolKeeper.StoreKey()), types.PoolKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{})

//goland:noinspection GoUnhandledErrorResult
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val Pool
err = cdc.Unmarshal(iterator.Value(), &val)
if err != nil {
return
}
list = append(list, val)
}

return
}
Loading

0 comments on commit 85f7e52

Please sign in to comment.