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

test: Add more sims test scenarios #23278

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

test: Add more sims test scenarios #23278

wants to merge 2 commits into from

Conversation

alpe
Copy link
Contributor

@alpe alpe commented Jan 9, 2025

Description

See #23265


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

  • Simulation Testing Improvements

    • Updated simulation test configurations and parameters
    • Removed period-based parameters from various test targets
    • Enhanced simulation framework with more flexible testing capabilities
    • Improved type safety for block height and number representations
    • Removed deprecated flag for inventory check period
  • Code Refactoring

    • Transitioned simulation-related variables to use uint64 type
    • Updated error handling in simulation environment setup
    • Simplified simulation app instance initialization
  • Testing Enhancements

    • Added new test functions for application state simulation
    • Improved determinism checks in simulation tests
    • Introduced more robust state export and import testing

Copy link
Contributor

coderabbitai bot commented Jan 9, 2025

📝 Walkthrough

Walkthrough

The pull request introduces comprehensive modifications to the Cosmos SDK simulation framework, focusing on enhancing test configurations, updating type representations, and refining simulation testing capabilities. The changes span multiple files across the simulation infrastructure, including updates to test runners, configuration handling, and simulation parameters. Key modifications include transitioning from int to uint64 for block-related fields, removing certain simulation flags, and improving the flexibility of simulation test setups.

Changes

File Change Summary
scripts/build/simulations.mk - Updated simulation test targets to use simapp/v2 directory
- Removed various -Period parameters
- Added test-sim-multi-seed-short target
- Commented out some test commands
simapp/v2/sim_runner.go - Exported DefaultSeeds variable
- Added InitGenesis method to SimulationApp interface
- Updated TestInstance and ChainState structures
- Refactored simulation test setup functions
simapp/v2/sim_test.go - Added build constraint
- Expanded import section
- Introduced new test functions
- Added ExportableApp interface
simsx/environment.go - Added panic condition for empty accounts in NewChainDataSource
simsx/runner.go - Removed FlagInvCheckPeriod option from app initialization
simsx/v2/valset_history.go - Changed blockOffset to uint64
- Updated constructor and methods
types/simulation/config.go - Converted InitialBlockHeight and NumBlocks to uint64
x/simulation/client/cli/flags.go - Changed flag variables to uint64
- Removed FlagPeriodValue

Suggested labels

C:Store, C:x/auth, C:x/accounts, C:x/accounts/multisig, C:server/v2, backport/v0.52.x

Suggested reviewers

  • kocubinski
  • facundomedica
  • julienrbrt
  • tac0turtle
  • testinginprod

Possibly related PRs

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@alpe alpe force-pushed the alex/sims2_v2_dev_tests branch 5 times, most recently from b55ebe5 to 1821bd2 Compare January 10, 2025 12:25
@alpe alpe force-pushed the alex/sims2_v2_dev_tests branch from 1821bd2 to 71c45a7 Compare January 10, 2025 13:12
@alpe alpe changed the title [WIP] test: Add other sims test scenarios test: Add more sims test scenarios Jan 10, 2025
@alpe alpe marked this pull request as ready for review January 10, 2025 13:18
@alpe alpe requested review from julienrbrt, tac0turtle and a team as code owners January 10, 2025 13:18
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

🧹 Nitpick comments (3)
simsx/environment.go (1)

266-268: LGTM! Consider adding error documentation.

The validation check for empty accounts is a good defensive programming practice. However, consider documenting this panic condition in the function's documentation comment to make it explicit for users of this constructor.

Add this documentation above the function:

