Skip to main content

Part 3: 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. Run this command to print the current size of the kernel:

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

    You can run this command inside or outside of the Docker container.

    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. In a terminal window outside of the Docker container, run this command to create a release build of the kernel:

    cargo build --release --target wasm32-unknown-unknown
  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. In a terminal window outside of the Docker container, 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 rollups. You must run this command outside of the Docker container because it does not have the wasm-strip command.

  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. Note that the changes that you make to the kernel outside of the Docker container also appear in the container and vice versa because the folder is mounted with the Docker --volume argument.

    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. Then you run the installer kernel, it requests these files and reconstructs the original kernel.

  6. Outside of the Docker container, run this command to install the installer kernel tool:

    cargo install tezos-smart-rollup-installer
  7. Outside of the Docker container, 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. Inside of the Docker container, run the installer kernel in debug mode by running this command:

    octez-smart-rollup-wasm-debugger --kernel hello_world_kernel_installer.wasm \
    --preimage-dir preimages/ --inputs empty_input.json

    Then you can use the step inbox command to simulate receiving the inbox from layer 1. 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. Press Ctrl + C to end the debugging session.

  10. 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, you originate this hex-encoded installer kernel on layer 1.