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 23, 2024
1 parent d609309 commit ab8f734
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 51 deletions.
1 change: 1 addition & 0 deletions docs/proto/logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,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
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ replace (
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0

github.com/ichiban/prolog => ../prolog

// replace broken goleveldb
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
Expand Down Expand Up @@ -112,6 +115,7 @@ require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/cockroachdb/errors v1.11.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/P
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM=
github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E=
github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4=
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
Expand Down Expand Up @@ -837,8 +839,6 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ichiban/prolog v1.2.0 h1:DrwolRMxdzI3126nCSpyxJtK4OVVqmbu7XpGhy8phXs=
github.com/ichiban/prolog v1.2.0/go.mod h1:RmvNfGaSktvEVZ7nmpn0gkWa5u0Y3zQcK0G+Pl+ul+s=
github.com/ignite/cli v0.27.2 h1:NFzZqQrt8vhRQeStSvAMhpq65ag+lEAVP5ulw7cu3Y4=
github.com/ignite/cli v0.27.2/go.mod h1:7uaYQQ07tyOBiVAlRYAcZk2g/Y1vtgU0J09oPNntR4E=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
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 @@ -69,6 +69,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 @@ -141,6 +141,7 @@ func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, fmt.St
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 @@ -21,6 +21,7 @@ var (
DefaultMaxGas = math.NewUint(uint64(100000))
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 @@ -157,6 +158,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 @@ -176,6 +184,10 @@ func NewLimits(opts ...LimitsOption) Limits {
l.MaxResultCount = &DefaultMaxResultCount
}

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

return l
}

Expand Down
155 changes: 106 additions & 49 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 ab8f734

Please sign in to comment.