-
Notifications
You must be signed in to change notification settings - Fork 12
feat(supernova): add mixed runtime with percentage for each kind of txs #114
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
Open
mvallenet
wants to merge
7
commits into
gnolang:main
Choose a base branch
from
mvallenet:feat/mixed-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3f24f4
feat(supernova): add mixed runtime with percentage for each kind of txs
mvallenet f8566e3
feat(supernova): add mixed runtime with percentage for each kind of txs
mvallenet 09508e3
feat(supernova): add mixed runtime with percentage for each kind of txs
mvallenet 85997e2
fix: lint errors
mvallenet 575c42b
fix: lint errors
mvallenet 7e70534
fix: use existing code
mvallenet 7ab57ae
fix(supernova): add seed for reproduce txs ordering
mvallenet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/gnolang/supernova/internal/runtime" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // validBaseConfig returns a config with all fields valid except Mode/MixRatio | ||
| func validBaseConfig() *Config { | ||
| return &Config{ | ||
| URL: "http://localhost:26657", | ||
| ChainID: "dev", | ||
| Mnemonic: "source bonus chronic canvas draft south burst lottery " + | ||
| "vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast", | ||
| SubAccounts: 10, | ||
| Transactions: 100, | ||
| BatchSize: 100, | ||
| } | ||
| } | ||
|
|
||
| func TestConfig_Validate_MixedModeRequiresRatio(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cfg := validBaseConfig() | ||
| cfg.Mode = runtime.Mixed.String() | ||
| cfg.MixRatio = "" | ||
|
|
||
| err := cfg.Validate() | ||
| require.Error(t, err) | ||
| assert.ErrorIs(t, err, errMixRatioRequired) | ||
| } | ||
|
|
||
| func TestConfig_Validate_MixedModeInvalidRatio(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cfg := validBaseConfig() | ||
| cfg.Mode = runtime.Mixed.String() | ||
| cfg.MixRatio = "REALM_CALL:50" // only one type, needs at least 2 | ||
|
|
||
| err := cfg.Validate() | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "invalid mix-ratio") | ||
| } | ||
|
|
||
| func TestConfig_Validate_MixedModeValidRatio(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cfg := validBaseConfig() | ||
| cfg.Mode = runtime.Mixed.String() | ||
| cfg.MixRatio = "REALM_CALL:70,REALM_DEPLOYMENT:30" | ||
|
|
||
| err := cfg.Validate() | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestConfig_Validate_NonMixedModeIgnoresRatio(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| cfg := validBaseConfig() | ||
| cfg.Mode = runtime.RealmCall.String() | ||
| cfg.MixRatio = "this is invalid but should be ignored" | ||
|
|
||
| err := cfg.Validate() | ||
| assert.NoError(t, err) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| errEmptyMixRatio = errors.New("mix ratio cannot be empty") | ||
| errInvalidRatioFormat = errors.New("invalid ratio format, expected TYPE:PERCENTAGE") | ||
| errInvalidPercentage = errors.New("percentage must be a positive integer between 1 and 100") | ||
| errUnknownType = errors.New("unknown runtime type in mix ratio") | ||
| errDuplicateType = errors.New("duplicate runtime type in mix ratio") | ||
| errMixedInMix = errors.New("MIXED type cannot be used in mix ratio") | ||
| errRatioSumNot100 = errors.New("mix ratio percentages must sum to 100") | ||
| errInsufficientTypes = errors.New("mix ratio must contain at least 2 types") | ||
| ) | ||
|
|
||
| type mixRatio struct { | ||
| Type Type | ||
| Percentage int | ||
| } | ||
|
|
||
| type MixConfig struct { | ||
| Ratios []mixRatio | ||
| Seed int64 // Optional seed for reproducible shuffling (0 = use current time) | ||
| } | ||
|
|
||
| // ParseMixRatio parses a mix ratio string into a MixConfig | ||
| // Example input: "REALM_CALL:70,REALM_DEPLOYMENT:20,PACKAGE_DEPLOYMENT:10" | ||
| func ParseMixRatio(input string) (*MixConfig, error) { | ||
| if strings.TrimSpace(input) == "" { | ||
| return nil, errEmptyMixRatio | ||
| } | ||
|
|
||
| parts := strings.Split(input, ",") | ||
| if len(parts) < 2 { | ||
| return nil, errInsufficientTypes | ||
| } | ||
|
|
||
| config := &MixConfig{ | ||
| Ratios: make([]mixRatio, 0, len(parts)), | ||
| } | ||
|
|
||
| seenTypes := make(map[Type]bool) | ||
|
|
||
| for _, part := range parts { | ||
| part = strings.TrimSpace(part) | ||
| if part == "" { | ||
| continue | ||
| } | ||
|
|
||
| ratio, err := parseRatioPart(part) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if seenTypes[ratio.Type] { | ||
| return nil, fmt.Errorf("%w: %s", errDuplicateType, ratio.Type) | ||
| } | ||
|
|
||
| seenTypes[ratio.Type] = true | ||
|
|
||
| config.Ratios = append(config.Ratios, ratio) | ||
| } | ||
|
|
||
| if err := config.Validate(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return config, nil | ||
| } | ||
|
|
||
| // parseRatioPart parses a single TYPE:PERCENTAGE part of the mix ratio | ||
| // Example input: "REALM_CALL:70" and ensures validity | ||
| func parseRatioPart(part string) (mixRatio, error) { | ||
| colonIdx := strings.LastIndex(part, ":") | ||
| if colonIdx == -1 { | ||
| return mixRatio{}, fmt.Errorf("%w: %s", errInvalidRatioFormat, part) | ||
| } | ||
|
|
||
| typeName := strings.TrimSpace(part[:colonIdx]) | ||
| percentageStr := strings.TrimSpace(part[colonIdx+1:]) | ||
|
|
||
| percentage, err := strconv.Atoi(percentageStr) | ||
| if err != nil || percentage < 1 || percentage > 100 { | ||
| return mixRatio{}, fmt.Errorf("%w: %s", errInvalidPercentage, percentageStr) | ||
| } | ||
|
|
||
| runtimeType := Type(typeName) | ||
|
|
||
| if runtimeType == Mixed { | ||
| return mixRatio{}, errMixedInMix | ||
| } | ||
|
|
||
| if !IsMixableRuntime(runtimeType) { | ||
| return mixRatio{}, fmt.Errorf("%w: %s", errUnknownType, typeName) | ||
| } | ||
|
|
||
| return mixRatio{ | ||
| Type: runtimeType, | ||
| Percentage: percentage, | ||
| }, nil | ||
| } | ||
|
|
||
| // Validate checks the MixConfig for correctness | ||
| // ensuring at least two types and that percentages sum to 100 | ||
| func (mc *MixConfig) Validate() error { | ||
| if len(mc.Ratios) < 2 { | ||
| return errInsufficientTypes | ||
| } | ||
|
|
||
| sum := 0 | ||
| for _, ratio := range mc.Ratios { | ||
| sum += ratio.Percentage | ||
| } | ||
|
|
||
| if sum != 100 { | ||
| return fmt.Errorf("%w: got %d", errRatioSumNot100, sum) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // HasType checks if the MixConfig includes the specified runtime type | ||
| // For example in: REALM_CALL:70,REALM_DEPLOYMENT:30, HasType(REALM_CALL) returns true | ||
| // but HasType(PACKAGE_DEPLOYMENT) returns false | ||
| func (mc *MixConfig) HasType(t Type) bool { | ||
| for _, ratio := range mc.Ratios { | ||
| if ratio.Type == t { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // CalculateTransactionCounts computes the number of transactions | ||
| // for each runtime type based on the total and the defined ratios | ||
| func (mc *MixConfig) CalculateTransactionCounts(total uint64) map[Type]uint64 { | ||
| counts := make(map[Type]uint64) | ||
|
|
||
| var allocated uint64 | ||
|
|
||
| for i, ratio := range mc.Ratios { | ||
| if i == len(mc.Ratios)-1 { | ||
| counts[ratio.Type] = total - allocated | ||
| } else { | ||
| count := (total * uint64(ratio.Percentage)) / 100 | ||
| counts[ratio.Type] = count | ||
| allocated += count | ||
| } | ||
| } | ||
|
|
||
| return counts | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
7ab57ae