{"id":19358427,"url":"https://github.com/paritytech/try-runtime-cli","last_synced_at":"2025-04-23T11:31:46.121Z","repository":{"id":66118715,"uuid":"601399591","full_name":"paritytech/try-runtime-cli","owner":"paritytech","description":"Substrate's programatic testing framework.","archived":false,"fork":false,"pushed_at":"2024-09-10T17:19:28.000Z","size":669780,"stargazers_count":13,"open_issues_count":25,"forks_count":12,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-09-11T19:38:42.237Z","etag":null,"topics":["blockchain","polkadot","substrate","testing"],"latest_commit_sha":null,"homepage":"https://paritytech.github.io/try-runtime-cli/try_runtime","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paritytech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-14T01:24:37.000Z","updated_at":"2024-09-10T17:00:17.000Z","dependencies_parsed_at":"2023-10-11T19:08:47.631Z","dependency_job_id":"ababc1cf-f1c5-48d0-84b3-0bcc6e7658d3","html_url":"https://github.com/paritytech/try-runtime-cli","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Ftry-runtime-cli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Ftry-runtime-cli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Ftry-runtime-cli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paritytech%2Ftry-runtime-cli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paritytech","download_url":"https://codeload.github.com/paritytech/try-runtime-cli/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223922085,"owners_count":17225636,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","polkadot","substrate","testing"],"created_at":"2024-11-10T07:11:43.906Z","updated_at":"2024-11-10T07:11:44.342Z","avatar_url":"https://github.com/paritytech.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- cargo-rdme start --\u003e\n\n# Try-runtime\n\nSubstrate's programmatic testing framework.\n\n\u003e As the name suggests, `try-runtime` is a detailed testing framework that gives you a lot of\n\u003e control over what is being executed in which environment. It is recommended that user's first\n\u003e familiarize themselves with substrate in depth, particularly the execution model. It is\n\u003e critical\n\u003e to deeply understand how the wasm/client/runtime interactions, and the runtime apis work in\n\u003e the\n\u003e substrate runtime, before commencing to working with `try-runtime`.\n\n#### Resources\n\nSome resources about the above:\n\n1. \u003chttps://www.crowdcast.io/e/substrate-seminar/41\u003e\n2. \u003chttps://docs.substrate.io/fundamentals/runtime-development/\u003e\n3. \u003chttps://www.youtube.com/watch?v=a_u3KMG-n-I\u003e\n\n---\n\n## Background Knowledge\n\nThe basis of all try-runtime commands is the same: connect to a live node, scrape its *state*\nand put it inside a [`TestExternalities`], then call into a *specific runtime-api* using the\ngiven state and some *runtime*.\n\nAlternatively, the state could come from a snapshot file.\n\nAll of the variables in the above statement are made *italic*. Let's look at each of them:\n\n1. **State** is the key-value pairs of data that comprise the canonical information that any\n   blockchain is keeping. A state can be full (all key-value pairs), or be partial (only pairs\n   related to some pallets/prefixes). Moreover, some keys are special and are not related to\n   specific pallets, known as [`well_known_keys`] in substrate. The most important of these is\n   the `:CODE:` key, which contains the code used for execution, when wasm execution is chosen.\n\n2. *A runtime-api* call is a call into a function defined in the runtime, *on top of a given\n   state*. Each subcommand of `try-runtime` utilizes a specific *runtime-api*.\n\n3. Finally, the **runtime** is the actual code that is used to execute the aforementioned\n   runtime-api. Everything in this crate assumes wasm execution, which means the runtime that\n   you use is the one stored onchain, namely under the `:CODE:` key.\n\nTo recap, a typical try-runtime command does the following:\n\n1. Download the state of a live chain, and write to an `externalities`.\n2. Overwrite the `:CODE:` with a given wasm blob\n3. Test some functionality via calling a runtime-api.\n\n## Installation\n```bash\n# Install latest version (recommended for local development)\ncargo install --git https://github.com/paritytech/try-runtime-cli --locked\n# Install a specific version (recommended for tools like CI)\ncargo install --git https://github.com/paritytech/try-runtime-cli --tag vX.Y.Z --locked\ntry-runtime --help\ntry-runtime on-runtime-upgrade --help\n```\n\n## Usage\n\nTo use any of the provided commands, [`SharedParams`] must be provided. The most important of\nwhich being [`SharedParams::runtime`], which specifies which runtime to use. Furthermore,\n[`SharedParams::overwrite_state_version`] can be used to alter the state-version (see\n\u003chttps://forum.polkadot.network/t/state-trie-migration/852\u003e for more info).\n\nThen, the specific command has to be specified. See [`Action`] for more information about each\ncommand's specific customization flags, and assumptions regarding the runtime being used.\n\nBriefly, this CLI is capable of executing:\n\n* [`Action::OnRuntimeUpgrade`]: execute all the [`OnRuntimeUpgrade`] hooks.\n* [`Action::ExecuteBlock`]: re-execute the given block.\n* [`Action::FastForward`]: execute [`OnRuntimeUpgrade`] hooks, then fast-forward the chain a\n  given number of blocks while checking try-state invarients.\n* [`Action::OffchainWorker`]: re-execute the given block's offchain worker code path.\n* [`Action::FollowChain`]: continuously execute the blocks of a remote chain on top of a given\n  runtime.\n* [`Action::CreateSnapshot`]: Create a snapshot file from a remote node.\n\nFinally, to make sure there are no errors regarding this, always run any `try-runtime` command\nwith `executor=trace` logging targets, which will specify which runtime is being used per api\ncall. Moreover, `remote-ext`, `try-runtime` and `runtime` logs targets will also be useful.\n\n## Spec name check\n\nA common pitfall is that you might be running some test on top of the state of chain `x`, with\nthe runtime of chain `y`. To avoid this all commands do a spec-name check before executing\nanything by default. This will check the, if any alterations are being made to the `:CODE:`,\nthen the spec names match. The spec versions are warned, but are not mandated to match.\n\n\u003e If anything, in most cases, we expect spec-versions to NOT match, because try-runtime is all\n\u003e about testing unreleased runtimes.\n\n## Note on signature and state-root checks\n\nAll of the commands calling into `TryRuntime_execute_block` ([`Action::ExecuteBlock`] and\n[`Action::FollowChain`]) disable both state root and signature checks. This is because in 99%\nof the cases, the runtime that is being tested is different from the one that is stored in the\ncanonical chain state. This implies:\n\n1. the state root will NEVER match, because `:CODE:` is different between the two.\n2. replaying all transactions will fail, because the spec-version is part of the transaction\n   signature.\n\n## Best Practices\n\nTry-runtime is all about battle-testing unreleased runtimes. The following list of suggestions\nhelp developers maximize their testing coverage and make the best use of `try-runtime` features.\n\n### Testing Runtime Upgrades\n\nOne of the most powerful abilities of `try-runtime` is using the\n[`OnRuntimeUpgrade::pre_upgrade`] and [`OnRuntimeUpgrade::post_upgrade`] hooks to test runtime\nupgrades implemented with [`OnRuntimeUpgrade`]. [`OnRuntimeUpgrade`] can be implemented inside\nthe pallet, or standalone in a runtime to define a migration to execute next runtime upgrade. In\nboth cases, these methods can be added:\n\n```rust\n#[cfg(feature = \"try-runtime\")]\nfn pre_upgrade() -\u003e Result\u003cVec\u003cu8\u003e, TryRuntimeError\u003e {}\n\n#[cfg(feature = \"try-runtime\")]\nfn post_upgrade(state: Vec\u003cu8\u003e) -\u003e Result\u003c(), TryRuntimeError\u003e {}\n```\n\n(The pallet macro syntax will support this simply as a part of `#[pallet::hooks]`).\n\nThese hooks will be called when you execute the [`Action::OnRuntimeUpgrade`] command, before and\nafter the migration. [`OnRuntimeUpgrade::pre_upgrade`] returns a [`Vec\u003cu8\u003e`] that can contain\narbitrary encoded data (usually some pre-upgrade state) which will be passed to\n[`OnRuntimeUpgrade::pre_upgrade`] after upgrading and used for post checking.\n\n### [`VersionedMigration`]\n\nIt is strongly suggested to use [`VersionedMigration`] when writing custom migrations for\npallets.\n\n### State Consistency\n\nSimilarly, each pallet can expose a function in `#[pallet::hooks]` section as follows:\n\n```rust\n#[cfg(feature = \"try-runtime\")]\nfn try_state(_: BlockNumber) -\u003e Result\u003c(), TryRuntimeError\u003e {}\n```\n\nwhich is called on numerous code paths in the try-runtime tool. These checks should ensure that\nthe state of the pallet is consistent and correct. See [`TryState`] for more info.\n\n### Logging\n\nIt is super helpful to make sure your migration code uses logging (always with a `runtime` log\ntarget prefix, e.g. `runtime::balance`) and state exactly at which stage it is, and what it is\ndoing.\n\n## Examples\n\nFor the following examples, we assume the existence of the following:\n\n1. a substrate node compiled with `--features try-runtime`, called `substrate`. This will be the\n   running node that you connect to, and provide a wasm blob that has try-runtime functionality\n   enabled.\n2. the `try-runtime` CLI binary on your path.\n\n```bash\n# this is like your running deployed node.\ncargo build --features try-runtime --release \u0026\u0026 cp target/release/substrate .\n```\n\n\u003e The above example is with `substrate`'s `kitchensink-runtime`, but is applicable to any\n\u003e substrate-based chain.\n\n* Run the migrations of a given runtime on top of a live state.\n\n```bash\n# assuming there's `./substrate --dev --tmp --ws-port 9999` or similar running.\ntry-runtime \\\n    --runtime /path-to-substrate/target/release/wbuild/my-runtime.wasm \\\n    on-runtime-upgrade \\\n    live --uri ws://localhost:9999\n```\n\n* Same as the previous example, but run it at specific block number's state and using the live\n  polkadot network. This means that this block hash's state should not yet have been pruned by\n  the node running at `rpc.polkadot.io`.\n\n```bash\ntry-runtime \\\n    --runtime /path-to-polkadot-runtimes/target/release/wbuild/polkadot-runtime/polkadot-runtime.wasm \\\n    on-runtime-upgrade \\\n    live --uri wss://rpc.polkadot.io:443 \\\n    # replace with your desired block hash!\n    --at 0xa1b16c1efd889a9f17375ec4dd5c1b4351a2be17fa069564fced10d23b9b3836\n```\n\n* Now, let's use a snapshot file. First, we create the snapshot:\n\n```bash\ntry-runtime --runtime existing create-snapshot --uri ws://localhost:9999 my-snapshot.snap\n2022-12-13 10:28:17.516  INFO                 main remote-ext: since no at is provided, setting it to latest finalized head, 0xe7d0b614dfe89af65b33577aae46a6f958c974bf52f8a5e865a0f4faeb578d22\n2022-12-13 10:28:17.516  INFO                 main remote-ext: since no prefix is filtered, the data for all pallets will be downloaded\n2022-12-13 10:28:17.550  INFO                 main remote-ext: writing snapshot of 1611464 bytes to \"node-268@latest.snap\"\n2022-12-13 10:28:17.551  INFO                 main remote-ext: initialized state externalities with storage root 0x925e4e95de4c08474fb7f976c4472fa9b8a1091619cd7820a793bf796ee6d932 and state_version V1\n```\n\n\u003e Note that the snapshot contains the `existing` runtime, which does not have the correct\n\u003e `try-runtime` feature. In the following commands, we still need to overwrite the runtime.\n\nThen, we can use it to have the same command as before, `on-runtime-upgrade`\n\n```bash\ntry-runtime \\\n    --runtime /path-to-substrate/target/release/wbuild/my-runtime.wasm \\\n    on-runtime-upgrade \\\n    snap -p my-snapshot.snap\n```\n\n* Execute the latest finalized block with the given runtime.\n\n```bash\ntry-runtime \\\n    --runtime /path-to-substrate/target/release/wbuild/my-runtime.wasm \\\n    execute-block live \\\n    --uri ws://localhost:9999\n```\n\nThis can still be customized at a given block with `--at`. If you want to use a snapshot, you\ncan still use `--block-ws-uri` to provide a node form which the block data can be fetched.\n\nMoreover, this runs the [`TryState`] hooks as well. The hooks to run can be customized with the\n`--try-state`. For example:\n\n```bash\ntry-runtime \\\n    --runtime /path-to-substrate/target/release/wbuild/my-runtime.wasm \\\n   execute-block \\\n   --try-state System,Staking \\\n   live \\\n   --uri ws://localhost:9999 \\\n   --pallet System Staking\n```\n\nWill only run the `try-state` of the two given pallets. When running `try-state` against\nsome real chain data it can take a long time for the command to execute since it has to\nquery all the key-value pairs. In scenarios like above where we only want to run the\n`try-state` for some specific pallets, we can use the `--pallet` option to specify from\nwhich pallets we want to query the state. This will greatly decrease the execution time.\n\nSee [`TryStateSelect`] for more information.\n\n* Follow our live chain's blocks using `follow-chain`, whilst running the try-state of 3 pallets\n  in a round robin fashion\n\n```bash\ntry-runtime \\\n    --runtime /path-to-substrate/target/release/wbuild/my-runtime.wasm \\\n    follow-chain \\\n    --uri ws://localhost:9999 \\\n    --try-state rr-3\n```\n\n[`VersionedMigration`]: frame_support::migrations::VersionedMigration\n[`OnRuntimeUpgrade`]: frame_support::traits::OnRuntimeUpgrade\n[`OnRuntimeUpgrade::pre_upgrade`]: frame_support::traits::OnRuntimeUpgrade::pre_upgrade\n[`OnRuntimeUpgrade::post_upgrade`]: frame_support::traits::OnRuntimeUpgrade::post_upgrade\n[`TryStateSelect`]: frame_support::traits::TryStateSelect\n[`TryState`]: frame_support::traits::TryState\n[`TestExternalities`]: sp_state_machine::TestExternalities\n[`well_known_keys`]: sp_storage::well_known_keys\n[`Action`]: try_runtime_core::commands::Action\n[`Action::FollowChain`]: try_runtime_core::commands::Action::FollowChain\n[`Action::OnRuntimeUpgrade`]: try_runtime_core::commands::Action::OnRuntimeUpgrade\n[`Action::ExecuteBlock`]: try_runtime_core::commands::Action::ExecuteBlock\n[`Action::OffchainWorker`]: try_runtime_core::commands::Action::OffchainWorker\n[`Action::CreateSnapshot`]: try_runtime_core::commands::Action::CreateSnapshot\n[`Action::FastForward`]: try_runtime_core::commands::Action::FastForward\n[`SharedParams`]: try_runtime_core::shared_parameters::SharedParams\n[`SharedParams::runtime`]: try_runtime_core::common::shared_parameters::SharedParams::runtime\n[`SharedParams::overwrite_state_version`]: try_runtime_core::common::shared_parameters::SharedParams::overwrite_state_version\n\n\u003c!-- cargo-rdme end --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparitytech%2Ftry-runtime-cli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparitytech%2Ftry-runtime-cli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparitytech%2Ftry-runtime-cli/lists"}