// NewChainDataSource constructor
+// Panics if oldSimAcc is empty or if any account has an empty bech32 address.
func NewChainDataSource(
simapp/v2/sim_test.go (2)

28-39: Externalize default seeds for better configurability

The default seeds in TestAppStateDeterminism are hardcoded within the function. Consider externalizing these seeds to a configuration file or constants to enhance maintainability and allow for easier updates without modifying the code.


52-54: Enhance error message for non-determinism detection

In the non-determinism check, consider including the expected and actual app hashes in the error message. This additional information can aid in debugging by showing the exact differences when non-determinism occurs.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a6b0924 and 71c45a7.

📒 Files selected for processing (8)
  • scripts/build/simulations.mk (3 hunks)
  • simapp/v2/sim_runner.go (10 hunks)
  • simapp/v2/sim_test.go (1 hunks)
  • simsx/environment.go (1 hunks)
  • simsx/runner.go (0 hunks)
  • simsx/v2/valset_history.go (3 hunks)
  • types/simulation/config.go (1 hunks)
  • x/simulation/client/cli/flags.go (2 hunks)
💤 Files with no reviewable changes (1)
  • simsx/runner.go
🧰 Additional context used
📓 Path-based instructions (6)
types/simulation/config.go (1)

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

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

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

simapp/v2/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"

simsx/environment.go (1)

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

simsx/v2/valset_history.go (1)

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

simapp/v2/sim_runner.go (1)

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

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Analyze
  • GitHub Check: Summary
🔇 Additional comments (24)
simapp/v2/sim_test.go (3)

20-22: Initialization of simulator flags is appropriate

The init function correctly initializes the simulator flags using simcli.GetSimulatorFlags(). This ensures that the simulator configuration is set before the tests run.


24-26: Test function TestFullAppSimulation is properly defined

The TestFullAppSimulation function uses RunWithSeeds to execute the full application simulation with the default seeds. This is appropriate for ensuring comprehensive simulation coverage.


70-102: TestAppSimulationAfterImport function is well-implemented

The TestAppSimulationAfterImport function properly tests the simulation after importing the genesis state. It ensures that the application can export its state and re-import it correctly, which is crucial for state consistency.

simapp/v2/sim_runner.go (7)

53-54: Exported DefaultSeeds for external use

The variable DefaultSeeds is now exported, allowing it to be used in other packages. This change promotes reusability and consistency across simulations that rely on default seed values.


91-98: Added InitGenesis method to SimulationApp interface

The addition of the InitGenesis method to the SimulationApp interface enhances initialization capabilities. It allows for setting up the application state from a genesis file, which is essential for simulations that require a predefined state.


109-109: Included Seed field in TestInstance struct

Adding the Seed field to TestInstance improves traceability and reproducibility of simulation tests. It enables the seed value to be easily accessed throughout the test instance, facilitating debugging and analysis.


125-130: Updated SetupTestInstance function to accept seed parameter

The SetupTestInstance function now accepts a seed parameter, which aids in creating deterministic test instances. This change enhances the ability to reproduce simulation scenarios using specific seeds.


200-205: Refactored RunWithSeeds for greater flexibility

By modifying RunWithSeeds to accept appFactory and appConfigFactory, the function now allows for customizable application setups. This refactoring increases flexibility and supports diverse simulation configurations.


254-262: Introduced RunWithSeedX for custom chain setups

The new RunWithSeedX function enables running simulations with custom chain state setups. This addition provides more control over simulation parameters and can accommodate specialized testing scenarios.


369-376: Exported ChainState type for broader accessibility

Changing chainState to ChainState by exporting the type allows it to be used outside the package. This enhances the usability of the chain state structure in other parts of the application or in external packages.

scripts/build/simulations.mk (8)

5-7: Updated simulation test path to simapp/v2

The test-sim-nondeterminism target now correctly points to the simapp/v2 directory, reflecting the updated location of simulation tests. This ensures that the makefile executes the tests in the intended directory.


32-33: Removed outdated test target and parameters

The test-sim-import-export target has been commented out, indicating that it may no longer be applicable or that it requires updates to work with simapp/v2. The removal of the -Period parameter simplifies the command and aligns it with current test configurations.


36-38: Adjusted test-sim-after-import to use simapp/v2

The test-sim-after-import target now uses the simapp/v2 directory and removes the -Period parameter. This update ensures that the simulation after import test runs with the latest simulation codebase.


47-49: Modified test-sim-multi-seed-long for updated simulations

The target is updated to point to simapp/v2 and removes the -Period parameter. This ensures that long multi-seed simulations are executed using the current simulation setup.


51-54: Added test-sim-multi-seed-short target

The new test-sim-multi-seed-short target provides a shorter multi-seed simulation test. It is beneficial for quicker feedback during development and testing.


79-81: Commented out deprecated test-sim-fuzz target

The test-sim-fuzz target and its associated commands have been commented out, possibly due to deprecation or pending migration to simapp/v2. This prevents the execution of outdated or non-functional test scripts.


85-86: Commented out test-sim-benchmark target

The test-sim-benchmark target has been commented out, indicating that benchmark tests may need to be updated to work with the new simulation setup or are temporarily disabled.


91-92: Commented out test-sim-profile target

Similar to other targets, the test-sim-profile target is commented out, suggesting that profiling is either not currently supported or requires updates to function with simapp/v2.

types/simulation/config.go (1)

16-16: LGTM! Type changes improve consistency and correctness

The change from int to uint64 for InitialBlockHeight and NumBlocks is appropriate as these values are inherently non-negative. This also maintains consistency with the CLI flag types.

Also applies to: 18-18

simsx/v2/valset_history.go (4)

19-19: LGTM! Type change improves correctness

The change from int to uint64 for blockOffset is appropriate as block heights are non-negative values.

Also applies to: 19-19


63-63: LGTM! Improved type safety

The explicit casting to int64 improves type safety in the height calculation.

Also applies to: 63-63


73-81: LGTM! Proper type handling in SetMaxHistory

The method correctly handles the type conversion when updating blockOffset.


23-29: Verify the impact of hardcoding maxElements

The constructor now hardcodes maxElements to 1, which might limit the validator set history functionality. Please confirm if this is intentional and doesn't impact the evidence handling capabilities.

✅ Verification successful

Hardcoding maxElements to 1 is intentional and safe

The constructor's maxElements=1 is intentional as evidence generation and validation only require the current validator set. The evidence module handles validation using time/block windows rather than historical validator sets. If needed, the history size can be adjusted using SetMaxHistory.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for references to maxElements to understand its usage
rg -A 5 "maxElements" .

Length of output: 1328


Script:

#!/bin/bash
# Search for tests and documentation
rg -l "TestValSetHistory" .
rg -A 10 "SetMaxElements" . | grep -v "^--$" | grep -v "^$"

# Check documentation mentioning validator set history
fd README.md | xargs rg -i "validator.*history|valset.*history"

# Look for evidence-related documentation since the comment mentions it
rg -i "evidence.*valid" .

Length of output: 109604


Script:

#!/bin/bash
# Search for tests of ValSetHistory
rg -A 10 "TestValSetHistory|func.*ValSetHistory" .

# Look for evidence time window configuration
rg -i "evidence.*window|evidence.*duration" .

Length of output: 108711


Script:

#!/bin/bash
# Search for tests and evidence validation code
rg -A 5 "TestValSetHistory|func.*MissBehaviour|func.*Evidence" .

# Look for evidence validation implementation
ast-grep --pattern 'func $_ ($_, $_, $_) {
  $$$
  evidence $$$
  $$$
}'

Length of output: 67462

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

21-22: LGTM! Consistent type changes

The changes to uint64 for block-related flags and their parsing functions maintain consistency with the Config struct while ensuring non-negative values. The default values remain appropriate.

Also applies to: 44-45

Comment on lines +169 to +171
func (ti TestInstance[T]) InitializeChain(tb testing.TB,
ctx context.Context,
chainID string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (ti TestInstance[T]) InitializeChain(tb testing.TB,
ctx context.Context,
chainID string,
func (ti TestInstance[T]) InitializeChain(
tb testing.TB,
ctx context.Context,
chainID string,

@@ -18,51 +19,45 @@ test-sim-nondeterminism:
test-sim-nondeterminism-streaming:
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we planning to bring these tests back as well?

test-sim-multi-seed-short:
@echo "Running short multi-seed application simulation. This may take awhile!"
@cd ${CURRENT_DIR}/simapp/v2 && go test -failfast -mod=readonly -timeout 30m -tags='sims' -run TestFullAppSimulation \
-NumBlocks=50

test-sim-benchmark-invariants:
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this as invariants have been removed

"github.com/stretchr/testify/require"

simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
v2 "github.com/cosmos/cosmos-sdk/x/genutil/v2"
Copy link
Contributor

Choose a reason for hiding this comment

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

probably better to name genutil as the import

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.

4 participants