diff --git a/proto/logic/v1beta2/types.proto b/proto/logic/v1beta2/types.proto index 8dfe42fe..c495c635 100644 --- a/proto/logic/v1beta2/types.proto +++ b/proto/logic/v1beta2/types.proto @@ -47,8 +47,13 @@ message Result { message Answer { option (gogoproto.goproto_stringer) = true; - // result is the result of the query. + // 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. diff --git a/x/logic/keeper/grpc_query_ask_test.go b/x/logic/keeper/grpc_query_ask_test.go index 8dd1458f..67ceb917 100644 --- a/x/logic/keeper/grpc_query_ask_test.go +++ b/x/logic/keeper/grpc_query_ask_test.go @@ -28,13 +28,13 @@ func TestGRPCAsk(t *testing.T) { cases := []struct { program string query string - expectedAsnwer *types.Answer - expectedError bool + expectedAnswer *types.Answer + expectedError string }{ { program: "father(bob, alice).", query: "father(bob, X).", - expectedAsnwer: &types.Answer{ + expectedAnswer: &types.Answer{ Success: true, HasMore: false, Variables: []string{"X"}, @@ -46,12 +46,27 @@ func TestGRPCAsk(t *testing.T) { }, }}}}, }, - expectedError: false, + }, + { + program: `father("bob", "alice").`, + query: `father("bob", X).`, + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"X"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "X", + Term: types.Term{ + Name: "[a,l,i,c,e]", + Arguments: nil, + }, + }}}}, + }, }, { program: "father(bob, alice). father(bob, john).", query: "father(bob, X).", - expectedAsnwer: &types.Answer{ + expectedAnswer: &types.Answer{ Success: true, HasMore: true, Variables: []string{"X"}, @@ -63,24 +78,74 @@ func TestGRPCAsk(t *testing.T) { }, }}}}, }, - expectedError: false, }, { program: "father(bob, alice).", query: "father(bob, john).", - expectedAsnwer: &types.Answer{ + expectedAnswer: &types.Answer{ Success: false, HasMore: false, Variables: nil, Results: nil, }, - expectedError: false, }, { - program: "father(bob, alice).", - query: "father(bob, X, O).", - expectedAsnwer: nil, - expectedError: true, + program: "", + query: "block_height(X).", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"X"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "X", + Term: types.Term{ + Name: "0", + Arguments: nil, + }, + }}}}, + }, + }, + { + program: "father(bob, 'élodie').", + query: "father(bob, X).", + expectedAnswer: &types.Answer{ + Success: true, + HasMore: false, + Variables: []string{"X"}, + Results: []types.Result{{Substitutions: []types.Substitution{{ + Variable: "X", + Term: types.Term{ + Name: "élodie", + Arguments: nil, + }, + }}}}, + }, + }, + { + program: "father(bob, alice).", + query: "father(bob, X, O).", + expectedAnswer: &types.Answer{ + Success: false, + Error: "error(existence_error(procedure,father/3),root)", + HasMore: false, + Variables: nil, + Results: nil, + }, + }, + { + program: "father°(bob, alice).", + query: "father(bob, X).", + expectedError: "error compiling query: unexpected token: invalid(°): invalid argument", + }, + { + program: "father(bob, alice).", + query: "father°(bob, X).", + expectedError: "error executing query: unexpected token: invalid(°): invalid argument", + }, + { + program: `father("bob", "alice").`, + query: `father("bob"", X).`, + expectedError: "error executing query: EOF: invalid argument", }, } @@ -128,13 +193,14 @@ func TestGRPCAsk(t *testing.T) { result, err := queryClient.Ask(gocontext.Background(), &query) Convey("Then it should return the expected answer", func() { - if tc.expectedError { - So(err, ShouldNotBeNil) + if tc.expectedError != "" { + So(err, ShouldNotEqual, nil) + So(err.Error(), ShouldEqual, tc.expectedError) So(result, ShouldBeNil) } else { - So(err, ShouldBeNil) + So(err, ShouldEqual, nil) So(result, ShouldNotBeNil) - So(result.Answer, ShouldResemble, tc.expectedAsnwer) + So(result.Answer, ShouldResemble, tc.expectedAnswer) } }) }) diff --git a/x/logic/keeper/interpreter.go b/x/logic/keeper/interpreter.go index 13df9178..a08451bc 100644 --- a/x/logic/keeper/interpreter.go +++ b/x/logic/keeper/interpreter.go @@ -58,12 +58,32 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS _ = sols.Close() }() - success := false - limits := k.limits(ctx) + var userOutput string + if userOutputBuffer != nil { + userOutput = userOutputBuffer.String() + } + answer, err := k.solsToAnswer(sdkCtx, sols) //nolint:contextcheck + if err != nil { + return nil, err + } + + return &types.QueryServiceAskResponse{ + Height: uint64(sdkCtx.BlockHeight()), + GasUsed: sdkCtx.GasMeter().GasConsumed(), + Answer: answer, + UserOutput: userOutput, + }, nil +} + +// solsToAnswer consumes the given prolog solutions and convert it to an answer. +func (k Keeper) solsToAnswer(sdkCtx sdk.Context, sols *prolog.Solutions) (*types.Answer, error) { + solSuccess := false + solError := "" + limits := k.limits(sdkCtx) var variables []string results := make([]types.Result, 0) for nb := sdkmath.ZeroUint(); nb.LT(*limits.MaxResultCount) && sols.Next(); nb = nb.Incr() { - success = true + solSuccess = true m := types.TermResults{} if err := sols.Scan(m); err != nil { @@ -80,24 +100,15 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS if sdkCtx.GasMeter().IsOutOfGas() { panic(sdk.ErrorOutOfGas{Descriptor: "Prolog interpreter execution"}) } - return nil, errorsmod.Wrapf(types.InvalidArgument, "error interpreting solutions: %v", err.Error()) - } - - var userOutput string - if userOutputBuffer != nil { - userOutput = userOutputBuffer.String() + solError = sols.Err().Error() } - return &types.QueryServiceAskResponse{ - Height: uint64(sdkCtx.BlockHeight()), - GasUsed: sdkCtx.GasMeter().GasConsumed(), - Answer: &types.Answer{ - Success: success, - HasMore: sols.Next(), - Variables: variables, - Results: results, - }, - UserOutput: userOutput, + return &types.Answer{ + Success: solSuccess, + Error: solError, + HasMore: sols.Next(), + Variables: variables, + Results: results, }, nil } diff --git a/x/logic/meter/safe.go b/x/logic/meter/safe.go index 1dee7806..4edf26de 100644 --- a/x/logic/meter/safe.go +++ b/x/logic/meter/safe.go @@ -7,7 +7,7 @@ import ( ) // safeMeterDecorater is a wrapper around sdk.GasMeter that provides go-routine-safe access to the underlying gas meter. -// This is needed because the interpreter is uses multiple go-routines, and the gas meter is shared between multiple +// This is needed because the interpreter uses multiple go-routines, and the gas meter is shared between multiple // goroutines. type safeMeterDecorater struct { gasMeter sdk.GasMeter diff --git a/x/logic/types/types.pb.go b/x/logic/types/types.pb.go index 1d536f5f..32a2ee93 100644 --- a/x/logic/types/types.pb.go +++ b/x/logic/types/types.pb.go @@ -181,8 +181,10 @@ func (m *Result) GetSubstitutions() []Substitution { // Answer represents the answer to a logic query. type Answer struct { - // result is the result of the query. + // 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. @@ -231,6 +233,13 @@ func (m *Answer) GetSuccess() bool { 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 @@ -262,36 +271,38 @@ func init() { func init() { proto.RegisterFile("logic/v1beta2/types.proto", fileDescriptor_f3c73c95465ca7a8) } var fileDescriptor_f3c73c95465ca7a8 = []byte{ - // 462 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x53, 0x41, 0x6b, 0xd4, 0x40, - 0x14, 0xde, 0xd9, 0x0d, 0xdb, 0xdd, 0xd1, 0x5e, 0x46, 0x0b, 0xd9, 0x6a, 0x93, 0x10, 0x41, 0xf6, - 0xa0, 0x09, 0x56, 0x11, 0x2d, 0x08, 0x9a, 0x83, 0x37, 0x0f, 0x56, 0xbd, 0x78, 0x91, 0x49, 0x1c, - 0xb2, 0xc1, 0x4c, 0x26, 0xcc, 0x4c, 0xaa, 0xf9, 0x17, 0x22, 0x88, 0x1e, 0xfd, 0x39, 0x3d, 0xf6, - 0xe8, 0x29, 0xc8, 0xee, 0x3f, 0xc8, 0x2f, 0x90, 0x4c, 0x26, 0x69, 0x52, 0xe8, 0x25, 0x84, 0xf7, - 0xbd, 0xf7, 0x7d, 0xdf, 0xfb, 0x86, 0x07, 0x57, 0x29, 0x8b, 0x93, 0xc8, 0x3f, 0x7b, 0x14, 0x12, - 0x89, 0x8f, 0x7d, 0x59, 0xe6, 0x44, 0x78, 0x39, 0x67, 0x92, 0xa1, 0x7d, 0x05, 0x79, 0x1a, 0x3a, - 0xbc, 0x1d, 0xb3, 0x98, 0x29, 0xc4, 0x6f, 0xfe, 0xda, 0x26, 0xf7, 0x07, 0x80, 0xc6, 0x7b, 0xc2, - 0x29, 0x7a, 0x08, 0x8d, 0x0c, 0x53, 0x62, 0x02, 0x07, 0xac, 0x97, 0xc1, 0xaa, 0xae, 0xec, 0x83, - 0x12, 0xd3, 0xf4, 0xc4, 0x6d, 0xaa, 0xee, 0x03, 0x46, 0x13, 0x49, 0x68, 0x2e, 0xcb, 0x53, 0xd5, - 0x86, 0x3e, 0xc0, 0x25, 0xe6, 0x71, 0x41, 0x49, 0x26, 0x85, 0x39, 0x75, 0x66, 0xeb, 0x1b, 0xc7, - 0xb7, 0xbc, 0x91, 0xa0, 0xd7, 0xd0, 0x06, 0xee, 0x79, 0x65, 0x4f, 0xea, 0xca, 0x3e, 0x6c, 0xc9, - 0xfa, 0x99, 0x21, 0xe3, 0x25, 0xd3, 0x89, 0xf1, 0xfb, 0x8f, 0x0d, 0xdc, 0x5f, 0x00, 0xde, 0x7c, - 0x57, 0x84, 0x42, 0x26, 0xb2, 0x90, 0x09, 0xcb, 0xd0, 0x73, 0xb8, 0x38, 0xc3, 0x3c, 0xc1, 0x61, - 0xda, 0x19, 0x3c, 0xaa,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f, 0x59, 0x93, 0x8b, 0x9d, 0x35, - 0xf9, 0xbb, 0xb3, 0x26, 0x1f, 0xef, 0xc5, 0x89, 0xdc, 0x14, 0xa1, 0x17, 0x31, 0xea, 0xb3, 0x2f, - 0xf9, 0x13, 0xf5, 0xf9, 0xec, 0x7f, 0xf3, 0xdb, 0x9b, 0x51, 0xb7, 0x12, 0xce, 0xd5, 0x1d, 0x3c, - 0xfe, 0x1f, 0x00, 0x00, 0xff, 0xff, 0x32, 0x02, 0x5a, 0x52, 0x49, 0x03, 0x00, 0x00, + // 489 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x53, 0x41, 0x6f, 0xd3, 0x30, + 0x18, 0xad, 0xdb, 0xac, 0x6b, 0x0d, 0xbb, 0x18, 0x86, 0xd2, 0xc1, 0x92, 0x28, 0x48, 0xa8, 0x07, + 0x48, 0xc4, 0x98, 0x10, 0x4c, 0x42, 0x82, 0x1c, 0xb8, 0x71, 0x60, 0xc0, 0x85, 0x0b, 0x72, 0x8a, + 0x95, 0x46, 0xd4, 0x75, 0x65, 0x3b, 0x83, 0xfe, 0x0b, 0xc4, 0x05, 0x8e, 0xfc, 0x9c, 0x4a, 0x5c, + 0x76, 0xe4, 0x14, 0xa1, 0xf6, 0x1f, 0xe4, 0x17, 0xa0, 0xd8, 0x4e, 0xe7, 0x54, 0xda, 0x25, 0x8a, + 0xbe, 0xf7, 0x7d, 0xef, 0x3d, 0x3f, 0xfb, 0x83, 0xa3, 0x19, 0xcb, 0xf2, 0x49, 0x7c, 0xf1, 0x38, + 0x25, 0x12, 0x9f, 0xc4, 0x72, 0xb9, 0x20, 0x22, 0x5a, 0x70, 0x26, 0x19, 0x3a, 0x50, 0x50, 0x64, + 0xa0, 0xa3, 0xdb, 0x19, 0xcb, 0x98, 0x42, 0xe2, 0xfa, 0x4f, 0x37, 0x85, 0x3f, 0x00, 0x74, 0xde, + 0x13,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, 0x4c, 0x26, 0x44, 0x08, 0x95, 0xc6, 0x20, 0xb9, 0x57, 0x95, + 0xbe, 0xdb, 0x90, 0x2a, 0xc0, 0xa6, 0x6b, 0x9a, 0xd1, 0x29, 0xdc, 0x23, 0x9c, 0x33, 0xee, 0xee, + 0xa9, 0x0c, 0xbd, 0x55, 0xe9, 0x83, 0xaa, 0xf4, 0xef, 0xe8, 0x49, 0x05, 0xd9, 0x73, 0xba, 0x19, + 0x3d, 0x83, 0x83, 0x29, 0x16, 0x9f, 0x28, 0xe3, 0x44, 0xa5, 0x38, 0xb0, 0xc3, 0x6f, 0x90, 0x96, + 0xde, 0x14, 0x8b, 0x37, 0x8c, 0x13, 0xf4, 0x12, 0x0e, 0x9b, 0x7b, 0x10, 0x6e, 0x2f, 0xe8, 0x8d, + 0x87, 0xea, 0x3d, 0x80, 0xab, 0xf7, 0xb0, 0x85, 0x5b, 0xef, 0x61, 0x5b, 0x45, 0x6f, 0xe1, 0x3e, + 0x57, 0x79, 0x0b, 0xd7, 0x51, 0xf9, 0x1e, 0xee, 0xe4, 0xab, 0x6f, 0x23, 0x09, 0x4c, 0xb2, 0x26, + 0x04, 0x33, 0xd3, 0x32, 0x65, 0x6a, 0x3a, 0xcd, 0xe4, 0xc5, 0x6a, 0xed, 0x81, 0xcb, 0xb5, 0x07, + 0xfe, 0xad, 0x3d, 0xf0, 0x7d, 0xe3, 0x75, 0x2e, 0x37, 0x5e, 0xe7, 0xef, 0xc6, 0xeb, 0x7c, 0xbc, + 0x9f, 0xe5, 0x72, 0x5a, 0xa4, 0xd1, 0x84, 0xd1, 0x98, 0x7d, 0x59, 0x9c, 0xaa, 0xcf, 0xe7, 0xf8, + 0x5b, 0xac, 0x37, 0x4d, 0x6d, 0x58, 0xda, 0x57, 0xdb, 0xf3, 0xe4, 0x7f, 0x00, 0x00, 0x00, 0xff, + 0xff, 0xc0, 0x13, 0x1d, 0xac, 0x7f, 0x03, 0x00, 0x00, } func (m *Term) Marshal() (dAtA []byte, err error) { @@ -435,6 +446,13 @@ 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-- { { @@ -565,6 +583,10 @@ 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 } @@ -1024,6 +1046,38 @@ 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:])