Skip to content

Commit

Permalink
feat: implement app-wiring for bundles module
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Mar 25, 2024
1 parent d50beb0 commit bd6740a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 65 deletions.
7 changes: 2 additions & 5 deletions x/bundles/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ import (

// Auth
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// Bank
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

// Mint
mintKeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
// Pool
"github.com/KYVENetwork/chain/x/pool/keeper"
// Team
teamKeeper "github.com/KYVENetwork/chain/x/team/keeper"
// Upgrade
upgradeKeeper "cosmossdk.io/x/upgrade/keeper"
)

func SplitInflation(ctx sdk.Context, k bundlesKeeper.Keeper, bk bankKeeper.Keeper, mk mintKeeper.Keeper, pk keeper.Keeper, tk teamKeeper.Keeper, uk upgradeKeeper.Keeper) {
func SplitInflation(ctx sdk.Context, k bundlesKeeper.Keeper, bk util.BankKeeper, mk mintKeeper.Keeper, pk keeper.Keeper, tk teamKeeper.Keeper, uk util.UpgradeKeeper) {
minter, err := mk.Minter.Get(ctx)
if err != nil {
util.PanicHalt(uk, ctx, "failed to get minter")
Expand Down
41 changes: 27 additions & 14 deletions x/bundles/keeper/getters_bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"fmt"
"github.com/cosmos/cosmos-sdk/runtime"
"time"

cosmossdk_io_math "cosmossdk.io/math"
Expand All @@ -20,7 +21,8 @@ import (
// SetBundleProposal stores a current bundle proposal in the KV-Store.
// There is only one bundle proposal per pool
func (k Keeper) SetBundleProposal(ctx sdk.Context, bundleProposal types.BundleProposal) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.BundleKeyPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.BundleKeyPrefix)
b := k.cdc.MustMarshal(&bundleProposal)
store.Set(types.BundleProposalKey(
bundleProposal.PoolId,
Expand All @@ -29,7 +31,8 @@ func (k Keeper) SetBundleProposal(ctx sdk.Context, bundleProposal types.BundlePr

// GetBundleProposal returns the bundle proposal for the given pool with id `poolId`
func (k Keeper) GetBundleProposal(ctx sdk.Context, poolId uint64) (val types.BundleProposal, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.BundleKeyPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.BundleKeyPrefix)

b := store.Get(types.BundleProposalKey(poolId))
if b == nil {
Expand All @@ -43,7 +46,8 @@ func (k Keeper) GetBundleProposal(ctx sdk.Context, poolId uint64) (val types.Bun

// GetAllBundleProposals returns all bundle proposals of all pools
func (k Keeper) GetAllBundleProposals(ctx sdk.Context) (list []types.BundleProposal) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.BundleKeyPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.BundleKeyPrefix)
iterator := storeTypes.KVStorePrefixIterator(store, []byte{})

for ; iterator.Valid(); iterator.Next() {
Expand All @@ -61,7 +65,8 @@ func (k Keeper) GetAllBundleProposals(ctx sdk.Context) (list []types.BundlePropo

// SetFinalizedBundle stores a finalized bundle identified by its `poolId` and `id`.
func (k Keeper) SetFinalizedBundle(ctx sdk.Context, finalizedBundle types.FinalizedBundle) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FinalizedBundlePrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.FinalizedBundlePrefix)
b := k.cdc.MustMarshal(&finalizedBundle)
store.Set(types.FinalizedBundleKey(
finalizedBundle.PoolId,
Expand All @@ -74,14 +79,16 @@ func (k Keeper) SetFinalizedBundle(ctx sdk.Context, finalizedBundle types.Finali
// SetFinalizedBundleIndexes sets an in-memory reference for every bundle sorted by pool/fromIndex
// to allow querying for specific bundle ranges.
func (k Keeper) SetFinalizedBundleIndexes(ctx sdk.Context, finalizedBundle types.FinalizedBundle) {
indexByStorageIndex := prefix.NewStore(ctx.KVStore(k.memKey), types.FinalizedBundleByIndexPrefix)
storeAdapter := runtime.KVStoreAdapter(k.memService.OpenMemoryStore(ctx))
indexByStorageIndex := prefix.NewStore(storeAdapter, types.FinalizedBundleByIndexPrefix)
indexByStorageIndex.Set(
types.FinalizedBundleByIndexKey(finalizedBundle.PoolId, finalizedBundle.FromIndex),
util.GetByteKey(finalizedBundle.Id))
}

func (k Keeper) GetAllFinalizedBundles(ctx sdk.Context) (list []types.FinalizedBundle) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FinalizedBundlePrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.FinalizedBundlePrefix)
iterator := storeTypes.KVStorePrefixIterator(store, []byte{})

for ; iterator.Valid(); iterator.Next() {
Expand All @@ -94,7 +101,8 @@ func (k Keeper) GetAllFinalizedBundles(ctx sdk.Context) (list []types.FinalizedB
}

func (k Keeper) GetFinalizedBundlesByPool(ctx sdk.Context, poolId uint64) (list []types.FinalizedBundle) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FinalizedBundlePrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.FinalizedBundlePrefix)
iterator := storeTypes.KVStorePrefixIterator(store, util.GetByteKey(poolId))

for ; iterator.Valid(); iterator.Next() {
Expand All @@ -108,7 +116,8 @@ func (k Keeper) GetFinalizedBundlesByPool(ctx sdk.Context, poolId uint64) (list

// GetFinalizedBundle returns a finalized bundle by its identifier
func (k Keeper) GetFinalizedBundle(ctx sdk.Context, poolId, id uint64) (val types.FinalizedBundle, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FinalizedBundlePrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.FinalizedBundlePrefix)

b := store.Get(types.FinalizedBundleKey(poolId, id))
if b == nil {
Expand Down Expand Up @@ -182,7 +191,8 @@ func (k Keeper) GetPaginatedFinalizedBundleQuery(ctx sdk.Context, pagination *qu
}

// Init Bundles Store
store := prefix.NewStore(ctx.KVStore(k.storeKey), util.GetByteKey(types.FinalizedBundlePrefix, poolId))
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, util.GetByteKey(types.FinalizedBundlePrefix, poolId))

// Get latest bundle id by obtaining last item from the iterator
reverseIterator := store.ReverseIterator(nil, nil)
Expand Down Expand Up @@ -241,7 +251,8 @@ func (k Keeper) GetPaginatedFinalizedBundleQuery(ctx sdk.Context, pagination *qu
}

func (k Keeper) GetFinalizedBundleByIndex(ctx sdk.Context, poolId, index uint64) (val queryTypes.FinalizedBundle, found bool) {
proposalIndexStore := prefix.NewStore(ctx.KVStore(k.memKey), util.GetByteKey(types.FinalizedBundleByIndexPrefix, poolId))
storeAdapter := runtime.KVStoreAdapter(k.memService.OpenMemoryStore(ctx))
proposalIndexStore := prefix.NewStore(storeAdapter, util.GetByteKey(types.FinalizedBundleByIndexPrefix, poolId))
proposalIndexIterator := proposalIndexStore.ReverseIterator(nil, util.GetByteKey(index+1))
defer proposalIndexIterator.Close()

Expand All @@ -263,15 +274,17 @@ func (k Keeper) GetFinalizedBundleByIndex(ctx sdk.Context, poolId, index uint64)

// SetBundleVersionMap stores the bundle version map
func (k Keeper) SetBundleVersionMap(ctx sdk.Context, bundleVersionMap types.BundleVersionMap) {
store := ctx.KVStore(k.storeKey)
store := k.storeService.OpenKVStore(ctx)
b := k.cdc.MustMarshal(&bundleVersionMap)
store.Set(types.FinalizedBundleVersionMapKey, b)
// TODO: handle ignored error
_ = store.Set(types.FinalizedBundleVersionMapKey, b)
}

// GetBundleVersionMap returns the bundle version map
func (k Keeper) GetBundleVersionMap(ctx sdk.Context) (val types.BundleVersionMap) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.FinalizedBundleVersionMapKey)
store := k.storeService.OpenKVStore(ctx)
// TODO: handle ignored error
b, _ := store.Get(types.FinalizedBundleVersionMapKey)
if b == nil {
val.Versions = make([]*types.BundleVersionEntry, 0)
return val
Expand Down
5 changes: 3 additions & 2 deletions x/bundles/keeper/getters_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package keeper
import (
"cosmossdk.io/math"
"github.com/KYVENetwork/chain/x/bundles/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetParams returns the current x/bundles module parameters.
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
store := ctx.KVStore(k.storeKey)
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))

bz := store.Get(types.ParamsKey)
if bz == nil {
Expand Down Expand Up @@ -41,7 +42,7 @@ func (k Keeper) GetMaxPoints(ctx sdk.Context) (res uint64) {

// SetParams sets the x/bundles module parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
store := ctx.KVStore(k.storeKey)
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
bz := k.cdc.MustMarshal(&params)
store.Set(types.ParamsKey, bz)
}
11 changes: 8 additions & 3 deletions x/bundles/keeper/getters_round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import (
"cosmossdk.io/store/prefix"
storeTypes "cosmossdk.io/store/types"
"github.com/KYVENetwork/chain/x/bundles/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// SetRoundRobinProgress stores the round-robin progress for a pool
func (k Keeper) SetRoundRobinProgress(ctx sdk.Context, roundRobinProgress types.RoundRobinProgress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.RoundRobinProgressPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.RoundRobinProgressPrefix)

b := k.cdc.MustMarshal(&roundRobinProgress)
store.Set(types.RoundRobinProgressKey(roundRobinProgress.PoolId), b)
}

// GetRoundRobinProgress returns the round-robin progress for a pool
func (k Keeper) GetRoundRobinProgress(ctx sdk.Context, poolId uint64) (val types.RoundRobinProgress, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.RoundRobinProgressPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.RoundRobinProgressPrefix)

b := store.Get(types.RoundRobinProgressKey(poolId))
if b == nil {
Expand All @@ -29,7 +33,8 @@ func (k Keeper) GetRoundRobinProgress(ctx sdk.Context, poolId uint64) (val types

// GetAllRoundRobinProgress returns the round-robin progress of all pools
func (k Keeper) GetAllRoundRobinProgress(ctx sdk.Context) (list []types.RoundRobinProgress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.RoundRobinProgressPrefix)
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.RoundRobinProgressPrefix)
iterator := storeTypes.KVStorePrefixIterator(store, []byte{})

for ; iterator.Valid(); iterator.Next() {
Expand Down
26 changes: 14 additions & 12 deletions x/bundles/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package keeper

import (
"cosmossdk.io/core/store"
"cosmossdk.io/log"
"fmt"
"github.com/KYVENetwork/chain/util"

storetypes "cosmossdk.io/store/types"
"github.com/KYVENetwork/chain/x/bundles/types"
Expand All @@ -12,15 +14,15 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
logger log.Logger
cdc codec.BinaryCodec
storeService store.KVStoreService
memService store.MemoryStoreService
logger log.Logger

authority string

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
bankKeeper util.BankKeeper
distrkeeper types.DistrKeeper
poolKeeper types.PoolKeeper
stakerKeeper types.StakerKeeper
Expand All @@ -31,25 +33,25 @@ type (

func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
memKey storetypes.StoreKey,
storeService store.KVStoreService,
memService store.MemoryStoreService,
logger log.Logger,

authority string,

accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
bankKeeper util.BankKeeper,
distrkeeper types.DistrKeeper,
poolKeeper types.PoolKeeper,
stakerKeeper types.StakerKeeper,
delegationKeeper types.DelegationKeeper,
fundersKeeper types.FundersKeeper,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
logger: logger,
cdc: cdc,
storeService: storeService,
memService: memService,
logger: logger,

authority: authority,

Expand Down
31 changes: 15 additions & 16 deletions x/bundles/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package bundles
import (
"context"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"encoding/json"
"fmt"
"github.com/KYVENetwork/chain/util"
delegationKeeper "github.com/KYVENetwork/chain/x/delegation/keeper"
stakersKeeper "github.com/KYVENetwork/chain/x/stakers/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

upgradeKeeper "cosmossdk.io/x/upgrade/keeper"
poolKeeper "github.com/KYVENetwork/chain/x/pool/keeper"
teamKeeper "github.com/KYVENetwork/chain/x/team/keeper"
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
distributionKeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
mintKeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"

Expand Down Expand Up @@ -116,10 +115,10 @@ type AppModule struct {

keeper keeper.Keeper
accountKeeper types.AccountKeeper
bankKeeper bankKeeper.Keeper
bankKeeper util.BankKeeper
distributionKeeper distributionKeeper.Keeper
mintKeeper mintKeeper.Keeper
upgradeKeeper upgradeKeeper.Keeper
upgradeKeeper util.UpgradeKeeper
poolKeeper poolKeeper.Keeper
teamKeeper teamKeeper.Keeper
}
Expand All @@ -128,10 +127,10 @@ func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
accountKeeper types.AccountKeeper,
bankKeeper bankKeeper.Keeper,
bankKeeper util.BankKeeper,
distributionKeeper distributionKeeper.Keeper,
mintKeeper mintKeeper.Keeper,
upgradeKeeper upgradeKeeper.Keeper,
upgradeKeeper util.UpgradeKeeper,
poolKeeper poolKeeper.Keeper,
teamKeeper teamKeeper.Keeper,
) AppModule {
Expand Down Expand Up @@ -209,17 +208,17 @@ func init() {
type ModuleInputs struct {
depinject.In

Cdc codec.Codec
Config *modulev1.Module
StoreKey storetypes.StoreKey
MemKey storetypes.StoreKey
Logger log.Logger
Cdc codec.Codec
Config *modulev1.Module
StoreService store.KVStoreService
MemService store.MemoryStoreService
Logger log.Logger

AccountKeeper types.AccountKeeper
BankKeeper bankKeeper.Keeper
BankKeeper util.BankKeeper
DistributionKeeper distributionKeeper.Keeper
MintKeeper mintKeeper.Keeper
UpgradeKeeper upgradeKeeper.Keeper
UpgradeKeeper util.UpgradeKeeper
PoolKeeper poolKeeper.Keeper
TeamKeeper teamKeeper.Keeper
StakersKeeper stakersKeeper.Keeper
Expand All @@ -242,8 +241,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
}
k := keeper.NewKeeper(
in.Cdc,
in.StoreKey,
in.MemKey,
in.StoreService,
in.MemService,
in.Logger,
authority.String(),
in.AccountKeeper,
Expand Down
13 changes: 0 additions & 13 deletions x/bundles/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
"context"
"cosmossdk.io/math"
"cosmossdk.io/x/upgrade/types"
delegationTypes "github.com/KYVENetwork/chain/x/delegation/types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,18 +17,6 @@ type DistrKeeper interface {
FundCommunityPool(ctx context.Context, amount sdk.Coins, sender sdk.AccAddress) error
}

// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
SendCoins(ctx context.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
}

type UpgradeKeeper interface {
ScheduleUpgrade(ctx context.Context, plan types.Plan) error
}

type PoolKeeper interface {
AssertPoolExists(ctx sdk.Context, poolId uint64) error
GetPoolWithError(ctx sdk.Context, poolId uint64) (pooltypes.Pool, error)
Expand Down

0 comments on commit bd6740a

Please sign in to comment.