Skip to content

Commit

Permalink
fix(staking): validate MaxRate value of the commission
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Sep 12, 2023
1 parent 801d6e6 commit e28f388
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
57 changes: 34 additions & 23 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,24 @@ func NewApp(
app.BlockedAddrs(),
)

skeeper := stakingkeeper.NewKeeper(
appCodec,
app.keys[stakingtypes.StoreKey],
app.Keepers.Cosmos.Acct,
app.Keepers.Cosmos.Bank,
app.GetSubspace(stakingtypes.ModuleName),
)
// allocation of staking keeper is scoped deliberately,
// so it's being referenced by pointer within modules that need it
{
skeeper := stakingkeeper.NewKeeper(
appCodec,
app.keys[stakingtypes.StoreKey],
app.Keepers.Cosmos.Acct,
app.Keepers.Cosmos.Bank,
app.GetSubspace(stakingtypes.ModuleName),
)
app.Keepers.Cosmos.Staking = &skeeper
}

app.Keepers.Cosmos.Mint = mintkeeper.NewKeeper(
appCodec,
app.keys[minttypes.StoreKey],
app.GetSubspace(minttypes.ModuleName),
&skeeper,
app.Keepers.Cosmos.Staking,
app.Keepers.Cosmos.Acct,
app.Keepers.Cosmos.Bank,
authtypes.FeeCollectorName,
Expand All @@ -243,34 +248,40 @@ func NewApp(
app.GetSubspace(distrtypes.ModuleName),
app.Keepers.Cosmos.Acct,
app.Keepers.Cosmos.Bank,
&skeeper,
app.Keepers.Cosmos.Staking,
authtypes.FeeCollectorName,
app.ModuleAccountAddrs(),
)

app.Keepers.Cosmos.Slashing = slashingkeeper.NewKeeper(
appCodec,
app.keys[slashingtypes.StoreKey],
&skeeper,
app.Keepers.Cosmos.Staking,
app.GetSubspace(slashingtypes.ModuleName),
)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.Keepers.Cosmos.Staking.SetHooks(
stakingtypes.NewMultiStakingHooks(
app.Keepers.Cosmos.Distr.Hooks(),
app.Keepers.Cosmos.Slashing.Hooks(),
),
)

app.Keepers.Cosmos.Crisis = crisiskeeper.NewKeeper(
app.GetSubspace(crisistypes.ModuleName),
invCheckPeriod,
app.Keepers.Cosmos.Bank,
authtypes.FeeCollectorName,
)

app.Keepers.Cosmos.Upgrade = upgradekeeper.NewKeeper(skipUpgradeHeights, app.keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp)

// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.Keepers.Cosmos.Staking = *skeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(
app.Keepers.Cosmos.Distr.Hooks(),
app.Keepers.Cosmos.Slashing.Hooks(),
),
app.Keepers.Cosmos.Upgrade = upgradekeeper.NewKeeper(
skipUpgradeHeights,
app.keys[upgradetypes.StoreKey],
appCodec,
homePath,
app.BaseApp,
)

// register IBC Keeper
Expand Down Expand Up @@ -309,7 +320,7 @@ func NewApp(
app.GetSubspace(govtypes.ModuleName),
app.Keepers.Cosmos.Acct,
app.Keepers.Cosmos.Bank,
&skeeper,
app.Keepers.Cosmos.Staking,
govRouter,
)

Expand Down Expand Up @@ -339,7 +350,7 @@ func NewApp(
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec,
app.keys[evidencetypes.StoreKey],
&app.Keepers.Cosmos.Staking,
app.Keepers.Cosmos.Staking,
app.Keepers.Cosmos.Slashing,
)

Expand Down Expand Up @@ -368,7 +379,7 @@ func NewApp(
// mint.NewAppModule(appCodec, app.Keepers.Cosmos.Mint, app.Keepers.Cosmos.Acct, nil),
slashing.NewAppModule(appCodec, app.Keepers.Cosmos.Slashing, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank, app.Keepers.Cosmos.Staking),
distr.NewAppModule(appCodec, app.Keepers.Cosmos.Distr, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank, app.Keepers.Cosmos.Staking),
staking.NewAppModule(appCodec, app.Keepers.Cosmos.Staking, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank),
staking.NewAppModule(appCodec, *app.Keepers.Cosmos.Staking, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank),
upgrade.NewAppModule(app.Keepers.Cosmos.Upgrade),
evidence.NewAppModule(app.Keepers.Cosmos.Evidence),
ibc.NewAppModule(app.Keepers.Cosmos.IBC),
Expand Down Expand Up @@ -414,7 +425,7 @@ func NewApp(
mint.NewAppModule(appCodec, app.Keepers.Cosmos.Mint, app.Keepers.Cosmos.Acct),
// todo akash-network/support#4
// mint.NewAppModule(appCodec, app.Keepers.Cosmos.Mint, app.Keepers.Cosmos.Acct, nil),
staking.NewAppModule(appCodec, app.Keepers.Cosmos.Staking, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank),
staking.NewAppModule(appCodec, *app.Keepers.Cosmos.Staking, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank),
distr.NewAppModule(appCodec, app.Keepers.Cosmos.Distr, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank, app.Keepers.Cosmos.Staking),
slashing.NewAppModule(appCodec, app.Keepers.Cosmos.Slashing, app.Keepers.Cosmos.Acct, app.Keepers.Cosmos.Bank, app.Keepers.Cosmos.Staking),
params.NewAppModule(app.Keepers.Cosmos.Params),
Expand Down
7 changes: 6 additions & 1 deletion app/decorators/min_commision.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ func (min *MinCommissionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula

func (min *MinCommissionDecorator) isValidMsg(ctx sdk.Context, m sdk.Msg) error {
var rate sdk.Dec
var maxRate *sdk.Dec

switch msg := m.(type) {
case *stakingtypes.MsgCreateValidator:

maxRate = &msg.Commission.MaxRate
rate = msg.Commission.Rate
case *stakingtypes.MsgEditValidator:
// if commission rate is nil, it means only
Expand All @@ -62,6 +63,10 @@ func (min *MinCommissionDecorator) isValidMsg(ctx sdk.Context, m sdk.Msg) error
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, fmt.Sprintf("commission can't be lower than %s%%", minRate))
}

if maxRate != nil && maxRate.LT(minRate) {
return sdkerrors.Wrap(sdkerrors.ErrUnauthorized, fmt.Sprintf("commission max rate can't be lower than %s%%", minRate))
}

Check warning on line 68 in app/decorators/min_commision.go

View check run for this annotation

Codecov / codecov/patch

app/decorators/min_commision.go#L67-L68

Added lines #L67 - L68 were not covered by tests

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (app *AkashApp) ExportAppStateAndValidators(
return servertypes.ExportedApp{}, err
}

validators, err := staking.WriteValidators(ctx, app.Keepers.Cosmos.Staking)
validators, err := staking.WriteValidators(ctx, *app.Keepers.Cosmos.Staking)
if err != nil {
return servertypes.ExportedApp{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion app/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type AppKeepers struct {
FeeGrant feegrantkeeper.Keeper
Bank bankkeeper.Keeper
Cap *capabilitykeeper.Keeper
Staking stakingkeeper.Keeper
Staking *stakingkeeper.Keeper
Slashing slashingkeeper.Keeper
Mint mintkeeper.Keeper
Distr distrkeeper.Keeper
Expand Down

0 comments on commit e28f388

Please sign in to comment.