Skip to content

Commit 85752cb

Browse files
authored
Merge pull request #713 from axone-protocol/feat/params_check
Align params behavior for unlimited limits
2 parents a61edcb + 6b474cb commit 85752cb

File tree

7 files changed

+109
-36
lines changed

7 files changed

+109
-36
lines changed

docs/proto/logic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ Limits defines the limits of the logic module.
276276

277277
| Field | Type | Label | Description |
278278
| ----- | ---- | ----- | ----------- |
279-
| `max_size` | [string](#string) | | max_size specifies the maximum size, in bytes, that is accepted for a program. nil value remove size limitation. |
280-
| `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. |
279+
| `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. |
280+
| `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. |
281281
| `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. |
282282
| `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. |
283283

proto/logic/v1beta2/params.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ message Limits {
3636
option (gogoproto.goproto_stringer) = true;
3737

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

4646
// max_result_count specifies the maximum number of results that can be requested for a query.
47-
// nil value remove max result count limitation.
47+
// nil value or 0 value remove max result count limitation.
4848
string max_result_count = 2 [
4949
(gogoproto.moretags) = "yaml:\"max_result_count\"",
5050
(gogoproto.customtype) = "cosmossdk.io/math.Uint",

x/logic/keeper/grpc_query_ask.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo
5252

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

6060
resultCount := util.DerefOrDefault(request.Limit, defaultSolutionsLimit)
61-
maxResultCount := util.DerefOrDefault(limits.MaxResultCount, sdkmath.NewUint(math.MaxInt64))
61+
maxResultCount := util.NonZeroOrDefaultUInt(limits.MaxResultCount, sdkmath.NewUint(math.MaxInt64))
6262
if resultCount.GT(maxResultCount) {
6363
return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxResultCount: %d", resultCount.Uint64(), maxResultCount.Uint64())
6464
}

x/logic/keeper/grpc_query_ask_test.go

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
"github.com/golang/mock/gomock"
11-
"github.com/samber/lo"
1211

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

@@ -36,8 +35,8 @@ func TestGRPCAsk(t *testing.T) {
3635
program string
3736
query string
3837
limit int
39-
maxResultCount int
40-
maxSize int
38+
maxResultCount uint64
39+
maxSize uint64
4140
predicateBlacklist []string
4241
maxGas uint64
4342
maxVariables uint64
@@ -127,6 +126,18 @@ func TestGRPCAsk(t *testing.T) {
127126
maxSize: 5,
128127
expectedError: "query: 15 > MaxSize: 5: limit exceeded",
129128
},
129+
{
130+
program: "father(bob, alice). father(bob, john).",
131+
query: "father(bob, X).",
132+
maxSize: 0,
133+
expectedAnswer: &types.Answer{
134+
HasMore: true,
135+
Variables: []string{"X"},
136+
Results: []types.Result{{Substitutions: []types.Substitution{{
137+
Variable: "X", Expression: "alice",
138+
}}}},
139+
},
140+
},
130141
{
131142
query: "block_height(X).",
132143
expectedAnswer: &types.Answer{
@@ -144,15 +155,15 @@ func TestGRPCAsk(t *testing.T) {
144155
{
145156
query: "bank_balances(X, Y).",
146157
maxGas: 3000,
147-
expectedError: "out of gas: logic <panic: {ValuePerByte}> (3102/3000): limit exceeded",
158+
expectedError: "out of gas: logic <panic: {ValuePerByte}> (3093/3000): limit exceeded",
148159
},
149160
{
150161
query: "block_height(X).",
151162
maxGas: 3000,
152163
predicateCosts: map[string]uint64{
153164
"block_height/1": 10000,
154165
},
155-
expectedError: "out of gas: logic <block_height/1> (11176/3000): limit exceeded",
166+
expectedError: "out of gas: logic <block_height/1> (11167/3000): limit exceeded",
156167
},
157168
{
158169
query: "length(List, 100000).",
@@ -248,11 +259,11 @@ func TestGRPCAsk(t *testing.T) {
248259
},
249260
{
250261
program: `
251-
foo(a1).
252-
foo(a2).
253-
foo(a3) :- throw(error(resource_error(foo))).
254-
foo(a4).
255-
`,
262+
foo(a1).
263+
foo(a2).
264+
foo(a3) :- throw(error(resource_error(foo))).
265+
foo(a4).
266+
`,
256267
query: `foo(X).`,
257268
maxResultCount: 1,
258269
expectedAnswer: &types.Answer{
@@ -265,11 +276,11 @@ foo(a4).
265276
},
266277
{
267278
program: `
268-
foo(a1).
269-
foo(a2).
270-
foo(a3) :- throw(error(resource_error(foo))).
271-
foo(a4).
272-
`,
279+
foo(a1).
280+
foo(a2).
281+
foo(a3) :- throw(error(resource_error(foo))).
282+
foo(a4).
283+
`,
273284
query: `foo(X).`,
274285
limit: 2,
275286
maxResultCount: 3,
@@ -284,11 +295,11 @@ foo(a4).
284295
},
285296
{
286297
program: `
287-
foo(a1).
288-
foo(a2).
289-
foo(a3) :- throw(error(resource_error(foo))).
290-
foo(a4).
291-
`,
298+
foo(a1).
299+
foo(a2).
300+
foo(a3) :- throw(error(resource_error(foo))).
301+
foo(a4).
302+
`,
292303
query: `foo(X).`,
293304
limit: 3,
294305
maxResultCount: 5,
@@ -303,11 +314,11 @@ foo(a4).
303314
},
304315
{
305316
program: `
306-
foo(a1).
307-
foo(a2).
308-
foo(a3) :- throw(error(resource_error(foo))).
309-
foo(a4).
310-
`,
317+
foo(a1).
318+
foo(a2).
319+
foo(a3) :- throw(error(resource_error(foo))).
320+
foo(a4).
321+
`,
311322
query: `foo(X).`,
312323
limit: 5,
313324
maxResultCount: 5,
@@ -320,6 +331,25 @@ foo(a4).
320331
},
321332
},
322333
},
334+
{
335+
program: `
336+
foo(a1).
337+
foo(a2).
338+
foo(a3) :- throw(error(resource_error(foo))).
339+
foo(a4).
340+
`,
341+
query: `foo(X).`,
342+
limit: 5,
343+
maxResultCount: 0,
344+
expectedAnswer: &types.Answer{
345+
Variables: []string{"X"},
346+
Results: []types.Result{
347+
{Substitutions: []types.Substitution{{Variable: "X", Expression: "a1"}}},
348+
{Substitutions: []types.Substitution{{Variable: "X", Expression: "a2"}}},
349+
{Error: "error(resource_error(foo))"},
350+
},
351+
},
352+
},
323353
}
324354

325355
for nc, tc := range cases {
@@ -352,8 +382,8 @@ foo(a4).
352382
return fsProvider
353383
},
354384
)
355-
maxResultCount := sdkmath.NewUint(uint64(lo.If(tc.maxResultCount == 0, 1).Else(tc.maxResultCount)))
356-
maxSize := sdkmath.NewUint(uint64(lo.If(tc.maxSize == 0, 5000).Else(tc.maxSize)))
385+
maxResultCount := sdkmath.NewUint(tc.maxResultCount)
386+
maxSize := sdkmath.NewUint(tc.maxSize)
357387
params := types.DefaultParams()
358388
params.Limits.MaxResultCount = &maxResultCount
359389
params.Limits.MaxSize = &maxSize

x/logic/types/params.pb.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/logic/util/pointer.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package util
22

3-
import "reflect"
3+
import (
4+
"reflect"
5+
6+
sdkmath "cosmossdk.io/math"
7+
)
48

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

26+
// NonZeroOrDefaultUInt returns the value of the argument if it is not nil and not zero, otherwise returns the default value.
27+
func NonZeroOrDefaultUInt(v *sdkmath.Uint, defaultValue sdkmath.Uint) sdkmath.Uint {
28+
if v != nil && !v.IsZero() {
29+
return *v
30+
}
31+
return defaultValue
32+
}
33+
2234
// IsNil returns true if the given value is nil, false otherwise.
2335
func IsNil(t any) bool {
2436
return t == nil

x/logic/util/pointer_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
. "github.com/smartystreets/goconvey/convey"
8+
9+
sdkmath "cosmossdk.io/math"
810
)
911

1012
func TestDerefOrDefault(t *testing.T) {
@@ -53,3 +55,32 @@ func TestNonZeroOrDefault(t *testing.T) {
5355
}
5456
})
5557
}
58+
59+
func TestNonZeroOrDefaultUInt(t *testing.T) {
60+
Convey("Given a value", t, func() {
61+
cases := []struct {
62+
v *sdkmath.Uint
63+
defaultValue sdkmath.Uint
64+
expected sdkmath.Uint
65+
}{
66+
{nil, sdkmath.ZeroUint(), sdkmath.ZeroUint()},
67+
{
68+
func() *sdkmath.Uint { u := sdkmath.ZeroUint(); return &u }(),
69+
sdkmath.NewUint(10),
70+
sdkmath.NewUint(10),
71+
},
72+
{
73+
func() *sdkmath.Uint { u := sdkmath.NewUint(1); return &u }(),
74+
sdkmath.ZeroUint(),
75+
sdkmath.NewUint(1),
76+
},
77+
}
78+
for _, tc := range cases {
79+
Convey(fmt.Sprintf("When the value is %v", tc.v), func() {
80+
Convey(fmt.Sprintf("Then the default value %v is returned", tc.defaultValue), func() {
81+
So(NonZeroOrDefaultUInt(tc.v, tc.defaultValue), ShouldEqual, tc.expected)
82+
})
83+
})
84+
}
85+
})
86+
}

0 commit comments

Comments
 (0)