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

Align params behavior for unlimited limits #713

Merged
merged 4 commits into from
Jul 30, 2024
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
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
84 changes: 57 additions & 27 deletions x/logic/keeper/grpc_query_ask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/samber/lo"

. "github.com/smartystreets/goconvey/convey"

Expand Down Expand Up @@ -36,8 +35,8 @@ func TestGRPCAsk(t *testing.T) {
program string
query string
limit int
maxResultCount int
maxSize int
maxResultCount uint64
maxSize uint64
predicateBlacklist []string
maxGas uint64
maxVariables uint64
Expand Down Expand Up @@ -127,6 +126,18 @@ func TestGRPCAsk(t *testing.T) {
maxSize: 5,
expectedError: "query: 15 > MaxSize: 5: limit exceeded",
},
{
program: "father(bob, alice). father(bob, john).",
query: "father(bob, X).",
maxSize: 0,
expectedAnswer: &types.Answer{
HasMore: true,
Variables: []string{"X"},
Results: []types.Result{{Substitutions: []types.Substitution{{
Variable: "X", Expression: "alice",
}}}},
},
},
{
query: "block_height(X).",
expectedAnswer: &types.Answer{
Expand All @@ -144,15 +155,15 @@ func TestGRPCAsk(t *testing.T) {
{
query: "bank_balances(X, Y).",
maxGas: 3000,
expectedError: "out of gas: logic <panic: {ValuePerByte}> (3102/3000): limit exceeded",
expectedError: "out of gas: logic <panic: {ValuePerByte}> (3093/3000): limit exceeded",
},
{
query: "block_height(X).",
maxGas: 3000,
predicateCosts: map[string]uint64{
"block_height/1": 10000,
},
expectedError: "out of gas: logic <block_height/1> (11176/3000): limit exceeded",
expectedError: "out of gas: logic <block_height/1> (11167/3000): limit exceeded",
},
{
query: "length(List, 100000).",
Expand Down Expand Up @@ -248,11 +259,11 @@ func TestGRPCAsk(t *testing.T) {
},
{
program: `
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
query: `foo(X).`,
maxResultCount: 1,
expectedAnswer: &types.Answer{
Expand All @@ -265,11 +276,11 @@ foo(a4).
},
{
program: `
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
query: `foo(X).`,
limit: 2,
maxResultCount: 3,
Expand All @@ -284,11 +295,11 @@ foo(a4).
},
{
program: `
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
query: `foo(X).`,
limit: 3,
maxResultCount: 5,
Expand All @@ -303,11 +314,11 @@ foo(a4).
},
{
program: `
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
query: `foo(X).`,
limit: 5,
maxResultCount: 5,
Expand All @@ -320,6 +331,25 @@ foo(a4).
},
},
},
{
program: `
foo(a1).
foo(a2).
foo(a3) :- throw(error(resource_error(foo))).
foo(a4).
`,
query: `foo(X).`,
limit: 5,
maxResultCount: 0,
expectedAnswer: &types.Answer{
Variables: []string{"X"},
Results: []types.Result{
{Substitutions: []types.Substitution{{Variable: "X", Expression: "a1"}}},
{Substitutions: []types.Substitution{{Variable: "X", Expression: "a2"}}},
{Error: "error(resource_error(foo))"},
},
},
},
}

for nc, tc := range cases {
Expand Down Expand Up @@ -352,8 +382,8 @@ foo(a4).
return fsProvider
},
)
maxResultCount := sdkmath.NewUint(uint64(lo.If(tc.maxResultCount == 0, 1).Else(tc.maxResultCount)))
maxSize := sdkmath.NewUint(uint64(lo.If(tc.maxSize == 0, 5000).Else(tc.maxSize)))
maxResultCount := sdkmath.NewUint(tc.maxResultCount)
maxSize := sdkmath.NewUint(tc.maxSize)
params := types.DefaultParams()
params.Limits.MaxResultCount = &maxResultCount
params.Limits.MaxSize = &maxSize
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.

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

import "reflect"
import (
"reflect"

sdkmath "cosmossdk.io/math"
)

// 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 +23,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
31 changes: 31 additions & 0 deletions x/logic/util/pointer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

. "github.com/smartystreets/goconvey/convey"

sdkmath "cosmossdk.io/math"
)

func TestDerefOrDefault(t *testing.T) {
Expand Down Expand Up @@ -53,3 +55,32 @@ 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()},
{
func() *sdkmath.Uint { u := sdkmath.ZeroUint(); return &u }(),
sdkmath.NewUint(10),
sdkmath.NewUint(10),
},
{
func() *sdkmath.Uint { u := sdkmath.NewUint(1); return &u }(),
sdkmath.ZeroUint(),
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)
})
})
}
})
}
Loading