crypto-com / chain-indexing

Other
35 stars 28 forks source link

Problem: One-off validator minimum commission update on upgrade are not applied #824

Open calvinaco opened 1 year ago

calvinaco commented 1 year ago

Problem On Crypto.org Chain v4 upgrade, there's a one-off minimum commission requirements applied to all the validators. These updates do not emit any events, which complicates how we could capture them.

app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
        // the minimal commission rate of 5% (0.05)
        // (default is needed to be set because of SDK store migrations that set the param)
        stakingtypes.DefaultMinCommissionRate = sdk.NewDecWithPrec(5, 2)

        stakingKeeper.IterateValidators(ctx, func(index int64, val stakingtypes.ValidatorI) (stop bool) {
            if val.GetCommission().LT(stakingtypes.DefaultMinCommissionRate) {
                validator, found := stakingKeeper.GetValidator(ctx, val.GetOperator())
                if !found {
                    ctx.Logger().Error("validator not found", val)
                    return true
                }
                ctx.Logger().Info("update validator's commission rate to a minimal one", val)
                validator.Commission.Rate = stakingtypes.DefaultMinCommissionRate
                if validator.Commission.MaxRate.LT(stakingtypes.DefaultMinCommissionRate) {
                    validator.Commission.MaxRate = stakingtypes.DefaultMinCommissionRate
                }
                stakingKeeper.SetValidator(ctx, validator)
            }
            return false
        })

Code ref: https://github.com/crypto-org-chain/chain-main/blob/v4.2.2/app/app.go#L702