-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add update minCommissionRate to validators (#1379)
## Description This pull request contains the update staking minimum commission rate to all the validators whose validator commission rate below the minimum commission rate closes: #1082 --- ### Author Checklist _All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues._ I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] added appropriate labels to the PR - [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist _All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items._ I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
Showing
6 changed files
with
240 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package v3 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
type StakingKeeper interface { | ||
GetParams(ctx sdk.Context) types.Params | ||
SetParams(ctx sdk.Context, params types.Params) | ||
GetAllValidators(ctx sdk.Context) (validators []types.Validator) | ||
BeforeValidatorModified(ctx sdk.Context, valAddr sdk.ValAddress) error | ||
SetValidator(ctx sdk.Context, validator types.Validator) | ||
GetValidator(ctx sdk.Context, addr sdk.ValAddress) (types.Validator, bool) | ||
} |
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,42 @@ | ||
package v3 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var ( | ||
minCommissionRate = sdk.MustNewDecFromStr("0.05") | ||
) | ||
|
||
// UpdateMinimumCommissionRateParam is update the minimum commission rate param of staking. | ||
func UpdateMinimumCommissionRateParam(ctx sdk.Context, keeper StakingKeeper) (sdk.Dec, error) { | ||
params := keeper.GetParams(ctx) | ||
// update the minCommissionRate param | ||
params.MinCommissionRate = minCommissionRate | ||
|
||
keeper.SetParams(ctx, params) | ||
|
||
return minCommissionRate, nil | ||
} | ||
|
||
// SetMinimumCommissionRateToValidatros is update the minimum commission rate to the validators rate | ||
// whose commission rate is below the minimum commission rate. | ||
func SetMinimumCommissionRateToValidatros( | ||
ctx sdk.Context, keeper StakingKeeper, minCommissionRate sdk.Dec) error { | ||
validators := keeper.GetAllValidators(ctx) | ||
|
||
for _, validator := range validators { | ||
if validator.Commission.Rate.IsNil() || validator.Commission.Rate.LT(minCommissionRate) { | ||
if err := keeper.BeforeValidatorModified(ctx, validator.GetOperator()); err != nil { | ||
return err | ||
} | ||
|
||
validator.Commission.Rate = minCommissionRate | ||
validator.Commission.UpdateTime = ctx.BlockTime() | ||
|
||
keeper.SetValidator(ctx, validator) | ||
} | ||
} | ||
|
||
return 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,51 @@ | ||
package v3 | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
) | ||
|
||
func TestUpdateMinimumCommissionRateParam(t *testing.T) { | ||
ctx := sdk.NewContext(nil, tmproto.Header{}, false, nil) | ||
_, sk := GenerateRandomTestCase() | ||
|
||
// check default min commission rate | ||
oldParams := sk.GetParams(ctx) | ||
require.Equal(t, types.DefaultMinCommissionRate, oldParams.MinCommissionRate) | ||
|
||
// update the min commission rate | ||
_, err := UpdateMinimumCommissionRateParam(ctx, &sk) | ||
require.NoError(t, err) | ||
|
||
// get the updated params | ||
updatedParams := sk.GetParams(ctx) | ||
require.Equal(t, minCommissionRate, updatedParams.MinCommissionRate) | ||
} | ||
|
||
func TestSetMinimumCommissionRateToValidatros(t *testing.T) { | ||
ctx := sdk.NewContext(nil, tmproto.Header{}, false, nil) | ||
valAddrs, sk := GenerateRandomTestCase() | ||
|
||
// update the min commission rate | ||
minCommissionRate, err := UpdateMinimumCommissionRateParam(ctx, &sk) | ||
require.NoError(t, err) | ||
require.NotNil(t, minCommissionRate) | ||
|
||
// update min commisson rate to all validators | ||
err = SetMinimumCommissionRateToValidatros(ctx, &sk, minCommissionRate) | ||
require.NoError(t, err) | ||
|
||
// get the validator | ||
validator, found := sk.GetValidator(ctx, valAddrs[0]) | ||
require.True(t, found) | ||
require.True(t, minCommissionRate.Equal(validator.Commission.Rate)) | ||
|
||
validator2, found := sk.GetValidator(ctx, valAddrs[1]) | ||
require.True(t, found) | ||
// validator2 commission rate should be greater than minCommissionRate | ||
require.True(t, minCommissionRate.LT(validator2.Commission.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package v3 | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
var ( | ||
_ StakingKeeper = &MockStakingKeeper{} | ||
) | ||
|
||
// MockStakingKeeper imlements the StakingKeeper interface. | ||
type MockStakingKeeper struct { | ||
validators []types.Validator | ||
params types.Params | ||
} | ||
|
||
// GetValidator implements StakingKeeper | ||
func (m *MockStakingKeeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (types.Validator, bool) { | ||
var ( | ||
validator types.Validator | ||
found bool | ||
) | ||
|
||
for _, v := range m.validators { | ||
if v.GetOperator().Equals(addr) { | ||
found = true | ||
validator = v | ||
break | ||
} | ||
} | ||
|
||
return validator, found | ||
} | ||
|
||
// BeforeValidatorModified implements StakingKeeper | ||
func (*MockStakingKeeper) BeforeValidatorModified(ctx sdk.Context, valAddr sdk.ValAddress) error { | ||
return nil | ||
} | ||
|
||
// GetAllValidators implements StakingKeeper | ||
func (m *MockStakingKeeper) GetAllValidators(ctx sdk.Context) (validators []types.Validator) { | ||
return m.validators | ||
} | ||
|
||
// GetParams implements StakingKeeper | ||
func (m *MockStakingKeeper) GetParams(ctx sdk.Context) types.Params { | ||
return m.params | ||
} | ||
|
||
// SetParams implements StakingKeeper | ||
func (m *MockStakingKeeper) SetParams(ctx sdk.Context, params types.Params) { | ||
m.params = params | ||
} | ||
|
||
// SetValidator implements StakingKeeper | ||
func (m *MockStakingKeeper) SetValidator(ctx sdk.Context, validator types.Validator) { | ||
for index, v := range m.validators { | ||
if v.GetOperator().Equals(validator.GetOperator()) { | ||
m.validators[index] = validator | ||
break | ||
} | ||
} | ||
} | ||
|
||
// GenerateRandomTestCase | ||
func GenerateRandomTestCase() ([]sdk.ValAddress, MockStakingKeeper) { | ||
mockValidators := []types.Validator{} | ||
|
||
var valAddrs []sdk.ValAddress | ||
randNum, _ := rand.Int(rand.Reader, big.NewInt(10000)) | ||
numInputs := 10 + int((randNum.Int64() % 100)) | ||
for i := 0; i < numInputs; i++ { | ||
pubKey := secp256k1.GenPrivKey().PubKey() | ||
valValAddr := sdk.ValAddress(pubKey.Address()) | ||
mockValidator, _ := types.NewValidator(valValAddr, pubKey, types.Description{}) | ||
mockValidators = append(mockValidators, mockValidator) | ||
} | ||
|
||
// adding 0.01 to first validator | ||
val := mockValidators[0] | ||
val.Commission.Rate = sdk.MustNewDecFromStr("0.01") | ||
mockValidators[0] = val | ||
|
||
// adding more then minimumCommissionRate to validator 2 | ||
val = mockValidators[1] | ||
val.Commission.Rate = types.DefaultMinCommissionRate.Add(sdk.MustNewDecFromStr("1")) | ||
mockValidators[1] = val | ||
|
||
valAddrs = []sdk.ValAddress{mockValidators[0].GetOperator(), mockValidators[1].GetOperator()} | ||
|
||
stakingKeeper := NewMockStakingKeeper(mockValidators) | ||
|
||
return valAddrs, stakingKeeper | ||
} | ||
|
||
func NewMockStakingKeeper(validatros []types.Validator) MockStakingKeeper { | ||
return MockStakingKeeper{ | ||
validators: validatros, | ||
params: types.DefaultParams(), | ||
} | ||
} |