-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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(sim): port simulator to run on top of testutil/network #17938
Conversation
55437d9
to
42e2d96
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work and thank you @elias-orijtech! Just an initial round of review.
network *network.Network | ||
} | ||
|
||
func (s *IntegrationTestSuite) SetupSuite() { | ||
s.T().Log("setting up integration test suite") | ||
|
||
s.rand = rand.New(rand.NewSource(42)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment here
// Using a hardcoded seed for predictability.
// TODO(@elias-orijtech): allow the seed to be passed in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For AppHash tests, the seed has to be hardcoded. I prefer to leave this for when MockComet support is added.
} | ||
return fixture | ||
}) | ||
s.cfg.NumValidators = s.rand.Intn(10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// TODO(@elias-orijtech): allow number of validators to
// be programmatically passed in perhaps via cli arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has to be some limit? I assume this particular parameter will be revisited once testutil runs on top of MockComet.
c1f6201
to
79dc580
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loving the cleanup
79dc580
to
ff0cd00
Compare
WalkthroughWalkthroughThe changes across various files in the Cosmos SDK-based application suggest a significant refactoring of the simulation framework. The Changes
TipsChat with CodeRabbit Bot (
|
ff0cd00
to
9ee6aec
Compare
90a9e2e
to
7fed617
Compare
I've rebased and pushed the current progress of the simulator, and described the approach and future direction in the last commit and the PR description. The first commit removes the simulator tests that lock in message content, which I don't think are worth their weight. The PR is ready for review, but I've left it a draft because someone should decide whether to revert deletion of the replaced generators. |
2b49651
to
42d2ff1
Compare
Not sure what to do about the linter errors:
They seem to be caused by a mismatch between the v1 and v2 |
65ef1d6
to
8e83401
Compare
@tac0turtle I've rebased the PR on top of the recent testutil changes and re-enabled genesis state generation for the old simulator for compatibility. The missing piece is the v1 vs v2 simapp difference in the |
…ction The tests don't carry their maintenance weight, because they merely enforce particular messages for a particular random generator state. This is part of the simulator rewrite which is going to replace the generators.
This change implements a replacement for the current simulator based on testutil/network. Most of the changes are porting the module specific message generators to no longer rely on SimulationState, and to generate "real" messages, not simulator messages. The simulator driver is in simapp, as part of the IntegationTestSuite. The new approach aims to improve simulation in two important ways: - Simulation should more closely mimic a real network. The current simulator message delivery is implemented parallel to non-simulator message delivery, leading to loss of fidelity and higher maintenance. One symptom is cosmos#13843. - Simulation should be layered on top of modules, not part of modules. This means that modules should not import simulation packages, nor refer to its generator package (x/module/simulation). This should eventually fix cosmos#7622. There are also downsides, however. Where the current simulator is too high level, testutil/network is too low level: it runs a real network of validators which is difficult to control. For example: - AppHashes differ between runs, because modules may depend on non- deterministic state such as block header timestamps. - The validators runs in separate goroutines, which makes it hard to query app state without introducing race conditions. - Blocks are produced according tot time, and not under control by the test driver. This makes it hard to trigger processing of messages in particular blocks, which ruins determinism. Some of the issues may be worked around, for example by forcing the block headers to be deterministic; however, the real fix is to make testutil/network itself deterministic, providing the goldilock level of simulation: close enough to a real network, yet deterministic enough to generate the same chain state for a given random seed. A deterministic testutil/network is part of cosmos#18145. Future work includes: - Porting of the remaining module message generators. - Generating (and verifying) deterministic AppHashes, allowing reliable replay when a problematic message is detected. Depends on cosmos#18145. - Save/reload of state for faster debugging cycles. - Removal of the old simulator, most importantly the reference to it from module code. Updates cosmos#14753 (Simulator rewrite epic) Updates cosmos#7622 (reducing imports from modules to simulator) Updates cosmos#13843 (using real message delivery for simulation)
84f15c3
to
dc967dd
Compare
There was a problem hiding this 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: 6
Configuration used: CodeRabbit UI
Files selected for processing (20)
- simapp/app.go (3 hunks)
- simapp/app_v2.go (2 hunks)
- simapp/testutil_network_test.go (2 hunks)
- tests/integration/bank/keeper/deterministic_test.go (2 hunks)
- tests/integration/distribution/keeper/msg_server_test.go (2 hunks)
- tests/integration/evidence/keeper/infraction_test.go (2 hunks)
- tests/integration/example/example_test.go (3 hunks)
- tests/integration/gov/keeper/keeper_test.go (2 hunks)
- tests/integration/staking/keeper/common_test.go (2 hunks)
- tests/integration/staking/keeper/deterministic_test.go (2 hunks)
- testutil/network/network.go (3 hunks)
- x/auth/module.go (6 hunks)
- x/auth/simulation/genesis.go (2 hunks)
- x/auth/simulation/proposals.go (1 hunks)
- x/bank/module.go (2 hunks)
- x/bank/simulation/genesis.go (2 hunks)
- x/bank/simulation/operations.go (3 hunks)
- x/gov/module.go (2 hunks)
- x/gov/simulation/genesis.go (2 hunks)
- x/gov/simulation/operations.go (3 hunks)
Files skipped from review due to trivial changes (4)
- simapp/app.go
- tests/integration/bank/keeper/deterministic_test.go
- tests/integration/example/example_test.go
- tests/integration/staking/keeper/common_test.go
Additional comments: 43
tests/integration/gov/keeper/keeper_test.go (1)
- 120-126: Ensure that all calls to
auth.NewAppModule
,bank.NewAppModule
,staking.NewAppModule
, andgov.NewAppModule
throughout the codebase have been updated to match the new function signatures.tests/integration/evidence/keeper/infraction_test.go (1)
- 129-135: The
auth.NewAppModule
function call no longer includesauthsims.RandomGenesisAccounts
as an argument. Ensure that this change does not affect the functionality of theauthModule
and that all necessary adjustments have been made in the codebase.x/bank/simulation/genesis.go (2)
56-85: The changes in this hunk are well done. The function signatures have been updated to accept the necessary parameters directly, instead of relying on a
simState
object. This improves the modularity and testability of the functions. The removal of the print statement also cleans up the code.60-70: The suggestion to use
make
with a capacity hint fromlen(accs)
was declined in a previous review. The reasoning was that genesis generation is called once per genesis, so the performance improvement would be negligible. This reasoning is sound, and the current implementation is acceptable.x/bank/module.go (2)
164-168: The
GenerateGenesisState
function now callssimulation.RandomizedGenState
with specific parameters. Ensure that the parameters passed are correct and that the function behaves as expected.181-183: The
WeightedOperations
function now returnsnil
instead of callingsimulation.WeightedOperations
. This change might affect the behavior of the function. Ensure that this change is intentional and does not cause any issues.simapp/app_v2.go (1)
- 231-237: The creation of a simulation manager with specific module overrides has been replaced with a new simulation manager created from app modules. This change seems to be in line with the goal of reducing module dependencies on the simulation package. However, ensure that all necessary modules are included in
app.ModuleManager.Modules
.- app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, nil) + app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, <necessary modules>)x/auth/simulation/proposals.go (1)
- 8-17: The function
GenerateMsgUpdateParams
has been updated to remove unused parameters and renamed fromSimulateMsgUpdateParams
. Ensure that all calls to this function throughout the codebase have been updated to match the new signature and name.x/auth/simulation/genesis.go (2)
17-51: The
RandomGenesisAccounts
function has been significantly modified. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the new logic for creatingBaseAccount
,ContinuousVestingAccount
, andDelayedVestingAccount
is correct and aligns with the intended behavior.86-101: The
RandomizedGenState
function has been significantly modified. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the new logic for generating a randomGenesisState
for auth is correct and aligns with the intended behavior.x/gov/module.go (4)
322-327: The
RandomizedGenState
function now takes four parameters instead of one. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.330-332: The
ProposalContents
function now returnsnil
instead of callingsimulation.ProposalContents()
. This change might affect the simulation of governance proposals. Ensure that this change is intentional and does not affect the expected behavior.334-337: The
ProposalMsgs
function now returnsnil
instead of callingsimulation.ProposalMsgs()
. This change might affect the simulation of governance proposals. Ensure that this change is intentional and does not affect the expected behavior.344-347: The
WeightedOperations
function now returnsnil
instead of callingsimulation.WeightedOperations()
. This change might affect the simulation of governance proposals. Ensure that this change is intentional and does not affect the expected behavior.tests/integration/distribution/keeper/msg_server_test.go (2)
15-20: The import of the package "cosmossdk.io/x/auth/simulation" has been removed. Ensure that this does not affect any other parts of the code that might be using it.
117-120: The initialization of the
authModule
in theinitFixture
function has been updated to use theauth.NewAppModule
function without theauthsims.RandomGenesisAccounts
argument. Make sure that this change is reflected whereverinitFixture
is used.simapp/testutil_network_test.go (4)
40-40: The hardcoded seed for the random number generator is still being used. This is fine for testing purposes as it ensures predictability and repeatability of tests. However, consider allowing the seed to be passed in for more varied testing in the future.
55-55: The number of validators is randomly generated with a maximum of 10. This is fine for testing purposes, but consider allowing this number to be programmatically passed in for more varied testing in the future.
64-66: Error handling is done correctly here. If an error occurs while generating a new mnemonic, the test fails immediately.
152-176: The simulation loop generates and broadcasts transactions, and handles future votes. Error handling is done correctly. If an error occurs while encoding a transaction or broadcasting it, the test fails immediately.
x/gov/simulation/genesis.go (5)
2-7: Imports are well organized and follow the standard Go conventions.
17-21: The constants are well defined and documented.
81-108: The
RandomizedGenState
function has been significantly refactored. It now uses therand.Rand
instance for random number generation and takes additional parameters forgenState
,cdc
, andbondDenom
. The function no longer relies on themodule.SimulationState
for random number generation and parameter retrieval. The function also no longer prints the randomly generated governance parameters to the console. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.102-105: The
v1.NewGenesisState
function is called with the correct parameters. The parameters are well defined and the function call is correct.107-107: The
govGenesis
is correctly marshaled into JSON and stored in thegenState
map.testutil/network/network.go (3)
19-19: The new import
github.com/cometbft/cometbft/config
is used for theFuzzConnConfig
type. Ensure that this package is maintained and reliable.136-136: The new field
FuzzConnConfig
is added to theConfig
struct. This field is used to conditionally setTestFuzz
andTestFuzzConfig
incmtCfg.P2P
.346-349: The
New
function is modified to conditionally setTestFuzz
andTestFuzzConfig
incmtCfg.P2P
based on the presence ofFuzzConnConfig
in the inputcfg
. This change is consistent with the PR's goal of improving simulation fidelity.x/bank/simulation/operations.go (1)
- 16-19: The constant
DistributionModuleName
is defined but not used anywhere in the code. If it's not needed, consider removing it to avoid confusion.x/auth/module.go (6)
93-97: The
randGenAccountsFn
field has been removed from theAppModule
struct. Ensure that this does not break any dependencies that might be using this field.106-111: The
NewAppModule
function no longer accepts therandGenAccountsFn
parameter. Make sure all calls to this function have been updated accordingly.157-159: The
GenerateGenesisState
function now uses a different method signature forsimulation.RandomizedGenState
. Verify that the new parameters are correctly passed and handled.190-195: The
RandomGenesisAccountsFn
field has been removed from theModuleInputs
struct. Ensure that this does not break any dependencies that might be using this field.213-218: The
ProvideModule
function has been updated to remove the logic for handling theRandomGenesisAccountsFn
field. Make sure this change is consistent with the overall design and does not introduce any issues.223-229: The
ProvideModule
function has been updated to remove the logic for handling theRandomGenesisAccountsFn
field. Make sure this change is consistent with the overall design and does not introduce any issues.x/gov/simulation/operations.go (7)
- 10-15: > Note: This review was outside of the patch, but no patch was found that overlapped with it. Original lines [19-15]
The import paths have been updated to reflect the new location of the packages. Ensure that the new paths are correct and the packages exist at those locations.
22-59: The
GenerateMsgSubmitProposal
function has been updated to include a newexpedited
variable which is used to determine the type of deposit. Ensure that this change is consistent with the rest of the codebase and that all calls to this function have been updated accordingly.73-268: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [61-126]
A new
VoteGenerator
type has been introduced along with associated methods for generating votes. This is a significant change and should be thoroughly tested to ensure it works as expected.
128-161: The
GenerateMsgDeposit
function has been updated to include a newexpedited
variable which is used to determine the type of deposit. Ensure that this change is consistent with the rest of the codebase and that all calls to this function have been updated accordingly.163-202: The
GenerateMsgVote
function has been updated to include a newproposalIDInt
variable which is used to determine the proposal ID. Ensure that this change is consistent with the rest of the codebase and that all calls to this function have been updated accordingly.204-234: The
GenerateMsgVoteWeighted
function has been introduced. This is a significant change and should be thoroughly tested to ensure it works as expected.236-266: The
GenerateMsgCancelProposal
function has been introduced. This is a significant change and should be thoroughly tested to ensure it works as expected.tests/integration/staking/keeper/deterministic_test.go (1)
- 110-110: The
auth.NewAppModule
function call has been updated to exclude theauthsims.RandomGenesisAccounts
argument. Ensure that this change does not affect the functionality of theauthModule
and that all dependencies are correctly handled.
@@ -405,7 +404,7 @@ func NewSimApp( | |||
txConfig, | |||
), | |||
accounts.NewAppModule(app.AccountsKeeper), | |||
auth.NewAppModule(appCodec, app.AuthKeeper, authsims.RandomGenesisAccounts), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this break existing simulator from running?
i think we may need to revert the removal of things in modules as we want the current simulator to still run, I believe there is still some work we need to do to make it sufficient to replace things. I can take ownership of this and push it over the finish line |
Thank you very much. Another option is to leave this PR in draft and start working on a deterministic testutil/network based on your conversion to interfaces and integration of MockComet. |
There was a problem hiding this 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 UI
Files selected for processing (3)
- simapp/app.go (3 hunks)
- tests/integration/distribution/keeper/msg_server_test.go (2 hunks)
- testutil/network/network.go (3 hunks)
Additional comments: 6
simapp/app.go (3)
29-34: The removal of the import
cosmossdk.io/x/auth/simulation
is not visible within the provided hunk. It's important to ensure that this import is no longer used within the file and that any associated functionality has been refactored or removed accordingly.412-418: The call to
auth.NewAppModule
does not show the removal of theauthsims.RandomGenesisAccounts
argument within the provided hunk. It's important to ensure that the call has been updated as per the PR objectives and that any necessary adjustments have been made to accommodate this change.509-515: The addition of the simulation manager and the registration of store decoders are consistent with the PR objectives of implementing a new simulator that runs on top of
testutil/network
. This change should help in achieving the goal of making simulations more realistic and deterministic.testutil/network/network.go (3)
19-19: The import of
cmtcfg
is correctly added to support the newFuzzConnConfig
field in theConfig
struct.345-348: The conditional logic to set
cmtCfg.P2P.TestFuzz
andcmtCfg.P2P.TestFuzzConfig
based on the presence ofFuzzConnConfig
is correctly implemented.132-137: The addition of
FuzzConnConfig
to theConfig
struct is consistent with the PR's objectives to improve the realism and determinism of simulations by allowing network fuzzing configurations.
@@ -131,6 +132,8 @@ type Config struct { | |||
AddressCodec address.Codec // address codec | |||
ValidatorAddressCodec runtime.ValidatorAddressCodec // validator address codec | |||
ConsensusAddressCodec runtime.ConsensusAddressCodec // consensus address codec | |||
|
|||
FuzzConnConfig *cmtcfg.FuzzConnConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new field FuzzConnConfig
has been added to the Config
struct. Ensure that this field is properly documented to explain its purpose and usage within the network configuration.
hmm i cant get this test to fail locally, @elias-orijtech are you able to do it locally? |
Sorry for dropping this ball. Do you mean the failing simapp v1 test? If so, did you test with the |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This change implements a replacement for the current simulator based
on testutil/network. Most of the changes are porting the module specific
message generators to no longer rely on SimulationState, and to generate
"real" messages, not simulator messages. The simulator driver is in
simapp, as part of the IntegationTestSuite.
The new approach aims to improve simulation in two important ways:
simulator message delivery is implemented parallel to non-simulator
message delivery, leading to loss of fidelity and higher maintenance.
One symptom is Add support for
Prepare/ProcessProposal
in simulation testing #13843.This means that modules should not import simulation packages, nor refer
to its generator package (x/module/simulation). This should eventually
fix Refactor simulation packages & subpackages to reduce frequency of import cycles #7622.
There are also downsides, however. Where the current simulator is too
high level, testutil/network is too low level: it runs a real network
of validators which is difficult to control. For example:
deterministic state such as block header timestamps.
query app state without introducing race conditions.
test driver. This makes it hard to trigger processing of messages in
particular blocks, which ruins determinism.
Some of the issues may be worked around, for example by forcing the
block headers to be deterministic; however, the real fix is to make
testutil/network itself deterministic, providing the goldilock level
of simulation: close enough to a real network, yet deterministic enough
to generate the same chain state for a given random seed.
A deterministic testutil/network is part of #18145.
Future work includes:
replay when a problematic message is detected. Depends on [Feature]: modular testutil.Network #18145.
module code.
Updates #14753 (Simulator rewrite epic)
Updates #7622 (reducing imports from modules to simulator)
Updates #13843 (using real message delivery for simulation)
CC @odeke-em @tac0turtle @alexanderbez
Summary by CodeRabbit
Refactor
auth
module across various test suites.auth
module's initialization process by excluding specific arguments related to random account generation.New Features
testutil_network_test.go
to enhance network and application configuration for testing.testutil/network/network.go
.Bug Fixes
RandomGenesisAccounts
andRandomizedGenState
functions in thex/auth/simulation
package to accept new parameters and return updated types, ensuring correct initialization of genesis states.Documentation
x/auth/simulation
andx/bank/simulation
packages to better reflect their purpose.Chores