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: fix sim test state persistence issue #19771

Closed

Conversation

hacheigriega
Copy link

@hacheigriega hacheigriega commented Mar 18, 2024

Description

Related: #16796

Fix the issue where the simulation tests fail to commit and update the states at every height.
Also fix x/gov simulation operations so that its future operations are actually run.

Summary by CodeRabbit

  • Refactor
    • Enhanced method naming consistency in the core application structure.
  • Chores
    • Adjusted simulation operations for governance proposals, including vote timing and deposit calculations.
    • Integrated a consistency check in simulation execution to enhance reliability.

@hacheigriega hacheigriega requested a review from a team as a code owner March 18, 2024 05:01
Copy link
Contributor

coderabbitai bot commented Mar 18, 2024

Walkthrough

Walkthrough

The changes encompass method visibility adjustments, simulation enhancements, and integration of state hash calculations in simulations. These updates aim to ensure consistency in method naming, improve simulation accuracy by adjusting parameters and calculations, and verify state integrity within the blockchain framework.

Changes

File Path Change Summary
baseapp/abci.go Modified workingHash() to WorkingHash() for consistent naming conventions, impacting FinalizeBlock.
x/gov/simulation/... Commented out SimulateMsgVote and adjusted simulation parameters and calculations in related functions.
x/simulation/simulate.go Introduced app.WorkingHash() call in SimulateFromSeed for state hash verification.

Related issues

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-tests 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 tests 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 tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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: 1

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 5a733e8 and 01a90ef.
Files selected for processing (3)
  • baseapp/abci.go (3 hunks)
  • x/gov/simulation/operations.go (8 hunks)
  • x/simulation/simulate.go (1 hunks)
Additional comments: 7
x/simulation/simulate.go (1)
  • 235-236: The addition of app.WorkingHash() before the commit check is a crucial step towards ensuring state hash consistency. It's important to verify the implementation of WorkingHash() to ensure it doesn't introduce any unintended side effects or significant performance overhead.
Verification successful

The implementation of WorkingHash() in baseapp/abci.go aligns with its intended purpose of ensuring state hash consistency before commits. The method finalizes block state, writes all state transitions to the root multi-store, and computes the hash of all writes, which is crucial for the integrity and consistency of the application state. There's no evidence of unintended side effects or significant performance issues based on the provided context. The operation seems necessary and well-implemented for the application's functionality.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the implementation details of WorkingHash to ensure no side effects or performance issues.
grep -A 10 -B 10 "func (app \*BaseApp) WorkingHash()" $(fd --type f "abci.go")

Length of output: 1787

x/gov/simulation/operations.go (3)
  • 232-232: Adjustments to the randomDeposit function usage with new parameters enhance the realism of simulation. Consider adding comments explaining the rationale behind these adjustments to improve code maintainability.

Also applies to: 341-341

  • 281-293: Modifications in simulateMsgSubmitProposal to adjust voteTime and deposit amounts aim to improve simulation accuracy. It's important to verify these changes don't negatively impact simulation performance or deviate from expected behavior.

Also applies to: 310-310

  • 594-619: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [564-616]

The adjustments in handling minInitAmt and minAmt in randomDeposit are crucial for accurate simulation of deposits. Consider adding unit tests to ensure the correctness and expected behavior of these adjustments.

Would you like assistance in creating unit tests for these adjustments?

baseapp/abci.go (3)
  • 993-993: The method WorkingHash has been made public by changing its name from workingHash to WorkingHash. This change aligns with the PR's objective to ensure consistency in method naming conventions and to allow external calls to this method for state hash consistency checks.
  • 907-907: The call to app.WorkingHash() within the FinalizeBlock method ensures that the application hash (AppHash) is correctly updated before committing changes. This addition addresses the core issue of state persistence in simulation tests by ensuring that the state hash is consistent at every block height.
  • 893-893: Similarly, the call to app.WorkingHash() within the FinalizeBlock method, but in the optimistic execution flow, ensures the application hash is correctly updated. This is crucial for maintaining state hash consistency across different execution paths.

x/gov/simulation/operations.go Outdated Show resolved Hide resolved
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

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 01a90ef and 0a9fecf.
Files selected for processing (1)
  • x/gov/simulation/operations.go (6 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/gov/simulation/operations.go

@@ -232,6 +232,8 @@ func SimulateFromSeed(

logWriter.AddEntry(EndBlockEntry(blockHeight))

app.WorkingHash()
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 we should not make this function public but see if all operations are run in the context of a FinalizeBlock call. Given that this function is called in there

minAmount := sdkmath.ZeroInt()
if useMinAmount {
minInitAmt := sdkmath.ZeroInt()
if useMinInitAmt {
Copy link
Member

Choose a reason for hiding this comment

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

can you explain this?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I was trying to keep the original code as much as possible. I made these changes to get the proposal to immediately enter the voting period. So the deposit amount has to satisfy both the "minimum initial deposit requirement" and the "minimum deposit requirement".

@@ -990,7 +990,7 @@ func (app *BaseApp) Commit() (*abci.ResponseCommit, error) {
// disk in the Commit phase. This means when the ABCI client requests Commit(), the application
// state transitions will be flushed to disk and as a result, but we already have
// an application Merkle root.
func (app *BaseApp) workingHash() []byte {
func (app *BaseApp) WorkingHash() []byte {
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't make this one public, if calling this solves the issue then the problem is that there are operations running outside of a FinalizeBlock call context. Because this function is called at the end of FinalizeBlock.

@facundomedica facundomedica self-assigned this Mar 18, 2024
@facundomedica
Copy link
Member

Thank you so much for opening this PR! Left some comments but I'm pretty sure you found the issue

@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Mar 18, 2024
@hacheigriega
Copy link
Author

@facundomedica Thank you for the prompt review! So FinalizeBlock() is run prior to the simulation operations in order to get the app ready for the FinalizeBlock stage. The only problem with this approach is that the method workingHash() has to run after transaction execution for the state to persist. I'm not sure how we can fix this without making workingHash() public.

@julienrbrt
Copy link
Member

julienrbrt commented Apr 15, 2024

Gentle bump @hacheigriega, do you have time to implement Facu's suggestion, or one of us should take this over?
We'll gladly get this in our upcoming patch release.

@hacheigriega
Copy link
Author

Hey @julienrbrt, it seems to me that a complete fix would require a much more substantial refactor of the simulation testing mechanism. The current setup runs FinalizeBlock first and then separately runs the simulation operations afterwards. This PR simply makes sure that these simulation operations are committed even though they run outside FinalizeBlock.

In short, I'm not sure how to improve this PR at the moment. Please let me know if you have a little more specific suggestions
or feel free to close the PR if you don't find this to be a sufficient solution.

@tac0turtle
Copy link
Member

hey we are doing a refactor of simulations now. @hacheigriega would you be fine with closing this for now once that works completes ?

@tac0turtle tac0turtle closed this May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release C:Simulations C:x/gov
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants