Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Toolchain: build, deploy, test

The worked example is examples/program_deployment.

A program crate

A guest program is a binary crate that depends on lee_core. Its source is a handler and one macro line:

use lee_core::program::{ReceiveInput, Response};

lee_core::actor_entrypoint!(handle_message);

fn handle_message(input: &ReceiveInput, greeting: Vec<u8>) -> Response {
    let mut bytes = input.pre_state.to_vec();
    bytes.extend(greeting);
    Response::set_state(bytes)
}

The second parameter can be any type that implements BorshDeserialize, in practice an enum of the messages the program accepts.

A guest crate outside this repository depends on lee_core by git, pinned to the release your node runs:

[dependencies]
lee_core = { git = "https://github.com/logos-blockchain/logos-execution-zone", tag = "v0.4.1" }
borsh = { version = "1.5.7", features = ["derive"] }
risc0-zkvm = { version = "3.1.0", default-features = false, features = ["std"] }

The same three lines serve the guest build and host unit tests. For a node built from a checkout, replace tag with rev = "<commit>", the node’s commit.

Put the message types in a separate core crate, as the shipped programs do (lez/programs/clock/core, or logos-blockchain/lez-programs: programs/token/core). Wallets, tests and other programs need the types without the guest code.

Building

RISC0_DOCKER_CONTAINER_TAG=r0.1.91.1 \
CARGO_TARGET_DIR=target/<methods dir> \
  cargo risczero build --manifest-path <methods dir>/guest/Cargo.toml

The output is one .bin file per binary in the guest crate, under target/<methods dir>/riscv32im-risc0-zkvm-elf/docker/.

Two caps bound the program’s memory layout, not its size on chain: at most 8 loadable segments (MAX_LOAD_SEGMENTS) and at most 4 MiB of loadable memory (MAX_LOADABLE_MEMORY). The loader checks both when it computes the image id in the header transaction, so a deployment exceeding either fails there, after its segments are already written and paid for. Large static arrays and a big .bss hit them. The 20-segment limit is separate: it bounds the bytecode chain the loader writes.

Pin the image tag: the image decides the image id, so a different builder gives a different id for identical source. Without the tag you get your installed cargo-risczero’s default image, which changes between its releases, and a default older than the pin can fail outright: cargo-risczero 3.0.5 defaults to r0.1.88.0, whose rustc 1.88 is too old for dependencies of this workspace’s guests, so the build stops before producing a .bin. CARGO_TARGET_DIR keeps each guest crate’s output apart, under target/<methods dir> relative to the directory you run the build from; without it the output lands in the guest crate’s workspace target directory (target/riscv32im-risc0-zkvm-elf/docker/ at the root, for a guest inside this repository), shared with every other guest built there. just build-artifact sets both, so a bare cargo risczero build copied from elsewhere gives a different result.

The directory you run the build from is also the Docker build context. The guest’s workspace, its root manifest and Cargo.lock included, and every path dependency of it, such as a sibling core crate, must lie inside it. For a crate outside this repository, run from the root of a workspace holding the guest and its core crate. A host crate that depends on a checkout of this repository by path belongs in a separate workspace: the container loads every member, and the checkout is outside the context.

Deploying

wallet account new public --label hello-header
wallet account new public --label hello-seg-0   # and seg-1, seg-2, ...
wallet program-loader deploy --elf path/to/hello_world.bin \
  --header hello-header --segments hello-seg-0 hello-seg-1 hello-seg-2 --immutable \
  --payer Public/<funded account>

deploy takes no positional path. The accounts must already exist with an empty program_loader state; the wallet chunks the ELF but never creates accounts. --payer pays every segment and header transaction; without it the wallet picks a payer from those new, empty accounts and the deploy fails on its fee reserve. You need one segment account per 96 KiB of the user ELF inside the .bin (MAX_SEGMENT_DATA_LEN), so a typical program takes several; a wrong count fails before anything is sent, with Program bytecode splits into 3 segment(s) but 2 were supplied. A deploy that fails partway cannot be resumed over the same accounts. wallet program-loader --help lists the arguments.

The wallet prints every transaction it waits for in full, segment bytes included, so a deploy can print over a megabyte. The program id is on the last line, Program deployed. Header account id: <id>. Set SUPPRESS_VERBOSE_PRINTS=1 to drop these dumps from every wallet command.

Accounts

wallet account new public  --label my-account
wallet account new private --label my-private-account
wallet account get --scope all --account-id Public/<id>
wallet account get --scope all --account-id Private/<id>

The Public/ or Private/ prefix tells the wallet which state to read: a public account from the sequencer, a private one from the wallet’s own synced copy. Without --raw, the wallet prints each actor state on the account separately.

Sending a message

wallet program call sends one public transaction to any deployed program, with the message already Borsh-encoded as hex:

wallet program call --program <program id> --message-hex <hex> \
  --to <account>[:sign] --payer Public/<funded account>

The program id is the header account id that program-loader deploy prints. --to is the actor the message goes to, and :sign makes the signature authorize it, which is what input.is_authorized() sees. Repeat --actor for every other actor the call touches. An actor is <account>[@program][:sign], with @program defaulting to --program, so --actor Public/<id>@11111111111111111111111111111111 declares that account’s native balance (that is the native token’s id, 32 zero bytes, which base58 writes as 32 ones). wallet program call --help gives the rest of the syntax, including PDAs; a PDA seed is 32 bytes, as base58 or as 0x and 64 hex digits. For the hello world program the message is a Vec<u8>, so Hola is its four-byte little-endian length then the bytes: 04000000486f6c61.

A private call, or a client of your own, needs code (What works with the reference wallet today). Each example has a small runner that loads the wallet, deploys the program (some can instead reuse one they are given), selects the program’s actor on an account, and submits a transaction:

cargo run --bin run_hello_world -- ...
cargo run --bin run_hello_world_private -- ...

The program is the same; the public runner builds and signs a PublicTransaction itself, and the private one hands a ProgramCatalog to the wallet’s send_privacy_preserving_tx. Read examples/program_deployment/src/bin/ for the code; it is the template for your own client. The runners do not sync: before proving on a private account, sync the wallet (wallet account sync-private, or sync_to_latest_block in the library). A proof built on state that a newer transaction already spent fails after the full proving time: refused with Nullifier already seen once that spend is in the head, or before then dropped from the block while the runner still returns (hello_world_with_move_function).

Receiving stored messages

Pending messages collects what other actors stored for your private accounts.

Testing a handler

A handler is an ordinary function: test it without a prover, chain or wallet by building the input by hand. The clock program’s tests show the shape (lez/programs/clock/src/main.rs):

const CLOCK: AccountId = AccountId::new([1; 32]);

fn data(block_id: u64) -> ClockAccountData {
    ClockAccountData { block_id, timestamp: 1_700_000_057 }
}

fn tick(block_id: u64) -> Message {
    Message::Tick { timestamp: 1_700_000_057, block_id }
}

fn run_on(account_id: AccountId, from: Option<Actor>, pre: ClockAccountData, message: Message)
    -> Transition
{
    let receiver = Actor::new(account_id, CLOCK);
    let input = ReceiveInput {
        receiver,
        from,
        grants: std::collections::BTreeSet::new(),
        pre_state: ActorState::from(pre.to_bytes()),
        message: borsh::to_vec(&message).unwrap(),
    };
    handle_message(&input, message).into_transition(input)
}

#[test]
#[should_panic(expected = "The clock program runs only on the clock account")]
fn another_account_is_refused() {
    let _transition = run_on(AccountId::new([2; 32]), None, data(9), tick(10));
}

Test each message as three questions:

  1. With an honest sender and authorization, is the new state right and are the right messages sent?
  2. With the wrong sender, is it refused?
  3. Without authorization, is it refused?

Because a handler sees only its own state and its input, these tests cover each handler on its own, but not the conversation between actors. For that, run a local sequencer and the wallet, as the integration tests under integration_tests/ do.

Proving in development

Set RISC0_DEV_MODE=1 to skip real proving while developing; private transactions then build in seconds. The proofs are fakes, so the sequencer accepts them only if it runs with RISC0_DEV_MODE=1 too; otherwise it refuses them with Invalid privacy preserving execution circuit proof. Never use it on a real network.

Versioning

Decide on the first day how your actor state and your messages are versioned. Both outlive the code: state stays on accounts, and async messages stay in mailboxes. See Upgrades.