Skip to content

Commit 2bbced9

Browse files
committed
optimize params validation
1 parent 407e105 commit 2bbced9

1 file changed

Lines changed: 67 additions & 6 deletions

File tree

x/farming/types/params.go

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,80 @@ func DefaultParams() Params {
3232

3333
// Validate validates the set of params
3434
func (p Params) Validate() error {
35-
if p.EpochDuration <= 0 {
35+
if err := validateEpochDuration(p); err != nil {
36+
return err
37+
}
38+
39+
if err := validateRewardsPerEpoch(p); err != nil {
40+
return err
41+
}
42+
43+
if err := validateLockDurations(p); err != nil {
44+
return err
45+
}
46+
47+
if err := validateEligibleAssets(p); err != nil {
48+
return err
49+
}
50+
51+
return nil
52+
}
53+
54+
// validateEpochDuration validates the given epoch duration
55+
func validateEpochDuration(p Params) error {
56+
if p.EpochDuration < 0 {
3657
return errorsmod.Wrap(ErrInvalidParams, "invalid epoch duration")
3758
}
3859

39-
if !p.RewardsPerEpoch.IsValid() || !p.RewardsPerEpoch.IsPositive() {
60+
if p.Enabled && p.EpochDuration == 0 {
61+
return errorsmod.Wrap(ErrInvalidParams, "epoch duration must be greater than 0 when farming enabled")
62+
}
63+
}
64+
65+
// validateRewardsPerEpoch validates the given rewards per epoch
66+
func validateRewardsPerEpoch(p Params) error {
67+
if !p.RewardsPerEpoch.IsValid() {
4068
return errorsmod.Wrap(ErrInvalidParams, "invalid rewards per epoch")
4169
}
4270

71+
if p.Enabled && !p.RewardsPerEpoch.IsPositive() {
72+
return errorsmod.Wrap(ErrInvalidParams, "rewards per epoch must be positive when farming enabled")
73+
}
74+
75+
return nil
76+
}
77+
78+
// validateLockDurations validates the given lock durations
79+
func validateLockDurations(p Params) error {
80+
if p.Enabled && len(p.LockDurations) == 0 {
81+
return errorsmod.Wrap(ErrInvalidParams, "lock durations cannot be empty when farming enabled")
82+
}
83+
4384
lockDurations := make(map[time.Duration]bool)
4485
for _, lockDuration := range p.LockDurations {
4586
if lockDurations[lockDuration] {
4687
return errorsmod.Wrap(ErrInvalidParams, "duplicate lock duration")
4788
}
4889

49-
if lockDuration <= 0 {
50-
return errorsmod.Wrap(ErrInvalidParams, "invalid lock duration")
90+
if lockDuration < p.EpochDuration {
91+
return errorsmod.Wrap(ErrInvalidParams, "lock duration cannot be less than epoch duration")
5192
}
5293

5394
lockDurations[lockDuration] = true
5495
}
5596

97+
return nil
98+
}
99+
100+
// validateEligibleAssets validates the given eligible assets
101+
func validateEligibleAssets(p Params) error {
102+
if p.Enabled && len(p.EligibleAssets) == 0 {
103+
return errorsmod.Wrap(ErrInvalidParams, "eligible assets cannot be empty when farming enabled")
104+
}
105+
56106
eligibleAssets := make(map[string]bool)
107+
totalRewardRatio := sdkmath.LegacyZeroDec()
108+
57109
for _, asset := range p.EligibleAssets {
58110
if eligibleAssets[asset.Denom] {
59111
return errorsmod.Wrap(ErrInvalidParams, "duplicate asset denom")
@@ -63,12 +115,21 @@ func (p Params) Validate() error {
63115
return errorsmod.Wrapf(ErrInvalidParams, "invalid asset denom: %v", err)
64116
}
65117

66-
if asset.RewardRatio.IsNil() || asset.RewardRatio.IsZero() || asset.RewardRatio.GTE(sdkmath.LegacyOneDec()) {
67-
return errorsmod.Wrap(ErrInvalidParams, "invalid asset reward ratio")
118+
if asset.RewardRatio.IsNegative() {
119+
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be negative")
68120
}
69121

122+
if asset.RewardRatio.GT(sdkmath.LegacyOneDec()) {
123+
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be greater than 1")
124+
}
125+
126+
totalRewardRatio = totalRewardRatio.Add(asset.RewardRatio)
70127
eligibleAssets[asset.Denom] = true
71128
}
72129

130+
if totalRewardRatio.GT(sdkmath.LegacyOneDec()) {
131+
return errorsmod.Wrap(ErrInvalidParams, "total asset reward ratio cannot be greater than 1")
132+
}
133+
73134
return nil
74135
}

0 commit comments

Comments
 (0)