Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust oracle FeedName&ValueJsonPath validation #109

Merged
merged 9 commits into from
Feb 2, 2021
3 changes: 0 additions & 3 deletions modules/oracle/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ func ValidateGenesis(data GenesisState) error {
if err := ValidateAggregateFunc(feed.AggregateFunc); err != nil {
return err
}
if err := ValidateValueJSONPath(feed.ValueJsonPath); err != nil {
return err
}
if err := ValidateLatestHistory(feed.LatestHistory); err != nil {
return err
}
Expand Down
4 changes: 0 additions & 4 deletions modules/oracle/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ func (msg MsgCreateFeed) ValidateBasic() error {
return err
}

if err := ValidateValueJSONPath(msg.ValueJsonPath); err != nil {
return err
}

if !msg.ServiceFeeCap.IsValid() {
return sdkerrors.Wrapf(ErrInvalidServiceFeeCap, msg.ServiceFeeCap.String())
}
Expand Down
18 changes: 0 additions & 18 deletions modules/oracle/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,6 @@ func TestMsgCreateFeed_ValidateBasic(t *testing.T) {
Creator: addr1,
},
false,
}, {
"wrong ValueJsonPath",
MsgCreateFeed{
FeedName: "feedEthPrice",
AggregateFunc: "avg",
ValueJsonPath: "",
LatestHistory: 10,
Description: "feed eth price",
ServiceName: "GetEthPrice",
Providers: []string{addr1, addr2},
Input: "eth",
Timeout: 5,
ServiceFeeCap: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))),
RepeatedFrequency: 5,
ResponseThreshold: 1,
Creator: addr1,
},
false,
}, {
"wrong MaxLatestHistory(=0)",
MsgCreateFeed{
Expand Down
26 changes: 10 additions & 16 deletions modules/oracle/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
)

const (
MaxLatestHistory = 100
MaxAggregateFuncLen = 10
MaxValueJsonPath = 70
MaxDescriptionLen = 280
//MaxLatestHistory define the the maximum number of feed value saved
MaxLatestHistory = 100
//MaxAggregateFuncNameLen define the the maximum lenght of the ggregate function name
MaxAggregateFuncNameLen = 10
//MaxDescriptionLen define the the maximum lenght of the description
MaxDescriptionLen = 280
)

var (
// the feed name only accepts alphanumeric characters, _ and -
regexpFeedName = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`)
// 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
Expand All @@ -39,8 +41,8 @@ func ValidateDescription(desc string) error {

// 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 len(aggregateFunc) == 0 || len(aggregateFunc) > MaxAggregateFuncNameLen {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "aggregate func must between [1, %d], got: %d", MaxAggregateFuncNameLen, len(aggregateFunc))
}

if _, err := GetAggregateFunc(aggregateFunc); err != nil {
Expand All @@ -49,14 +51,6 @@ func ValidateAggregateFunc(aggregateFunc string) error {
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 {
Expand Down
52 changes: 52 additions & 0 deletions modules/oracle/types/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package types

import "testing"

func TestValidateFeedName(t *testing.T) {
type args struct {
feedName string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "contain -",
args: args{feedName: "usdt-atom"},
wantErr: false,
},
{
name: "contain /",
args: args{feedName: "usdt/atom"},
wantErr: false,
},
{
name: "contain uppercase letter",
args: args{feedName: "USDT-atom"},
wantErr: false,
},
{
name: "contain digital",
args: args{feedName: "USDT-atom2"},
wantErr: false,
},
{
name: "start with a number",
args: args{feedName: "2USDT-atom2"},
wantErr: true,
},
{
name: "contain special characters",
args: args{feedName: "USDT$-atom2"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ValidateFeedName(tt.args.feedName); (err != nil) != tt.wantErr {
t.Errorf("ValidateFeedName() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}