infrablockchain-docs
en
en
  • InfraBlockchain
    • Learn
      • Architecture
        • Architecture
        • Network Participants
        • Parachain
          • System Parachains
      • Protocol
        • System Token
        • Transaction Fee
        • Proof of Transaction
      • Substrate
        • Learn
          • Basic
            • Cryptography
            • Blockchain Basics
            • Consensus
            • Networks and Nodes
            • Blockchain Transaction
            • Transaction Life Cycle
            • Offchain Operations
            • Light Client
            • Rust for Substrate
            • Introduction to Library
            • Architecture and Rust Libraries
            • File Architecture
            • Accounts, Addresses, and Keys
            • Transaction Format
            • Blockchain Randomness
          • FRAME
            • FRAME Pallets
            • FRAME Macros
            • Custom Pallets
            • Pallet Coupling
            • Origin
            • Events and Erros
            • Runtime Storage
            • State Transitions and Storage
            • SCALE Encoding
            • Weight and Fee
            • Runtime API
            • Runtime Development
          • Account
          • Address Format
          • Glossary
          • CLI
            • Archive
            • Memory Profiler
            • Node Template
            • sidecar
            • srtool
            • Subkey
            • subxt
            • try-runtime
            • tx-wrapper
          • Runtime Development
            • Basics
              • Configure Genesis State
              • Configure Runtime Constants
              • Customize a Chain Spec
              • Import a Pallet
              • Use Helper Function
            • Consensus Model
              • PoW
              • Create a Hybrid Node
            • Offchain Worker
              • Request Offchain HTTP
              • Offchain Indexing
              • Offchain Local Storage
            • Pallet Design
              • Create a Storage Structure
              • Implement Lockable Currency
              • Incorporate Randomness
              • Loose Coupling
              • Tight Coupling
            • Parachain Development
              • Add HRMP Channel
              • Add Paranodes
              • Connect to a Local Relay Chain
              • Convert a Solo Chain
              • Prepare to Launch
              • Select Collator
              • Upgrade a Parachain
            • Storage Migration
              • Basics
              • Trigger Migration
            • Test
              • Basics
              • Test a Transfer Transaction
            • Tools
              • Create a TxWrapper
              • Use Sidecar
              • try-runtime
              • Verify WASM
            • Weigths
              • Benchmark
              • Calculate Fees
              • Use Conditional Weights
              • Use Custom Weights
        • Build
          • Decide What to Build
          • Build Process
          • Determinisitc Runtime
          • Chain Spec
          • Genesis Configuration
          • Application Development
          • RPC
          • Troubleshoot Your Code
        • Tutorials
          • Install
            • Developer Tools
            • Linux
            • macOS
            • Rust Toolchain
            • Issues
            • Windows
          • Quick Start
            • Explore the Code
            • Modify Runtime
            • Start a Node
            • Substrate Basics
          • Build a Blockchain
            • Add Trusted Nodes
            • Authorize Specific Nodes
            • Build a Local Blockchain
            • Simulate Network
            • Upgrade a Running Network
          • Build Application Logic
            • Add a Pallet
            • Add Offchasin Workers
            • Publish Custom Pallets
            • Specify Origin for a Call
            • Use Macros in a Custom Pallet
          • Integrate with Tools
            • Access EVM Accounts
            • EVM Integration
            • Explore Sidecar Endpoints
            • Integrate a Light Client Node
          • Smart Contracts
            • Strategy
            • Build a Token Contract
            • Develop a Smart Contract
            • Prepare Your First Contract
            • Troubleshoot Smart Contracts
            • Use Maps for Storing Values
      • XCM
        • XCM
        • XCM Format
    • Service Chains
      • InfraDID
      • InfraEVM
      • URAuth(Universal Resource Auth)
    • DevOps
      • Build
      • Deploy
      • Monitoring
      • Runtime Upgrade
    • Tutorials
      • Basic
        • How to Interact with System Token
        • How To Pay Transaction Fee
        • How To Vote with TaaV
        • Hot to Get Validator Reward
      • Build
        • Build InfraRelayChain
        • Build Parachain
        • Open Message Passing Channels
        • Transfer Assets with XCM
      • Test
        • Benchmark
        • Check Runtime
        • Debug
        • Simulate Parachains
        • Unit Testing
      • Service Chains
        • Play with InfraDID
          • Build
          • Add Keys
          • Add Service Endpoint
          • Create InfraDID
        • Play with InfraEVM
          • Build
          • Deposit and Withdraw Token
          • Deploy ERC20 Contract
          • Deploy ERC721 Contract
          • Deploy ERC1155 Contract
  • Newnal Data Market
Powered by GitBook
On this page
  • Motivation
  • How it works
  • Usage
  • Command-line examples
  • Where to go next
  1. InfraBlockchain
  2. Tutorials
  3. Test

Check Runtime

Describes the try-runtime command-line tool for testing a specified runtime state against a production snapshot of chain state.

PreviousBenchmarkNextDebug

Last updated 1 year ago

The try-runtime command-line tool enables you to query a snapshot of runtime storage using an data structure to store state. By using the in-memory storage, you can write tests for a specified runtime state so that you can test against real chain state before going to production.

