Skip to content

Commit ab8f734

Browse files
committed
feat(logic)!: add max_variables limits params
1 parent d609309 commit ab8f734

File tree

8 files changed

+146
-51
lines changed

8 files changed

+146
-51
lines changed

docs/proto/logic.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Limits defines the limits of the logic module.
278278
| `max_size` | [string](#string) | | max_size specifies the maximum size, in bytes, that is accepted for a program. nil value remove size limitation. |
279279
| `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. |
280280
| `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. |
281+
| `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. |
281282

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

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ replace (
7373
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
7474
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
7575
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
76+
77+
github.com/ichiban/prolog => ../prolog
78+
7679
// replace broken goleveldb
7780
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
7881
)
@@ -112,6 +115,7 @@ require (
112115
github.com/chzyer/readline v1.5.1 // indirect
113116
github.com/cloudflare/circl v1.3.7 // indirect
114117
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
118+
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
115119
github.com/cockroachdb/errors v1.11.1 // indirect
116120
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
117121
github.com/cockroachdb/pebble v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/P
383383
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM=
384384
github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E=
385385
github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
386+
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
387+
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
386388
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
387389
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f h1:otljaYPt5hWxV3MUfO5dFPFiOXg9CyG5/kCfayTqsJ4=
388390
github.com/cockroachdb/datadriven v1.0.3-0.20230413201302-be42291fc80f/go.mod h1:a9RdTaap04u637JoCzcUoIcDmvwSUtcUFtT/C3kJlTU=
@@ -837,8 +839,6 @@ github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSAS
837839
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
838840
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
839841
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
840-
github.com/ichiban/prolog v1.2.0 h1:DrwolRMxdzI3126nCSpyxJtK4OVVqmbu7XpGhy8phXs=
841-
github.com/ichiban/prolog v1.2.0/go.mod h1:RmvNfGaSktvEVZ7nmpn0gkWa5u0Y3zQcK0G+Pl+ul+s=
842842
github.com/ignite/cli v0.27.2 h1:NFzZqQrt8vhRQeStSvAMhpq65ag+lEAVP5ulw7cu3Y4=
843843
github.com/ignite/cli v0.27.2/go.mod h1:7uaYQQ07tyOBiVAlRYAcZk2g/Y1vtgU0J09oPNntR4E=
844844
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=

proto/logic/v1beta2/params.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ message Limits {
6969
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
7070
(gogoproto.nullable) = true
7171
];
72+
73+
// max_variables specifies the maximum number of variables that can be create by the interpreter.
74+
// nil value or 0 value means that no limit is set.
75+
string max_variables = 5 [
76+
(gogoproto.moretags) = "yaml:\"max_variables\"",
77+
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
78+
(gogoproto.nullable) = true
79+
];
7280
}
7381

7482
// Filter defines the parameters for filtering the set of strings which can designate anything.

x/logic/interpreter/interpreter.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package interpreter
22

33
import (
44
goctx "context"
5+
"cosmossdk.io/math"
56
"fmt"
67
"io"
78
"io/fs"
@@ -68,6 +69,17 @@ func WithFS(fs fs.FS) Option {
6869
}
6970
}
7071

72+
func WithMaxVariables(maxVariables *math.Uint) Option {
73+
return func(i *prolog.Interpreter) error {
74+
if maxVariables != nil {
75+
i.SetMaxVariables(maxVariables.Uint64())
76+
} else {
77+
i.SetMaxVariables(0)
78+
}
79+
return nil
80+
}
81+
}
82+
7183
// New creates a new prolog.Interpreter with the specified options.
7284
func New(
7385
opts ...Option,

x/logic/keeper/interpreter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func (k Keeper) newInterpreter(ctx context.Context) (*prolog.Interpreter, fmt.St
141141
interpreter.WithBootstrap(ctx, util.NonZeroOrDefault(interpreterParams.GetBootstrap(), bootstrap.Bootstrap())),
142142
interpreter.WithFS(filtered.NewFS(k.fsProvider(ctx), whitelistUrls, blacklistUrls)),
143143
interpreter.WithUserOutputWriter(userOutputBuffer),
144+
interpreter.WithMaxVariables(limits.MaxVariables),
144145
}
145146

146147
i, err := interpreter.New(options...)

x/logic/types/params.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
DefaultMaxGas = math.NewUint(uint64(100000))
2222
DefaultMaxSize = math.NewUint(uint64(5000))
2323
DefaultMaxResultCount = math.NewUint(uint64(1))
24+
DefaultMaxVariables = math.NewUint(uint64(100000))
2425
)
2526

2627
// NewParams creates a new Params object.
@@ -157,6 +158,13 @@ func WithMaxUserOutputSize(maxUserOutputSize math.Uint) LimitsOption {
157158
}
158159
}
159160

161+
// WithMaxVariables sets the maximum number of variables that can be created by the interpreter.
162+
func WithMaxVariables(maxVariables math.Uint) LimitsOption {
163+
return func(i *Limits) {
164+
i.MaxVariables = &maxVariables
165+
}
166+
}
167+
160168
// NewLimits creates a new Limits object.
161169
func NewLimits(opts ...LimitsOption) Limits {
162170
l := Limits{}
@@ -176,6 +184,10 @@ func NewLimits(opts ...LimitsOption) Limits {
176184
l.MaxResultCount = &DefaultMaxResultCount
177185
}
178186

187+
if l.MaxVariables == nil {
188+
l.MaxVariables = &DefaultMaxVariables
189+
}
190+
179191
return l
180192
}
181193

x/logic/types/params.pb.go

Lines changed: 106 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)