Skip to content
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

refactor(sims): Introduce new sims method factory and helper structures #21037

Closed
wants to merge 4 commits into from

Conversation

alpe
Copy link
Contributor

@alpe alpe commented Jul 23, 2024

Description

This PR introduces some new helper types to simplify message construction for simulations (sims). The focus is on better dev UX for new message factories.
Technically, they are adapters that build upon the existing sims framework.

* Message factory

Simple functions as factories for dedicated sdk.Msgs. They have access to the context, reporter and test data environment. For example:

func MsgSendFactory() simsx.SimMsgFactoryFn[*types.MsgSend] {
	return func(ctx context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgSend) {
		from := testData.AnyAccount(reporter, simsx.WithSpendableBalance())
		to := testData.AnyAccount(reporter, simsx.ExcludeAccounts(from))
		coins := from.LiquidBalance().RandSubsetCoins(reporter, simsx.WithSendEnabledCoins())
		return []simsx.SimAccount{from}, types.NewMsgSend(from.AddressBech32, to.AddressBech32, coins)
	}
}

* Sims registry

A new helper to register message factories with a default weight value. They can be overwritten by a parameters file as before. The registry is passed to the AppModule type. For example:

func (am AppModule) WeightedOperationsX(weights simsx.WeightSource, reg simsx.Registry) {
	reg.Add(weights.Get("msg_send", 100), simulation.MsgSendFactory())
	reg.Add(weights.Get("msg_multisend", 10), simulation.MsgMultiSendFactory())
}

* Reporter

The reporter is a flow control structure that can be used in message factories to skip execution at any point. The idea is similar to the testing.T Skip in Go stdlib. Internally, it converts skip, success and failure events to legacy sim messages.
The reporter also provides some capability to print an execution summary.
It is also used to interact with the test data environment to not have errors checked all the time.
Message factories may want to abort early via

if reporter.IsSkipped() {
	return nil, nil
}

* Test data environment

The test data environment provides simple access to accounts and other test data used in most message factories. It also encapsulates some app internals like bank keeper or address codec.

To discuss

  • Where to put simsx package
  • pass bank/ account keeper via module manger or simsx framework

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced a new boolean flag for the simulation client to toggle the use of a faux Merkle tree.
    • Enhanced the simulation manager to utilize account and balance sources, improving the fidelity of simulations.
    • Established a simulation environment for managing accounts and balances, including flexible filtering capabilities.
  • Improvements

    • Updated logging mechanisms for improved output clarity.
    • Simplified the testing framework with new utility functions for slice operations.
    • Enhanced account management with structures that support random account selection and balance manipulation.
  • Test Enhancements

    • Added comprehensive tests for new functionalities, ensuring robust validation of simulation behavior.
  • Bug Fixes

    • Improved error handling and resource management in several simulation functions.

Copy link
Contributor

coderabbitai bot commented Jul 23, 2024

Walkthrough

Walkthrough

The recent changes significantly enhance the simulation framework within the Cosmos SDK by optimizing testing capabilities, account management, and message delivery processes. Key improvements include the addition of new interfaces and methods, refined error handling, and better logging and reporting mechanisms. These updates result in a more modular, maintainable, and flexible testing environment that allows for improved management of simulation behaviors and outcomes.

Changes

Files Change Summary
scripts/build/simulations.mk Added -failfast flag to multiple test targets, improving immediate failure feedback. Removed streaming test targets.
simapp/app.go, simapp/app_di.go Enhanced simulation manager instantiation with AuthKeeper and BankKeeper for better integration.
simapp/sim_bench_test.go Streamlined benchmarking by replacing detailed setup with a call to sims.RunWithSeed.
simapp/sim_test.go Improved type safety by refactoring test functions to use testing.TB and introduced importGenesisStateFactory.
simsx/common_test.go Introduced utilities for simulating account behavior, enhancing testing capabilities.
simsx/environment.go, simsx/environment_test.go Implemented a simulation environment for account and balance management, with corresponding tests.
simsx/msg_factory.go, simsx/msg_factory_test.go Developed a message factory system for simulations with unit tests for functionality.
simsx/params.go, simsx/registry.go Introduced a weight retrieval system and a simulation registry for message management.
simsx/reporter.go, simsx/reporter_test.go Developed a reporting framework for simulations with validation tests.
simsx/slices.go, simsx/slices_test.go Added utility functions for slice operations with corresponding validation tests.
tests/go.mod Marked cosmossdk.io/x/nft as an indirect dependency.
testutils/sims/runner.go Introduced RunWithSeed for single-seed simulation runs; modified parameter types for testing functions.
types/simulation/account.go Expanded Account struct to include AddressBech32 for comprehensive account information.
x/simulation/simulate.go, x/simulation/simulate_test.go Renamed package to simtypes, updated function signatures, and added tests for queued operations.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SimulationManager
    participant AccountSource
    participant BalanceSource
    participant Logger

    User->>+SimulationManager: Initiate simulation
    SimulationManager->>+AccountSource: Fetch account info
    AccountSource-->>-SimulationManager: Return account info
    SimulationManager->>+BalanceSource: Fetch balance info
    BalanceSource-->>-SimulationManager: Return balance info
    SimulationManager->>Logger: Log simulation stats
    Logger-->>-SimulationManager: Confirmation
    SimulationManager-->>-User: Simulation results
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

simsx/registry.go Dismissed Show dismissed Hide dismissed
simsx/reporter.go Dismissed Show dismissed Hide dismissed
@alpe alpe changed the base branch from main to alex/sims2_s0 July 23, 2024 16:07
@alpe alpe mentioned this pull request Jul 23, 2024
12 tasks
Base automatically changed from alex/sims2_s0 to main July 24, 2024 12:44
@alpe alpe changed the title Introduce new sims method factory and helper structures refactor(sims): Introduce new sims method factory and helper structures Jul 24, 2024
@alpe alpe marked this pull request as ready for review July 24, 2024 15:51
@alpe alpe requested a review from a team as a code owner July 24, 2024 15:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (5)
x/simulation/simulate_test.go (1)

