Skip to content

Commit

Permalink
feat(logic): generate default values for the interpreter in genesis file
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 23, 2024
1 parent da93e81 commit 6146f20
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
25 changes: 25 additions & 0 deletions x/logic/keeper/grpc_query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ func TestGRPCParams(t *testing.T) {
types.WithMaxUserOutputSize(4),
types.WithMaxVariables(5),
),
types.GasPolicy{},
),
},
{
params: types.NewParams(
types.NewInterpreter(
types.WithBootstrap("bootstrap"),
types.WithPredicatesBlacklist([]string{"halt/1"}),
types.WithPredicatesWhitelist([]string{"source_file/1"}),
types.WithVirtualFilesBlacklist([]string{"file1"}),
types.WithVirtualFilesWhitelist([]string{"file2"}),
),
types.NewLimits(
types.WithMaxSize(2),
types.WithMaxResultCount(3),
types.WithMaxUserOutputSize(4),
types.WithMaxVariables(5),
),
types.GasPolicy{
WeightingFactor: 2,
DefaultPredicateCost: 1,
PredicateCosts: []types.PredicateCost{
{Predicate: "foo", Cost: 1},
},
},
),
},
}
Expand Down
57 changes: 48 additions & 9 deletions x/logic/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ var (
ParamsKey = []byte("Params")
)

var (
DefaultPredicatesWhitelist = make([]string, 0)
DefaultPredicatesBlacklist = make([]string, 0)
)

// NewParams creates a new Params object.
func NewParams(interpreter Interpreter, limits Limits) Params {
func NewParams(interpreter Interpreter, limits Limits, gasPolicy GasPolicy) Params {
return Params{
Interpreter: interpreter,
Limits: limits,
GasPolicy: gasPolicy,
}
}

// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return NewParams(NewInterpreter(), NewLimits())
return NewParams(
NewInterpreter(),
NewLimits(
WithMaxSize(5000),
WithMaxResultCount(3),
WithMaxVariables(100000)),
NewGasPolicy(
WithWeightingFactor(1),
WithDefaultPredicateCost(1)),
)
}

// Validate validates the set of params.
Expand All @@ -50,11 +55,11 @@ func NewInterpreter(opts ...InterpreterOption) Interpreter {
}

if i.PredicatesFilter.Whitelist == nil {
i.PredicatesFilter.Whitelist = DefaultPredicatesWhitelist
i.PredicatesFilter.Whitelist = []string{}
}

if i.PredicatesFilter.Blacklist == nil {
i.PredicatesFilter.Blacklist = DefaultPredicatesBlacklist
i.PredicatesFilter.Blacklist = []string{}
}

return i
Expand Down Expand Up @@ -168,3 +173,37 @@ func validateLimits(i interface{}) error {
// TODO: Validate limits params.
return nil
}

// GasPolicyOption is a functional option for configuring the GasPolicy.
type GasPolicyOption func(*GasPolicy)

// WithWeightingFactor sets the weighting factor.
func WithWeightingFactor(weightingFactor uint64) GasPolicyOption {
return func(i *GasPolicy) {
i.WeightingFactor = weightingFactor
}
}

// WithDefaultPredicateCost sets the default cost of a predicate.
func WithDefaultPredicateCost(defaultPredicateCost uint64) GasPolicyOption {
return func(i *GasPolicy) {
i.DefaultPredicateCost = defaultPredicateCost
}
}

// WithPredicateCosts sets the cost of a predicate.
func WithPredicateCosts(predicateCosts []PredicateCost) GasPolicyOption {
return func(i *GasPolicy) {
i.PredicateCosts = predicateCosts
}

Check warning on line 198 in x/logic/types/params.go

View check run for this annotation

Codecov / codecov/patch

x/logic/types/params.go#L195-L198

Added lines #L195 - L198 were not covered by tests
}

// NewGasPolicy creates a new GasPolicy object.
func NewGasPolicy(opts ...GasPolicyOption) GasPolicy {
g := GasPolicy{}
for _, opt := range opts {
opt(&g)
}

return g
}
6 changes: 6 additions & 0 deletions x/logic/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestValidateParams(t *testing.T) {
types.WithMaxUserOutputSize(4),
types.WithMaxVariables(5),
),
types.GasPolicy{
WeightingFactor: 2,
DefaultPredicateCost: 1,
},
),
expectErr: false,
err: nil,
Expand All @@ -50,6 +54,7 @@ func TestValidateParams(t *testing.T) {
types.WithVirtualFilesBlacklist([]string{"https://foo{bar/"}),
),
types.NewLimits(),
types.GasPolicy{},
),
expectErr: true,
err: fmt.Errorf("invalid virtual file in blacklist: https://foo{bar/"),
Expand All @@ -61,6 +66,7 @@ func TestValidateParams(t *testing.T) {
types.WithVirtualFilesWhitelist([]string{"https://foo{bar/"}),
),
types.NewLimits(),
types.GasPolicy{},
),
expectErr: true,
err: fmt.Errorf("invalid virtual file in whitelist: https://foo{bar/"),
Expand Down

0 comments on commit 6146f20

Please sign in to comment.