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
mergify[bot] and shifty11 authored Nov 8, 2023
1 parent 231b741 commit 67764a5
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 67764a5

Please sign in to comment.