Skip to content

Commit

Permalink
Update validator min commission in upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed Oct 26, 2023
1 parent 3784791 commit 8c09d6d
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
64 changes: 64 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

communitytypes "github.com/kava-labs/kava/x/community/types"
Expand Down Expand Up @@ -52,6 +53,9 @@ var (
sdkmath.LegacyNewDec(0), // stakingRewardsPerSecond
sdkmath.LegacyNewDec(1000), // upgradeTimeSetstakingRewardsPerSecond
)

// ValidatorMinimumCommission is the new 5% minimum commission rate for validators
ValidatorMinimumCommission = sdk.NewDecWithPrec(5, 2)
)

// RegisterUpgradeHandlers registers the upgrade handlers for the app.
Expand Down Expand Up @@ -109,6 +113,8 @@ func upgradeHandler(
return toVM, err
}

UpdateValidatorMinimumCommission(ctx, app)

app.communityKeeper.SetParams(ctx, communityParams)
app.Logger().Info(
"initialized x/community params",
Expand All @@ -120,3 +126,61 @@ func upgradeHandler(
return toVM, nil
}
}

// UpdateValidatorMinimumCommission updates the commission rate for all
// validators to be at least the new min commission rate, and sets the minimum
// commission rate in the staking params.
func UpdateValidatorMinimumCommission(
ctx sdk.Context,
app App,
) {
resultCount := make(map[stakingtypes.BondStatus]int)

// Iterate over *all* validators including inactive
app.stakingKeeper.IterateValidators(
ctx,
func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
// Skip if validator commission is already >= 5%
if validator.GetCommission().GTE(ValidatorMinimumCommission) {
return false
}

val, ok := validator.(stakingtypes.Validator)
if !ok {
panic("expected stakingtypes.Validator")
}

// Set minimum commission rate to 5%, when commission is < 5%
val.Commission.Rate = ValidatorMinimumCommission
app.stakingKeeper.SetValidator(ctx, val)

// Keep track of counts just for logging purposes
switch val.GetStatus() {
case stakingtypes.Bonded:
resultCount[stakingtypes.Bonded]++
case stakingtypes.Unbonded:
resultCount[stakingtypes.Unbonded]++
case stakingtypes.Unbonding:
resultCount[stakingtypes.Unbonding]++
}

return false
},
)

app.Logger().Info(
"updated validator minimum commission rate for all existing validators",
stakingtypes.BondStatusBonded, resultCount[stakingtypes.Bonded],
stakingtypes.BondStatusUnbonded, resultCount[stakingtypes.Unbonded],
stakingtypes.BondStatusUnbonding, resultCount[stakingtypes.Unbonding],
)

stakingParams := app.stakingKeeper.GetParams(ctx)
stakingParams.MinCommissionRate = ValidatorMinimumCommission
app.stakingKeeper.SetParams(ctx, stakingParams)

app.Logger().Info(
"updated x/staking params minimum commission rate",
"MinCommissionRate", stakingParams.MinCommissionRate,
)
}
104 changes: 104 additions & 0 deletions app/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import (
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/kava-labs/kava/app"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)

func TestUpgradeCommunityParams_Mainnet(t *testing.T) {
Expand Down Expand Up @@ -39,3 +44,102 @@ func TestUpgradeCommunityParams_Testnet(t *testing.T) {
"testnet kava per second should be correct",
)
}

func TestUpdateValidatorMinimumCommission(t *testing.T) {
tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})

sk := tApp.GetStakingKeeper()
stakingParams := sk.GetParams(ctx)
stakingParams.MinCommissionRate = sdk.ZeroDec()
sk.SetParams(ctx, stakingParams)

// Set some validators with varying commission rates

vals := []struct {
operatorAddr sdk.ValAddress
consPriv *ethsecp256k1.PrivKey
commissionRate sdk.Dec
}{
{
operatorAddr: sdk.ValAddress("val0"),
consPriv: generateConsKey(t),
commissionRate: sdk.ZeroDec(),
},
{
operatorAddr: sdk.ValAddress("val1"),
consPriv: generateConsKey(t),
commissionRate: sdk.MustNewDecFromStr("0.01"),
},
{
operatorAddr: sdk.ValAddress("val2"),
consPriv: generateConsKey(t),
commissionRate: sdk.MustNewDecFromStr("0.05"),
},
{
operatorAddr: sdk.ValAddress("val3"),
consPriv: generateConsKey(t),
commissionRate: sdk.MustNewDecFromStr("0.06"),
},
{
operatorAddr: sdk.ValAddress("val4"),
consPriv: generateConsKey(t),
commissionRate: sdk.MustNewDecFromStr("0.5"),
},
}

for _, v := range vals {
val, err := stakingtypes.NewValidator(
v.operatorAddr,
v.consPriv.PubKey(),
stakingtypes.Description{},
)
require.NoError(t, err)
val.Commission.Rate = v.commissionRate

err = sk.SetValidatorByConsAddr(ctx, val)
require.NoError(t, err)
sk.SetValidator(ctx, val)
}

require.NotPanics(
t, func() {
app.UpdateValidatorMinimumCommission(ctx, tApp.App)
},
)

stakingParamsAfter := sk.GetParams(ctx)
require.Equal(t, stakingParamsAfter.MinCommissionRate, app.ValidatorMinimumCommission)

// Check that all validators have a commission rate >= 5%
count := 0
sk.IterateValidators(ctx, func(index int64, validator stakingtypes.ValidatorI) (stop bool) {
require.True(
t,
validator.GetCommission().GTE(app.ValidatorMinimumCommission),
"commission rate should be >= 5%",
)

count++
return false
})

require.Equal(
t,
len(vals)+1, // InitializeFromGenesisStates adds a validator
count,
"validator count should match test validators",
)
}

func generateConsKey(
t *testing.T,
) *ethsecp256k1.PrivKey {
t.Helper()

key, err := ethsecp256k1.GenerateKey()
require.NoError(t, err)

return key
}

0 comments on commit 8c09d6d

Please sign in to comment.