diff --git a/docs/proto/logic.md b/docs/proto/logic.md index 92b3fb887..9c00abc91 100644 --- a/docs/proto/logic.md +++ b/docs/proto/logic.md @@ -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. | diff --git a/proto/logic/v1beta2/params.proto b/proto/logic/v1beta2/params.proto index 8193b207c..99dc29c95 100644 --- a/proto/logic/v1beta2/params.proto +++ b/proto/logic/v1beta2/params.proto @@ -36,7 +36,7 @@ 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", @@ -44,7 +44,7 @@ message Limits { ]; // 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", diff --git a/x/logic/keeper/grpc_query_ask.go b/x/logic/keeper/grpc_query_ask.go index 9fd129d3f..678276588 100644 --- a/x/logic/keeper/grpc_query_ask.go +++ b/x/logic/keeper/grpc_query_ask.go @@ -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()) } diff --git a/x/logic/keeper/grpc_query_ask_test.go b/x/logic/keeper/grpc_query_ask_test.go index 18da07b38..76d1042a4 100644 --- a/x/logic/keeper/grpc_query_ask_test.go +++ b/x/logic/keeper/grpc_query_ask_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/golang/mock/gomock" - "github.com/samber/lo" . "github.com/smartystreets/goconvey/convey" @@ -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 @@ -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{ @@ -144,7 +155,7 @@ func TestGRPCAsk(t *testing.T) { { query: "bank_balances(X, Y).", maxGas: 3000, - expectedError: "out of gas: logic (3102/3000): limit exceeded", + expectedError: "out of gas: logic (3093/3000): limit exceeded", }, { query: "block_height(X).", @@ -152,7 +163,7 @@ func TestGRPCAsk(t *testing.T) { predicateCosts: map[string]uint64{ "block_height/1": 10000, }, - expectedError: "out of gas: logic (11176/3000): limit exceeded", + expectedError: "out of gas: logic (11167/3000): limit exceeded", }, { query: "length(List, 100000).", @@ -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{ @@ -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, @@ -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, @@ -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, @@ -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 { @@ -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 diff --git a/x/logic/types/params.pb.go b/x/logic/types/params.pb.go index 047a45d65..9a4cb9d3c 100644 --- a/x/logic/types/params.pb.go +++ b/x/logic/types/params.pb.go @@ -92,10 +92,10 @@ func (m *Params) GetGasPolicy() GasPolicy { // Limits defines the limits of the logic module. type Limits struct { // 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. MaxSize *cosmossdk_io_math.Uint `protobuf:"bytes,3,opt,name=max_size,json=maxSize,proto3,customtype=cosmossdk.io/math.Uint" json:"max_size,omitempty" yaml:"max_size"` // 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. MaxResultCount *cosmossdk_io_math.Uint `protobuf:"bytes,2,opt,name=max_result_count,json=maxResultCount,proto3,customtype=cosmossdk.io/math.Uint" json:"max_result_count,omitempty" yaml:"max_result_count"` // 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. diff --git a/x/logic/util/pointer.go b/x/logic/util/pointer.go index 585bf1b30..bfc29541a 100644 --- a/x/logic/util/pointer.go +++ b/x/logic/util/pointer.go @@ -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 { @@ -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 diff --git a/x/logic/util/pointer_test.go b/x/logic/util/pointer_test.go index 8ffb79ba7..350b0cd57 100644 --- a/x/logic/util/pointer_test.go +++ b/x/logic/util/pointer_test.go @@ -5,6 +5,8 @@ import ( "testing" . "github.com/smartystreets/goconvey/convey" + + sdkmath "cosmossdk.io/math" ) func TestDerefOrDefault(t *testing.T) { @@ -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) + }) + }) + } + }) +}