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

feat(server/v2): add SimulateWithState to AppManager #22335

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

mark-rushakoff
Copy link
Member

@mark-rushakoff mark-rushakoff commented Oct 22, 2024

There is already the QueryWithState method to query against a modified state, but the Simulate analog was missing.

This is useful in particular when a server component receives different transactions at different times, and wants to ensure that subsequent transactions would be valid to apply after the prior transactions.

In other words, the first received transaction can be validated with Simulate; then the returned corestore.WriterMap is used to seed the input to the following call to SimulateWithState; and then the returned corestore.WriterMap is used for all following SimulateWithState calls.

While it is currently impossible for SimulateWithState to return an error, an error value is in the return signature for symmetry with other AppManager methods and to allow the possibility of returned errors in the future.

There were no existing tests in the server/v2/appmanager package, to be expanded to include test coverage for the new method.

Summary by CodeRabbit

  • New Features
    • Introduced a new method for simulating transactions using a specified state, enhancing flexibility in transaction validation.
  • Bug Fixes
    • No bug fixes were included in this update.

There is already the QueryWithState method to query against a modified
state, but the Simulate analog was missing.

This is useful in particular when a server component receives different
transactions at different times, and wants to ensure that subsequent
transactions would be valid to apply after the prior transactions.

In other words, the first received transaction can be validated with
Simulate; then the returned corestore.WriterMap is used to seed the
input to the following call to SimulateWithState; and then the returned
corestore.WriterMap is used for all following SimulateWithState calls.

While it is currently impossible for SimulateWithState to return an
error, an error value is  in the return signature for symmetry with
other AppManager methods and to allow the possibility of returned errors
in the future.

There were no existing tests in the server/v2/appmanager package, to be
expanded to include test coverage for the new method.
Copy link
Contributor

coderabbitai bot commented Oct 22, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new method, SimulateWithState, to the AppManager interface and its implementation in the appManager struct. This method enables transaction simulation using a specified state, allowing for validation and execution flow without fetching the latest state from the database. The method's signature is defined, and its implementation closely resembles the existing Simulate method, focusing on processing with the provided state.

Changes

File Path Change Summary
server/v2/appmanager/appmanager.go Added method SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error) to AppManager interface and appManager struct.

Possibly related PRs

  • feat(runtime/v2): store loader on simappv2 #21704: This PR introduces a StoreLoader functionality that enhances state management during upgrades, which is conceptually related to the SimulateWithState method added in the main PR, as both involve managing application state in different contexts.

Suggested labels

C:Simulations, backport/v0.52.x

Suggested reviewers

  • akhilkumarpilli
  • lucaslopezf
  • kocubinski

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 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.

@github-actions github-actions bot added C:server/v2 Issues related to server/v2 C:server/v2 appmanager labels Oct 22, 2024
Copy link
Contributor

@mark-rushakoff your pull request is missing a changelog!

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

🧹 Outside diff range and nitpick comments (1)
server/v2/appmanager/appmanager.go (1)

44-46: LGTM with a minor documentation enhancement suggestion.

The method signature and placement are well-designed. Consider enhancing the documentation to include a usage example or common use case, similar to how QueryWithState's documentation explains its practical application.

 // SimulateWithState runs validation and execution flow of a Tx,
 // using the provided state instead of loading the latest state from the underlying database.
+// For example, it can be used to validate subsequent transactions by passing the WriterMap
+// from a previous simulation as the state parameter.
 SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error)
📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cd3ce0c and bbcbc07.

📒 Files selected for processing (1)
  • server/v2/appmanager/appmanager.go (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
server/v2/appmanager/appmanager.go (1)

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

🔇 Additional comments (1)
server/v2/appmanager/appmanager.go (1)

203-203: Address or remove the TODO comment.

The TODO comment about checking if gas limit is handled in the antehandler appears in both Simulate and SimulateWithState. This should be investigated and either addressed or removed to maintain code clarity.

Let's check if the antehandler handles the gas limit:

Comment on lines +200 to +205
// SimulateWithState runs validation and execution flow of a Tx,
// using the provided state instead of loading the latest state from the underlying database.
func (a appManager[T]) SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error) {
result, cs := a.stf.Simulate(ctx, state, a.config.SimulationGasLimit, tx) // TODO: check if this is done in the antehandler
return result, cs, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider explicit error handling for invalid state.

While the PR objectives mention that errors aren't currently returned, consider adding validation for the provided state parameter to prevent potential panics or undefined behavior.

 func (a appManager[T]) SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error) {
+	if state == nil {
+		return server.TxResult{}, nil, errors.New("provided state cannot be nil")
+	}
 	result, cs := a.stf.Simulate(ctx, state, a.config.SimulationGasLimit, tx)
 	return result, cs, nil
 }
📝 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
// SimulateWithState runs validation and execution flow of a Tx,
// using the provided state instead of loading the latest state from the underlying database.
func (a appManager[T]) SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error) {
result, cs := a.stf.Simulate(ctx, state, a.config.SimulationGasLimit, tx) // TODO: check if this is done in the antehandler
return result, cs, nil
}
// SimulateWithState runs validation and execution flow of a Tx,
// using the provided state instead of loading the latest state from the underlying database.
func (a appManager[T]) SimulateWithState(ctx context.Context, state corestore.ReaderMap, tx T) (server.TxResult, corestore.WriterMap, error) {
if state == nil {
return server.TxResult{}, nil, errors.New("provided state cannot be nil")
}
result, cs := a.stf.Simulate(ctx, state, a.config.SimulationGasLimit, tx) // TODO: check if this is done in the antehandler
return result, cs, nil
}

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.

ACK

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

lgtm

@julienrbrt julienrbrt added this pull request to the merge queue Oct 23, 2024
Merged via the queue into main with commit 762fad2 Oct 23, 2024
77 of 80 checks passed
@julienrbrt julienrbrt deleted the mr/serverv2-appmanager-simulate-with-state branch October 23, 2024 12:40
alpe added a commit that referenced this pull request Oct 25, 2024
* main: (157 commits)
  feat(core): add ConfigMap type (#22361)
  test: migrate e2e/genutil to systemtest (#22325)
  feat(accounts): re-introduce bundler (#21562)
  feat(log): add slog-backed Logger type (#22347)
  fix(x/tx): add feePayer as signer (#22311)
  test: migrate e2e/baseapp to integration tests (#22357)
  test: add x/circuit system tests (#22331)
  test: migrate e2e/tx tests to systemtest (#22152)
  refactor(simdv2): allow non-comet server components (#22351)
  build(deps): Bump rtCamp/action-slack-notify from 2.3.1 to 2.3.2 (#22352)
  fix(server/v2): respect context cancellation in start command (#22346)
  chore(simdv2): allow overriding the --home flag (#22348)
  feat(server/v2): add SimulateWithState to AppManager (#22335)
  docs(x/accounts): improve comments (#22339)
  chore: remove unused local variables (#22340)
  test: x/accounts systemtests (#22320)
  chore(client): use command's configured output (#22334)
  perf(log): use sonic json lib  (#22233)
  build(deps): bump x/tx (#22327)
  fix(x/accounts/lockup): fix proto path (#22319)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 appmanager C:server/v2 Issues related to server/v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants