forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add MinCommissionRate param (#3)
* add MinCommissionRate param * migrate store * expose migrate13to14 * fix migrate * set default MinCommissionRate to zero * update rosetta data
- Loading branch information
1 parent
3e328f6
commit 39362c4
Showing
18 changed files
with
1,032 additions
and
723 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
Binary file not shown.
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
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,49 @@ | ||
package v045 | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
// MinCommissionRate is set to 5% | ||
var MinCommissionRate = sdk.NewDecWithPrec(5, 2) | ||
|
||
// Migrate performs in-place store migrations from v0.45.13 to v0.45.14. | ||
// The migration includes: | ||
// | ||
// - Adding MinCommissionRate param | ||
// - Setting validaotr commission rate and max commission rate to MinCommissionRate if they are lower | ||
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, paramstore paramtypes.Subspace) { | ||
migrateParamsStore(ctx, paramstore) | ||
migrateValidators(ctx, storeKey, cdc) | ||
} | ||
|
||
func migrateParamsStore(ctx sdk.Context, paramstore paramtypes.Subspace) { | ||
if paramstore.HasKeyTable() { | ||
paramstore.Set(ctx, types.KeyMinCommissionRate, MinCommissionRate) | ||
} else { | ||
paramstore.WithKeyTable(types.ParamKeyTable()) | ||
paramstore.Set(ctx, types.KeyMinCommissionRate, MinCommissionRate) | ||
} | ||
} | ||
|
||
func migrateValidators(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { | ||
store := ctx.KVStore(storeKey) | ||
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
validator := types.MustUnmarshalValidator(cdc, iterator.Value()) | ||
if validator.Commission.CommissionRates.Rate.LT(MinCommissionRate) { | ||
validator.Commission.CommissionRates.Rate = MinCommissionRate | ||
} | ||
|
||
if validator.Commission.CommissionRates.MaxRate.LT(MinCommissionRate) { | ||
validator.Commission.CommissionRates.MaxRate = MinCommissionRate | ||
} | ||
store.Set(iterator.Key(), types.MustMarshalValidator(cdc, &validator)) | ||
} | ||
} |
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,120 @@ | ||
package v045_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" | ||
v045staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v045" | ||
"github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func TestMigrate(t *testing.T) { | ||
encCfg := simapp.MakeTestEncodingConfig() | ||
stakingKey := sdk.NewKVStoreKey("staking") | ||
tStakingKey := sdk.NewTransientStoreKey("transient_test") | ||
ctx := testutil.DefaultContext(stakingKey, tStakingKey) | ||
paramstore := paramtypes.NewSubspace(encCfg.Marshaler, encCfg.Amino, stakingKey, tStakingKey, "staking") | ||
|
||
testCases := []struct { | ||
OldValidator types.Validator | ||
NewValidator types.Validator | ||
}{ | ||
{ | ||
OldValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x00}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.01"), | ||
MaxRate: sdk.MustNewDecFromStr("0.02"), | ||
}, | ||
}, | ||
}, | ||
NewValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x00}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.05"), | ||
MaxRate: sdk.MustNewDecFromStr("0.05"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
OldValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x01}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.05"), | ||
MaxRate: sdk.MustNewDecFromStr("0.05"), | ||
}, | ||
}, | ||
}, | ||
NewValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x01}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.05"), | ||
MaxRate: sdk.MustNewDecFromStr("0.05"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
OldValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x02}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.1"), | ||
MaxRate: sdk.MustNewDecFromStr("0.2"), | ||
}, | ||
}, | ||
}, | ||
NewValidator: types.Validator{ | ||
OperatorAddress: sdk.ValAddress{0x02}.String(), | ||
Commission: types.Commission{ | ||
CommissionRates: types.CommissionRates{ | ||
Rate: sdk.MustNewDecFromStr("0.1"), | ||
MaxRate: sdk.MustNewDecFromStr("0.2"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
store := ctx.KVStore(stakingKey) | ||
for _, vs := range testCases { | ||
bz := types.MustMarshalValidator(encCfg.Marshaler, &vs.OldValidator) | ||
store.Set(types.GetValidatorKey(vs.OldValidator.GetOperator()), bz) | ||
} | ||
|
||
// Check no params | ||
require.False(t, paramstore.Has(ctx, types.KeyMinCommissionRate)) | ||
|
||
// Run migrations. | ||
v045staking.MigrateStore(ctx, stakingKey, encCfg.Marshaler, paramstore) | ||
|
||
// Make sure the new params are set. | ||
require.True(t, paramstore.Has(ctx, types.KeyMinCommissionRate)) | ||
|
||
minCommissionRate := sdk.Dec{} | ||
paramstore.Get(ctx, types.KeyMinCommissionRate, &minCommissionRate) | ||
require.Equal(t, v045staking.MinCommissionRate, minCommissionRate) | ||
|
||
// Make sure the validators commission is set correctly. | ||
iterator := sdk.KVStorePrefixIterator(store, types.ValidatorsKey) | ||
defer iterator.Close() | ||
|
||
i := int64(0) | ||
for ; iterator.Valid(); iterator.Next() { | ||
validator := types.MustUnmarshalValidator(encCfg.Marshaler, iterator.Value()) | ||
require.Equal(t, testCases[i].NewValidator.OperatorAddress, validator.GetOperator().String()) | ||
require.Equal(t, testCases[i].NewValidator.Commission.Rate, validator.Commission.Rate) | ||
require.Equal(t, testCases[i].NewValidator.Commission.MaxRate, validator.Commission.MaxRate) | ||
i++ | ||
} | ||
} |
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
Oops, something went wrong.