15-57: Add a comment explaining the purpose of runQueuedTimeOperations.

The test function is well-structured and covers multiple scenarios. However, adding a brief comment explaining the purpose of runQueuedTimeOperations would enhance readability.

+ // runQueuedTimeOperations processes queued operations and returns the number of executed operations and future operations.
simsx/environment_test.go (2)

13-50: Add a comment explaining the purpose of AnyAccount.

The test function is well-structured and covers multiple scenarios. However, adding a brief comment explaining the purpose of AnyAccount would enhance readability.

+ // AnyAccount returns a random account from the chain data source, optionally filtered by the provided filters.

52-67: Add a comment explaining the purpose of GetAccount and HasAccount.

The test function is well-structured and covers both existing and non-existing accounts. However, adding a brief comment explaining the purpose of GetAccount and HasAccount would enhance readability.

+ // GetAccount returns the account with the given address from the chain data source.
+ // HasAccount checks if the chain data source contains an account with the given address.
x/simulation/operation.go (1)

Action Required: Incorrect function call to queueOperations

The function queueOperations is called with an incorrect argument type at the following location:

  • x/simulation/simulate.go: queueOperations(operationQueue, timeOperationQueue, futureOps)

Please update this call to pass a pointer to a slice of simulation.FutureOperation:

queueOperations(operationQueue, &timeOperationQueue, futureOps)
Analysis chain

Line range hint 79-107:
LGTM! But verify the function usage in the codebase.

The change to use a pointer to a slice improves efficiency by avoiding unnecessary copying. Ensure that all function calls to queueOperations pass a pointer to a slice of simulation.FutureOperation.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `queueOperations` pass a pointer to a slice of `simulation.FutureOperation`.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'queueOperations'

Length of output: 1277

types/module/simulation.go (1)

30-33: Issues Found Due to Method Removal

The WeightedOperations method is still referenced in various parts of the codebase. Removing this method from the HasLegacyWeightedOperations interface in types/module/simulation.go will cause issues in the following files:

  • x/authz/simulation/operations.go
  • x/authz/module/module.go
  • x/slashing/module.go
  • x/slashing/simulation/operations.go
  • x/staking/simulation/operations.go
  • x/protocolpool/depinject.go
  • x/staking/depinject.go
  • x/params/module.go
  • x/nft/module/module.go
  • x/group/simulation/operations.go
  • x/group/simulation/operations_test.go
  • x/gov/module.go
  • x/group/module/module.go
  • x/gov/simulation/operations.go
  • x/nft/simulation/operations.go
  • x/mint/module.go
  • x/feegrant/simulation/operations.go
  • x/distribution/module.go
  • x/distribution/simulation/operations.go
  • x/epochs/module.go
  • x/evidence/module.go
  • x/feegrant/module/depinject.go
  • x/auth/module.go
  • x/bank/simulation/operations.go
  • x/bank/module.go
  • testutils/sims/runner.go
  • tests/integration/staking/simulation/operations_test.go

Please ensure these references are updated or removed accordingly.

Analysis chain

Verify Impact of Method Removal

The WeightedOperations method has been removed from the SimulationManager. Ensure that this removal does not impact other parts of the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing the `WeightedOperations` method.

# Test: Search for the method usage. Expect: No occurrences of the removed method.
rg --type go -A 5 $'WeightedOperations'

Length of output: 29775

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0fda53f and 690ec63.

Files selected for processing (31)
  • scripts/build/simulations.mk (3 hunks)
  • simapp/app.go (1 hunks)
  • simapp/app_di.go (1 hunks)
  • simapp/sim_bench_test.go (2 hunks)
  • simapp/sim_test.go (5 hunks)
  • simsx/common_test.go (1 hunks)
  • simsx/delivery.go (1 hunks)
  • simsx/delivery_test.go (1 hunks)
  • simsx/environment.go (1 hunks)
  • simsx/environment_test.go (1 hunks)
  • simsx/msg_factory.go (1 hunks)
  • simsx/msg_factory_test.go (1 hunks)
  • simsx/params.go (1 hunks)
  • simsx/registry.go (1 hunks)
  • simsx/registry_test.go (1 hunks)
  • simsx/reporter.go (1 hunks)
  • simsx/reporter_test.go (1 hunks)
  • simsx/slices.go (1 hunks)
  • simsx/slices_test.go (1 hunks)
  • tests/go.mod (1 hunks)
  • testutil/sims/simulation_helpers.go (2 hunks)
  • testutil/sims/state_helpers.go (2 hunks)
  • testutils/sims/runner.go (8 hunks)
  • types/module/simulation.go (6 hunks)
  • types/simulation/account.go (2 hunks)
  • types/simulation/config.go (1 hunks)
  • x/simulation/client/cli/flags.go (3 hunks)
  • x/simulation/log.go (1 hunks)
  • x/simulation/operation.go (2 hunks)
  • x/simulation/simulate.go (14 hunks)
  • x/simulation/simulate_test.go (1 hunks)
Files skipped from review due to trivial changes (2)
  • tests/go.mod
  • x/simulation/log.go
Additional context used
Path-based instructions (28)
simapp/sim_bench_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

simsx/slices_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

simsx/slices.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

types/simulation/config.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/params.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/msg_factory_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

x/simulation/simulate_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

simsx/environment_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

types/simulation/account.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/delivery_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

x/simulation/client/cli/flags.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/delivery.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/simulation/operation.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/msg_factory.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

testutil/sims/simulation_helpers.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/common_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

simsx/reporter_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

types/module/simulation.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/registry.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/registry_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

simsx/reporter.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/sim_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

testutil/sims/state_helpers.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

