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

fix(simulation): fix the problem of validator set is empty after InitGenesis in simulation test #18196

Merged
merged 1 commit into from
Oct 23, 2023

Conversation

luchenqun
Copy link
Contributor

@luchenqun luchenqun commented Oct 21, 2023

Description

When I execute make test-sim-nondeterminism for simulation testing, there is a certain probability that validator set is empty after InitGenesis, please ensure at least one validator is initialized with a delegation greater than or equal to the Problem with DefaultPowerReduction ({1374391948096}). The place for panic is:

https://github.com/cosmos/cosmos-sdk/blob/2fbc547ce9/x/simulation/simulate.go#L50

The actual error occurs at:

https://github.com/cosmos/cosmos-sdk/blob/2fbc547/types/module/module.go#L517

After analysis, there are two reasons that trigger this error.

Reason 1: When AppStateRandomizedFn is a random parameter, when initialStake < DefaultPowerReduction, the ConsensusPower of each validator is equal to 0, resulting in no validator.

Reason 2: When AppStateRandomizedFn is a random parameter, when numInitiallyBonded is 0, there is no validator.

So we must ensure that initialStake >= DefaultPowerReduction and numInitiallyBonded >= 1 when using AppStateRandomizedFn random parameters

Closes: #XXXX


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
  • added ! to 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
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • run make lint and make test
  • 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.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Summary by CodeRabbit

  • Bug Fix: Addressed an issue in the simulation test where the validator set was empty after InitGenesis. This fix enhances the reliability of our testing environment, ensuring more accurate results.
  • New Feature: Updated the AppStateRandomizedFn function to generate random values for initialStake and numInitiallyBonded parameters. This change introduces an element of randomness to our simulations, making them more robust and representative of real-world scenarios.

@luchenqun luchenqun requested a review from a team as a code owner October 21, 2023 05:28
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2023

Walkthrough

This pull request (#18196) introduces a fix for the simulation test issue where the validator set is empty after InitGenesis. It includes changes to the AppStateRandomizedFn function, generating random values for initialStake and numInitiallyBonded parameters.

Changes

File Summary
CHANGELOG.md Documents a new pull request (#18196) that addresses an issue in the simulation test where the validator set is empty after InitGenesis.
testutil/sims/state_helpers.go Modifies the AppStateRandomizedFn function to generate random values for initialStake and numInitiallyBonded parameters. If numInitiallyBonded exceeds the total number of accounts, it is set to the total number of accounts.

"In the land of code, where logic is king, 🤴
A rabbit hopped in, a fix to bring. 🐇
With randomness tamed, and tests now stable, 🎲
We celebrate this change, as much as we're able! 🎉"


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.

CodeRabbit Commands

  • Enter @coderabbitai pause as a PR comment to pause the reviews on a PR.
  • Enter @coderabbitai resume as a PR comment to resume the paused reviews.
  • For on-demand reviews, enter @coderabbitai review as a PR comment to trigger a review. This is useful when reviews are automatic disabled at the repository level.
  • Enter @coderabbitai: ignore in the PR description to stop a specific PR from being reviewed.

Note these commands work only in the PR comment. For conversations with the bot, please use the review comments on code diffs or files.

@github-prbot github-prbot requested review from a team, kocubinski and alexanderbez and removed request for a team October 21, 2023 05:28
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.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 2fbc547 and a06c21b.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • testutil/sims/state_helpers.go (1 hunks)
Additional comments (Suppressed): 3
CHANGELOG.md (1)
  • 73-76: The changes in the changelog accurately reflect the fixes made in the pull request. The links to the pull requests provide a good reference for further details.
testutil/sims/state_helpers.go (2)
  • 208-216: The changes in these lines ensure that initialStake is always greater than or equal to sdk.DefaultPowerReduction and numInitiallyBonded is always greater than or equal to 1. This is a good approach to prevent the simulation test from failing due to an empty validator set. The use of sdk.DefaultPowerReduction.AddRaw(r.Int63n(1e12)) and int64(r.Intn(299) + 1) ensures that the values are randomized while still meeting the minimum requirements.

  • 218-218: The check if numInitiallyBonded > numAccs is still necessary to ensure that the number of initially bonded validators does not exceed the total number of accounts. However, the change in the random number generation for numInitiallyBonded (from r.Intn(300) to r.Intn(299) + 1) means that the maximum possible value for numInitiallyBonded is now 299 instead of 300. This change is likely to have a negligible impact on the overall behavior of the simulation, but it's worth noting.

@julienrbrt julienrbrt added this pull request to the merge queue Oct 23, 2023
Merged via the queue into cosmos:main with commit f2f47ff Oct 23, 2023
51 of 52 checks passed
@fmorency
Copy link
Contributor

fmorency commented Jul 5, 2024

@julienrbrt Could we get this fix in the next CosmosSDK 0.50.x version?

@julienrbrt
Copy link
Member

@Mergifyio backport release/v0.50.x

Copy link
Contributor

mergify bot commented Jul 5, 2024

backport release/v0.50.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jul 5, 2024
…tGenesis` in simulation test (#18196)

(cherry picked from commit f2f47ff)

# Conflicts:
#	CHANGELOG.md
julienrbrt added a commit that referenced this pull request Jul 5, 2024
…tGenesis` in simulation test (backport #18196) (#20897)

Co-authored-by: Chenqun Lu <luchenqun@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants