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 all problems make test-sim-custom-genesis-fast for simulation test. #17911

Merged
merged 9 commits into from
Oct 20, 2023

Conversation

luchenqun
Copy link
Contributor

@luchenqun luchenqun commented Sep 27, 2023

cosmos sdk provides a powerful simulation testing capability, and one of the simulation modes TestFullAppSimulation allows for simulation testing using data from genesis.json. The original implementation can be found at #3428. However, with the release of v0.40.0, there have been significant changes to the code and TestFullAppSimulation is no longer being maintained.

The following are my steps:

To begin, the chain-related data can be initialized using the following command, at least 2 accounts must be added for genesis account, otherwise when executing SimulateMsgSend, two accounts will be selected to meet the conditions from != to and it will fall into an infinite loop.

simd init cosmos --chain-id simulation-app

simd keys add my_validator --keyring-backend test

MY_VALIDATOR_ADDRESS=$(simd keys show my_validator -a --keyring-backend test)

simd genesis add-genesis-account $MY_VALIDATOR_ADDRESS 100000000000stake
simd genesis add-genesis-account cosmos1gfg9ucc7rrzc207y9qfmf58erftzf8z8ww5lr7 100000000000stake
simd genesis add-genesis-account cosmos1lh6pmahlkytz7lmrs25a42j6kxlmztyt204kyq 100000000000stake
simd genesis add-genesis-account cosmos18v3mjs2pmssd03v0qz80fr0u2j5p7lxw8nchxg 100000000000stake

simd genesis gentx my_validator 100000000stake --chain-id simulation-app --keyring-backend test

simd genesis collect-gentxs

Then, in the latest main branch, execute the command make test-sim-custom-genesis-fast (note: you need to modify -Genesis=${HOME}/.gaiad/config/genesis.json to -Genesis=${HOME}/.simapp/config/genesis.json in the Makefile first). You may encounter the following error:

Running custom genesis simulation...
By default, /Users/lcq/.gaiad/config/genesis.json will be used.
=== RUN   TestFullAppSimulation
Starting SimulateFromSeed with randomness created with seed 99
Randomized simulation params: 
{}
Selected randomly generated consensus parameters:
{
 "block": {
  "max_bytes": 21456269,
  "max_gas": -1
 },
 "evidence": {
  "max_age_num_blocks": 302400,
  "max_age_duration": 1814400000000000
 },
 "validator": {
  "pub_key_types": [
   "ed25519"
  ]
 }
}
Starting the simulation from time Wed Sep 27 15:49:06 UTC 2023 (unixtime 1695829746)
Simulating... block 1/100, operation 0/269. Logs to writing to /Users/lcq/.simapp/simulations/2023-09-28_00:18:10.log
    simulate.go:347: error on block  1/100, operation (1/269) from x/group:
        group: not found [cosmos/cosmos-sdk/x/group/keeper/grpc_query.go:27]
        Comment: 
--- FAIL: TestFullAppSimulation (0.12s)
FAIL

Based on the errors encountered during each execution of make test-sim-custom-genesis-fast, I have identified and attempted to resolve three potential issues:

Issue 1: Fix the problem where the account randomly generated by AppStateFromGenesisFileFn does not have a ConsKey, causing SimulateMsgCreateValidator to fail.
This issue primarily arises when executing SimulateMsgCreateValidator. The accounts generated by AppStateFromGenesisFileFn do not have the ConsKey field specified, leading to a null pointer exception when calling simAccount.ConsKey.PubKey().

Issue 2: Fix the problem of operating on a group, such as SimulateMsgCreateGroupPolicy, when the group does not exist in the chain during simulation testing.
The problem is encountered when executing the simulation test of the group module. It occurs when there is no group created, meaning there is no group in the chain. Some operations are performed on an empty group, such as executing SimulateMsgCreateGroupPolicy.

Issue 3: Add the SigverifyTx flag in the simulation test whether to check the transaction signature.
During the execution of AppStateFromGenesisFileFn, the private keys corresponding to addresses are generated randomly. Therefore, when executing transactions, we need to skip the signature verification check.
Of course, if the genesis data is also randomly generated, such as the simulation test executed by the make test-sim-nondeterminism command, the private key corresponding to the address generated at this time is correct, and the signature needs to be verified at this time.
Or when we also read the data initialization chain from the genesis.json file. If the private keys corresponding to all addresses are passed in, we can also verify the signature at this time.
So I added a SigverifyTx flag that leaves it up to the user to decide whether signature verification of the transaction is required. For example, if I want to skip signature verification when executing simulation, then I can run go test -mod=readonly -run TestFullAppSimulation -Genesis=genesis.json -SigverifyTx=false

@alexanderbez
Copy link
Contributor

cc @elias-orijtech