testutils/sims/runner.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/simulation/simulate.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/app_di.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simsx/environment.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/app.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (110)
simapp/sim_bench_test.go (1)

21-21: LGTM! Verify the correctness of sims.RunWithSeed usage.

The refactoring simplifies the benchmark setup and improves readability. Ensure that sims.RunWithSeed correctly encapsulates the necessary setup logic.

Verification successful

Verified the correctness of sims.RunWithSeed usage.

The RunWithSeed function correctly encapsulates the necessary setup logic, including environment setup, simulation execution, and post-run actions.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correctness of `sims.RunWithSeed` usage.

# Test: Search for the `RunWithSeed` function implementation. Expect: Correct encapsulation of setup logic.
rg --type go -A 10 'func RunWithSeed' | grep -A 10 'RunWithSeed'

Length of output: 1309


Script:

#!/bin/bash
# Description: Retrieve the complete implementation of the `RunWithSeed` function.

# Test: Extract the full implementation of the `RunWithSeed` function.
rg --type go -A 50 'func RunWithSeed' testutils/sims/runner.go

Length of output: 2745

simsx/slices_test.go (1)

1-40: Good test coverage!

The unit tests cover the Collect, First, and OneOf functions effectively. Ensure that edge cases are also considered in future tests.

simsx/slices.go (1)

1-38: Well-documented and clean code!

The helper functions for slice operations are well-documented and follow the Uber Golang style guide. Ensure to handle edge cases and panics as mentioned in the comments.

types/simulation/config.go (4)

28-29: LGTM! Renaming T to TB improves clarity.

The renaming enhances the clarity regarding the type of this field.


29-29: LGTM! Adding FauxMerkle enhances configuration capabilities.

The addition of this field expands the configuration options available.


37-40: LGTM! Changing the parameter to testing.TB increases flexibility.

This change allows the method to accept both *testing.T and *testing.B, enhancing its versatility.


40-40: Verify the usage of the With method in the codebase.

Ensure all calls to the With method pass the correct type of parameter.

Verification successful

The With method is used correctly throughout the codebase. The method is designed to accept variadic parameters of type interface{}, allowing for flexibility in the types of parameters passed.

  • The method definitions confirm that it can accept any type of parameter.
  • The instances found in the codebase primarily pass strings and key-value pairs, which are appropriate for logging contexts.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all calls to the `With` method pass the correct type of parameter.

# Test: Search for the method usage. Expect: Only occurances of the new signature.
rg --type go -A 5 $'.With('

Length of output: 91


Script:

#!/bin/bash
# Description: Verify all calls to the `With` method pass the correct type of parameter.

# Test: Search for the method usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 '\.With\('

Length of output: 25234


Script:

#!/bin/bash
# Description: Find the definition of the `With` method to verify its parameter types.

# Search for the method definition in Go files.
rg --type go 'func (.*) With\(' -A 5

Length of output: 2274

simsx/params.go (3)

10-12: LGTM! The WeightSource interface is simple and clear.

The interface defines a single method for retrieving weights, adhering to the Uber Golang style guide.


20-24: LGTM! The WeightSourceFn type and its method Get are correctly implemented.

The function adapter correctly implements the WeightSource interface.


44-51: LGTM! The ParamWeightSource function is well-documented and correctly implemented.

The function provides a clear and correct implementation for retrieving weights.

simsx/msg_factory_test.go (1)

14-52: LGTM! The TestMsgFactories function covers various scenarios and uses assertions correctly.

The test cases are well-structured and verify the expected outcomes.

Verify sufficient code coverage.

Ensure that the tests provide sufficient coverage for the changes in the pull request.

x/simulation/simulate_test.go (1)

3-13: Imports look good.

The imports are appropriate for the test file.

simsx/environment_test.go (1)

3-11: Imports look good.

The imports are appropriate for the test file.

types/simulation/account.go (2)

17-21: Enhancement to Account struct looks good.

The addition of the AddressBech32 field enhances the functionality of the Account struct.


54-58: Modifications to RandomAccounts function look good.

The modifications ensure that the AddressBech32 field is populated correctly.

simsx/delivery_test.go (4)

15-42: Good coverage for skipped reporter scenario.

The test case for "error when reporter skipped" is well-written and covers the scenario effectively. Ensure that the SimDeliverFn function handles the skip correctly.


43-50: Good coverage for successful delivery scenario.

The test case for "successful delivery" is well-written and covers the scenario effectively. Ensure that the SimDeliverFn function handles the success correctly.


51-58: Good coverage for error delivery scenario.

The test case for "error delivery" is well-written and covers the scenario effectively. Ensure that the SimDeliverFn function handles the error correctly.


59-66: Good coverage for error delivery handled scenario.

The test case for "error delivery handled" is well-written and covers the scenario effectively. Ensure that the SimDeliverFn function handles the error correctly and that the deliveryResultHandler processes the error as expected.

x/simulation/client/cli/flags.go (3)

33-33: Declare the new flag FlagFauxMerkle.

The new flag FlagFauxMerkle is declared correctly.


58-58: Add the new flag to GetSimulatorFlags.

The new flag FlagFauxMerkle is added to the GetSimulatorFlags function correctly.


78-78: Include the new flag in NewConfigFromFlags.

The new flag FlagFauxMerkle is included in the NewConfigFromFlags function correctly.

simsx/delivery.go (4)

1-1: Package declaration.

The package declaration is correct.


3-14: Imports.

The imports are necessary and correctly included.


16-26: Type declarations.

The type declarations are correct and necessary for the function implementation.


28-98: Function DeliverSimsMsg implementation.

The function DeliverSimsMsg is well-implemented and covers the necessary logic for delivering simulation messages. The error handling and reporting are correctly implemented.

simsx/msg_factory.go (4)

9-13: Interface SimMsgFactoryX looks good.

The interface methods are well-defined and align with the purpose of message factories.


