-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
fix(sims): TestAppSimulationAfterImport and legacy proposal handling #21800
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package simsx | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"iter" | ||
"maps" | ||
|
@@ -36,7 +37,7 @@ type ( | |
) | ||
|
||
// WeightedProposalMsgIter iterator for weighted gov proposal payload messages | ||
type WeightedProposalMsgIter = iter.Seq2[uint32, SimMsgFactoryX] | ||
type WeightedProposalMsgIter = iter.Seq2[uint32, FactoryMethod] | ||
|
||
var _ Registry = &WeightedOperationRegistryAdapter{} | ||
|
||
|
@@ -108,6 +109,7 @@ type HasFutureOpsRegistry interface { | |
SetFutureOpsRegistry(FutureOpsRegistry) | ||
} | ||
|
||
// msg factory to legacy Operation type | ||
func legacyOperationAdapter(l regCommon, fx SimMsgFactoryX) simtypes.Operation { | ||
return func( | ||
r *rand.Rand, app AppEntrypoint, ctx sdk.Context, | ||
|
@@ -167,15 +169,15 @@ func (s UniqueTypeRegistry) Add(weight uint32, f SimMsgFactoryX) { | |
} | ||
|
||
// Iterator returns an iterator function for a Go for loop sorted by weight desc. | ||
func (s UniqueTypeRegistry) Iterator() iter.Seq2[uint32, SimMsgFactoryX] { | ||
func (s UniqueTypeRegistry) Iterator() WeightedProposalMsgIter { | ||
x := maps.Values(s) | ||
sortedWeightedFactory := slices.SortedFunc(x, func(a, b WeightedFactory) int { | ||
return a.Compare(b) | ||
}) | ||
|
||
return func(yield func(uint32, SimMsgFactoryX) bool) { | ||
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. Unrelated, but can we add godocs to |
||
return func(yield func(uint32, FactoryMethod) bool) { | ||
for _, v := range sortedWeightedFactory { | ||
if !yield(v.Weight, v.Factory) { | ||
if !yield(v.Weight, v.Factory.Create()) { | ||
return | ||
} | ||
} | ||
|
@@ -204,3 +206,63 @@ func (f WeightedFactory) Compare(b WeightedFactory) int { | |
return strings.Compare(sdk.MsgTypeURL(f.Factory.MsgType()), sdk.MsgTypeURL(b.Factory.MsgType())) | ||
} | ||
} | ||
|
||
// WeightedFactoryMethod is a data tuple used for registering legacy proposal operations | ||
type WeightedFactoryMethod struct { | ||
Weight uint32 | ||
Factory FactoryMethod | ||
} | ||
|
||
type WeightedFactoryMethods []WeightedFactoryMethod | ||
|
||
// NewWeightedFactoryMethods constructor | ||
func NewWeightedFactoryMethods() WeightedFactoryMethods { | ||
return make(WeightedFactoryMethods, 0) | ||
} | ||
|
||
// Add adds a new WeightedFactoryMethod to the WeightedFactoryMethods slice. | ||
// If weight is zero or f is nil, it returns without making any changes. | ||
func (s *WeightedFactoryMethods) Add(weight uint32, f FactoryMethod) { | ||
if weight == 0 { | ||
return | ||
} | ||
if f == nil { | ||
panic("message factory must not be nil") | ||
} | ||
*s = append(*s, WeightedFactoryMethod{Weight: weight, Factory: f}) | ||
} | ||
|
||
// Iterator returns an iterator function for a Go for loop sorted by weight desc. | ||
func (s WeightedFactoryMethods) Iterator() WeightedProposalMsgIter { | ||
slices.SortFunc(s, func(e, e2 WeightedFactoryMethod) int { | ||
return cmp.Compare(e.Weight, e2.Weight) | ||
}) | ||
return func(yield func(uint32, FactoryMethod) bool) { | ||
for _, v := range s { | ||
if !yield(v.Weight, v.Factory) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
// legacy operation to Msg factory type | ||
func legacyToMsgFactoryAdapter(fn simtypes.MsgSimulatorFnX) FactoryMethod { | ||
return func(ctx context.Context, testData *ChainDataSource, reporter SimulationReporter) (signer []SimAccount, msg sdk.Msg) { | ||
msg, err := fn(ctx, testData.r, testData.AllAccounts(), testData.AddressCodec()) | ||
if err != nil { | ||
reporter.Skip(err.Error()) | ||
return nil, nil | ||
} | ||
return []SimAccount{}, msg | ||
} | ||
} | ||
|
||
// AppendIterators takes multiple WeightedProposalMsgIter and returns a single iterator that sequentially yields items after each one. | ||
func AppendIterators(iterators ...WeightedProposalMsgIter) WeightedProposalMsgIter { | ||
return func(yield func(uint32, FactoryMethod) bool) { | ||
for _, it := range iterators { | ||
it(yield) | ||
} | ||
} | ||
} |
This file contains 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 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
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.
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.
Use the
accs
parameter to initialize the application state.The
accs
parameter has been added to the function signature but is not being used within the function. Consider using it to initialize the application state to ensure consistency during simulation.