Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(staking): validate MaxRate value of the commission #1885

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 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,9 +425,9 @@ 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),
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),
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),
evidence.NewAppModule(app.Keepers.Cosmos.Evidence),
ibc.NewAppModule(app.Keepers.Cosmos.IBC),
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) 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 @@
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/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, err, "simulation setup failed")

defer func() {
db.Close()
_ = db.Close()
require.NoError(t, os.RemoveAll(dir))
}()

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