28-56: Type ResultHandlingSimMsgFactory looks good.

The struct and its methods are well-implemented, providing functionality for handling delivery results.


63-81: Type LazyStateSimMsgFactory looks good.

The struct and its methods are well-implemented, providing functionality for handling future operations.


90-112: Type SimMsgFactoryFn looks good.

The type alias and its methods are well-defined, ensuring successful message delivery.

testutil/sims/simulation_helpers.go (1)

49-52: Refactor of PrintStats function looks good.

The change enhances the function's usability by allowing it to integrate with various logging mechanisms.

scripts/build/simulations.mk (12)

5-5: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


19-19: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


25-25: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


30-30: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


35-35: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


42-42: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


47-47: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


52-53: LGTM! The addition of -failfast and -FauxMerkle=true flags are correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure, and the -FauxMerkle=true flag will influence the test's behavior regarding Merkle tree verification.


57-57: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


80-80: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


85-85: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.


91-91: LGTM! The addition of -failfast flag is correct.

The -failfast flag will help in getting quicker feedback by stopping the tests on the first failure.

simsx/common_test.go (5)

24-35: LGTM! The function SimAccountFixture is correct.

The function creates a SimAccount with optional mutators and follows best practices.


38-46: LGTM! The function MemoryAccountSource is correct.

The function creates an AccountSourceFn from a list of SimAccount and follows best practices.


49-62: LGTM! The function txConfig is correct.

The function creates a client.TxConfig with custom options and follows best practices.


136-154: LGTM! The type MockAccountSourceX is correct.

The type implements AccountSourceX with mock methods for testing and follows best practices.


156-161: LGTM! The function must is correct.

The function panics if an error is encountered and follows best practices.

simsx/reporter_test.go (4)

14-70: LGTM! The test function TestSimulationReporterToLegacy is correct.

The test function covers various scenarios for converting SimulationReporter to legacy operation messages and follows best practices.


73-134: LGTM! The test function TestSimulationReporterTransitions is correct.

The test function covers various state transitions of SimulationReporter and follows best practices.


136-156: LGTM! The test function TestSkipHook is correct.

The test function covers the skip hook functionality of SimulationReporter and follows best practices.


158-217: LGTM! The test function TestReporterSummary is correct.

The test function covers the summary generation of SimulationReporter and follows best practices.

types/module/simulation.go (5)

50-57: New Interfaces Approved

The AccountSource and BalanceSource interfaces enhance modularity and flexibility by providing methods to retrieve account information and spendable coins.


34-46: New Interfaces Approved

The HasLegacyProposalMsgs and HasLegacyProposalContents interfaces standardize the simulation of governance proposals, replacing deprecated functionality.


34-56: New Types Approved

The regCommon and AbstractRegistry types enhance code reuse and modularity by providing common functionality for registries.


72-77: Function Signature Updated

The NewSimulationManager function now accepts AccountSource and BalanceSource parameters, enhancing the setup of the simulation manager.

Ensure that all calls to NewSimulationManager are updated to match the new signature.

Verification successful

Function Signature Updated

The NewSimulationManager function now accepts AccountSource and BalanceSource parameters, enhancing the setup of the simulation manager.

  • All calls to NewSimulationManager have been updated to match the new signature.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `NewSimulationManager` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'NewSimulationManager'

Length of output: 3417


Line range hint 87-109:
Function Signature Updated

The NewSimulationManagerFromAppModules function now accepts AccountSource and BalanceSource parameters, enhancing the setup of the simulation manager.

Ensure that all calls to NewSimulationManagerFromAppModules are updated to match the new signature.

Verification successful

Function Signature Updated

The NewSimulationManagerFromAppModules function now accepts AccountSource and BalanceSource parameters, enhancing the setup of the simulation manager.

All calls to NewSimulationManagerFromAppModules have been updated to match the new signature:

  • simapp/app.go
  • simapp/app_di.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `NewSimulationManagerFromAppModules` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go -A 5 $'NewSimulationManagerFromAppModules'

Length of output: 1943

simsx/registry.go (6)

17-27: New Interfaces Approved

The Registry, FutureOpsRegistry, and AccountSourceX interfaces enhance modularity and flexibility by providing methods to register message factories and manage accounts.


34-56: New Types Approved

The regCommon and AbstractRegistry types enhance code reuse and modularity by providing common functionality for registries.


58-96: New Type and Functions Approved

The WeightedOperationRegistryAdapter type and its functions enhance the integration of new message factories with the legacy simulation system.


102-121: New Function Approved

The legacyOperationAdapter function ensures compatibility between new message factories and the legacy simulation system.


123-141: New Type and Functions Approved

The FutureOperationRegistryAdapter type and its functions enhance the management of future operations in the simulation framework.


143-188: New Type and Functions Approved

The SimsProposalRegistryAdapter type and its functions enhance the integration of new proposal message factories with the legacy simulation system.

simsx/registry_test.go (3)

118-172: Test Function Approved

The TestSimsProposalRegistryAdapter function covers various scenarios, including successful execution, skipping execution, and error handling.

Ensure that the test coverage is sufficient for the new functionality.


174-215: Test Function Approved

The TestRunWithFailFast function covers various scenarios, including successful execution, skipping execution, and handling panics.

Ensure that the test coverage is sufficient for the new functionality.

Verification successful

Test Function Approved

The TestRunWithFailFast function is adequately covered by the tests in the simsx/registry_test.go file.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the test coverage for the new functionality.

# Test: Run the tests and generate a coverage report. Expect: High coverage for the new functionality.
go test ./simsx -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html

Length of output: 236


Script:

#!/bin/bash
# Search for test functions in the Go codebase
ast-grep --lang go --pattern 'func Test$_($_ *testing.T) { $$$ }'

# Check for the usage of the `runWithFailFast` function in the test files
rg 'runWithFailFast'

Length of output: 545


