-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidation.go
124 lines (97 loc) · 3.71 KB
/
validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package poa
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/staking/types"
"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
)
// x/staking/types/msgs.go
// Validate validates the MsgCreateValidator sdk msg.
func (msg MsgCreateValidator) Validate(ac address.Codec) error {
// note that unmarshaling from bech32 ensures both non-empty and valid
_, err := ac.StringToBytes(msg.ValidatorAddress)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}
if msg.Pubkey == nil {
return types.ErrEmptyValidatorPubKey
}
// if !msg.Value.IsValid() || !msg.Value.Amount.IsPositive() {
// return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid delegation amount")
// }
if msg.Description == (Description{}) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty description")
}
if msg.Commission == (CommissionRates{}) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "empty commission")
}
if err := msg.Commission.Validate(); err != nil {
return err
}
// if !msg.MinSelfDelegation.IsPositive() {
// return errorsmod.Wrap(
// sdkerrors.ErrInvalidRequest,
// "minimum self delegation must be a positive integer",
// )
// }
// if msg.Value.Amount.LT(msg.MinSelfDelegation) {
// return ErrSelfDelegationBelowMinimum
// }
return nil
}
// x/staking/types/commission.go
// Validate performs basic sanity validation checks of initial commission
// parameters. If validation fails, an SDK error is returned.
func (cr CommissionRates) Validate() error {
switch {
case cr.MaxRate.IsNegative():
// max rate cannot be negative
return types.ErrCommissionNegative
case cr.MaxRate.GT(math.LegacyOneDec()):
// max rate cannot be greater than 1
return types.ErrCommissionHuge
case cr.Rate.IsNegative():
// rate cannot be negative
return types.ErrCommissionNegative
case cr.Rate.GT(cr.MaxRate):
// rate cannot be greater than the max rate
return types.ErrCommissionGTMaxRate
case cr.MaxChangeRate.IsNegative():
// change rate cannot be negative
return types.ErrCommissionChangeRateNegative
case cr.MaxChangeRate.GT(cr.MaxRate):
// change rate cannot be greater than the max rate
return types.ErrCommissionChangeRateGTMaxRate
}
return nil
}
// x/staking/types/validator.go
// EnsureLength ensures the length of a validator's description.
func (d Description) EnsureLength() (Description, error) {
if len(d.Moniker) > types.MaxMonikerLength {
return d, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid moniker length; got: %d, max: %d", len(d.Moniker), types.MaxMonikerLength)
}
if len(d.Identity) > types.MaxIdentityLength {
return d, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid identity length; got: %d, max: %d", len(d.Identity), types.MaxIdentityLength)
}
if len(d.Website) > types.MaxWebsiteLength {
return d, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid website length; got: %d, max: %d", len(d.Website), types.MaxWebsiteLength)
}
if len(d.SecurityContact) > types.MaxSecurityContactLength {
return d, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid security contact length; got: %d, max: %d", len(d.SecurityContact), types.MaxSecurityContactLength)
}
if len(d.Details) > types.MaxDetailsLength {
return d, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid details length; got: %d, max: %d", len(d.Details), types.MaxDetailsLength)
}
return d, nil
}
func (msg MsgSetPower) Validate(ac address.Codec) error {
if _, err := ac.StringToBytes(msg.ValidatorAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid validator address: %s", err)
}
if msg.Power < 1_000_000 {
return ErrPowerBelowMinimum
}
return nil
}