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(sims): Add sims2 framework and factory methods (backport #21613) #21752

Merged
merged 6 commits into from
Sep 17, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Sep 16, 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.
This PR also included the refactoring for all x</module>/simulation/operations into the new factory structures.

(Reviewer note at the end)

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

Note to the reviewers

Apologies for this big PR. 🙈 It created too much extra work to keep the smaller PRs updated with changes in main as they pile up.

  • A good start for the review are the message factories in the modules. They can be compared against the old operations to confirm the behaviour is still the same (more or less).
    Please note. the gov module is the most complex one compared to the others due to the proposal and legacy support.
  • The package name simsx is not necessarily the best name. If somebody would come up with a better name, I would refactor this in a new PR. Naming is hard

Summary by CodeRabbit

  • New Features

    • Introduced a new SelectBy method in the API for enhanced thread-safe operations.
    • Added the sims2 framework with factory methods for simplified message creation.
    • Enhanced simulation capabilities with a new delivery mechanism for blockchain transactions.
  • Bug Fixes

    • Improved functionality by ensuring blocks are committed only up to the specified halt height.
  • Refactor

    • Streamlined module initialization by reducing dependencies for several modules.
    • Refactored simulation-related functionalities for better management.
  • Documentation

    • Updated CHANGELOG.md to reflect significant updates and enhancements in the API.
  • Tests

    • Adjusted test configurations to improve context management and streamline testing processes.

This is an automatic backport of pull request #21613 done by [Mergify](https://mergify.com).

(cherry picked from commit bf77680)

# Conflicts:
#	CHANGELOG.md
#	go.mod
#	simapp/sim_bench_test.go
#	testutil/sims/state_helpers.go
#	x/params/go.mod
#	x/staking/go.mod
@mergify mergify bot requested a review from a team as a code owner September 16, 2024 13:50
Copy link
Contributor Author

mergify bot commented Sep 16, 2024

Cherry-pick of bf77680 has failed:

On branch mergify/bp/release/v0.52.x/pr-21613
Your branch is up to date with 'origin/release/v0.52.x'.

You are currently cherry-picking commit bf7768006.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   docs/build/building-modules/14-simulator.md
	modified:   scripts/build/simulations.mk
	modified:   simapp/app.go
	modified:   simapp/sim_test.go
	new file:   simsx/README.md
	new file:   simsx/common_test.go
	new file:   simsx/context.go
	new file:   simsx/delivery.go
	new file:   simsx/delivery_test.go
	new file:   simsx/environment.go
	new file:   simsx/environment_test.go
	new file:   simsx/msg_factory.go
	new file:   simsx/msg_factory_test.go
	new file:   simsx/params.go
	new file:   simsx/registry.go
	new file:   simsx/registry_test.go
	new file:   simsx/reporter.go
	new file:   simsx/reporter_test.go
	new file:   simsx/runner.go
	new file:   simsx/slices.go
	new file:   simsx/slices_test.go
	modified:   tests/go.mod
	modified:   tests/integration/bank/keeper/deterministic_test.go
	modified:   tests/integration/distribution/keeper/msg_server_test.go
	modified:   tests/integration/evidence/keeper/infraction_test.go
	modified:   tests/integration/genutil/init_test.go
	modified:   tests/integration/gov/keeper/keeper_test.go
	modified:   tests/integration/slashing/keeper/keeper_test.go
	modified:   tests/integration/staking/keeper/common_test.go
	modified:   tests/integration/staking/keeper/deterministic_test.go
	deleted:    tests/integration/staking/simulation/operations_test.go
	deleted:    tests/sims/authz/operations_test.go
	deleted:    tests/sims/bank/operations_test.go
	deleted:    tests/sims/distribution/app_config.go
	deleted:    tests/sims/distribution/operations_test.go
	deleted:    tests/sims/feegrant/operations_test.go
	deleted:    tests/sims/gov/operations_test.go
	deleted:    tests/sims/nft/app_config.go
	deleted:    tests/sims/nft/operations_test.go
	deleted:    tests/sims/protocolpool/app_config.go
	deleted:    tests/sims/protocolpool/operations_test.go
	deleted:    tests/sims/slashing/app_config.go
	deleted:    tests/sims/slashing/operations_test.go
	modified:   testutil/sims/simulation_helpers.go
	deleted:    testutils/sims/runner.go
	modified:   types/module/simulation.go
	modified:   types/simulation/account.go
	modified:   types/simulation/config.go
	modified:   types/simulation/types.go
	modified:   x/auth/module.go
	modified:   x/auth/simulation/genesis.go
	new file:   x/auth/simulation/msg_factory.go
	deleted:    x/auth/simulation/proposals.go
	deleted:    x/auth/simulation/proposals_test.go
	modified:   x/authz/module/depinject.go
	modified:   x/authz/module/module.go
	modified:   x/authz/simulation/genesis.go
	new file:   x/authz/simulation/msg_factory.go
	deleted:    x/authz/simulation/operations.go
	modified:   x/bank/module.go
	modified:   x/bank/simulation/genesis.go
	new file:   x/bank/simulation/msg_factory.go
	deleted:    x/bank/simulation/operations.go
	modified:   x/distribution/depinject.go
	modified:   x/distribution/go.mod
	modified:   x/distribution/module.go
	modified:   x/distribution/simulation/genesis.go
	new file:   x/distribution/simulation/msg_factory.go
	deleted:    x/distribution/simulation/operations.go
	deleted:    x/distribution/simulation/proposals.go
	deleted:    x/distribution/simulation/proposals_test.go
	modified:   x/epochs/module.go
	modified:   x/epochs/simulation/genesis.go
	modified:   x/evidence/module.go
	modified:   x/evidence/simulation/genesis.go
	modified:   x/feegrant/module/depinject.go
	modified:   x/feegrant/module/module.go
	new file:   x/feegrant/simulation/msg_factory.go
	deleted:    x/feegrant/simulation/operations.go
	modified:   x/genutil/client/cli/validate_genesis_test.go
	modified:   x/gov/go.mod
	modified:   x/gov/module.go
	modified:   x/gov/simulation/genesis.go
	new file:   x/gov/simulation/msg_factory.go
	deleted:    x/gov/simulation/operations.go
	modified:   x/gov/simulation/proposals.go
	deleted:    x/gov/simulation/proposals_test.go
	modified:   x/group/keeper/keeper.go
	modified:   x/group/keeper/keeper_test.go
	modified:   x/group/keeper/msg_server.go
	modified:   x/group/keeper/msg_server_test.go
	modified:   x/group/module/module.go
	modified:   x/group/simulation/genesis.go
	new file:   x/group/simulation/msg_factory.go
	deleted:    x/group/simulation/operations.go
	deleted:    x/group/simulation/operations_test.go
	modified:   x/mint/go.mod
	modified:   x/mint/module.go
	modified:   x/mint/simulation/genesis.go
	new file:   x/mint/simulation/msg_factory.go
	deleted:    x/mint/simulation/proposals_test.go
	modified:   x/nft/module/module.go
	new file:   x/nft/simulation/msg_factory.go
	deleted:    x/nft/simulation/operations.go
	modified:   x/params/go.sum
	modified:   x/params/module.go
	deleted:    x/params/simulation/operations.go
	deleted:    x/params/simulation/operations_test.go
	deleted:    x/params/simulation/proposals.go
	deleted:    x/params/simulation/proposals_test.go
	modified:   x/protocolpool/depinject.go
	modified:   x/protocolpool/go.mod
	new file:   x/protocolpool/simulation/msg_factory.go
	deleted:    x/protocolpool/simulation/operations.go
	deleted:    x/protocolpool/simulation/proposals.go
	deleted:    x/protocolpool/simulation/proposals_test.go
	modified:   x/simulation/client/cli/flags.go
	modified:   x/simulation/log.go
	modified:   x/simulation/operation.go
	modified:   x/simulation/params.go
	modified:   x/simulation/params_test.go
	modified:   x/simulation/simulate.go
	new file:   x/simulation/simulate_test.go
	modified:   x/slashing/module.go
	modified:   x/slashing/simulation/genesis.go
	new file:   x/slashing/simulation/msg_factory.go
	deleted:    x/slashing/simulation/operations.go
	modified:   x/staking/depinject.go
	modified:   x/staking/module.go
	modified:   x/staking/simulation/genesis.go
	new file:   x/staking/simulation/msg_factory.go
	deleted:    x/staking/simulation/operations.go
	deleted:    x/staking/simulation/proposals.go
	deleted:    x/staking/simulation/proposals_test.go

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   CHANGELOG.md
	both modified:   go.mod
	both modified:   simapp/sim_bench_test.go
	both modified:   testutil/sims/state_helpers.go
	both modified:   x/params/go.mod
	both modified:   x/staking/go.mod

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Copy link
Contributor

coderabbitai bot commented Sep 16, 2024

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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 generate interesting stats about this repository and render them as a table.
    -- @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.

@julienrbrt julienrbrt marked this pull request as draft September 16, 2024 15:08
@julienrbrt
Copy link
Member

Please cherry-pick this PR in here: #21763
Or we'll backport it afterwards.

 Please enter the commit message for your changes. Lines starting
simsx/msg_factory.go Dismissed Show dismissed Hide dismissed
@alpe alpe marked this pull request as ready for review September 17, 2024 08:35
(cherry picked from commit 3314cfb)
simsx/reporter.go Dismissed Show dismissed Hide dismissed
Copy link
Contributor

@alpe alpe left a comment

Choose a reason for hiding this comment

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

Looks good now

@julienrbrt julienrbrt enabled auto-merge (squash) September 17, 2024 09:59
@julienrbrt julienrbrt merged commit 8823508 into release/v0.52.x Sep 17, 2024
82 of 85 checks passed
@julienrbrt julienrbrt deleted the mergify/bp/release/v0.52.x/pr-21613 branch September 17, 2024 10:04
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.

2 participants