Skip to content

Commit

Permalink
fix(upgrade): use custom commission validator during v0.24.0 upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Jul 27, 2023
1 parent 16e686c commit 5073884
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion upgrades/software/v0.24.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ package v0_24_0

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/feegrant"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
Expand Down Expand Up @@ -117,7 +120,7 @@ func (up *upgrade) enforceMinValidatorCommission(ctx sdk.Context) error {
// set max change rate temporarily to 100%
maxRateCh := validator.Commission.MaxChangeRate
validator.Commission.MaxChangeRate = sdk.NewDecWithPrec(1, 0)
if _, err := up.Keepers.Cosmos.Staking.UpdateValidatorCommission(ctx, validator, minRate); err != nil {
if _, err := updateValidatorCommission(ctx, validator, minRate); err != nil {
return err
}

Expand All @@ -131,6 +134,47 @@ func (up *upgrade) enforceMinValidatorCommission(ctx sdk.Context) error {
return nil
}

// updateValidatorCommission use custom implementation of update commission,
// this prevents panic during upgrade if any of validators have changed their
// commission within 24h of upgrade height
func updateValidatorCommission(
ctx sdk.Context,
validator types.Validator,
newRate sdk.Dec,
) (types.Commission, error) {
commission := validator.Commission
blockTime := ctx.BlockHeader().Time

if err := validateNewRate(commission, newRate, blockTime); err != nil {
return commission, err
}

commission.Rate = newRate
commission.UpdateTime = blockTime

return commission, nil
}

// validateNewRate performs basic sanity validation checks of a new commission
// rate. If validation fails, an SDK error is returned.
func validateNewRate(commission stakingtypes.Commission, newRate sdk.Dec, blockTime time.Time) error {
switch {
case newRate.IsNegative():
// new rate cannot be negative
return stakingtypes.ErrCommissionNegative

case newRate.GT(commission.MaxRate):
// new rate cannot be greater than the max rate
return stakingtypes.ErrCommissionGTMaxRate

case newRate.Sub(commission.Rate).GT(commission.MaxChangeRate):
// new rate % points change cannot be greater than the max change rate
return stakingtypes.ErrCommissionGTMaxChangeRate
}

return nil
}

func (up *upgrade) patchDanglingEscrowPayments(ctx sdk.Context) {
up.Keepers.Akash.Escrow.WithPayments(ctx, func(payment v1beta3.FractionalPayment) bool {
acc, _ := up.Keepers.Akash.Escrow.GetAccount(ctx, payment.AccountID)
Expand Down

0 comments on commit 5073884

Please sign in to comment.