In its simplest form, you can use try-runtime to test the runtime state by doing the following:

  1. Connect to a remote node.

  2. Call a specific runtime API.

  3. Retrieve state from the node at a specific block.

  4. Write tests for the data retrieved.

Motivation

The initial motivation for try-runtime came from the need to test runtime changes against state from a real chain. Prior and existed for writing unit and integrated tests with mock data, but lacked the ability to test against a chain's actual state. The try-runtime tool extends TestExternalities and BasicExternalities by retrieving state using the following RPC endpoints for the node:

  • rpc_get_storage

  • rpc_get_keys_paged

(see for more details.)

After using the key-value database to retrieve state, try-runtime inserts the data into TestExternalities.

How it works

The try-runtime tool has its own implementation of externalities called which is just a wrapper around TestExternalities that uses a generic where data is .

The diagram below illustrates the way externalities sits outside a compiled runtime as a means to capture the storage of that runtime.

With remote_externalities, you can capture some chain state and run tests on it. Essentially, RemoteExternalities will populate a TestExternalities with a real chain's data.

Usage

The most common use case for try-runtime is to help you prepare for storage migration and runtime upgrades.

Because the RPC calls that query storage are computationally expensive, there are a number of command-line options you should set for a running node before you use the try-runtime command. To prepare a node for try-runtime testing, set the following options:

  • Set --rpc-max-payload 1000 to ensure large RPC queries can work.

  • Set --rpc-cors all to ensure WebSocket connections can come through.

Runtime upgrade hooks

By default, runtime upgrade hooks—which can be defined inside of the runtime or inside pallets—specify what should happen when there's been a runtime upgrade. That is, the default on_runtime_upgrade method only describes runtime state after the upgrade. However, it is possible to use methods provided by try-runtime to inspect and compare the runtime state before and after a runtime upgrade for testing purposes.

If you enable the try-runtime feature for the runtime, you can define pre-upgrade and post-upgrade hooks for the runtime as follows:

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
		Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
		Ok(())
}

With these function, you can use the pre_upgrade hook to retrieve the runtime state and return it as a Vec result. You can the pass the Vec as input parameter to the post_upgrade hook.

Command-line examples

To use try-runtime from the command-line, run your node with the --features=try-runtime flag.

For example:

cargo run --release --features=try-runtime try-runtime

You can use the following subcommands with try-runtime:

  • on-runtime-upgrade: Executes tryRuntime_on_runtime_upgrade against the given runtime state.

  • offchain-worker: Executes offchainWorkerApi_offchain_worker against the given runtime state.

  • execute-block: Executes core_execute_block using the given block and the runtime state of the parent block.

  • follow-chain: Follows a given chain's finalized blocks and applies to all its extrinsics. This allows the behavior of a new runtime to be inspected over a long period of time, with real transactions coming as input.

To view usage information for a specific try-runtime subcommand, specify the subcommand and the --help flag. For example, to see usage information for try-runtime on-runtime-upgrade, you can run the following command:

cargo run --release --features=try-runtime try-runtime on-runtime-upgrade --help

For example, you can run try-runtime with the on-runtime-upgrade subcommand for a chain running locally with a command like this:

cargo run --release --features=try-runtime try-runtime on-runtime-upgrade live ws://localhost:9944

You can use try-runtime to re-execute code from the ElectionProviderMultiPhase offchain worker on localhost:9944 with a command like this:

cargo run -- --release \
   --features=try-runtime \
   try-runtime \
   --execution Wasm \
   --wasm-execution Compiled \
   offchain-worker \
   --header-at 0x491d09f313c707b5096650d76600f063b09835fd820e2916d3f8b0f5b45bec30 \
   live \
   -b 0x491d09f313c707b5096650d76600f063b09835fd820e2916d3f8b0f5b45bec30 \
   -m ElectionProviderMultiPhase \
   --uri wss://localhost:9944

You can run the migrations of the local runtime on the state of SomeChain with a command like this:

RUST_LOG=runtime=trace,try-runtime::cli=trace,executor=trace \
   cargo run try-runtime \
   --execution Native \
   --chain somechain-dev \
   on-runtime-upgrade \
   live \
   --uri wss://rpc.polkadot.io

You can run try-runtime against the state for a specific block number with a command like this:

RUST_LOG=runtime=trace,try-runtime::cli=trace,executor=trace \
   cargo run try-runtime \
   --execution Native \
   --chain dev \
   --no-spec-name-check \
   on-runtime-upgrade \
   live \
   --uri wss://rpc.polkadot.io \
   --at <block-hash>

Notice that this command requires the --no-spec-name-check command-line option.

Where to go next

To query state, try-runtime uses the RPC methods provided by . In particular:

This method returns the storage value for the key that represents the block you specify.

This method returns the keys that match a prefix you specify with pagination support.

You can combine try-runtime with to test your chain before production. Use try-runtime to test your chain's migration and its pre and post states. Then, use fork-off-substrate if you want to check that block production continues after the migration.

StateApiClient
storage
storage_key_paged
fork-off-substrate
Storage keys
OnRuntimeUpgrade
try-runtime-upgrade
Staking pallet
in-memory-externalities
TestExternalities
BasicExternalities
remote externalities lib
remote_externalities
key-value store
type encoded
Storage externalities
Testing with externalities