Skip to content

Commit

Permalink
refactor(logic): allow max_result_count and max_size set zero as unli…
Browse files Browse the repository at this point in the history
…mited value
  • Loading branch information
bdeneux committed Jul 29, 2024
1 parent 6f266e9 commit 707d5a1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/proto/logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ Limits defines the limits of the logic module.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `max_size` | [string](#string) | | max_size specifies the maximum size, in bytes, that is accepted for a program. nil value remove size limitation. |
| `max_result_count` | [string](#string) | | max_result_count specifies the maximum number of results that can be requested for a query. nil value remove max result count limitation. |
| `max_size` | [string](#string) | | max_size specifies the maximum size, in bytes, that is accepted for a program. nil value or 0 value remove size limitation. |
| `max_result_count` | [string](#string) | | max_result_count specifies the maximum number of results that can be requested for a query. nil value or 0 value remove max result count limitation. |
| `max_user_output_size` | [string](#string) | | max_user_output_size specifies the maximum number of bytes to keep in the user output. If the user output exceeds this size, the interpreter will overwrite the oldest bytes with the new ones to keep the size constant. nil value or 0 value means that no user output is used at all. |
| `max_variables` | [string](#string) | | max_variables specifies the maximum number of variables that can be create by the interpreter. nil value or 0 value means that no limit is set. |

Expand Down
4 changes: 2 additions & 2 deletions proto/logic/v1beta2/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ message Limits {
option (gogoproto.goproto_stringer) = true;

// max_size specifies the maximum size, in bytes, that is accepted for a program.
// nil value remove size limitation.
// nil value or 0 value remove size limitation.
string max_size = 3 [
(gogoproto.moretags) = "yaml:\"max_size\"",
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
(gogoproto.nullable) = true
];

// max_result_count specifies the maximum number of results that can be requested for a query.
// nil value remove max result count limitation.
// nil value or 0 value remove max result count limitation.
string max_result_count = 2 [
(gogoproto.moretags) = "yaml:\"max_result_count\"",
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/grpc_query_ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo

func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error {
size := sdkmath.NewUint(uint64(len(request.GetQuery())))
maxSize := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
maxSize := util.NonZeroOrDefaultUInt(limits.MaxSize, sdkmath.NewUint(math.MaxInt64))
if size.GT(maxSize) {
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size.Uint64(), maxSize.Uint64())
}

resultCount := util.DerefOrDefault(request.Limit, defaultSolutionsLimit)
maxResultCount := util.DerefOrDefault(limits.MaxResultCount, sdkmath.NewUint(math.MaxInt64))
maxResultCount := util.NonZeroOrDefaultUInt(limits.MaxResultCount, sdkmath.NewUint(math.MaxInt64))
if resultCount.GT(maxResultCount) {
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxResultCount: %d", resultCount.Uint64(), maxResultCount.Uint64())
}
Expand Down
4 changes: 2 additions & 2 deletions x/logic/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion x/logic/util/pointer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "reflect"
import (
sdkmath "cosmossdk.io/math"
"reflect"
)

// DerefOrDefault returns the value of the pointer if it is not nil, otherwise returns the default value.
func DerefOrDefault[T any](ptr *T, defaultValue T) T {
Expand All @@ -19,6 +22,14 @@ func NonZeroOrDefault[T any](v, defaultValue T) T {
return defaultValue
}

// NonZeroOrDefaultUInt returns the value of the argument if it is not nil and not zero, otherwise returns the default value.
func NonZeroOrDefaultUInt(v *sdkmath.Uint, defaultValue sdkmath.Uint) sdkmath.Uint {
if v != nil && !v.IsZero() {
return *v
}
return defaultValue
}

// IsNil returns true if the given value is nil, false otherwise.
func IsNil(t any) bool {
return t == nil
Expand Down
22 changes: 22 additions & 0 deletions x/logic/util/pointer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
sdkmath "cosmossdk.io/math"
"fmt"
"testing"

Expand Down Expand Up @@ -53,3 +54,24 @@ func TestNonZeroOrDefault(t *testing.T) {
}
})
}

func TestNonZeroOrDefaultUInt(t *testing.T) {
Convey("Given a value", t, func() {
cases := []struct {
v *sdkmath.Uint
defaultValue sdkmath.Uint
expected sdkmath.Uint
}{
{nil, sdkmath.ZeroUint(), sdkmath.ZeroUint()},
{v: func() *sdkmath.Uint { u := sdkmath.ZeroUint(); return &u }(), defaultValue: sdkmath.NewUint(10), expected: sdkmath.NewUint(10)},
{v: func() *sdkmath.Uint { u := sdkmath.NewUint(1); return &u }(), defaultValue: sdkmath.ZeroUint(), expected: sdkmath.NewUint(1)},
}
for _, tc := range cases {
Convey(fmt.Sprintf("When the value is %v", tc.v), func() {
Convey(fmt.Sprintf("Then the default value %v is returned", tc.defaultValue), func() {
So(NonZeroOrDefaultUInt(tc.v, tc.defaultValue), ShouldEqual, tc.expected)
})
})
}
})
}

0 comments on commit 707d5a1

Please sign in to comment.