/srtool

A fork of chevdor's srtool

Primary LanguageShellMIT LicenseMIT

Substrate Runtime Toolbox: srtool

1. Intro

Note
This is a fork of chevdor's tool, which can be found here

srtool is a collection of dockerized tools helping with Substrate & Polkadot Runtime development.

It especially helps with building and verifying Wasm Runtime Blobs.

The Docker image is paritytech/srtool. You can find it at https://hub.docker.com/r/paritytech/srtool.

2. Install

2.1. Pull image

    docker pull paritytech/srtool:<version> # (1)
  1. where <version> is the rustc version you want to use.

…​or just skip that step, the right image will be fetched at first run.

2.2. Using an alias

Creating an alias helps hiding the docker complexity behind one simple command. We will see more powerful options but this one is simple enough.

    export RUSTC_VERSION=nightly-2020-10-27; export PACKAGE=kusama-runtime; alias srtool='docker run --rm -it -e PACKAGE=$PACKAGE -v $PWD:/build -v /tmp/cargo:/cargo-home paritytech/srtool:$RUSTC_VERSION'
Note
Note that defining the alias as done above will hardcode the runtime. Using kusama-runtime as show above means you will always check the kusama runtime. If you need more, check the next chapter.
Note
If you want to check what your alias is, use type srtool

The command to invoke a build will then be srtool build.

2.3. Power users

If you want flexibility, using a function is slightly more complex but more flexible.

If you already defined an alias (you can check with type srtool), you should first unset it with unlias srtool.

Note
You cannot use both an alias and a function but you could make a function called fsrtool for instance for the function…​

2.3.1. Hardcoded compiler version

This option should be enough most of the time.

    function srtool() { docker run --rm -it -e PACKAGE=$1 -v $PWD:/build -v /tmp/cargo:/cargo-home paritytech/srtool:nightly-2020-10-27 $2; }

The commands to use srtool are then:

    srtool kusama-runtime build
Note
The <kusama-runtime> keyword above specifies we will check the kusama runtime. If your runtime is called xyz-runtime, swap accordingly.

2.3.2. Parametric compiler version

Using this version is more powerful if you know what you are doing but also requires more typing.

    function srtool() { docker run --rm -it -e PACKAGE=$1 -v $PWD:/build -v /tmp/cargo:/cargo-home paritytech/srtool:$2 $3; }

The command to invoke a build will then be srtool <package> <rust_version> build. For instance:

    srtool my-super-runtime nightly-2020-10-27 build
    srtool polkadot-runtime nightly-2019-12-05 build

WRNING: The second example is only there to illustrate how it works. If you try it today, it will likely not work as I did not build a container for this version yet.

You likely want to make this persistent with:

    echo "export RUSTC_VERSION=nightly-2020-10-27; export PACKAGE=polkadot-runtime; alias srtool='docker run --rm -it -e PACKAGE=$PACKAGE -v $PWD:/build -v /tmp/cargo:/cargo-home paritytech/srtool:$RUSTC_VERSION'" >> ~/.bash_profile && source ~/.bash_profile

If you wish to see more (it takes a little longer), you may set the VERBOSE ENV variable:

    export RUSTC_VERSION=nightly-2020-10-27; export PACKAGE=polkadot-runtime; alias srtool='docker run --rm -it -e PACKAGE=$PACKAGE -e VERBOSE=1 -v $PWD:/build -v /tmp/cargo:/cargo-home paritytech/srtool:$RUSTC_VERSION'
Note
The part -v /tmp/cargo:/cargo-home exposes the docker container’s cargo cache on your local machine. This helps making subsequent builds faster.

3. Use

Now that you defined the srtool alias, you can use it as shown below:

See the help
srtool help
Build the runtime
srtool build
Typical run

Invoking srtool build with:

$ srtool build

Will output something that looks like this:

    🧰 Substrate Runtime Toolbox 🧰
    🏗  Building polkadot-runtime as release
    ⏳ That can take a little while, be patient... Subsequent builds will be faster.
        Finished release [optimized] target(s) in 37.43s

    real	0m37.931s
    user	0m1.560s
    sys	0m3.220s
    ✨ Your Substrate WASM Runtime is ready! ✨
    Summary:
    Used rustc nightly-2020-10-27 (4560ea788 2019-11-04)
    Wasm     : ./srtool/release/wbuild/polkadot-runtime/polkadot_runtime.compact.wasm
    Content  : 0x0061736d0100000001a4022b60037f7f...3435663020323031392d31322d303429
    Size     : 1.1M
    Proposal : 0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f
    SHA256   : d5930520676994fc55a29c547f0159ea860cb46edd710a5be35e62565af1ad8b
JSON output

If you prefer a json output, srtool has you covered:

$ srtool build --json

Will give you such an output:

{
  "gen": "srtool",
  "rustc": "rustc 1.41.0-nightly (ae1b871cc 2019-12-06)",
  "wasm": "./target/srtool/release/wbuild/kusama-runtime/kusama_runtime.compact.wasm",
  "size": "1205052",
  "pkg": "kusama-runtime",
  "prop": "0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f",
  "sha256": "d93126c814f8366b651e425e34390212a98f8e77a8b73f9e1d2b07fc229a25f1",
  "tmsp": "2020-01-14T10:15:28Z"
}

4. Troubleshooting

4.1. Outdated repo

If you run into issues while running srtool, make sure you are using a decently recent version of Polkadot/Substrate:

Then run the following commands:

rm -rf target/srtool
cargo clean
cargo update

You can now try running srtool build again.

4.2. srtool tells me the folder is not a cargo project

The error is probably: !!! The folder on your host computer does not look like a Cargo project. Are you really in your repo?`

Run the following command:

alias srtool

And make sure that you see $PWD:/build/ and not /home/your_name/:/build. If you are running into this issue, your .bash_profile likely contains double quotes (") where you should have used single ones (').

4.3. Other cases

If you still run into troubles, please open a new issue and describe the error you see and the steps you took.

5. Proposal field

What is important in the output of srtool is the Proposal field:

    🧰 Substrate Runtime Toolbox 🧰
    ... Bla bla ...
    Proposal : 0x5931690e71e9d3d9f04a43d8c15e45e0968e563858dd87ad6485b2368a286a8f
    ... more blabla ...

The Proposal field value should should match the value of the proposal you can see in the Polkadot UI.

Advanced usage

if you feel fancy, you may also:

srtool bash

and look around the /srtool folder

6. Build the Docker image

While you don’t have to build the image yourself, you still may!

First you may want to double check what rustc versions are available as you will HAVE to build an image for a given version:

rustup check

So say you want to build a builder for rustc nightly-2020-03-12:

    RUSTC_VERSION=nightly-2020-10-27 && docker build --build-arg RUSTC_VERSION=$RUSTC_VERSION -t paritytech/srtool:$RUSTC_VERSION .