-
Notifications
You must be signed in to change notification settings - Fork 3
Support uniform and zipfian gas distribution in workload modeler #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ var ( | |
| _ gasGenerator = (*GasPicker)(nil) | ||
| _ gasGenerator = (*FixedGasGenerator)(nil) | ||
| _ gasGenerator = (*RandomGasGenerator)(nil) | ||
| _ gasGenerator = (*UniformGasGenerator)(nil) | ||
| _ gasGenerator = (*ZipfianGasGenerator)(nil) | ||
| ) | ||
|
|
||
| type gasGenerator interface { | ||
|
|
@@ -48,12 +50,22 @@ func (g *GasPicker) UnmarshalJSON(data []byte) error { | |
| } | ||
| g.delegate = &fixed | ||
| return nil | ||
| case "random": | ||
| var random RandomGasGenerator | ||
| if err := json.Unmarshal(data, &random); err != nil { | ||
| case "random", "uniform": | ||
| var uniform UniformGasGenerator | ||
| if err := json.Unmarshal(data, &uniform); err != nil { | ||
| return err | ||
| } | ||
| g.delegate = &random | ||
| g.delegate = &uniform | ||
| return nil | ||
| case "zipfian": | ||
| var zipfian ZipfianGasGenerator | ||
| if err := json.Unmarshal(data, &zipfian); err != nil { | ||
| return err | ||
| } | ||
| if err := zipfian.dist.validate(); err != nil { | ||
| return err | ||
| } | ||
| g.delegate = &zipfian | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unknown gas generator name: %s", g.name) | ||
|
|
@@ -74,9 +86,40 @@ type RandomGasGenerator struct { | |
| } | ||
|
|
||
| func (r *RandomGasGenerator) GenerateGas(rng *mrand.Rand) (uint64, error) { | ||
| if r.Min >= r.Max { | ||
| return 0, fmt.Errorf("invalid random gas range: min %d must be less than max %d", r.Min, r.Max) | ||
| u := UniformGasGenerator(*r) | ||
| return u.GenerateGas(rng) | ||
| } | ||
|
|
||
| type UniformGasGenerator struct { | ||
| Min uint64 `json:"Min"` | ||
| Max uint64 `json:"Max"` | ||
| } | ||
|
|
||
| func (u *UniformGasGenerator) GenerateGas(rng *mrand.Rand) (uint64, error) { | ||
| if u.Min >= u.Max { | ||
| return 0, fmt.Errorf("invalid uniform gas range: min %d must be less than max %d", u.Min, u.Max) | ||
| } | ||
| span := u.Max - u.Min + 1 | ||
| return u.Min + rng.Uint64N(span), nil | ||
| } | ||
|
|
||
| type ZipfianGasGenerator struct { | ||
| Min uint64 `json:"Min"` | ||
| Max uint64 `json:"Max"` | ||
| Theta float64 `json:"theta"` | ||
|
|
||
| dist ZipfianDistribution | ||
| } | ||
|
|
||
| func (z *ZipfianGasGenerator) GenerateGas(rng *mrand.Rand) (uint64, error) { | ||
| if z.Min >= z.Max { | ||
| return 0, fmt.Errorf("invalid zipfian gas range: min %d must be less than max %d", z.Min, z.Max) | ||
| } | ||
| z.dist.Theta = z.Theta | ||
| n := z.Max - z.Min + 1 | ||
| idx, err := z.dist.SampleIndex(rng, n) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zipfian gas range is uncappedHigh Severity
Triggered by learned rule: config/: Validate() must reject silently-degrading misconfigurations Reviewed by Cursor Bugbot for commit c9adbb0. Configure here. |
||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| span := r.Max - r.Min + 1 | ||
| return r.Min + rng.Uint64N(span), nil | ||
| return z.Min + idx, nil | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zipfian gas skips theta validation
High Severity
UnmarshalJSONcallsvalidateon the zero-valuedistrather than the unmarshaledtheta, so every zipfian gas config passes.GenerateGasthen copies the uncheckedthetaontodistand samples. An out-of-range value, including1.0wherealphadiverges, yields a numerically broken gas distribution instead of a parse error.Additional Locations (1)
config/gas.go#L117-L118Reviewed by Cursor Bugbot for commit c9adbb0. Configure here.