From cc860317cd40cc8462e462267e31c527b0e09d25 Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 11 Mar 2024 17:22:10 +0100 Subject: [PATCH] feat(logic): introduce parameter "limit" for queries --- proto/logic/v1beta2/query.proto | 8 ++ x/logic/keeper/grpc_query_ask.go | 7 +- x/logic/keeper/interpreter.go | 4 +- x/logic/types/query.pb.go | 126 +++++++++++++++++++++++-------- x/logic/wasm/query.go | 1 + x/logic/wasm/types.go | 11 ++- 6 files changed, 118 insertions(+), 39 deletions(-) diff --git a/proto/logic/v1beta2/query.proto b/proto/logic/v1beta2/query.proto index 5cc6611e..4f242ddb 100644 --- a/proto/logic/v1beta2/query.proto +++ b/proto/logic/v1beta2/query.proto @@ -44,6 +44,14 @@ message QueryServiceAskRequest { string program = 1 [(gogoproto.moretags) = "yaml:\"program\",omitempty"]; // query is the query string to be executed. string query = 2 [(gogoproto.moretags) = "yaml:\"query\",omitempty"]; + // limit specifies the maximum number of solutions to be returned. This field is governed by + // max_result_count, which defines the upper limit of results that may be requested per query. + // If this field is not explicitly set, a default value of 1 is applied. + string limit = 3 [ + (gogoproto.moretags) = "yaml:\"limit\",omitempty", + (gogoproto.customtype) = "cosmossdk.io/math.Uint", + (gogoproto.nullable) = true + ]; } // QueryServiceAskResponse is response type for the QueryService/Ask RPC method. diff --git a/x/logic/keeper/grpc_query_ask.go b/x/logic/keeper/grpc_query_ask.go index 270dcae2..eb65dfc6 100644 --- a/x/logic/keeper/grpc_query_ask.go +++ b/x/logic/keeper/grpc_query_ask.go @@ -4,14 +4,18 @@ import ( goctx "context" errorsmod "cosmossdk.io/errors" + sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/v7/x/logic/meter" "github.com/okp4/okp4d/v7/x/logic/types" + "github.com/okp4/okp4d/v7/x/logic/util" ) +var defaultLimits = sdkmath.OneUint() + func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (response *types.QueryServiceAskResponse, err error) { sdkCtx := sdk.UnwrapSDKContext(ctx) @@ -43,7 +47,8 @@ func (k Keeper) Ask(ctx goctx.Context, req *types.QueryServiceAskRequest) (respo return k.execute( sdkCtx, req.Program, - req.Query) + req.Query, + util.DerefOrDefault(req.Limit, defaultLimits)) } // withGasMeter returns a new context with a gas meter that has the given limit. diff --git a/x/logic/keeper/interpreter.go b/x/logic/keeper/interpreter.go index c95a437c..1685feaa 100644 --- a/x/logic/keeper/interpreter.go +++ b/x/logic/keeper/interpreter.go @@ -40,7 +40,7 @@ func (k Keeper) enhanceContext(ctx context.Context) context.Context { return sdkCtx } -func (k Keeper) execute(ctx context.Context, program, query string) (*types.QueryServiceAskResponse, error) { +func (k Keeper) execute(ctx context.Context, program, query string, limit sdkmath.Uint) (*types.QueryServiceAskResponse, error) { ctx = k.enhanceContext(ctx) sdkCtx := sdk.UnwrapSDKContext(ctx) limits := k.limits(sdkCtx) @@ -53,7 +53,7 @@ func (k Keeper) execute(ctx context.Context, program, query string) (*types.Quer return nil, errorsmod.Wrapf(types.InvalidArgument, "error compiling query: %v", err.Error()) } - answer, err := k.queryInterpreter(ctx, i, query, *limits.MaxResultCount) + answer, err := k.queryInterpreter(ctx, i, query, sdkmath.MinUint(limit, *limits.MaxResultCount)) if err != nil { return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error()) } diff --git a/x/logic/types/query.pb.go b/x/logic/types/query.pb.go index 3c71a311..a29513cb 100644 --- a/x/logic/types/query.pb.go +++ b/x/logic/types/query.pb.go @@ -5,6 +5,7 @@ package types import ( context "context" + cosmossdk_io_math "cosmossdk.io/math" fmt "fmt" _ "github.com/cosmos/gogoproto/gogoproto" grpc1 "github.com/cosmos/gogoproto/grpc" @@ -118,6 +119,10 @@ type QueryServiceAskRequest struct { Program string `protobuf:"bytes,1,opt,name=program,proto3" json:"program,omitempty" yaml:"program",omitempty` // query is the query string to be executed. Query string `protobuf:"bytes,2,opt,name=query,proto3" json:"query,omitempty" yaml:"query",omitempty` + // limit specifies the maximum number of solutions to be returned. This field is governed by + // max_result_count, which defines the upper limit of results that may be requested per query. + // If this field is not explicitly set, a default value of 1 is applied. + Limit *cosmossdk_io_math.Uint `protobuf:"bytes,3,opt,name=limit,proto3,customtype=cosmossdk.io/math.Uint" json:"limit,omitempty" yaml:"limit",omitempty` } func (m *QueryServiceAskRequest) Reset() { *m = QueryServiceAskRequest{} } @@ -251,40 +256,43 @@ func init() { func init() { proto.RegisterFile("logic/v1beta2/query.proto", fileDescriptor_008a54e610b23239) } var fileDescriptor_008a54e610b23239 = []byte{ - // 526 bytes of a gzipped FileDescriptorProto + // 567 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x4d, 0x6f, 0xd3, 0x40, - 0x10, 0x8d, 0xd3, 0x10, 0x60, 0x0b, 0x97, 0x15, 0x6d, 0x1c, 0x37, 0xb5, 0x23, 0x23, 0x50, 0x91, - 0x90, 0x0d, 0x29, 0x42, 0xa8, 0x12, 0x87, 0xf8, 0xc8, 0x81, 0x8f, 0x22, 0x2e, 0x5c, 0xaa, 0x4d, - 0xb2, 0xda, 0x58, 0x89, 0xbd, 0xae, 0x77, 0x5d, 0xc8, 0x09, 0x09, 0x71, 0xe0, 0x88, 0xc4, 0x85, - 0x0b, 0x12, 0x3f, 0xa7, 0xc7, 0x4a, 0x5c, 0x38, 0x59, 0x28, 0xe1, 0x17, 0xf8, 0x17, 0xa0, 0xec, - 0x6c, 0x54, 0xbb, 0x2d, 0x1f, 0x97, 0x28, 0x9a, 0xf7, 0xde, 0xbc, 0x37, 0xe3, 0x59, 0xd4, 0x9e, - 0x72, 0x16, 0x0e, 0xfd, 0xa3, 0xfb, 0x03, 0x2a, 0x49, 0xcf, 0x3f, 0xcc, 0x68, 0x3a, 0xf3, 0x92, - 0x94, 0x4b, 0x8e, 0xaf, 0x2b, 0xc8, 0xd3, 0x90, 0x75, 0x83, 0x71, 0xc6, 0x15, 0xe2, 0x2f, 0xff, - 0x01, 0xc9, 0xea, 0x30, 0xce, 0xd9, 0x94, 0xfa, 0x24, 0x09, 0x7d, 0x12, 0xc7, 0x5c, 0x12, 0x19, - 0xf2, 0x58, 0x68, 0xd4, 0xaa, 0x76, 0x4f, 0x48, 0x4a, 0xa2, 0x15, 0x76, 0xc6, 0x59, 0xce, 0x12, - 0xaa, 0x21, 0x77, 0x0b, 0xb5, 0x5f, 0x2c, 0x83, 0xbc, 0xa4, 0xe9, 0x51, 0x38, 0xa4, 0xcf, 0x95, - 0x6c, 0x9f, 0x1e, 0x66, 0x54, 0x48, 0x77, 0x8a, 0xac, 0x8b, 0x40, 0x91, 0xf0, 0x58, 0x50, 0xfc, - 0x14, 0x35, 0xc1, 0xc5, 0x34, 0xba, 0xc6, 0xce, 0x7a, 0x6f, 0xc3, 0xab, 0x4c, 0xe1, 0x01, 0x3d, - 0x70, 0x8e, 0x73, 0xa7, 0x56, 0xe4, 0x4e, 0x6b, 0x46, 0xa2, 0xe9, 0x9e, 0x0b, 0x12, 0xf7, 0x2e, - 0x8f, 0x42, 0x49, 0xa3, 0x44, 0xce, 0xf6, 0x75, 0x17, 0xf7, 0xa3, 0x81, 0x36, 0xcb, 0x76, 0x7d, - 0x31, 0xd1, 0x41, 0xf0, 0x43, 0x74, 0x39, 0x49, 0x39, 0x4b, 0x49, 0xa4, 0xbc, 0xae, 0x06, 0x9d, - 0x22, 0x77, 0x4c, 0xdd, 0x10, 0x80, 0x72, 0xc7, 0x15, 0x19, 0xdf, 0x43, 0x97, 0xd4, 0x9a, 0xcd, - 0xba, 0x52, 0x59, 0x45, 0xee, 0x6c, 0x82, 0x4a, 0x95, 0xcb, 0x1a, 0x20, 0xee, 0x35, 0xbe, 0x7c, - 0x73, 0x0c, 0xf7, 0x6b, 0x1d, 0xb5, 0xce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, 0x62, 0x66, 0x6a, 0x3f, 0x66, 0xa6, 0xf6, 0x71, 0x6e, 0x96, 0x2e, 0xe6, 0x66, + 0xe9, 0x72, 0x6e, 0x96, 0x5e, 0xdf, 0xf1, 0x98, 0x1c, 0xc6, 0x3d, 0xa7, 0xcf, 0xfd, 0x3c, 0xf5, + 0x6d, 0x46, 0x86, 0x17, 0xdd, 0xab, 0xc2, 0x93, 0xde, 0xff, 0x15, 0x00, 0x00, 0xff, 0xff, 0x55, + 0x66, 0x3e, 0x63, 0x69, 0x04, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -487,6 +495,18 @@ func (m *QueryServiceAskRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) _ = i var l int _ = l + if m.Limit != nil { + { + size := m.Limit.Size() + i -= size + if _, err := m.Limit.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } if len(m.Query) > 0 { i -= len(m.Query) copy(dAtA[i:], m.Query) @@ -601,6 +621,10 @@ func (m *QueryServiceAskRequest) Size() (n int) { if l > 0 { n += 1 + l + sovQuery(uint64(l)) } + if m.Limit != nil { + l = m.Limit.Size() + n += 1 + l + sovQuery(uint64(l)) + } return n } @@ -859,6 +883,42 @@ func (m *QueryServiceAskRequest) Unmarshal(dAtA []byte) error { } m.Query = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Limit", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + var v cosmossdk_io_math.Uint + m.Limit = &v + if err := m.Limit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipQuery(dAtA[iNdEx:]) diff --git a/x/logic/wasm/query.go b/x/logic/wasm/query.go index cf90205c..ca941dc1 100644 --- a/x/logic/wasm/query.go +++ b/x/logic/wasm/query.go @@ -27,6 +27,7 @@ func (querier LogicQuerier) Ask(ctx sdk.Context, query AskQuery) ([]byte, error) grpcResp, err := querier.k.Ask(ctx, &types.QueryServiceAskRequest{ Program: query.Program, Query: query.Query, + Limit: query.Limit, }) if err != nil { return nil, err diff --git a/x/logic/wasm/types.go b/x/logic/wasm/types.go index ef4e72f9..ebec8c01 100644 --- a/x/logic/wasm/types.go +++ b/x/logic/wasm/types.go @@ -1,13 +1,18 @@ package wasm -import "github.com/okp4/okp4d/v7/x/logic/types" +import ( + sdkmath "cosmossdk.io/math" + + "github.com/okp4/okp4d/v7/x/logic/types" +) // AskQuery implements the wasm custom Ask query JSON schema, it basically redefined the Ask gRPC request parameters // to keep control in case of eventual breaking change in the logic module definition, and to decouple the // serialization logic. type AskQuery struct { - Program string `json:"program"` - Query string `json:"query"` + Program string `json:"program"` + Query string `json:"query"` + Limit *sdkmath.Uint `json:"limit"` } // AskResponse implements the Ask query response JSON schema in a wasm custom query purpose, it redefines the existing