-
Notifications
You must be signed in to change notification settings - Fork 28
/
validation.go
108 lines (91 loc) · 3.38 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
package types
import (
"regexp"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/irisnet/irismod/modules/service/exported"
)
const (
MaxLatestHistory = 100
MaxAggregateFuncLen = 10
MaxValueJsonPath = 70
MaxDescriptionLen = 280
)
var (
// the feed name only accepts alphanumeric characters, _ and -
regexpFeedName = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
)
// ValidateFeedName verify that the feedName is legal
func ValidateFeedName(feedName string) error {
if !regexpFeedName.MatchString(feedName) {
return sdkerrors.Wrap(ErrInvalidFeedName, feedName)
}
return nil
}
// ValidateDescription verify that the desc is legal
func ValidateDescription(desc string) error {
if len(desc) > MaxDescriptionLen {
return sdkerrors.Wrap(ErrInvalidDescription, desc)
}
return nil
}
// ValidateAggregateFunc verify that the aggregateFunc is legal
func ValidateAggregateFunc(aggregateFunc string) error {
if len(aggregateFunc) == 0 || len(aggregateFunc) > MaxAggregateFuncLen {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "aggregate func must between [1, %d], got: %d", MaxAggregateFuncLen, len(aggregateFunc))
}
if _, err := GetAggregateFunc(aggregateFunc); err != nil {
return err
}
return nil
}
// ValidateValueJSONPath verify that the valueJsonPath is legal
func ValidateValueJSONPath(valueJSONPath string) error {
if len(valueJSONPath) == 0 || len(valueJSONPath) > MaxValueJsonPath {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "the length of valueJson path func must less than %d, got: %d", MaxAggregateFuncLen, len(valueJSONPath))
}
return nil
}
// ValidateLatestHistory verify that the latestHistory is legal
func ValidateLatestHistory(latestHistory uint64) error {
if latestHistory < 1 || latestHistory > MaxLatestHistory {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "latest history is invalid, should be between 1 and %d", MaxLatestHistory)
}
return nil
}
// ValidateCreator verify that the creator is legal
func ValidateCreator(creator string) error {
if _, err := sdk.AccAddressFromBech32(creator); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator")
}
return nil
}
// ValidateServiceName verifies whether the parameters are legal
func ValidateServiceName(serviceName string) error {
return exported.ValidateServiceName(serviceName)
}
// ValidateResponseThreshold verifies whether the parameters are legal
func ValidateResponseThreshold(responseThreshold uint32, maxCnt int) error {
if (maxCnt != 0 && int(responseThreshold) > maxCnt) || responseThreshold < 1 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "response threshold should be between 1 and %d", maxCnt)
}
return nil
}
// ValidateTimeout verifies whether the parameters are legal
func ValidateTimeout(timeout int64, frequency uint64) error {
if frequency < uint64(timeout) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "timeout [%d] should be no more than frequency [%d]", timeout, frequency)
}
return nil
}
// ValidateServiceFeeCap verifies whether the parameters are legal
func ValidateServiceFeeCap(serviceFeeCap sdk.Coins) error {
if !serviceFeeCap.IsValid() {
return sdkerrors.Wrapf(ErrInvalidServiceFeeCap, serviceFeeCap.String())
}
return nil
}
// Modified return whether the parameters are modified
func Modified(target string) bool {
return target != DoNotModify
}