diff --git a/proto/logic/v1beta2/types.proto b/proto/logic/v1beta2/types.proto index 299e0d56..9ad5f3b8 100644 --- a/proto/logic/v1beta2/types.proto +++ b/proto/logic/v1beta2/types.proto @@ -20,8 +20,14 @@ message Substitution { message Result { option (gogoproto.goproto_stringer) = true; + // error specifies the error message if the query caused an error. + string error = 5 [ + (gogoproto.nullable) = true, + (gogoproto.moretags) = "yaml:\"error\",omitempty" + ]; + // substitutions represent all the substitutions made to the variables in the query to obtain the answer. - repeated Substitution substitutions = 1 [ + repeated Substitution substitutions = 2 [ (gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"substitutions\",omitempty" ]; @@ -31,18 +37,11 @@ message Result { message Answer { option (gogoproto.goproto_stringer) = true; - // success specifies if the query was successful. - bool success = 1 [(gogoproto.moretags) = "yaml:\"success\",omitempty"]; - // error specifies the error message if the query caused an error. - string error = 5 [ - (gogoproto.nullable) = true, - (gogoproto.moretags) = "yaml:\"error\",omitempty" - ]; // has_more specifies if there are more solutions than the ones returned. bool has_more = 2 [(gogoproto.moretags) = "yaml:\"has_more\",omitempty"]; // variables represent all the variables in the query. repeated string variables = 3 [ - (gogoproto.nullable) = true, + (gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"variables\",omitempty" ]; // results represent all the results of the query. diff --git a/x/logic/types/types.pb.go b/x/logic/types/types.pb.go index e9f3be5f..eb1d65eb 100644 --- a/x/logic/types/types.pb.go +++ b/x/logic/types/types.pb.go @@ -80,8 +80,10 @@ func (m *Substitution) GetExpression() string { // Result represents the result of a query. type Result struct { + // error specifies the error message if the query caused an error. + Error string `protobuf:"bytes,5,opt,name=error,proto3" json:"error,omitempty" yaml:"error",omitempty` // substitutions represent all the substitutions made to the variables in the query to obtain the answer. - Substitutions []Substitution `protobuf:"bytes,1,rep,name=substitutions,proto3" json:"substitutions" yaml:"substitutions",omitempty` + Substitutions []Substitution `protobuf:"bytes,2,rep,name=substitutions,proto3" json:"substitutions" yaml:"substitutions",omitempty` } func (m *Result) Reset() { *m = Result{} } @@ -117,6 +119,13 @@ func (m *Result) XXX_DiscardUnknown() { var xxx_messageInfo_Result proto.InternalMessageInfo +func (m *Result) GetError() string { + if m != nil { + return m.Error + } + return "" +} + func (m *Result) GetSubstitutions() []Substitution { if m != nil { return m.Substitutions @@ -126,10 +135,6 @@ func (m *Result) GetSubstitutions() []Substitution { // Answer represents the answer to a logic query. type Answer struct { - // success specifies if the query was successful. - Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty" yaml:"success",omitempty` - // error specifies the error message if the query caused an error. - Error string `protobuf:"bytes,5,opt,name=error,proto3" json:"error,omitempty" yaml:"error",omitempty` // has_more specifies if there are more solutions than the ones returned. HasMore bool `protobuf:"varint,2,opt,name=has_more,json=hasMore,proto3" json:"has_more,omitempty" yaml:"has_more",omitempty` // variables represent all the variables in the query. @@ -171,20 +176,6 @@ func (m *Answer) XXX_DiscardUnknown() { var xxx_messageInfo_Answer proto.InternalMessageInfo -func (m *Answer) GetSuccess() bool { - if m != nil { - return m.Success - } - return false -} - -func (m *Answer) GetError() string { - if m != nil { - return m.Error - } - return "" -} - func (m *Answer) GetHasMore() bool { if m != nil { return m.HasMore @@ -215,34 +206,33 @@ func init() { func init() { proto.RegisterFile("logic/v1beta2/types.proto", fileDescriptor_f3c73c95465ca7a8) } var fileDescriptor_f3c73c95465ca7a8 = []byte{ - // 430 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x92, 0x41, 0x6f, 0xd3, 0x30, - 0x14, 0xc7, 0xe3, 0x75, 0xeb, 0x52, 0xc3, 0x2e, 0x16, 0xa0, 0x6c, 0x03, 0x3b, 0x0a, 0x12, 0xea, - 0x01, 0x25, 0x62, 0x4c, 0x08, 0x26, 0x21, 0x20, 0x77, 0x0e, 0x8c, 0x1b, 0x17, 0x94, 0x04, 0x2b, - 0xb5, 0x48, 0xea, 0xc8, 0x76, 0xc6,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, 0xe5, - 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x19, 0x3c, 0x55, 0x00, 0xe3, 0x02, 0x00, 0x00, + // 408 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x5c, 0x92, 0xb1, 0xae, 0xd3, 0x30, + 0x18, 0x85, 0xe3, 0xdb, 0x7b, 0x7b, 0x5b, 0xc3, 0x5d, 0x2c, 0x40, 0xb9, 0xb7, 0xc2, 0x8e, 0x82, + 0x84, 0x3a, 0xa0, 0x44, 0x94, 0x0e, 0x50, 0x09, 0x01, 0xd9, 0x19, 0x28, 0x1b, 0x0b, 0x4a, 0x8a, + 0x95, 0x5a, 0x24, 0x75, 0x64, 0x3b, 0xa5, 0x7d, 0x8b, 0x8e, 0x8c, 0xbc, 0x01, 0xaf, 0xd1, 0xb1, + 0x23, 0x0b, 0x11, 0x6a, 0xdf, 0x20, 0x4f, 0x80, 0x6a, 0xa7, 0xd4, 0x61, 0x89, 0xa2, 0x9c, 0xff, + 0x7c, 0xf9, 0xcf, 0xd1, 0x0f, 0x6f, 0x33, 0x9e, 0xb2, 0x59, 0xb8, 0x7c, 0x9e, 0x50, 0x15, 0x8f, + 0x42, 0xb5, 0x2e, 0xa8, 0x0c, 0x0a, 0xc1, 0x15, 0x47, 0x37, 0x5a, 0x0a, 0x1a, 0xe9, 0xee, 0x41, + 0xca, 0x53, 0xae, 0x95, 0xf0, 0xf8, 0x66, 0x86, 0xfc, 0x0d, 0x80, 0xf7, 0x3f, 0x96, 0x89, 0x54, + 0x4c, 0x95, 0x8a, 0xf1, 0x05, 0x7a, 0x05, 0x7b, 0xcb, 0x58, 0xb0, 0x38, 0xc9, 0xa8, 0x0b, 0x3c, + 0x30, 0xec, 0x47, 0x8f, 0xeb, 0x8a, 0xdc, 0xae, 0xe3, 0x3c, 0x9b, 0xf8, 0x27, 0xc5, 0x7f, 0xc6, + 0x73, 0xa6, 0x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c, 0xd1, 0xeb, 0xed, 0x1e, 0x83, 0xdd, 0x1e, + 0x83, 0x3f, 0x7b, 0x0c, 0x36, 0x07, 0xec, 0xec, 0x0e, 0xd8, 0xf9, 0x75, 0xc0, 0xce, 0xa7, 0x27, + 0x29, 0x53, 0xf3, 0x32, 0x09, 0x66, 0x3c, 0x0f, 0xf9, 0xd7, 0x62, 0xac, 0x1f, 0x5f, 0xc2, 0x55, + 0x68, 0xce, 0x52, 0x9f, 0x63, 0xd2, 0xd5, 0xa7, 0xf6, 0xe2, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, + 0x29, 0x03, 0xd0, 0xbf, 0xac, 0x02, 0x00, 0x00, } func (m *Substitution) Marshal() (dAtA []byte, err error) { @@ -302,6 +292,13 @@ func (m *Result) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Error) > 0 { + i -= len(m.Error) + copy(dAtA[i:], m.Error) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Error))) + i-- + dAtA[i] = 0x2a + } if len(m.Substitutions) > 0 { for iNdEx := len(m.Substitutions) - 1; iNdEx >= 0; iNdEx-- { { @@ -313,7 +310,7 @@ func (m *Result) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintTypes(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0xa + dAtA[i] = 0x12 } } return len(dAtA) - i, nil @@ -339,13 +336,6 @@ func (m *Answer) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.Error) > 0 { - i -= len(m.Error) - copy(dAtA[i:], m.Error) - i = encodeVarintTypes(dAtA, i, uint64(len(m.Error))) - i-- - dAtA[i] = 0x2a - } if len(m.Results) > 0 { for iNdEx := len(m.Results) - 1; iNdEx >= 0; iNdEx-- { { @@ -379,16 +369,6 @@ func (m *Answer) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x10 } - if m.Success { - i-- - if m.Success { - dAtA[i] = 1 - } else { - dAtA[i] = 0 - } - i-- - dAtA[i] = 0x8 - } return len(dAtA) - i, nil } @@ -432,6 +412,10 @@ func (m *Result) Size() (n int) { n += 1 + l + sovTypes(uint64(l)) } } + l = len(m.Error) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } return n } @@ -441,9 +425,6 @@ func (m *Answer) Size() (n int) { } var l int _ = l - if m.Success { - n += 2 - } if m.HasMore { n += 2 } @@ -459,10 +440,6 @@ func (m *Answer) Size() (n int) { n += 1 + l + sovTypes(uint64(l)) } } - l = len(m.Error) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) - } return n } @@ -615,7 +592,7 @@ func (m *Result) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: Result: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { - case 1: + case 2: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Substitutions", wireType) } @@ -649,6 +626,38 @@ func (m *Result) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + 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 ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Error = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) @@ -699,26 +708,6 @@ func (m *Answer) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: Answer: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { - case 1: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field Success", wireType) - } - var v int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - v |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - m.Success = bool(v != 0) case 2: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field HasMore", wireType) @@ -805,38 +794,6 @@ func (m *Answer) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 5: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Error", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - 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 ErrInvalidLengthTypes - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Error = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/x/logic/util/prolog.go b/x/logic/util/prolog.go index f77e5d93..7a4f1bfe 100644 --- a/x/logic/util/prolog.go +++ b/x/logic/util/prolog.go @@ -13,6 +13,10 @@ import ( "github.com/okp4/okp4d/x/logic/types" ) +const ( + defaultEnvCap = uint64(50) +) + // QueryInterpreter interprets a query and returns the solutions up to the given limit. func QueryInterpreter( ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint, @@ -25,7 +29,7 @@ func QueryInterpreter( var env *engine.Env count := sdkmath.ZeroUint() - envs := make([]*engine.Env, 0, limit.Uint64()) + envs := make([]*engine.Env, 0, sdkmath.MinUint(limit, sdkmath.NewUint(defaultEnvCap)).Uint64()) _, callErr := engine.Call(&i.VM, t, func(env *engine.Env) *engine.Promise { if count.LT(limit) { envs = append(envs, env) @@ -34,21 +38,25 @@ func QueryInterpreter( return engine.Bool(count.GT(limit)) }, env).Force(ctx) - answerErr := lo.IfF(callErr != nil, func() string { - return callErr.Error() - }).Else("") - success := len(envs) > 0 - hasMore := count.GT(limit) vars := parsedVarsToVars(p.Vars) + results, err := envsToResults(envs, p.Vars, i) if err != nil { return nil, err } + if callErr != nil { + if sdkmath.NewUint(uint64(len(results))).LT(limit) { + // error is not part of the look-ahead and should be included in the solutions + results = append(results, types.Result{Error: callErr.Error()}) + } else { + // error is part of the look-ahead, so let's consider that there's one more solution + count = count.Incr() + } + } + return &types.Answer{ - Success: success, - Error: answerErr, - HasMore: hasMore, + HasMore: count.GT(limit), Variables: vars, Results: results, }, nil diff --git a/x/logic/wasm/types.go b/x/logic/wasm/types.go index e8d6a217..57415c67 100644 --- a/x/logic/wasm/types.go +++ b/x/logic/wasm/types.go @@ -34,16 +34,12 @@ func (to *AskResponse) from(from types.QueryServiceAskResponse) { // Answer denotes the Answer element JSON representation in an AskResponse for wasm custom query purpose, it redefines // the existing generated type from proto to ensure a dedicated serialization logic. type Answer struct { - Success bool `json:"success"` - Error string `json:"error,omitempty"` HasMore bool `json:"has_more"` Variables []string `json:"variables"` Results []Result `json:"results"` } func (to *Answer) from(from types.Answer) { - to.Success = from.Success - to.Error = from.Error to.HasMore = from.HasMore to.Variables = from.Variables if to.Variables == nil { @@ -60,10 +56,12 @@ func (to *Answer) from(from types.Answer) { // Result denotes the Result element JSON representation in an AskResponse for wasm custom query purpose, it redefines // the existing generated type from proto to ensure a dedicated serialization logic. type Result struct { + Error string `json:"error,omitempty"` Substitutions []Substitution `json:"substitutions"` } func (to *Result) from(from types.Result) { + to.Error = from.Error to.Substitutions = make([]Substitution, 0, len(from.Substitutions)) for _, fromSubstitution := range from.Substitutions { substitution := new(Substitution)