Skip to content

Commit

Permalink
Change wiring for mint and gov to use ProviderKeeper instead of Staki…
Browse files Browse the repository at this point in the history
…ngKeeper
  • Loading branch information
p-offtermatt committed Jul 17, 2024
1 parent 51ad903 commit a0cedbe
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 23 deletions.
52 changes: 29 additions & 23 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,6 @@ func New(
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)
app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[minttypes.StoreKey]),
app.StakingKeeper,
app.AccountKeeper,
app.BankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.DistrKeeper = distrkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[distrtypes.StoreKey]),
Expand Down Expand Up @@ -456,19 +447,6 @@ func New(
runtime.ProvideCometInfoService(),
)

govConfig := govtypes.DefaultConfig()
app.GovKeeper = govkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[govtypes.StoreKey]),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
app.DistrKeeper,
app.MsgServiceRouter(),
govConfig,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

app.ProviderKeeper = ibcproviderkeeper.NewKeeper(
appCodec,
keys[providertypes.StoreKey],
Expand All @@ -483,13 +461,41 @@ func New(
app.AccountKeeper,
app.DistrKeeper,
app.BankKeeper,
*app.GovKeeper,
govkeeper.Keeper{}, // will be set after the GovKeeper is created
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
authtypes.FeeCollectorName,
)

govConfig := govtypes.DefaultConfig()
app.GovKeeper = govkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[govtypes.StoreKey]),
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
app.DistrKeeper,
app.MsgServiceRouter(),
govConfig,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// set the GovKeeper in the ProviderKeeper
app.ProviderKeeper.SetGovKeeper(*app.GovKeeper)

app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[minttypes.StoreKey]),
// use the ProviderKeeper as StakingKeeper for mint
// because minting should be based on the consensus-active validators
app.ProviderKeeper,
app.AccountKeeper,
app.BankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

// gov router must be set after the provider keeper is created
// otherwise the provider keeper will not be able to handle proposals (will be nil)
govRouter := govv1beta1.NewRouter()
Expand Down
73 changes: 73 additions & 0 deletions testutil/keeper/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func (k Keeper) mustValidateFields() {
// ccv.PanicIfZeroOrNil(k.govKeeper, "govKeeper") // 17
}

func (k *Keeper) SetGovKeeper(govKeeper govkeeper.Keeper) {
k.govKeeper = govKeeper
}

// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx context.Context) log.Logger {
sdkCtx := sdk.UnwrapSDKContext(ctx)
Expand Down
81 changes: 81 additions & 0 deletions x/ccv/provider/keeper/staking_keeper_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package keeper

import (
"context"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// Iterates over the consensus-active validators by power.
// The same as IterateBondedValidatorsByPower in the StakingKeeper,
// but only returns the first MaxProviderConsensusValidators validators.
// This is used to implement the interface of the staking keeper to interface with
// modules that need to reference the consensus-active validators.
func (k Keeper) IterateBondedValidatorsByPower(ctx context.Context, fn func(index int64, validator stakingtypes.ValidatorI) (stop bool)) error {
maxProviderConsensusVals := k.GetMaxProviderConsensusValidators(sdk.UnwrapSDKContext(ctx))
counter := int64(0)
return k.stakingKeeper.IterateBondedValidatorsByPower(ctx, func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
if counter >= maxProviderConsensusVals {
return true
}
counter++
return fn(index, validator)
})
}

// Gets the amount of bonded tokens, which is equal
// to the amount of tokens of the consensus-active validators.
// The same as TotalBondedTokens, but only counts
// tokens of the first MaxProviderConsensusValidators validators.
// This is used to implement the interface of the staking keeper to interface with
// modules that need to reference the consensus-active validators.
func (k Keeper) TotalBondedTokens(ctx context.Context) (math.Int, error) {
// iterate through the bonded validators
totalBondedTokens := math.ZeroInt()

k.IterateBondedValidatorsByPower(ctx, func(_ int64, validator stakingtypes.ValidatorI) (stop bool) {
tokens := validator.GetTokens()
totalBondedTokens = totalBondedTokens.Add(tokens)
return false
})

return totalBondedTokens, nil
}

// The same as IterateDelegations in the StakingKeeper.
// Necessary to implement the interface of the staking keeper to interface with
// other modules.
func (k Keeper) IterateDelegations(ctx context.Context, delegator sdk.AccAddress, fn func(index int64, delegation stakingtypes.DelegationI) (stop bool)) error {
return k.stakingKeeper.IterateDelegations(ctx, delegator, fn)
}

// The same as StakingTotalSupply in the StakingKeeper.
// Necessary to implement the interface of the staking keeper to interface with
// other modules.
func (k Keeper) StakingTokenSupply(ctx context.Context) (math.Int, error) {
return k.stakingKeeper.StakingTokenSupply(ctx)
}

// Gets the ratio of tokens staked to validators active in the consensus
// to the total supply of tokens.
// Same as BondedRatio in the StakingKeeper, but only counts
// tokens of the first MaxProviderConsensusValidators bonded validators.
func (k Keeper) BondedRatio(ctx context.Context) (math.LegacyDec, error) {
totalSupply, err := k.StakingTokenSupply(ctx)
if err != nil {
return math.LegacyZeroDec(), err
}

bondedTokens, err := k.TotalBondedTokens(ctx)
if err != nil {
return math.LegacyZeroDec(), err
}

if !totalSupply.IsPositive() {
return math.LegacyZeroDec(), nil
}

return math.LegacyNewDecFromInt(bondedTokens).QuoInt(totalSupply), nil
}
11 changes: 11 additions & 0 deletions x/ccv/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
context "context"
"time"

addresscodec "cosmossdk.io/core/address"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
Expand Down Expand Up @@ -57,6 +58,16 @@ type StakingKeeper interface {
GetRedelegationByUnbondingID(ctx context.Context, id uint64) (stakingtypes.Redelegation, error)
GetValidatorByUnbondingID(ctx context.Context, id uint64) (stakingtypes.Validator, error)
GetBondedValidatorsByPower(ctx context.Context) ([]stakingtypes.Validator, error)
ValidatorAddressCodec() addresscodec.Codec
IterateDelegations(
ctx context.Context, delegator sdk.AccAddress,
fn func(index int64, delegation stakingtypes.DelegationI) (stop bool),
) error
IterateBondedValidatorsByPower(
context.Context, func(index int64, validator stakingtypes.ValidatorI) (stop bool),
) error
StakingTokenSupply(ctx context.Context) (math.Int, error)
BondedRatio(ctx context.Context) (math.LegacyDec, error)
}

// SlashingKeeper defines the contract expected to perform ccv slashing
Expand Down

0 comments on commit a0cedbe

Please sign in to comment.