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

CNS-362: Subscription unit tests #473

Merged
merged 7 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ issues:

linters-settings:
dogsled:
max-blank-identifiers: 3
max-blank-identifiers: 5
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
Expand Down
45 changes: 42 additions & 3 deletions common/types/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,51 @@ const (
ASCII_DEL = 127 // ascii for DEL
)

func IsASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
type charRestrictionEnum string

const (
NAME_RESTRICTIONS charRestrictionEnum = "name"
DESCRIPTION_RESTRICTIONS charRestrictionEnum = "description"
)

func isCharDisallowed(c rune, disallowedChars []rune) bool {
for _, char := range disallowedChars {
if char == c {
return true
}
}
return false
}

// Validates name/description strings.
// Current policy:
//
// name: lowercase ascii letters and digits only and the characters {' ', '_'}. can't be empty.
// description: lowercase ascii letters and digits only and the characters {' ', ',', '.', '_', '(', ')'}
func ValidateString(s string, restrictType charRestrictionEnum, disallowedChars []rune) bool {
if restrictType == NAME_RESTRICTIONS && len(s) == 0 {
return false
}

for _, r := range s {
if disallowedChars != nil && isCharDisallowed(r, disallowedChars) {
return false
} else {
switch restrictType {
case NAME_RESTRICTIONS:
if r == ',' {
return false
} else if !unicode.IsLower(r) && r != ' ' && r != '_' && !unicode.IsDigit(r) {
return false
}
case DESCRIPTION_RESTRICTIONS:
if !unicode.IsLower(r) && r != ' ' && r != '.' && r != ',' && r != '_' && r != ')' && r != '(' && !unicode.IsDigit(r) {
return false
}
}
}
}

return true
}

Expand Down
48 changes: 48 additions & 0 deletions common/types/ascii_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestStringValidation(t *testing.T) {
tests := []struct {
name string
s string
restrictType charRestrictionEnum
disallowedChars []rune
valid bool
}{
// name restrictions tests
{"valid_name", "hello", NAME_RESTRICTIONS, nil, true},
{"valid_name_with_space", "hel lo", NAME_RESTRICTIONS, nil, true},
{"valid_name_with_underscore", "hel_lo", NAME_RESTRICTIONS, nil, true},
{"valid_name_with_digit", "hel2lo", NAME_RESTRICTIONS, nil, true},
{"invalid_name_not_lowercase", "hEllo", NAME_RESTRICTIONS, nil, false},
{"invalid_name_not_ascii", "he¢llo", NAME_RESTRICTIONS, nil, false},
{"invalid_name_with_disallowed_char", "heallo", NAME_RESTRICTIONS, []rune{'a'}, false},

// description restrictions test
{"valid_description", "hello", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_space", "hello s", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_comma", "hello,s", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_dot", "hello.s", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_underscore", "hello_s", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_digit", "hello2s", DESCRIPTION_RESTRICTIONS, nil, true},
{"valid_description_with_parenthesis", "hello()s", DESCRIPTION_RESTRICTIONS, nil, true},
{"invalid_description_not_lowercase", "hEllo,s", DESCRIPTION_RESTRICTIONS, nil, false},
{"invalid_description_not_ascii", "h¢llo,s", DESCRIPTION_RESTRICTIONS, nil, false},
{"invalid_description_with_disallowed_char", "heallo", DESCRIPTION_RESTRICTIONS, []rune{'a'}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := ValidateString(tt.s, tt.restrictType, tt.disallowedChars)
if tt.valid {
require.True(t, res)
} else {
require.False(t, res)
}
})
}
}
2 changes: 1 addition & 1 deletion cookbook/plans/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
],
"geolocation_profile": "18446744073709551615",
"total_cu_limit": "1000000",
"epoch_cu_limit": "20000",
"epoch_cu_limit": "100000",
"max_providers_to_pair": "3"
}
}
Expand Down
3 changes: 2 additions & 1 deletion x/conflict/keeper/msg_server_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ func setupForConflictTests(t *testing.T, numOfProviders int) testStruct {
// init keepers state
var balance int64 = 100000

// setup consumer
ts.consumer = common.CreateNewAccount(ts.ctx, *ts.keepers, balance)

// setup consumer
// setup providers
for i := 0; i < numOfProviders; i++ {
ts.Providers = append(ts.Providers, common.CreateNewAccount(ts.ctx, *ts.keepers, balance))
}
Expand Down
21 changes: 16 additions & 5 deletions x/pairing/keeper/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,24 @@ func (k Keeper) getProjectStrictestPolicy(ctx sdk.Context, project projectstypes
}

planPolicy := plan.GetPlanPolicy()
policies := []*projectstypes.Policy{project.AdminPolicy, project.SubscriptionPolicy, &planPolicy}
policies := []*projectstypes.Policy{&planPolicy}
if project.SubscriptionPolicy != nil {
policies = append(policies, project.SubscriptionPolicy)
}
if project.AdminPolicy != nil {
policies = append(policies, project.AdminPolicy)
}

if !projectstypes.CheckChainIdExistsInPolicies(chainID, policies) {
return 0, 0, "", 0, fmt.Errorf("chain ID not found in any of the policies")
}

geolocation := k.CalculateEffectiveGeolocationFromPolicies(policies)

providersToPair := k.CalculateEffectiveProvidersToPairFromPolicies(policies)
if providersToPair == uint64(math.MaxUint64) {
return 0, 0, "", 0, fmt.Errorf("could not calculate providersToPair value: all policies are nil")
}

sub, found := k.subscriptionKeeper.GetSubscription(ctx, project.GetSubscription())
if !found {
Expand All @@ -194,15 +204,16 @@ func (k Keeper) CalculateEffectiveGeolocationFromPolicies(policies []*projectsty
}

func (k Keeper) CalculateEffectiveProvidersToPairFromPolicies(policies []*projectstypes.Policy) uint64 {
var providersToPairValues []uint64
providersToPair := uint64(math.MaxUint64)

for _, policy := range policies {
if policy != nil {
providersToPairValues = append(providersToPairValues, policy.GetMaxProvidersToPair())
val := policy.GetMaxProvidersToPair()
if policy != nil && val < providersToPair {
providersToPair = val
}
}

return commontypes.FindMin(providersToPairValues)
return providersToPair
}

func (k Keeper) CalculateEffectiveAllowedCuPerEpochFromPolicies(policies []*projectstypes.Policy, cuUsedInProject uint64, cuLeftInSubscription uint64) uint64 {
Expand Down
Loading