Skip to content

Commit

Permalink
feat: implement funders module (backport #141) (#144)
Browse files Browse the repository at this point in the history
Co-authored-by: Raphael <raphael.thurnherr1990@gmail.com>
  • Loading branch information
2 people authored and mbreithecker committed Nov 10, 2023
1 parent 1aaf221 commit 125f109
Show file tree
Hide file tree
Showing 168 changed files with 21,984 additions and 5,055 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ An '!' indicates a state machine breaking change.

## [Unreleased]

### Features

- ! (`x/funders`) [#141](https://github.com/KYVENetwork/chain/pull/141) Implementation of the new [funders concept](https://commonwealth.im/kyve/discussion/13420-enhancing-kyves-funders-concept).

### Improvements

- ! (`x/bundles`) [#142](https://github.com/KYVENetwork/chain/pull/142) Halt the pool if a single validator has more than 50% voting power.
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ proto-lint:

proto-setup:
@echo "🤖 Setting up protobuf environment..."
@docker build --rm --tag kyve-proto:latest --file proto/Dockerfile .
@docker build --rm --tag kyve-proto:latest --file proto/Dockerfile \
--build-arg USER_ID=$$(id -u) --build-arg USER_GID=$$(id -g) .
@echo "✅ Setup protobuf environment!"

###############################################################################
Expand Down
35 changes: 35 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ import (
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradeKeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
upgradeTypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
// Funders
"github.com/KYVENetwork/chain/x/funders"
fundersKeeper "github.com/KYVENetwork/chain/x/funders/keeper"
fundersTypes "github.com/KYVENetwork/chain/x/funders/types"
)

const (
Expand Down Expand Up @@ -293,6 +297,7 @@ func NewKYVEApp(
queryTypes.StoreKey,
stakersTypes.StoreKey,
teamTypes.StoreKey,
fundersTypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramsTypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(
Expand Down Expand Up @@ -425,6 +430,12 @@ func NewKYVEApp(
app.AccountKeeper,
)

app.FeeGrantKeeper = feeGrantKeeper.NewKeeper(
appCodec,
keys[feeGrantTypes.StoreKey],
app.AccountKeeper,
)

app.GroupKeeper = groupKeeper.NewKeeper(
keys[groupTypes.StoreKey],
appCodec,
Expand Down Expand Up @@ -504,8 +515,22 @@ func NewKYVEApp(
app.StakersKeeper,
)

app.FundersKeeper = *fundersKeeper.NewKeeper(
appCodec,
keys[fundersTypes.StoreKey],
memKeys[fundersTypes.MemStoreKey],

authTypes.NewModuleAddress(govTypes.ModuleName).String(),

app.AccountKeeper,
app.BankKeeper,
app.PoolKeeper,
app.UpgradeKeeper,
)

stakersKeeper.SetDelegationKeeper(&app.StakersKeeper, app.DelegationKeeper)
poolKeeper.SetStakersKeeper(&app.PoolKeeper, app.StakersKeeper)
poolKeeper.SetFundersKeeper(&app.PoolKeeper, app.FundersKeeper)

app.BundlesKeeper = *bundlesKeeper.NewKeeper(
appCodec,
Expand All @@ -520,6 +545,7 @@ func NewKYVEApp(
app.PoolKeeper,
app.StakersKeeper,
app.DelegationKeeper,
app.FundersKeeper,
)

app.IBCKeeper = ibcKeeper.NewKeeper(
Expand Down Expand Up @@ -633,6 +659,7 @@ func NewKYVEApp(
app.GlobalKeeper,
*app.GovKeeper,
app.TeamKeeper,
app.FundersKeeper,
)
// this line is used by starport scaffolding # stargate/app/keeperDefinition

Expand Down Expand Up @@ -709,6 +736,7 @@ func NewKYVEApp(
query.NewAppModule(appCodec, app.QueryKeeper, app.AccountKeeper, app.BankKeeper),
stakers.NewAppModule(appCodec, app.StakersKeeper, app.AccountKeeper, app.BankKeeper),
team.NewAppModule(appCodec, app.BankKeeper, app.MintKeeper, app.TeamKeeper, *app.UpgradeKeeper),
funders.NewAppModule(appCodec, app.FundersKeeper, app.AccountKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -750,6 +778,7 @@ func NewKYVEApp(
delegationTypes.ModuleName,
queryTypes.ModuleName,
globalTypes.ModuleName,
fundersTypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -784,6 +813,7 @@ func NewKYVEApp(
queryTypes.ModuleName,
globalTypes.ModuleName,
teamTypes.ModuleName,
fundersTypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -823,6 +853,7 @@ func NewKYVEApp(
queryTypes.ModuleName,
globalTypes.ModuleName,
teamTypes.ModuleName,
fundersTypes.ModuleName,
)

// Uncomment if you want to set a custom migration order here.
Expand Down Expand Up @@ -880,6 +911,10 @@ func NewKYVEApp(
*app.GovKeeper,
*app.IBCKeeper,
app.ParamsKeeper,
app.PoolKeeper,
app.FundersKeeper,
app.BankKeeper,
app.AccountKeeper,
),
)

Expand Down
2 changes: 2 additions & 0 deletions app/keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
fundersKeeper "github.com/KYVENetwork/chain/x/funders/keeper"
"github.com/cosmos/cosmos-sdk/codec"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"

Expand Down Expand Up @@ -112,6 +113,7 @@ type Keepers struct {
QueryKeeper queryKeeper.Keeper
StakersKeeper stakersKeeper.Keeper
TeamKeeper teamKeeper.Keeper
FundersKeeper fundersKeeper.Keeper

// ----- Scoped Keepers -----
// make scoped keepers public for test purposes
Expand Down
4 changes: 4 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"github.com/KYVENetwork/chain/x/funders"
fundersTypes "github.com/KYVENetwork/chain/x/funders/types"
"github.com/cosmos/cosmos-sdk/types/module"

// Auth
Expand Down Expand Up @@ -129,6 +131,7 @@ var appModuleBasics = []module.AppModuleBasic{
query.AppModuleBasic{},
stakers.AppModuleBasic{},
team.AppModuleBasic{},
funders.AppModuleBasic{},
}

// moduleAccountPermissions ...
Expand All @@ -152,6 +155,7 @@ var moduleAccountPermissions = map[string][]string{
poolTypes.ModuleName: nil,
stakersTypes.ModuleName: nil,
teamTypes.ModuleName: nil,
fundersTypes.ModuleName: nil,
}

// BlockedModuleAccountAddrs returns all the app's blocked module account addresses.
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v1_4/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1_4

import (
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
"github.com/cosmos/cosmos-sdk/baseapp"
storeTypes "github.com/cosmos/cosmos-sdk/store/types"

Expand All @@ -16,7 +17,7 @@ import (
func CreateStoreLoader(upgradeHeight int64) baseapp.StoreLoader {
storeUpgrades := storeTypes.StoreUpgrades{
Added: []string{
consensusTypes.StoreKey, crisisTypes.StoreKey,
consensusTypes.StoreKey, crisisTypes.StoreKey, funderstypes.StoreKey,
},
}

Expand Down
162 changes: 155 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,128 @@ 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{},
})

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
}
Loading

0 comments on commit 125f109

Please sign in to comment.