Skip to content

Commit

Permalink
feat(staking): add locked token type param (cosmos#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezreal1997 authored Oct 17, 2024
1 parent a806101 commit 71c6e99
Show file tree
Hide file tree
Showing 11 changed files with 1,124 additions and 983 deletions.
290 changes: 174 additions & 116 deletions api/cosmos/staking/v1beta1/staking.pulsar.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion proto/cosmos/staking/v1beta1/staking.proto
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ message Params {
int32 flexible_period_type = 8;
repeated Period periods = 9 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];

repeated TokenTypeInfo token_types = 10 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
int32 locked_token_type = 10;
repeated TokenTypeInfo token_types = 11 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// DelegationResponse is equivalent to Delegation except that it contains a
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/adr-024-coin-metadata_genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
"rewards_multiplier": "1.34"
}
],
"locked_token_type": 0,
"token_types":[
{
"token_type": 0,
Expand Down
2 changes: 1 addition & 1 deletion x/genutil/types/testdata/app_genesis.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x/genutil/types/testdata/cmt_genesis.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x/genutil/types/testdata/parse_chain_id.json

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion x/staking/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (k Keeper) GetTokenTypes(ctx context.Context) ([]types.TokenTypeInfo, error
}

// GetFlexiblePeriodType gets the flexible period type from x/staking module parameters.
func (k Keeper) GetFlexiblePeriodType(ctx context.Context) (periodType int32, err error) {
func (k Keeper) GetFlexiblePeriodType(ctx context.Context) (int32, error) {
store := k.storeService.OpenKVStore(ctx)

bz, err := store.Get(types.ParamsKey)
Expand All @@ -147,6 +147,25 @@ func (k Keeper) GetFlexiblePeriodType(ctx context.Context) (periodType int32, er
return params.FlexiblePeriodType, nil
}

// GetLockedTokenType gets the locked token type from x/staking module parameters.
func (k Keeper) GetLockedTokenType(ctx context.Context) (int32, error) {
store := k.storeService.OpenKVStore(ctx)

bz, err := store.Get(types.ParamsKey)
if err != nil {
return 0, err
} else if bz == nil {
return 0, nil
}

var params types.Params
if err := k.cdc.Unmarshal(bz, &params); err != nil {
return 0, err
}

return params.LockedTokenType, nil
}

// GetPeriodInfo gets the period info from x/staking module parameters.
func (k Keeper) GetPeriodInfo(ctx context.Context, periodType int32) (types.Period, error) {
store := k.storeService.OpenKVStore(ctx)
Expand Down
1 change: 1 addition & 0 deletions x/staking/migrations/v3/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestMigrateJSON(t *testing.T) {
"bond_denom": "stake",
"flexible_period_type": 0,
"historical_entries": 10000,
"locked_token_type": 0,
"max_entries": 7,
"max_validators": 100,
"min_commission_rate": "0.000000000000000000",
Expand Down
2 changes: 1 addition & 1 deletion x/staking/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func RandomizedGenState(simState *module.SimulationState) {
// NOTE: the slashing module need to be defined after the staking module on the
// NewSimulationManager constructor for this to work
simState.UnbondTime = unbondTime
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, simState.BondDenom, minCommissionRate, types.DefaultMinDelegation, types.DefaultFlexiblePeriodType, types.DefaultPeriods, types.DefaultTokenTypes)
params := types.NewParams(simState.UnbondTime, maxVals, 7, histEntries, simState.BondDenom, minCommissionRate, types.DefaultMinDelegation, types.DefaultFlexiblePeriodType, types.DefaultPeriods, types.DefaultLockedTokenType, types.DefaultTokenTypes)

// validators & delegations
var (
Expand Down
23 changes: 22 additions & 1 deletion x/staking/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
DefaultHistoricalEntries uint32 = 10000

DefaultFlexiblePeriodType = 0

DefaultLockedTokenType = 0
)

// DefaultMinCommissionRate is set to 0%
Expand Down Expand Up @@ -75,7 +77,7 @@ var DefaultTokenTypes = []TokenTypeInfo{
// NewParams creates a new Params instance
func NewParams(
unbondingTime time.Duration, maxValidators, maxEntries, historicalEntries uint32, bondDenom string, minCommissionRate math.LegacyDec,
minDelegation math.Int, flexiblePeriodType int32, periods []Period, tokenTypes []TokenTypeInfo,
minDelegation math.Int, flexiblePeriodType int32, periods []Period, lockedTokenType int32, tokenTypes []TokenTypeInfo,
) Params {
return Params{
UnbondingTime: unbondingTime,
Expand All @@ -87,6 +89,7 @@ func NewParams(
MinDelegation: minDelegation,
FlexiblePeriodType: flexiblePeriodType,
Periods: periods,
LockedTokenType: lockedTokenType,
TokenTypes: tokenTypes,
}
}
Expand All @@ -103,6 +106,7 @@ func DefaultParams() Params {
DefaultMinDelegation,
DefaultFlexiblePeriodType,
DefaultPeriods,
DefaultLockedTokenType,
DefaultTokenTypes,
)
}
Expand Down Expand Up @@ -161,6 +165,10 @@ func (p Params) Validate() error {
return err
}

if err := validateLockedTokenType(p.LockedTokenType); err != nil {
return err
}

if err := validateTokenTypes(p.TokenTypes); err != nil {
return err
}
Expand Down Expand Up @@ -296,6 +304,19 @@ func validatePeriods(i interface{}) error {
return nil
}

func validateLockedTokenType(i interface{}) error {
v, ok := i.(int32)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v < 0 {
return fmt.Errorf("invalid locked token type: %d", v)
}

return nil
}

func validateTokenTypes(i interface{}) error {
tokenTypes, ok := i.([]TokenTypeInfo)
if !ok {
Expand Down
Loading

0 comments on commit 71c6e99

Please sign in to comment.