Skip to content

Commit

Permalink
feat(logic)!: add max_variables limits params
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jul 25, 2024
1 parent e08560d commit 94e54e0
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 46 deletions.
1 change: 1 addition & 0 deletions docs/proto/logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Limits defines the limits of the logic module.
| `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_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. |

<a name="logic.v1beta2.Params"></a>

Expand Down
8 changes: 8 additions & 0 deletions proto/logic/v1beta2/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ message Limits {
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
(gogoproto.nullable) = true
];

// 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.
string max_variables = 5 [
(gogoproto.moretags) = "yaml:\"max_variables\"",
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
(gogoproto.nullable) = true
];
}

// Filter defines the parameters for filtering the set of strings which can designate anything.
Expand Down
12 changes: 12 additions & 0 deletions x/logic/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package interpreter

import (
goctx "context"
"cosmossdk.io/math"
"fmt"
"io"
"io/fs"
Expand Down Expand Up @@ -68,6 +69,17 @@ func WithFS(fs fs.FS) Option {
}
}

func WithMaxVariables(maxVariables *math.Uint) Option {
return func(i *prolog.Interpreter) error {
if maxVariables != nil {
i.SetMaxVariables(maxVariables.Uint64())
} else {
i.SetMaxVariables(0)
}
return nil
}
}

// New creates a new prolog.Interpreter with the specified options.
func New(
opts ...Option,
Expand Down
1 change: 1 addition & 0 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (k Keeper) newInterpreter(ctx context.Context, params types.Params) (*prolo
interpreter.WithBootstrap(ctx, util.NonZeroOrDefault(interpreterParams.GetBootstrap(), bootstrap.Bootstrap())),
interpreter.WithFS(filtered.NewFS(k.fsProvider(ctx), whitelistUrls, blacklistUrls)),
interpreter.WithUserOutputWriter(userOutputBuffer),
interpreter.WithMaxVariables(limits.MaxVariables),
}

i, err := interpreter.New(options...)
Expand Down
12 changes: 12 additions & 0 deletions x/logic/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
DefaultPredicatesBlacklist = make([]string, 0)
DefaultMaxSize = math.NewUint(uint64(5000))
DefaultMaxResultCount = math.NewUint(uint64(1))
DefaultMaxVariables = math.NewUint(uint64(100000))
)

// NewParams creates a new Params object.
Expand Down Expand Up @@ -146,6 +147,13 @@ func WithMaxUserOutputSize(maxUserOutputSize math.Uint) LimitsOption {
}
}

// WithMaxVariables sets the maximum number of variables that can be created by the interpreter.
func WithMaxVariables(maxVariables math.Uint) LimitsOption {
return func(i *Limits) {
i.MaxVariables = &maxVariables
}
}

// NewLimits creates a new Limits object.
func NewLimits(opts ...LimitsOption) Limits {
l := Limits{}
Expand All @@ -161,6 +169,10 @@ func NewLimits(opts ...LimitsOption) Limits {
l.MaxResultCount = &DefaultMaxResultCount
}

if l.MaxVariables == nil {
l.MaxVariables = &DefaultMaxVariables
}

return l
}

Expand Down
149 changes: 103 additions & 46 deletions x/logic/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 94e54e0

Please sign in to comment.