forked from realiotech/realio-network
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write upgrade handler for new commission rate
- Loading branch information
Showing
11 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package commission | ||
|
||
const ( | ||
// UpgradeName defines the on-chain upgrade name. | ||
UpgradeName = "Commission" | ||
NewMinCommisionRate = "0.05" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package commission | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
func CreateUpgradeHandler( | ||
mm *module.Manager, | ||
configurator module.Configurator, | ||
sk *stakingkeeper.Keeper, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
ctx.Logger().Info("Starting upgrade for multi staking...") | ||
fixMinCommisionRate(ctx, sk) | ||
return mm.RunMigrations(ctx, configurator, vm) | ||
} | ||
} | ||
|
||
func fixMinCommisionRate(ctx sdk.Context, staking *stakingkeeper.Keeper) { | ||
// Upgrade every validators min-commission rate | ||
validators := staking.GetAllValidators(ctx) | ||
minComm := sdk.MustNewDecFromStr(NewMinCommisionRate) | ||
params := staking.GetParams(ctx) | ||
params.MinCommissionRate = minComm | ||
|
||
staking.SetParams(ctx, params) | ||
|
||
for _, v := range validators { | ||
//nolint | ||
if v.Commission.Rate.LT(minComm) { | ||
comm, err := updateValidatorCommission(ctx, staking, v, minComm) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
v.Commission = comm | ||
|
||
// call the before-modification hook since we're about to update the commission | ||
staking.BeforeValidatorModified(ctx, v.GetOperator()) | ||
staking.SetValidator(ctx, v) | ||
} | ||
} | ||
} | ||
|
||
func updateValidatorCommission(ctx sdk.Context, staking *stakingkeeper.Keeper, | ||
validator stakingtypes.Validator, newRate sdk.Dec, | ||
) (stakingtypes.Commission, error) { | ||
commission := validator.Commission | ||
blockTime := ctx.BlockHeader().Time | ||
|
||
if newRate.LT(staking.MinCommissionRate(ctx)) { | ||
return commission, fmt.Errorf("cannot set validator commission to less than minimum rate of %s", staking.MinCommissionRate(ctx)) | ||
} | ||
|
||
commission.Rate = newRate | ||
if commission.MaxRate.LT(newRate) { | ||
commission.MaxRate = newRate | ||
} | ||
|
||
commission.UpdateTime = blockTime | ||
|
||
return commission, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package app | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
"github.com/realiotech/realio-network/app/upgrades/commission" | ||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
) | ||
|
||
func TestCommissionUpgrade(t *testing.T) { | ||
app := Setup(false, nil, 4) | ||
ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: app.LastBlockHeight() + 1}) | ||
validators := app.StakingKeeper.GetAllValidators(ctx) | ||
|
||
comm0 := stakingtypes.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.01"), | ||
MaxRate: sdk.MustNewDecFromStr("0.01"), | ||
MaxChangeRate: sdk.MustNewDecFromStr("0.02"), | ||
} | ||
comm1 := stakingtypes.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.02"), | ||
MaxRate: sdk.MustNewDecFromStr("0.03"), | ||
MaxChangeRate: sdk.MustNewDecFromStr("0.02"), | ||
} | ||
comm2 := stakingtypes.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.06"), | ||
MaxRate: sdk.MustNewDecFromStr("0.07"), | ||
MaxChangeRate: sdk.MustNewDecFromStr("0.02"), | ||
} | ||
comm3 := stakingtypes.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.1"), | ||
MaxRate: sdk.MustNewDecFromStr("0.2"), | ||
MaxChangeRate: sdk.MustNewDecFromStr("0.1"), | ||
} | ||
|
||
validators[0].Commission.CommissionRates = comm0 | ||
validators[1].Commission.CommissionRates = comm1 | ||
validators[2].Commission.CommissionRates = comm2 | ||
validators[3].Commission.CommissionRates = comm3 | ||
|
||
app.StakingKeeper.SetValidator(ctx, validators[0]) | ||
app.StakingKeeper.SetValidator(ctx, validators[1]) | ||
app.StakingKeeper.SetValidator(ctx, validators[2]) | ||
app.StakingKeeper.SetValidator(ctx, validators[3]) | ||
|
||
upgradePlan := upgradetypes.Plan{ | ||
Name: commission.UpgradeName, | ||
Height: ctx.BlockHeight(), | ||
} | ||
err := app.UpgradeKeeper.ScheduleUpgrade(ctx, upgradePlan) | ||
|
||
require.NoError(t, err) | ||
ctx = ctx.WithBlockHeader(tmproto.Header{Time: time.Now()}) | ||
app.UpgradeKeeper.ApplyUpgrade(ctx, upgradePlan) | ||
|
||
validatorsAfter := app.StakingKeeper.GetAllValidators(ctx) | ||
|
||
upgradeMinCommRate := sdk.MustNewDecFromStr(commission.NewMinCommisionRate) | ||
require.Equal(t, validatorsAfter[0].Commission.CommissionRates.Rate, upgradeMinCommRate) | ||
require.Equal(t, validatorsAfter[1].Commission.CommissionRates.Rate, upgradeMinCommRate) | ||
require.Equal(t, validatorsAfter[0].Commission.CommissionRates.MaxRate, upgradeMinCommRate) | ||
require.Equal(t, validatorsAfter[1].Commission.CommissionRates.MaxRate, upgradeMinCommRate) | ||
require.Equal(t, validatorsAfter[2].Commission.CommissionRates.Rate, validators[2].Commission.CommissionRates.Rate) | ||
require.Equal(t, validatorsAfter[3].Commission.CommissionRates.Rate, validators[3].Commission.CommissionRates.Rate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters