Skip to main content

Part 2: Optimizing the kernel

To originate the kernel on Tezos, it must fit within the maximum size for a layer 1 operation (32KB). In these steps, you reduce the size of the kernel:

  1. From the hello-world-kernel directory, run this command to print the current size of the kernel:

    du -h target/wasm32-unknown-unknown/debug/hello_world_kernel.wasm

    Because you ran it in debug mode, the size of the compiled kernel and its dependencies may be 18MB or more, which is too large to originate.

  2. Run this command to create a release build of the kernel:

    cargo build --release --target wasm32-unknown-unknown

    This command creates a release build of the kernel in the target/wasm32-unknown-unknown/release/ directory.

  3. Check the size of the release build of the kernel:

    du -h target/wasm32-unknown-unknown/release/hello_world_kernel.wasm

    The release build is significantly smaller, but still too large.

  4. Run the wasm-strip command to reduce the size of the kernel:

    wasm-strip target/wasm32-unknown-unknown/release/hello_world_kernel.wasm

    This command removes WebAssembly code that is not necessary to run Smart Rollups.

  5. Run the du command again to see the new size of the kernel:

    du -h target/wasm32-unknown-unknown/release/hello_world_kernel.wasm

    The size of the kernel is smaller now.

    To get the kernel running with an even smaller size, you can use the installer kernel, which includes only enough information to install your original kernel. To do this, your kernel is split up and stored in separate files called preimages. When the installer kernel runs, it requests these files and reconstructs the original kernel.

  6. Run this command to install the installer kernel tool:

    cargo install tezos-smart-rollup-installer
  7. Run this command to create an installer kernel:

    smart-rollup-installer get-reveal-installer \
    --upgrade-to target/wasm32-unknown-unknown/release/hello_world_kernel.wasm \
    --output hello_world_kernel_installer.wasm --preimages-dir preimages/

    This command creates the following files:

    • hello_world_kernel_installer.wasm: The installer kernel
    • preimages/: A directory that contains the preimages that allow nodes to restore the original kernel code

    When a node runs the installer kernel, it retrieves the preimages through the reveal data channel, a channel that Smart Rollups use to request data from outside of layer 1. For more information about the reveal data channel, see Reveal data channel.

  8. Verify the size of the installer kernel by running this command:

    du -h hello_world_kernel_installer.wasm

    Now the kernel is small enough to originate on layer 1.

  9. As you did in the previous section, from the root of the Octez repository, run the installer kernel in debug mode by running this command, changing <REPO_PATH> to the path to the hello-world-kernel directory:

    dune exec src/bin_wasm_debugger/main_wasm_debugger.exe -- \
    --kernel <REPO_PATH>/hello_world_kernel_installer.wasm \
    --preimage-dir <REPO_PATH>/preimages \
    --inputs <REPO_PATH>/empty_input.json

    Note that this command uses the installer kernel and provides the location of the preimages in the --preimage-dir argument.

    Then you can use the step inbox command to simulate receiving the inbox from layer 1, as you did in the previous section. You can see the hello world kernel messages in the log, which shows that the upgrade from the installer kernel to the full kernel was successful.

  10. Press Ctrl + C to end the debugging session.

  11. From the hello-world-kernel directory, create a hexadecimal version of the installer kernel by running this command outside of the Docker container:

    smart-rollup-installer get-reveal-installer \
    --upgrade-to target/wasm32-unknown-unknown/release/hello_world_kernel.wasm \
    --output hello_world_kernel_installer.hex --preimages-dir preimages/

In the next section, set up a sandbox environment to deploy this kernel. Continue to Part 3: Deploying (originating) to a sandbox.