Skip to content

Commit

Permalink
refactor(logic)!: specify whitelist / blacklist in its own type
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Apr 11, 2023
1 parent 1b416f0 commit a8b2500
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 131 deletions.
48 changes: 28 additions & 20 deletions proto/logic/v1beta2/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,38 @@ message Limits {
];
}

// Interpreter defines the various parameters for the interpreter.
message Interpreter {
option (gogoproto.goproto_stringer) = true;

// predicates_whitelist specifies a list of prolog predicates that are allowed and can be used by the interpreter.
// The predicates are represented as `<predicate_name>/[<arity>]`, for example: `findall/3`, or `call`. If a predicate name without arity
// is included in this list, then all predicates with that name will be considered regardless of arity. For example, if `call` is included
// in the whitelist, then all predicates `call/1`, `call/2`, `call/3`... will be allowed.
// If this field is not specified, the interpreter will use the default set of predicates.
repeated string predicates_whitelist = 1 [
// Filter defines the parameters for filtering the set of strings which can designate anything.
// The filter is used to whitelist or blacklist strings.
message Filter {
// whitelist specifies a list of strings that are allowed.
// If this field is not specified, all strings (in the context of the filter) are allowed.
repeated string whitelist = 1 [
(gogoproto.nullable) = true,
(gogoproto.moretags) = "yaml:\"predicates_whitelist\""
(gogoproto.moretags) = "yaml:\"whitelist\""
];

// predicates_blacklist specifies a list of prolog predicates that are excluded from the set of registered predicates
// and can never be executed by the interpreter.
// The predicates are represented as `<predicate_name>/[<arity>]`, for example: `findall/3`, or `call`. If a predicate name without arity
// is included in this list, then all predicates with that name will be considered regardless of arity. For example, if `call` is included
// in the blacklist, then all predicates `call/1`, `call/2`, `call/3`... will be excluded.
// If a predicate is included in both whitelist and blacklist, it will be excluded. This means that blacklisted predicates prevails
// on whitelisted predicates.
repeated string predicates_blacklist = 2 [
// blacklist specifies a list of strings that are excluded from the set of allowed strings.
// If a string is included in both whitelist and blacklist, it will be excluded. This means that
// blacklisted strings prevails over whitelisted ones.
// If this field is not specified, no strings are excluded.
repeated string blacklist = 2 [
(gogoproto.nullable) = true,
(gogoproto.moretags) = "yaml:\"predicates_blacklist\""
(gogoproto.moretags) = "yaml:\"blacklist\""
];
}

// Interpreter defines the various parameters for the interpreter.
message Interpreter {
option (gogoproto.goproto_stringer) = true;

// predicates_filter specifies the filter for the predicates that are allowed to be used by the interpreter.
// The filter is used to whitelist or blacklist predicates represented as `<predicate_name>/[<arity>]`, for example:
// `findall/3`, or `call`. If a predicate name without arity is included in the filter, then all predicates with that
// name will be considered regardless of arity. For example, if `call` is included in the filter, then all predicates
// `call/1`, `call/2`, `call/3`... will be allowed.
Filter predicates_filter = 1 [
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"predicates_filter\""
];

// bootstrap specifies the initial program to run when booting the logic interpreter.
Expand Down
4 changes: 2 additions & 2 deletions x/logic/keeper/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func (k Keeper) newInterpreter(ctx goctx.Context) (*prolog.Interpreter, *util.Bo
gasPolicy := params.GetGasPolicy()
limits := params.GetLimits()

whitelist := util.NonZeroOrDefault(interpreterParams.PredicatesWhitelist, interpreter.RegistryNames)
blacklist := interpreterParams.GetPredicatesBlacklist()
whitelist := util.NonZeroOrDefault(interpreterParams.PredicatesFilter.Whitelist, interpreter.RegistryNames)
blacklist := interpreterParams.PredicatesFilter.Blacklist
gasMeter := meter.WithWeightedMeter(sdkctx.GasMeter(), nonNilNorZeroOrDefaultUint64(gasPolicy.WeightingFactor, defaultWeightFactor))

predicates := lo.Reduce(
Expand Down
8 changes: 5 additions & 3 deletions x/logic/migrations/v3/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func MigrateStore(ctx sdk.Context,

newParams := types.Params{
Interpreter: types.Interpreter{
Bootstrap: oldParams.Interpreter.Bootstrap,
PredicatesWhitelist: oldParams.Interpreter.RegisteredPredicates,
PredicatesBlacklist: []string{},
Bootstrap: oldParams.Interpreter.Bootstrap,
PredicatesFilter: types.Filter{
Whitelist: oldParams.Interpreter.RegisteredPredicates,
Blacklist: []string{},
},
},
Limits: types.Limits{
MaxGas: oldParams.Limits.MaxGas,
Expand Down
12 changes: 6 additions & 6 deletions x/logic/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func NewInterpreter(opts ...InterpreterOption) Interpreter {
opt(&i)
}

if i.PredicatesWhitelist == nil {
i.PredicatesWhitelist = DefaultPredicatesWhitelist
if i.PredicatesFilter.Whitelist == nil {
i.PredicatesFilter.Whitelist = DefaultPredicatesWhitelist
}

if i.PredicatesBlacklist == nil {
i.PredicatesBlacklist = DefaultPredicatesBlacklist
if i.PredicatesFilter.Blacklist == nil {
i.PredicatesFilter.Blacklist = DefaultPredicatesBlacklist
}

return i
Expand All @@ -77,14 +77,14 @@ type InterpreterOption func(*Interpreter)
// WithPredicatesWhitelist sets the whitelist of predicates.
func WithPredicatesWhitelist(whitelist []string) InterpreterOption {
return func(i *Interpreter) {
i.PredicatesWhitelist = whitelist
i.PredicatesFilter.Whitelist = whitelist
}
}

// WithPredicatesBlacklist sets the blacklist of predicates.
func WithPredicatesBlacklist(blacklist []string) InterpreterOption {
return func(i *Interpreter) {
i.PredicatesBlacklist = blacklist
i.PredicatesFilter.Blacklist = blacklist
}
}

Expand Down
Loading

0 comments on commit a8b2500

Please sign in to comment.