20-115: Test Function Approved

The TestSimsMsgRegistryAdapter function covers various scenarios, including successful execution, skipping execution, future ops registration, and error handling.

Ensure that the test coverage is sufficient for the new functionality.

simsx/reporter.go (16)

19-34: Interface definition looks good.

The SimulationReporter interface is well-defined and covers essential methods for reporting simulation results.


72-86: Struct definition looks good.

The BasicSimulationReporter structure is well-defined and includes necessary fields for simulation reporting.


88-99: Constructor looks good.

The NewBasicSimulationReporter method correctly initializes a BasicSimulationReporter instance with optional skip hooks.


101-124: Method definition looks good.

The WithScope method correctly creates a new SimulationReporter instance with additional scope.


126-128: Method definition looks good.

The Skip method correctly sets the status to skipped with a comment.


130-132: Method definition looks good.

The Skipf method correctly sets the status to skipped with a formatted comment.


134-136: Method definition looks good.

The IsSkipped method correctly checks if the status is skipped or completed.


138-154: Method definition looks good.

The ToLegacyOperationMsg method correctly converts the current status to a legacy operation message. Ensure proper error handling in case of operation abortion.


157-164: Method definition looks good.

The Fail method correctly sets the status to completed with an error.


166-180: Method definition looks good.

The Success method correctly sets the status to completed with a success message. Ensure proper handling of proto bytes.


182-187: Method definition looks good.

The Close method correctly returns the error captured on fail.


189-208: Method definition looks good.

The toStatus method correctly transitions the status with comments and triggers skip callbacks if needed. Ensure the use of panic is justified.


221-225: Struct definition looks good.

The ExecutionSummary structure is well-defined and includes necessary fields for managing execution summaries.


231-245: Method definition looks good.

The Add method correctly adds a new entry to the execution summary.


247-260: Method definition looks good.

The String method correctly returns a string representation of the execution summary.


262-268: Method definition looks good.

The sum method correctly returns the sum of integer values in a slice.

simapp/sim_test.go (6)

71-71: Verify the use of testing.TB.

Ensure that the use of testing.TB instead of *testing.T is justified and does not introduce any issues.


113-113: Verify the use of testing.TB.

Ensure that the use of testing.TB instead of *testing.T is justified and does not introduce any issues.


186-186: Verify the use of testing.TB.

Ensure that the use of testing.TB instead of *testing.T is justified and does not introduce any issues.


222-222: Verify the use of testing.TB.

Ensure that the use of testing.TB instead of *testing.T is justified and does not introduce any issues.


187-187: Verify the use of testing.TB.

Ensure that the use of testing.TB instead of *testing.T is justified and does not introduce any issues.


Line range hint 272-278: Function definition looks good.

The FuzzFullAppSimulation function correctly fuzz tests the full app simulation.

testutil/sims/state_helpers.go (2)

254-273: Verify the use of _ for the unused parameter.

Ensure that the use of _ for the unused io.Reader parameter is justified and does not introduce any issues.


275-307: Function definition looks good.

The AccountsFromAppState function correctly extracts accounts from the app state.

testutils/sims/runner.go (8)

75-75: LGTM! The parameter type change for postRunActions broadens the scope of the testing interface.

This change allows the use of any testing type that implements the testing.TB interface, increasing versatility.


103-103: LGTM! The parameter type change for postRunActions broadens the scope of the testing interface.

This change allows the use of any testing type that implements the testing.TB interface, increasing versatility.


117-153: LGTM! The new function RunWithSeed improves readability and maintainability.

This function encapsulates the logic for running a single simulation run, separating concerns and enhancing clarity.


156-158: LGTM! The new interface HasWeightedOperationsX facilitates the integration of weighted operations.

This interface defines a contract for modules participating in the simulation, enhancing the capabilities for simulating complex behaviors.


159-162: LGTM! The new interface HasWeightedOperationsXWithProposals facilitates the integration of weighted operations and proposals.

This interface defines a contract for modules participating in the simulation with proposals, enhancing the capabilities for simulating complex behaviors.


163-165: LGTM! The new interface HasProposalMsgsX facilitates the integration of proposal messages.

This interface defines a contract for modules participating in the simulation with proposal messages, enhancing the capabilities for simulating complex behaviors.


169-172: LGTM! The new interface HasLegacyWeightedOperations facilitates the integration of legacy weighted operations.

This interface defines a contract for modules participating in the simulation with legacy weighted operations, enhancing the capabilities for simulating complex behaviors.


175-178: LGTM! The new interface HasLegacyProposalMsgs facilitates the integration of legacy proposal messages.

This interface defines a contract for modules participating in the simulation with legacy proposal messages, enhancing the capabilities for simulating complex behaviors.

x/simulation/simulate.go (5)

34-39: LGTM! The parameter type changes improve clarity.

Using simtypes instead of simulation explicitly indicates that these types are related to simulation functionality.


68-72: LGTM! The parameter type changes improve clarity.

Using simtypes instead of simulation explicitly indicates that these types are related to simulation functionality.


88-92: LGTM! The parameter type changes improve clarity.

Using simtypes instead of simulation explicitly indicates that these types are related to simulation functionality.


362-366: LGTM! The parameter type changes improve clarity and the error handling improvements enhance robustness.

Using simtypes instead of simulation explicitly indicates that these types are related to simulation functionality. The error handling improvements ensure that logs are printed upon encountering errors.


399-404: LGTM! The parameter type changes improve clarity and the error handling improvements enhance robustness.

Using simtypes instead of simulation explicitly indicates that these types are related to simulation functionality. The error handling improvements ensure that logs are printed upon encountering errors.

simapp/app_di.go (1)

280-280: LGTM! The parameter list modification enhances the capability of the simulation manager.

Including app.AuthKeeper and app.BankKeeper enhances the simulation manager's capability to manage simulations involving authentication and banking functionalities.