@github-actions github-actions bot added the C:CLI label Sep 28, 2023
return genesis, nil, err
// NOTE: cometbft uses a custom JSON decoder for GenesisDoc
// On some machines, the above will go wrong, so try using cbftjson for Unmarshal again.
if err = cbftjson.Unmarshal(bytes, &genesis); err != nil {
Copy link
Member

@julienrbrt julienrbrt Sep 28, 2023

Choose a reason for hiding this comment

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

We have a helper for that un x/genutil (

func AppGenesisFromReader(reader io.Reader) (*AppGenesis, error) {
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, have used the AppGenesisFromReader function to Unmarshal the genesis.json file

@@ -31,6 +31,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/mempool"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
Copy link
Member

Choose a reason for hiding this comment

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

This feels not right to import a x/ path in baseapp.

Copy link
Contributor Author

@luchenqun luchenqun Oct 1, 2023

Choose a reason for hiding this comment

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

Yes, but as I mentioned above: "Add the SigverifyTx flag in the simulation test whether to check the transaction signature.", in order to achieve this requirement, I followed the previous implementation and added a FlagSigverifyTx global variable(x/simulation/client/cli/flags.go), and during in the simulation test,whether check the user's signature based on this global variable. In order to achieve this requirement, I have not thought of a better implementation for the time being. Can you give me any more suggestions or tips?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This feels not right to import a x/ path in baseapp.

@julienrbrt A private variable sigverifyTx has been added to the type BaseApp struct to solve this problem. Please help me review the code again.

errorsmod "cosmossdk.io/errors"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
Copy link
Member

Choose a reason for hiding this comment

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

please rn make lint-fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test in sigverify_test to harden this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! Please help me review the code again.

@@ -89,6 +89,7 @@ type BaseApp struct {
addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
fauxMerkleMode bool // if true, IAVL MountStores uses MountStoresDB for simulation speed.
sigverifyTx bool // whether to sigverify check for transaction
Copy link
Member

Choose a reason for hiding this comment

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

can we precise here, as in context what it is used for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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.

lgtm!

@julienrbrt julienrbrt requested review from alexanderbez and removed request for tac0turtle and atheeshp October 20, 2023 08:21
@tac0turtle tac0turtle added this pull request to the merge queue Oct 20, 2023
Merged via the queue into cosmos:main with commit 611cd36 Oct 20, 2023
44 of 47 checks passed
@fmorency
Copy link
Contributor

fmorency commented Jul 5, 2024

@julienrbrt Could we get those fixes in the next CosmosSDK 0.50.x version?

The fix in x/simulation/simulate.go prevents a failed simulation edge case.

@fmorency
Copy link
Contributor

fmorency commented Jul 8, 2024

Hey @julienrbrt, I'm sorry for poking you again but I want to make sure you saw my post above.

@julienrbrt
Copy link
Member

Perfectly on time for our monthly patch release (this week), thanks for pinging me

@julienrbrt
Copy link
Member

@Mergifyio backport release/v0.50.x

Copy link
Contributor

mergify bot commented Jul 9, 2024

backport release/v0.50.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jul 9, 2024
… for simulation test. (#17911)

Co-authored-by: Luke <chenqun.l@kaitaitech.cn>
(cherry picked from commit 611cd36)

# Conflicts:
#	CHANGELOG.md
julienrbrt added a commit that referenced this pull request Jul 11, 2024
… for simulation test. (backport #17911) (#20909)

Co-authored-by: Chenqun Lu <luchenqun@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Reecepbcups pushed a commit to rollchains/cosmos-sdk that referenced this pull request Jul 19, 2024
* build(deps): Bump github.com/cosmos/gogoproto from 1.4.12 to 1.5.0 (cosmos#20567)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>

* refactor(x/authz,x/feegrant): provide updated keeper in depinject (cosmos#20590)

* docs: Update high level overview and introduction (backport cosmos#20535) (cosmos#20627)

Co-authored-by: samricotta <37125168+samricotta@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>

* fix: Properly parse json in the wait-tx command. (backport cosmos#20631) (cosmos#20660)

Co-authored-by: Daniel Wedul <github@wedul.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>

* docs: remove Ineffective code block (backport cosmos#20703) (cosmos#20711)

* feat(client): Add flag & reading mnemonic from file (backport cosmos#20690) (cosmos#20712)

Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>

* fix: nested multisig signatures using CLI (backport cosmos#20438) (cosmos#20692)

Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Facundo <facundomedica@gmail.com>

* feat(client/v2): get keyring from context (backport cosmos#19646) (cosmos#20727)

Co-authored-by: Julien Robert <julien@rbrt.fr>

* docs(x/group): orm codespace comment (backport cosmos#20749) (cosmos#20751)

* feat: parse home flag earlier (backport cosmos#20771) (cosmos#20777)

Co-authored-by: Julien Robert <julien@rbrt.fr>

* build(deps): Bump github.com/cometbft/cometbft from 0.38.7 to 0.38.8 (cosmos#20805)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>

* build(deps): Bump github.com/cometbft/cometbft from 0.38.8 to 0.38.9 (cosmos#20836)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>

* fix(simulation): fix the problem of `validator set is empty after InitGenesis` in simulation test (backport cosmos#18196) (cosmos#20897)

Co-authored-by: Chenqun Lu <luchenqun@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>

* fix(simulation): Fix all problems `make test-sim-custom-genesis-fast` for simulation test. (backport cosmos#17911) (cosmos#20909)

Co-authored-by: Chenqun Lu <luchenqun@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>

* chore: prepare v0.50.8 (cosmos#20910)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: samricotta <37125168+samricotta@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Daniel Wedul <github@wedul.com>
Co-authored-by: Hieu Vu <72878483+hieuvubk@users.noreply.github.com>
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: Facundo <facundomedica@gmail.com>
Co-authored-by: Chenqun Lu <luchenqun@qq.com>
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.

6 participants