diff --git a/proto/logic/v1beta2/types.proto b/proto/logic/v1beta2/types.proto index c495c635..299e0d56 100644 --- a/proto/logic/v1beta2/types.proto +++ b/proto/logic/v1beta2/types.proto @@ -6,30 +6,14 @@ import "gogoproto/gogo.proto"; option go_package = "github.com/okp4/okp4d/x/logic/types"; -// Term is the representation of a piece of data and can be a constant, a variable, or an atom. -message Term { - option (gogoproto.goproto_stringer) = true; - - // name is the name of the term. - string name = 1 [(gogoproto.moretags) = "yaml:\"name\",omitempty"]; - // arguments are the arguments of the term, which can be constants, variables, or atoms. - repeated Term arguments = 2 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"arguments\",omitempty" - ]; -} - // Substitution represents a substitution made to the variables in the query to obtain the answer. message Substitution { option (gogoproto.goproto_stringer) = true; // variable is the name of the variable. string variable = 1 [(gogoproto.moretags) = "yaml:\"variable\",omitempty"]; - // term is the term that the variable is substituted with. - Term term = 2 [ - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"term\",omitempty" - ]; + // expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). + string expression = 2 [(gogoproto.moretags) = "yaml:\"expression\",omitempty"]; } // Result represents the result of a query. diff --git a/x/logic/keeper/interpreter.go b/x/logic/keeper/interpreter.go index da5ca07b..4d95349e 100644 --- a/x/logic/keeper/interpreter.go +++ b/x/logic/keeper/interpreter.go @@ -1,15 +1,16 @@ package keeper import ( - goctx "context" + "context" "math" + "strings" "github.com/ichiban/prolog" + "github.com/ichiban/prolog/engine" "github.com/samber/lo" errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" - storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,21 +28,22 @@ const ( defaultWeightFactor = uint64(1) ) -func (k Keeper) limits(ctx goctx.Context) types.Limits { +func (k Keeper) limits(ctx context.Context) types.Limits { params := k.GetParams(sdk.UnwrapSDKContext(ctx)) return params.GetLimits() } -func (k Keeper) enhanceContext(ctx goctx.Context) goctx.Context { +func (k Keeper) enhanceContext(ctx context.Context) context.Context { sdkCtx := sdk.UnwrapSDKContext(ctx) sdkCtx = sdkCtx.WithValue(types.AuthKeeperContextKey, k.authKeeper) sdkCtx = sdkCtx.WithValue(types.BankKeeperContextKey, k.bankKeeper) return sdkCtx } -func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryServiceAskResponse, error) { +func (k Keeper) execute(ctx context.Context, program, query string) (*types.QueryServiceAskResponse, error) { ctx = k.enhanceContext(ctx) sdkCtx := sdk.UnwrapSDKContext(ctx) + limits := k.limits(sdkCtx) i, userOutputBuffer, err := k.newInterpreter(ctx) if err != nil { @@ -51,22 +53,15 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS return nil, errorsmod.Wrapf(types.InvalidArgument, "error compiling query: %v", err.Error()) } - sols, err := i.QueryContext(ctx, query) + answer, err := k.queryInterpreter(ctx, i, query, *limits.MaxResultCount) if err != nil { return nil, errorsmod.Wrapf(types.InvalidArgument, "error executing query: %v", err.Error()) } - defer func() { - _ = sols.Close() - }() var userOutput string if userOutputBuffer != nil { userOutput = userOutputBuffer.String() } - answer, err := k.solsToAnswer(sdkCtx, sols) - if err != nil { - return nil, err - } return &types.QueryServiceAskResponse{ Height: uint64(sdkCtx.BlockHeight()), @@ -76,55 +71,47 @@ func (k Keeper) execute(ctx goctx.Context, program, query string) (*types.QueryS }, 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() { - solSuccess = true - - m := types.TermResults{} - if err := sols.Scan(m); err != nil { - return nil, errorsmod.Wrapf(types.Internal, "error scanning solution: %v", err.Error()) - } - if nb.IsZero() { - variables = m.ToVariables() - } - - results = append(results, types.Result{Substitutions: m.ToSubstitutions()}) +// queryInterpreter executes the given query on the given interpreter and returns the answer. +func (k Keeper) queryInterpreter(ctx context.Context, i *prolog.Interpreter, query string, limit sdkmath.Uint) (*types.Answer, error) { + p := engine.NewParser(&i.VM, strings.NewReader(query)) + t, err := p.Term() + if err != nil { + return nil, err } - if err := sols.Err(); err != nil { - if sdkCtx.GasMeter().IsOutOfGas() { - panic(storetypes.ErrorOutOfGas{Descriptor: "Prolog interpreter execution"}) + var env *engine.Env + count := sdkmath.ZeroUint() + envs := make([]*engine.Env, 0, limit.Uint64()) + _, callErr := engine.Call(&i.VM, t, func(env *engine.Env) *engine.Promise { + if count.LT(limit) { + envs = append(envs, env) } - solError = sols.Err().Error() + count = count.Incr() + 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 } return &types.Answer{ - Success: solSuccess, - Error: solError, - HasMore: sols.Next(), - Variables: variables, + Success: success, + Error: answerErr, + HasMore: hasMore, + Variables: vars, Results: results, }, nil } -func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error { - size := sdkmath.NewUint(uint64(len(request.GetQuery()))) - limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64)) - if size.GT(limit) { - return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit) - } - - return nil -} - // newInterpreter creates a new interpreter properly configured. -func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.BoundedBuffer, error) { +func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, *util.BoundedBuffer, error) { sdkctx := sdk.UnwrapSDKContext(ctx) params := k.GetParams(sdkctx) @@ -173,6 +160,16 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.Bo return i, userOutputBuffer, err } +func checkLimits(request *types.QueryServiceAskRequest, limits types.Limits) error { + size := sdkmath.NewUint(uint64(len(request.GetQuery()))) + limit := util.DerefOrDefault(limits.MaxSize, sdkmath.NewUint(math.MaxInt64)) + if size.GT(limit) { + return errorsmod.Wrapf(types.LimitExceeded, "query: %d > MaxSize: %d", size, limit) + } + + return nil +} + // toPredicate converts the given predicate costs to a function that returns the cost for the given predicate as // a pair of predicate name and cost. func toPredicate(defaultCost uint64, predicateCosts []types.PredicateCost) func(string, int) lo.Tuple2[string, uint64] { @@ -196,3 +193,30 @@ func nonNilNorZeroOrDefaultUint64(v *sdkmath.Uint, defaultValue uint64) uint64 { return v.Uint64() } + +func parsedVarsToVars(vars []engine.ParsedVariable) []string { + return lo.Map(vars, func(v engine.ParsedVariable, _ int) string { + return v.Name.String() + }) +} + +func envsToResults(envs []*engine.Env, vars []engine.ParsedVariable, i *prolog.Interpreter) ([]types.Result, error) { + results := make([]types.Result, 0, len(envs)) + for _, rEnv := range envs { + substitutions := make([]types.Substitution, 0, len(vars)) + for _, v := range vars { + var expression prolog.TermString + err := expression.Scan(&i.VM, v.Variable, rEnv) + if err != nil { + return nil, err + } + substitution := types.Substitution{ + Variable: v.Name.String(), + Expression: string(expression), + } + substitutions = append(substitutions, substitution) + } + results = append(results, types.Result{Substitutions: substitutions}) + } + return results, nil +} diff --git a/x/logic/types/logic.go b/x/logic/types/logic.go deleted file mode 100644 index de9f6b75..00000000 --- a/x/logic/types/logic.go +++ /dev/null @@ -1,43 +0,0 @@ -package types - -import ( - "sort" - - "github.com/ichiban/prolog" -) - -// TermResults is a map from variable strings to prolog term values. -type TermResults map[string]prolog.TermString - -// ToSubstitutions converts a TermResults value to a slice of Substitution values. -// The slice is sorted in ascending order of variable names. -func (t TermResults) ToSubstitutions() []Substitution { - substitutions := make([]Substitution, 0, len(t)) - for v, ts := range t { - substitution := Substitution{ - Variable: v, - Term: Term{ - Name: string(ts), - }, - } - substitutions = append(substitutions, substitution) - } - - sort.Slice(substitutions, func(i, j int) bool { - return substitutions[i].Variable < substitutions[j].Variable - }) - - return substitutions -} - -// ToVariables extract from a TermResults value the variable names. -// The variable names are sorted in ascending order. -func (t TermResults) ToVariables() []string { - variables := make([]string, 0, len(t)) - for v := range t { - variables = append(variables, v) - } - sort.Strings(variables) - - return variables -} diff --git a/x/logic/types/types.pb.go b/x/logic/types/types.pb.go index 32a2ee93..e9f3be5f 100644 --- a/x/logic/types/types.pb.go +++ b/x/logic/types/types.pb.go @@ -23,74 +23,19 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// Term is the representation of a piece of data and can be a constant, a variable, or an atom. -type Term struct { - // name is the name of the term. - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty" yaml:"name",omitempty` - // arguments are the arguments of the term, which can be constants, variables, or atoms. - Arguments []Term `protobuf:"bytes,2,rep,name=arguments,proto3" json:"arguments" yaml:"arguments",omitempty` -} - -func (m *Term) Reset() { *m = Term{} } -func (m *Term) String() string { return proto.CompactTextString(m) } -func (*Term) ProtoMessage() {} -func (*Term) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{0} -} -func (m *Term) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Term) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Term.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *Term) XXX_Merge(src proto.Message) { - xxx_messageInfo_Term.Merge(m, src) -} -func (m *Term) XXX_Size() int { - return m.Size() -} -func (m *Term) XXX_DiscardUnknown() { - xxx_messageInfo_Term.DiscardUnknown(m) -} - -var xxx_messageInfo_Term proto.InternalMessageInfo - -func (m *Term) GetName() string { - if m != nil { - return m.Name - } - return "" -} - -func (m *Term) GetArguments() []Term { - if m != nil { - return m.Arguments - } - return nil -} - // Substitution represents a substitution made to the variables in the query to obtain the answer. type Substitution struct { // variable is the name of the variable. Variable string `protobuf:"bytes,1,opt,name=variable,proto3" json:"variable,omitempty" yaml:"variable",omitempty` - // term is the term that the variable is substituted with. - Term Term `protobuf:"bytes,2,opt,name=term,proto3" json:"term" yaml:"term",omitempty` + // expression is the value substituted for the variable, represented directly as a Prolog term (e.g., atom, number, compound). + Expression string `protobuf:"bytes,2,opt,name=expression,proto3" json:"expression,omitempty" yaml:"expression",omitempty` } func (m *Substitution) Reset() { *m = Substitution{} } func (m *Substitution) String() string { return proto.CompactTextString(m) } func (*Substitution) ProtoMessage() {} func (*Substitution) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{1} + return fileDescriptor_f3c73c95465ca7a8, []int{0} } func (m *Substitution) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -126,11 +71,11 @@ func (m *Substitution) GetVariable() string { return "" } -func (m *Substitution) GetTerm() Term { +func (m *Substitution) GetExpression() string { if m != nil { - return m.Term + return m.Expression } - return Term{} + return "" } // Result represents the result of a query. @@ -143,7 +88,7 @@ func (m *Result) Reset() { *m = Result{} } func (m *Result) String() string { return proto.CompactTextString(m) } func (*Result) ProtoMessage() {} func (*Result) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{2} + return fileDescriptor_f3c73c95465ca7a8, []int{1} } func (m *Result) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -197,7 +142,7 @@ func (m *Answer) Reset() { *m = Answer{} } func (m *Answer) String() string { return proto.CompactTextString(m) } func (*Answer) ProtoMessage() {} func (*Answer) Descriptor() ([]byte, []int) { - return fileDescriptor_f3c73c95465ca7a8, []int{3} + return fileDescriptor_f3c73c95465ca7a8, []int{2} } func (m *Answer) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -262,7 +207,6 @@ func (m *Answer) GetResults() []Result { } func init() { - proto.RegisterType((*Term)(nil), "logic.v1beta2.Term") proto.RegisterType((*Substitution)(nil), "logic.v1beta2.Substitution") proto.RegisterType((*Result)(nil), "logic.v1beta2.Result") proto.RegisterType((*Answer)(nil), "logic.v1beta2.Answer") @@ -271,82 +215,34 @@ func init() { func init() { proto.RegisterFile("logic/v1beta2/types.proto", fileDescriptor_f3c73c95465ca7a8) } var fileDescriptor_f3c73c95465ca7a8 = []byte{ - // 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, 0x4e, 0xd1, 0x23, 0xe8, 0xc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 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) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Term) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Term) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Arguments) > 0 { - for iNdEx := len(m.Arguments) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Arguments[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTypes(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 - } - } - if len(m.Name) > 0 { - i -= len(m.Name) - copy(dAtA[i:], m.Name) - i = encodeVarintTypes(dAtA, i, uint64(len(m.Name))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil + // 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, 0xf2, 0x2d, 0x76, 0xe4, 0xc8, 0xc7, 0xa9, 0xc4, 0x65, 0x47, + 0x4e, 0x11, 0x6a, 0xbf, 0x41, 0x3e, 0x01, 0xaa, 0xe3, 0x50, 0x67, 0x97, 0xaa, 0xca, 0xff, 0xfd, + 0xdf, 0xff, 0xf9, 0xf7, 0x1e, 0x3c, 0x2e, 0x78, 0xce, 0xb2, 0xe8, 0xea, 0x45, 0x4a, 0x55, 0x72, + 0x16,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, 0x03, 0xfd, 0x7e, 0xbc, 0x6a, 0x09, 0xe8, 0x5a, 0xf2, + 0xc8, 0x30, 0xd8, 0x4a, 0xb6, 0xaf, 0x2f, 0x46, 0xaf, 0xa1, 0xbb, 0x48, 0xe4, 0xd7, 0x92, 0x0b, + 0xaa, 0xc1, 0xb9, 0x36, 0xf5, 0x41, 0x19, 0xe5, 0x2d, 0x12, 0xf9, 0x91, 0x0b, 0x8a, 0xde, 0xc3, + 0xd9, 0xb0, 0x00, 0xe9, 0x4d, 0xfc, 0xc9, 0x7c, 0x16, 0x07, 0x26, 0xf3, 0x64, 0xbc, 0xb4, 0xd1, + 0xbc, 0x3b, 0x13, 0xfa, 0x04, 0x0f, 0x85, 0xe6, 0x2d, 0xbd, 0x7d, 0xcd, 0xf7, 0xe1, 0x1d, 0xbe, + 0xfd, 0x36, 0x62, 0xdf, 0x90, 0x35, 0x10, 0x8c, 0x67, 0x34, 0x94, 0xf9, 0xd6, 0xd3, 0x8c, 0xdf, + 0xae, 0xd6, 0x18, 0xdc, 0xae, 0x31, 0xf8, 0xbb, 0xc6, 0xe0, 0x66, 0x83, 0x9d, 0xdb, 0x0d, 0x76, + 0xfe, 0x6c, 0xb0, 0xf3, 0xe5, 0x69, 0xce, 0xd4, 0xa2, 0x4e, 0xc3, 0x8c, 0x97, 0x11, 0xff, 0x5e, + 0x9d, 0xeb, 0x9f, 0x6f, 0xd1, 0x75, 0xd4, 0x5f, 0xb3, 0xbe, 0xe2, 0x74, 0xaa, 0x2f, 0xf4, 0xe5, + 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x19, 0x3c, 0x55, 0x00, 0xe3, 0x02, 0x00, 0x00, } func (m *Substitution) Marshal() (dAtA []byte, err error) { @@ -369,16 +265,13 @@ func (m *Substitution) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - { - size, err := m.Term.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTypes(dAtA, i, uint64(size)) + if len(m.Expression) > 0 { + i -= len(m.Expression) + copy(dAtA[i:], m.Expression) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Expression))) + i-- + dAtA[i] = 0x12 } - i-- - dAtA[i] = 0x12 if len(m.Variable) > 0 { i -= len(m.Variable) copy(dAtA[i:], m.Variable) @@ -510,37 +403,20 @@ func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *Term) Size() (n int) { +func (m *Substitution) Size() (n int) { if m == nil { return 0 } var l int _ = l - l = len(m.Name) + l = len(m.Variable) if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - if len(m.Arguments) > 0 { - for _, e := range m.Arguments { - l = e.Size() - n += 1 + l + sovTypes(uint64(l)) - } - } - return n -} - -func (m *Substitution) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Variable) + l = len(m.Expression) if l > 0 { n += 1 + l + sovTypes(uint64(l)) } - l = m.Term.Size() - n += 1 + l + sovTypes(uint64(l)) return n } @@ -596,122 +472,6 @@ func sovTypes(x uint64) (n int) { func sozTypes(x uint64) (n int) { return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *Term) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Term: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Term: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Name", 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.Name = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Arguments", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Arguments = append(m.Arguments, Term{}) - if err := m.Arguments[len(m.Arguments)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTypes(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTypes - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func (m *Substitution) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -775,9 +535,9 @@ func (m *Substitution) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Term", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Expression", wireType) } - var msglen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowTypes @@ -787,24 +547,23 @@ func (m *Substitution) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - msglen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if msglen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthTypes } - postIndex := iNdEx + msglen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthTypes } if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.Term.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } + m.Expression = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/x/logic/wasm/types.go b/x/logic/wasm/types.go index d5c5e07e..e8d6a217 100644 --- a/x/logic/wasm/types.go +++ b/x/logic/wasm/types.go @@ -13,9 +13,10 @@ type AskQuery struct { // AskResponse implements the Ask query response JSON schema in a wasm custom query purpose, it redefines the existing // generated type from proto to ensure a dedicated serialization logic. type AskResponse struct { - Height uint64 `json:"height"` - GasUsed uint64 `json:"gas_used"` - Answer *Answer `json:"answer,omitempty"` + Height uint64 `json:"height"` + GasUsed uint64 `json:"gas_used"` + Answer *Answer `json:"answer,omitempty"` + UserOutput string `json:"user_output,omitempty"` } func (to *AskResponse) from(from types.QueryServiceAskResponse) { @@ -27,12 +28,14 @@ func (to *AskResponse) from(from types.QueryServiceAskResponse) { answer.from(*from.Answer) to.Answer = answer } + to.UserOutput = from.UserOutput } // 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"` @@ -40,6 +43,7 @@ type Answer struct { 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 { @@ -71,30 +75,11 @@ func (to *Result) from(from types.Result) { // Substitution denotes the Substitution 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 Substitution struct { - Variable string `json:"variable"` - Term Term `json:"term"` + Variable string `json:"variable"` + Expression string `json:"expression"` } func (to *Substitution) from(from types.Substitution) { to.Variable = from.Variable - term := new(Term) - term.from(from.Term) - to.Term = *term -} - -// Term denotes the Term 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 Term struct { - Name string `json:"name"` - Arguments []Term `json:"arguments"` -} - -func (to *Term) from(from types.Term) { - to.Name = from.Name - to.Arguments = make([]Term, 0, len(from.Arguments)) - for _, fromTerm := range from.Arguments { - term := new(Term) - term.from(fromTerm) - to.Arguments = append(to.Arguments, *term) - } + to.Expression = from.Expression }