Skip to content

Commit

Permalink
feat(evmstaking): separate staking msg checks (#22)
Browse files Browse the repository at this point in the history
* feat(evmstaking): msg checks

* feat(evmstaking): msg checks

* feat(distribution): add max_ubi param

* feat(distribution): add max_ubi param

* feat(distribution): add max_ubi param

* feat(distribution): add max_ubi param
  • Loading branch information
ezreal1997 authored Oct 23, 2024
1 parent 3d7ade1 commit aa41558
Show file tree
Hide file tree
Showing 19 changed files with 757 additions and 385 deletions.
293 changes: 185 additions & 108 deletions api/cosmos/distribution/v1beta1/distribution.pulsar.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions proto/cosmos/distribution/v1beta1/distribution.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ message Params {
];

bool withdraw_addr_enabled = 4;

string max_ubi = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(amino.dont_omitempty) = true,
(gogoproto.nullable) = false
];
}

// ValidatorHistoricalRewards represents historical rewards for a validator.
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/adr-024-coin-metadata_genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
"ubi": "0.020000000000000000",
"base_proposer_reward": "0.000000000000000000",
"bonus_proposer_reward": "0.000000000000000000",
"withdraw_addr_enabled": true
"withdraw_addr_enabled": true,
"max_ubi": "0.200000000000000000"
},
"fee_pool": {
"ubi": []
Expand Down
5 changes: 3 additions & 2 deletions x/distribution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,11 @@ Example Output:
```json
{
"params": {
"ubi": "20000000000000000",
"ubi": "0.020000000000000000",
"baseProposerReward": "00000000000000000",
"bonusProposerReward": "00000000000000000",
"withdrawAddrEnabled": true
"withdrawAddrEnabled": true,
"max_ubi": "0.200000000000000000"
}
}
```
Expand Down
7 changes: 7 additions & 0 deletions x/distribution/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// GetUbi returns the current distribution ubi.
Expand All @@ -25,6 +28,10 @@ func (k Keeper) SetUbi(ctx context.Context, newUbi math.LegacyDec) error {

params.Ubi = newUbi

if err := params.Validate(); err != nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

return k.Params.Set(ctx, params)
}

Expand Down
1 change: 1 addition & 0 deletions x/distribution/migrations/v3/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestMigrateJSON(t *testing.T) {
"params": {
"base_proposer_reward": "0.000000000000000000",
"bonus_proposer_reward": "0.000000000000000000",
"max_ubi": "0.200000000000000000",
"ubi": "0.020000000000000000",
"withdraw_addr_enabled": true
},
Expand Down
10 changes: 10 additions & 0 deletions x/distribution/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ import (
const (
Ubi = "ubi"
WithdrawEnabled = "withdraw_enabled"
MaxUbi = "max_ubi"
)

// GenUbi randomized Ubi parameter.
func GenUbi(r *rand.Rand) math.LegacyDec {
return math.LegacyNewDecWithPrec(1, 2).Add(math.LegacyNewDecWithPrec(int64(r.Intn(30)), 2))
}

// GenMaxUbi randomized MaxUbi parameter.
func GenMaxUbi(r *rand.Rand) math.LegacyDec {
return math.LegacyNewDecWithPrec(1, 1).Add(math.LegacyNewDecWithPrec(int64(r.Intn(30)), 2))
}

// GenWithdrawEnabled returns a randomized WithdrawEnabled parameter.
func GenWithdrawEnabled(r *rand.Rand) bool {
return r.Int63n(101) <= 95 // 95% chance of withdraws being enabled
Expand All @@ -35,11 +41,15 @@ func RandomizedGenState(simState *module.SimulationState) {
var withdrawEnabled bool
simState.AppParams.GetOrGenerate(WithdrawEnabled, &withdrawEnabled, simState.Rand, func(r *rand.Rand) { withdrawEnabled = GenWithdrawEnabled(r) })

var maxUbi math.LegacyDec
simState.AppParams.GetOrGenerate(MaxUbi, &maxUbi, simState.Rand, func(r *rand.Rand) { maxUbi = GenMaxUbi(r) })

distrGenesis := types.GenesisState{
FeePool: types.InitialFeePool(),
Params: types.Params{
Ubi: ubi,
WithdrawAddrEnabled: withdrawEnabled,
MaxUbi: maxUbi,
},
}

Expand Down
159 changes: 105 additions & 54 deletions x/distribution/types/distribution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 46 additions & 5 deletions x/distribution/types/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"

"cosmossdk.io/math"
Expand All @@ -13,12 +14,33 @@ func DefaultParams() Params {
BaseProposerReward: math.LegacyZeroDec(), // deprecated
BonusProposerReward: math.LegacyZeroDec(), // deprecated
WithdrawAddrEnabled: true,
MaxUbi: math.LegacyNewDecWithPrec(2, 1), // 20%
}
}

func (p Params) Validate() error {
if err := validateUbi(p.Ubi); err != nil {
return err
}

if err := validateMaxUbi(p.MaxUbi); err != nil {
return err
}

if p.Ubi.GT(p.MaxUbi) {
return errors.New("ubi must be less than or equal to max ubi")
}

return nil
}

// ValidateBasic performs basic validation on distribution parameters.
func (p Params) ValidateBasic() error {
return validateUbi(p.Ubi)
if err := validateUbi(p.Ubi); err != nil {
return err
}

return validateMaxUbi(p.MaxUbi)
}

func validateUbi(i interface{}) error {
Expand All @@ -28,13 +50,32 @@ func validateUbi(i interface{}) error {
}

if v.IsNil() {
return fmt.Errorf("ubi pool must be not nil")
return fmt.Errorf("ubi must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("ubi must be positive: %s", v)
}
if v.GTE(math.LegacyOneDec()) {
return fmt.Errorf("ubi too large: %s", v)
}

return nil
}

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

if v.IsNil() {
return fmt.Errorf("max ubi must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("ubi pool must be positive: %s", v)
return fmt.Errorf("max ubi must be positive: %s", v)
}
if v.GT(math.LegacyOneDec()) {
return fmt.Errorf("ubi pool too large: %s", v)
if v.GTE(math.LegacyOneDec()) {
return fmt.Errorf("max ubi too large: %s", v)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/types/params_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_validateAuxFuncs(t *testing.T) {
{"wrong type", args{10.5}, true},
{"empty math.LegacyDec", args{math.LegacyDec{}}, true},
{"negative", args{math.LegacyNewDec(-1)}, true},
{"one dec", args{math.LegacyNewDec(1)}, false},
{"one dec", args{math.LegacyNewDecWithPrec(2, 2)}, false},
{"two dec", args{math.LegacyNewDec(2)}, true},
}
for _, tt := range tests {
Expand Down
Loading

0 comments on commit aa41558

Please sign in to comment.