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

Examples Index

examples/ is a runnable catalog of Hooks written with rshooks, built with rshooks (from the rshooks-build package) — its own Cargo workspace, separate from the root workspace, because these crates are no_std cdylibs with a Hook-specific release profile that must not leak into rshooks-core/rshooks/ rshooks-build, and they don’t build for host targets. Every code sample in this book is adapted from one of these.

Reading order: 01–15

Numbered in suggested reading order — start at 01_accept-all and work down; each one builds on ideas from the examples before it. The example column is each crate’s actual package name (Cargo package names can’t start with a digit, so only the directory is prefixed).

#exampledemonstratesbook chapter
01accept-allminimal hook: accept everything (starter template)Anatomy of a Hook
02state-counterstate/state_set round-trip, counter in hook stateHook State
03hook-paramshook_param-configurable threshold, with a compiled-in defaultHook and Transaction Parameters
04errorsa meaningful hook_errors!-based rollback error-code system, matched to HookReturnCodeAccept, Rollback, and Errors
05firewallread otxn_field(sfAccount) + a hook parameter blacklist → rollbackReading the Originating Transaction
06guard-patternsguard!/guard_m! correctness, choosing maxiter, and the array-== memcmp-loop pitfallGuards and Loops
07xfl-mathreading Amount as XFL (slot_float/sto_set), mulratio, checked Add/Sub/Mul/Div/Neg operators, .compare()-family methods, and XFLUnchecked’s hot-path chainXFL: Decimal Floating Point
08slot-ledgerthe typed slot layer: SlotObject::from_otxn().get(sfXxx).value(), with no slot numbers in sight, measured against the raw numbered API it replacedSlots and Ledger Objects
09state-foreignstate_foreign: reading another (hook-parameter-configured) account’s hook stateHook State
10emit-txnetxn_reserve + a txn_template!-declared Payment/emit, with a cbakEmitting Transactions
12typed-data#[derive(HookData)]: composite (multi-field) state keys/values and otxn_param/hook_param structs, in place of hand-packed byte buffersTyped Data with Derives
13keyletsrshooks::api::keylet’s 26 typed keylet_xxx helpers (one per KEYLET_* constant), in place of the single untyped util_keyletKeylets
14account-id-macrorshooks::account_id!: compile-time r-address → AccountId decode, cross-checked against hook_account/util_accid/util_raddrReading the Originating Transaction
15slot-objectsthe typed slot layer’s live acceptance harness: account-root walk, native-amount drops round-trip, parent-clear/child-read, and two 300-iteration loops proving take_* recycling and leak-free slot_path! failuresSlots and Ledger Objects

There is no 11 — the numbering follows the historical example order, with gaps where an example was retired.

80+: production hooks in Rust

Unlike 0115 (one concept each, in suggested reading order), the 80+ series are behavior-equivalent Rust ports of real, deployed xahaud C hooks — read them after 0115, not instead of them. Each has its own README with a full behavior-equivalence table against its C source, a differences table for any intentional deviation, and a “Toolchain limitation” section documenting a real Guard-type nesting-depth/floating-point constraint discovered while porting them.

#exampleports
80rewardhook/genesis/reward.c — the RewardHook: computes and emits a GenesisMint crediting ClaimReward claimants and active-validator L1 seats
81governhook/genesis/govern.c — the GovernanceHook: the 20-seat L1/L2 round-table governance state machine

Building

Build every example (this is also the toolchain’s own end-to-end test: each one is built via cargo run -p rshooks-build -- build ... from the root workspace, and the resulting out/<name>.wasm is re-validated with rshooks check):

mise run build-examples

Build a single example directly:

cargo run -p rshooks-build -- build --manifest-path examples/02_state-counter/Cargo.toml

See The rshooks CLI for the CLI itself, and each example’s own README for its exact command.

E2E tests

e2e/ deploys the examples’ rshooks-build output to a real, standalone xahaud node (via SetHook) and asserts on the resulting transaction metadata and ledger state — proof of runtime behavior, not just that the binaries are SetHook-valid.