simsx/environment.go (5)

18-29: LGTM!

The contextAwareBalanceSource struct and its methods are well-implemented.


32-45: LGTM! But verify the randomness quality.

The SimAccount struct and its methods are well-implemented. Ensure that the rand package provides sufficient randomness for your use case.


59-66: LGTM!

The CoinsFilter interface and CoinsFilterFn function type are well-implemented.


80-99: LGTM!

The statefulCoinsFilter struct and its methods are well-implemented.


386-455: LGTM!

The XRand struct and its methods are well-implemented.

simapp/app.go (1)

539-539: LGTM!

The changes to the NewSimApp function enhance the simulation manager's capabilities by including AuthKeeper and BankKeeper. The overall control flow remains intact.

Comment on lines +245 to +277
type ChainDataSource struct {
r *rand.Rand
addressToAccountsPosIndex map[string]int
accounts []SimAccount
accountSource ModuleAccountSource
addressCodec address.Codec
bank contextAwareBalanceSource
}

// NewChainDataSource constructor
func NewChainDataSource(
ctx context.Context,
r *rand.Rand,
ak ModuleAccountSource,
bk BalanceSource,
codec address.Codec,
oldSimAcc ...simtypes.Account,
) *ChainDataSource {
acc := make([]SimAccount, len(oldSimAcc))
index := make(map[string]int, len(oldSimAcc))
bank := contextAwareBalanceSource{ctx: ctx, bank: bk}
for i, a := range oldSimAcc {
acc[i] = SimAccount{Account: a, r: r, bank: bank}
index[a.AddressBech32] = i
}
return &ChainDataSource{
r: r,
accountSource: ak,
addressCodec: codec,
accounts: acc,
bank: bank,
addressToAccountsPosIndex: index,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisit the use of panic in ChainDataSource.

The ChainDataSource struct and its methods are well-implemented. However, the use of panic in some methods should be revisited to ensure proper error handling.

-		panic(err)
+		reporter.Skipf("failed to initialize chain data source: %s", err)

Also applies to: 280-384

Comment on lines 48 to 168
return statefulCoinsFilterFn(func(s *SimAccount, coins sdk.Coins) bool {
for _, coin := range coins {
if !s.bank.IsSendEnabledDenom(coin.Denom) {
return false
}
}
return true
})
}

// filter with context of SimAccount
type statefulCoinsFilter struct {
s *SimAccount
do func(s *SimAccount, c sdk.Coins) bool
}

// constructor
func statefulCoinsFilterFn(f func(s *SimAccount, c sdk.Coins) bool) CoinsFilter {
return &statefulCoinsFilter{do: f}
}

func (f statefulCoinsFilter) Accept(c sdk.Coins) bool {
if f.s == nil {
panic("account not set")
}
return f.do(f.s, c)
}

func (f *statefulCoinsFilter) visit(s *SimAccount) {
f.s = s
}

var _ visitable = &statefulCoinsFilter{}

type visitable interface {
visit(s *SimAccount)
}

// RandSubsetCoins return random amounts from the current balance. When the coins are empty, skip is called on the reporter.
// The amounts are removed from the liquid balance.
func (b *SimsAccountBalance) RandSubsetCoins(reporter SimulationReporter, filters ...CoinsFilter) sdk.Coins {
amount := b.randomAmount(5, reporter, b.Coins, filters...)
b.Coins = b.Coins.Sub(amount...)
if amount.Empty() {
reporter.Skip("got empty amounts")
}
return amount
}

// RandSubsetCoin return random amount from the current balance. When the coins are empty, skip is called on the reporter.
// The amount is removed from the liquid balance.
func (b *SimsAccountBalance) RandSubsetCoin(reporter SimulationReporter, denom string, filters ...CoinsFilter) sdk.Coin {
ok, coin := b.Find(denom)
if !ok {
reporter.Skipf("no such coin: %s", denom)
return sdk.NewCoin(denom, math.ZeroInt())
}
amounts := b.randomAmount(5, reporter, sdk.Coins{coin}, filters...)
if amounts.Empty() {
reporter.Skip("empty coin")
return sdk.NewCoin(denom, math.ZeroInt())
}
b.BlockAmount(amounts[0])
return amounts[0]
}

// BlockAmount returns true when balance is > requested amount and subtracts the amount from the liquid balance
func (b *SimsAccountBalance) BlockAmount(amount sdk.Coin) bool {
ok, coin := b.Coins.Find(amount.Denom)
if !ok || !coin.IsPositive() || !coin.IsGTE(amount) {
return false
}
b.Coins = b.Coins.Sub(amount)
return true
}

func (b *SimsAccountBalance) randomAmount(retryCount int, reporter SimulationReporter, coins sdk.Coins, filters ...CoinsFilter) sdk.Coins {
if retryCount < 0 || b.Coins.Empty() {
reporter.Skip("failed to find matching amount")
return sdk.Coins{}
}
amount := simtypes.RandSubsetCoins(b.r, coins)
for _, filter := range filters {
if f, ok := filter.(visitable); ok {
f.visit(b.owner)
}
if !filter.Accept(amount) {
return b.randomAmount(retryCount-1, reporter, coins, filters...)
}
}
return amount
}

func (b *SimsAccountBalance) RandFees() sdk.Coins {
amount, err := simtypes.RandomFees(b.r, b.Coins)
if err != nil {
panic(err.Error()) // todo: setup a better way to abort execution
} // todo: revisit the panic
return amount
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revisit the use of panic in RandFees.

The SimsAccountBalance struct and its methods are well-implemented. However, the use of panic in RandFees should be revisited to ensure proper error handling.

-		panic(err.Error()) // todo: setup a better way to abort execution
+		reporter.Skipf("failed to generate random fees: %s", err)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
type SimsAccountBalance struct {
sdk.Coins
owner *SimAccount
r *rand.Rand
}
// NewSimsAccountBalance constructor
func NewSimsAccountBalance(o *SimAccount, r *rand.Rand, coins sdk.Coins) *SimsAccountBalance {
return &SimsAccountBalance{Coins: coins, r: r, owner: o}
}
type CoinsFilter interface {
Accept(c sdk.Coins) bool // returns false to reject
}
type CoinsFilterFn func(c sdk.Coins) bool
func (f CoinsFilterFn) Accept(c sdk.Coins) bool {
return f(c)
}
func WithSendEnabledCoins() CoinsFilter {
return statefulCoinsFilterFn(func(s *SimAccount, coins sdk.Coins) bool {
for _, coin := range coins {
if !s.bank.IsSendEnabledDenom(coin.Denom) {
return false
}
}
return true
})
}
// filter with context of SimAccount
type statefulCoinsFilter struct {
s *SimAccount
do func(s *SimAccount, c sdk.Coins) bool
}
// constructor
func statefulCoinsFilterFn(f func(s *SimAccount, c sdk.Coins) bool) CoinsFilter {
return &statefulCoinsFilter{do: f}
}
func (f statefulCoinsFilter) Accept(c sdk.Coins) bool {
if f.s == nil {
panic("account not set")
}
return f.do(f.s, c)
}
func (f *statefulCoinsFilter) visit(s *SimAccount) {
f.s = s
}
var _ visitable = &statefulCoinsFilter{}
type visitable interface {
visit(s *SimAccount)
}
// RandSubsetCoins return random amounts from the current balance. When the coins are empty, skip is called on the reporter.
// The amounts are removed from the liquid balance.
func (b *SimsAccountBalance) RandSubsetCoins(reporter SimulationReporter, filters ...CoinsFilter) sdk.Coins {
amount := b.randomAmount(5, reporter, b.Coins, filters...)
b.Coins = b.Coins.Sub(amount...)
if amount.Empty() {
reporter.Skip("got empty amounts")
}
return amount
}
// RandSubsetCoin return random amount from the current balance. When the coins are empty, skip is called on the reporter.
// The amount is removed from the liquid balance.
func (b *SimsAccountBalance) RandSubsetCoin(reporter SimulationReporter, denom string, filters ...CoinsFilter) sdk.Coin {
ok, coin := b.Find(denom)
if !ok {
reporter.Skipf("no such coin: %s", denom)
return sdk.NewCoin(denom, math.ZeroInt())
}
amounts := b.randomAmount(5, reporter, sdk.Coins{coin}, filters...)
if amounts.Empty() {
reporter.Skip("empty coin")
return sdk.NewCoin(denom, math.ZeroInt())
}
b.BlockAmount(amounts[0])
return amounts[0]
}
// BlockAmount returns true when balance is > requested amount and subtracts the amount from the liquid balance
func (b *SimsAccountBalance) BlockAmount(amount sdk.Coin) bool {
ok, coin := b.Coins.Find(amount.Denom)
if !ok || !coin.IsPositive() || !coin.IsGTE(amount) {
return false
}
b.Coins = b.Coins.Sub(amount)
return true
}
func (b *SimsAccountBalance) randomAmount(retryCount int, reporter SimulationReporter, coins sdk.Coins, filters ...CoinsFilter) sdk.Coins {
if retryCount < 0 || b.Coins.Empty() {
reporter.Skip("failed to find matching amount")
return sdk.Coins{}
}
amount := simtypes.RandSubsetCoins(b.r, coins)
for _, filter := range filters {
if f, ok := filter.(visitable); ok {
f.visit(b.owner)
}
if !filter.Accept(amount) {
return b.randomAmount(retryCount-1, reporter, coins, filters...)
}
}
return amount
}
func (b *SimsAccountBalance) RandFees() sdk.Coins {
amount, err := simtypes.RandomFees(b.r, b.Coins)
if err != nil {
panic(err.Error()) // todo: setup a better way to abort execution
} // todo: revisit the panic
return amount
}
func (b *SimsAccountBalance) RandFees(reporter SimulationReporter) sdk.Coins {
amount, err := simtypes.RandomFees(b.r, b.Coins)
if err != nil {
reporter.Skipf("failed to generate random fees: %s", err)
return sdk.Coins{} // return an empty Coins object to handle the error case gracefully
} // todo: revisit the panic
return amount
}

Comment on lines +85 to +132
type FakeAccountI struct {
SimAccount
id, seq uint64
}

func (m FakeAccountI) GetAddress() sdk.AccAddress {
return m.Address
}

func (m FakeAccountI) GetPubKey() cryptotypes.PubKey {
return m.PubKey
}

func (m FakeAccountI) GetAccountNumber() uint64 {
return m.id
}

func (m FakeAccountI) GetSequence() uint64 {
return m.seq
}

func (m FakeAccountI) Reset() {
panic("implement me")
}

func (m FakeAccountI) String() string {
panic("implement me")
}

func (m FakeAccountI) ProtoMessage() {
panic("implement me")
}

func (m FakeAccountI) SetAddress(address sdk.AccAddress) error {
panic("implement me")
}

func (m FakeAccountI) SetPubKey(key cryptotypes.PubKey) error {
panic("implement me")
}

func (m FakeAccountI) SetAccountNumber(u uint64) error {
panic("implement me")
}

func (m FakeAccountI) SetSequence(u uint64) error {
panic("implement me")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid methods that panic in FakeAccountI.

While the type FakeAccountI is correct, methods like Reset, String, ProtoMessage, SetAddress, SetPubKey, SetAccountNumber, and SetSequence should ideally not panic. Consider providing meaningful implementations or documenting why they panic.

alpe added 2 commits July 25, 2024 10:00
* main:
  build(deps): Bump bufbuild/buf-setup-action from 1.35.0 to 1.35.1 (#21063)
  chore: add test for account creation (#21053)
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 690ec63 and f495a50.

Files selected for processing (1)
  • simsx/environment.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • simsx/environment.go

@@ -0,0 +1,98 @@
package simsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont follow the x suffix, it feels odd

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it feels experimental

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simulation helpers are now spread across three pkgs, is there a plan to unify them or deprecate some?

* main:
  feat(log): remove core dependency and update core interface to be dependency free (#21045)
  chore: fix some comments (#21085)
  feat: simulate nested messages (#20291)
  chore(network): remove `DefaultConfigWithAppConfigWithQueryGasLimit` (#21055)
  fix(runtime): remove `appv1alpha1.Config` from runtime (#21042)
  feat(simapp/v2): Add store server to testnet init cmd (#21076)
  chore(indexer/postgres): update to changes on main (#21077)
  feat(schema/appdata): async listener mux'ing (#20879)
  ci: Use large box for 052 branch sims on CI (#21067)
  chore(all): replace all `fmt.Errorf` without paramters with `errors.New` (#21068)
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f495a50 and e7a9942.

Files selected for processing (6)
  • simapp/app.go (1 hunks)
  • simapp/app_di.go (1 hunks)
  • tests/go.mod (1 hunks)
  • testutils/sims/runner.go (8 hunks)
  • types/simulation/account.go (2 hunks)
  • x/simulation/simulate.go (14 hunks)
Files skipped from review as they are similar to previous changes (4)
  • simapp/app.go
  • tests/go.mod
  • types/simulation/account.go
  • x/simulation/simulate.go
Additional context used
Path-based instructions (2)
testutils/sims/runner.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

simapp/app_di.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (9)
testutils/sims/runner.go (8)

117-153: New function added: RunWithSeed.

The addition of this function improves readability and maintainability by encapsulating the logic for a single simulation run.


156-158: New interface added: HasWeightedOperationsX.

This interface defines a contract for modules to provide weighted operations for simulations.


159-162: New interface added: HasWeightedOperationsXWithProposals.

This interface defines a contract for modules to provide weighted operations and proposals for simulations.


163-165: New interface added: HasProposalMsgsX.

This interface defines a contract for modules to provide proposal messages for simulations.


169-171: New interface added: HasLegacyWeightedOperations.

This interface defines a contract for modules to provide legacy weighted operations for simulations.


175-177: New interface added: HasLegacyProposalMsgs.

This interface defines a contract for modules to provide legacy proposal messages for simulations.


75-75: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to Run match the new signature.


103-103: LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to RunWithSeeds match the new signature.

simapp/app_di.go (1)

279-279: Enhanced SimulationManager instantiation.

The addition of app.AuthKeeper and app.BankKeeper as parameters to NewSimulationManagerFromAppModules likely enhances the simulation manager's capability to manage simulations involving authentication and banking functionalities.

@tac0turtle
Copy link
Member

simulation helpers are now spread across three pkgs, is there a plan to unify them or deprecate some?

discussed offline the deprecation/cleanup will come later, this was done to split prs

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Looks good to me.
It would be super useful if you copy/paste your PR description in a README.md in simsx. I agree that the name is weird, but not having it under x/ makes more sense.

Do we backport this to v0.52? And delete x/simulation in the next version?
Also, as said in a comment above, I think the new sims API should be in core/appmodule/v2/sims.go (aliased to v1 too).

@@ -276,7 +276,7 @@ func NewSimApp(
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AuthKeeper, &app.AccountsKeeper, authsims.RandomGenesisAccounts),
}
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)
app.sm = module.NewSimulationManagerFromAppModules(app.AuthKeeper, app.BankKeeper, app.ModuleManager.Modules, overrideModules)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API breaking changelog entry in main changelog, and wiring change in simapp/CHANGELOG.md

// SetupSimulation creates the config, db (levelDB), temporary directory and logger for the simulation tests.
// If `skip` is false it skips the current test. `skip` should be set using the `FlagEnabledValue` flag.
// Returns error on an invalid db instantiation or temp dir creation.
func SetupSimulation(config simtypes.Config, dirPrefix, dbName string, verbose, skip bool) (dbm.DB, string, log.Logger, bool, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changelog entry

@@ -250,52 +251,58 @@ func AppStateRandomizedFn(

// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (genutiltypes.AppGenesis, []simtypes.Account, error) {
func AppStateFromGenesisFileFn(_ io.Reader, cdc codec.JSONCodec, genesisFile string) (genutiltypes.AppGenesis, []simtypes.Account, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we have other breaking changes? Why not breaking it here too?

}

type (
HasWeightedOperationsX interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have docs for this?

}
}

type (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think simulations interfaces should maybe get a place in core/appmodule(v2).
type/module is actually for legacy stuff, so the legacy interfaces can stay there but it would be cool if the new one could be in core, as it is the central place of module APIs.

PubKey cryptotypes.PubKey
Address sdk.AccAddress
ConsKey cryptotypes.PrivKey
AddressBech32 string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store the bytes and use the address codec wherever we need this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The encoding had some significant performance impact. The sim accounts are not modified during the whole sims run for a seed. Therefore it made sense for me to do the encoding once in the beginning and then use this field instead of re-encoding. This field is only used by sims code and not in the app or modules. WDYT?


accs = tmpAccs
accs = slices.DeleteFunc(accs, func(acc simtypes.Account) bool {
return blockedAddrs[acc.AddressBech32]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we had the address codec available, why removing it?

@@ -191,6 +186,10 @@ func SimulateFromSeedX(
exportedParams = params
}

if _, err := app.FinalizeBlock(finalizeBlockReq); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏾

@alpe
Copy link
Contributor Author

alpe commented Aug 1, 2024

Good feedback! Very much appreciated

@alpe alpe marked this pull request as draft August 12, 2024 08:55
@alpe
Copy link
Contributor Author

alpe commented Sep 9, 2024

Closing in favour of #21613

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants