Skip to content

Commit 94e54e0

Browse files
committed
feat(logic)!: add max_variables limits params
1 parent e08560d commit 94e54e0

File tree

6 files changed

+137
-46
lines changed

6 files changed

+137
-46
lines changed

docs/proto/logic.md

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

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

proto/logic/v1beta2/params.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ message Limits {
5959
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
6060
(gogoproto.nullable) = true
6161
];
62+
63+
// max_variables specifies the maximum number of variables that can be create by the interpreter.
64+
// nil value or 0 value means that no limit is set.
65+
string max_variables = 5 [
66+
(gogoproto.moretags) = "yaml:\"max_variables\"",
67+
(gogoproto.customtype) = "cosmossdk.io/math.Uint",
68+
(gogoproto.nullable) = true
69+
];
6270
}
6371

6472
// 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
@@ -133,6 +133,7 @@ func (k Keeper) newInterpreter(ctx context.Context, params types.Params) (*prolo
133133
interpreter.WithBootstrap(ctx, util.NonZeroOrDefault(interpreterParams.GetBootstrap(), bootstrap.Bootstrap())),
134134
interpreter.WithFS(filtered.NewFS(k.fsProvider(ctx), whitelistUrls, blacklistUrls)),
135135
interpreter.WithUserOutputWriter(userOutputBuffer),
136+
interpreter.WithMaxVariables(limits.MaxVariables),
136137
}
137138

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

x/logic/types/params.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
DefaultPredicatesBlacklist = make([]string, 0)
1818
DefaultMaxSize = math.NewUint(uint64(5000))
1919
DefaultMaxResultCount = math.NewUint(uint64(1))
20+
DefaultMaxVariables = math.NewUint(uint64(100000))
2021
)
2122

2223
// NewParams creates a new Params object.
@@ -146,6 +147,13 @@ func WithMaxUserOutputSize(maxUserOutputSize math.Uint) LimitsOption {
146147
}
147148
}
148149

150+
// WithMaxVariables sets the maximum number of variables that can be created by the interpreter.
151+
func WithMaxVariables(maxVariables math.Uint) LimitsOption {
152+
return func(i *Limits) {
153+
i.MaxVariables = &maxVariables
154+
}
155+
}
156+
149157
// NewLimits creates a new Limits object.
150158
func NewLimits(opts ...LimitsOption) Limits {
151159
l := Limits{}
@@ -161,6 +169,10 @@ func NewLimits(opts ...LimitsOption) Limits {
161169
l.MaxResultCount = &DefaultMaxResultCount
162170
}
163171

172+
if l.MaxVariables == nil {
173+
l.MaxVariables = &DefaultMaxVariables
174+
}
175+
164176
return l
165177
}
166178

x/logic/types/params.pb.go

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

0 commit comments

Comments
 (0)