# IC Relay
A Rust library for durable, ordered delivery between two fixed Internet Computer canisters. Each destination has its own durable FIFO channel. Frames carry up to 4 KiB. The single-frame API retains one frame in each direction.
Open a channel with an exclusive named allocation lease from the host’s admitted ic-storage runtime and the remote ic-platform-types principal. Call `enqueue`, then send a clone of `outbound()` to that peer. The receiving update passes the system-authenticated caller and decoded frame to `accept`. Return its successful sequence to the sender, which calls `acknowledge` on the channel for the responding peer. On rejection, timeout, or backpressure, retry the retained frame. Duplicate frames acknowledge the same sequence without occupying a second slot. Acknowledge means durably received. Apply each `consume()` result within the same synchronous canister update, before any await.
The host owns its IC Wire endpoints, ingress size limits, authorization of local producers, scheduling and peer calls. Use one live channel handle per dedicated memory and reopen it on upgrade. Both peers must start fresh together and preserve their sequence history. Reinstalling one side independently can lose or suppress deliveries. The batched channel uses a single V1 format. Settle outstanding deliveries before switching both peers to fresh allocations.
Development targets Ubuntu 24.04 and macOS 15 (Apple Silicon and Intel). Use Rust 1.100.0 with the wasm32-unknown-unknown target. Run `cargo fetch --locked` once, then `make check` for snapshot verification, formatting, Clippy, Rustdoc, native tests and the Wasm build. The integration test simulates two endpoints over stable memory and reopens both after a lost acknowledgement.
## Batched notifications
The first modeled Toko workload is the generator export-completion notification in the `toko-new` porting tree. Its producer supplies a generator ID, collection ID, and token count. The local fixture routes each export completion by a synthetic recipient ID and verifies the generator ID, collection ID, and token count in the stored user feed.
Open one `Channel::open_batched` per destination with its own Storage lease. `BatchConfig::default()` retains up to 16 frames in each direction and selects up to 8 per call. Set `queue_items` and `batch_items` when opening the channel. Both must be positive, the batch must fit the queue, and queue capacity cannot exceed 16. A full outbox returns `Error::Full`.
On flush, take `outbound_batch()` from each nonempty peer queue and send that batch in one call. The receiver passes the authenticated caller and frames to `accept_batch`. Its receipt identifies the last frame durably accepted in sequence. Check that the returned sequence belongs to the attempted batch before calling `acknowledge`. A partial receipt frees only the accepted prefix. Retry the remaining frames on a later flush. Applications own the flush cadence.
Run `POCKET_IC_BIN=/absolute/path/to/pocket-ic make notification-demo` for the three-canister example. A producer routes even and odd user IDs to two fixed notification shards. The fixture checks per-user feeds, counts calls at the receiving endpoints, upgrades with pending work, and stops one shard while the other continues. The driver explicitly triggers generation and flushing.
## Two-canister demo
The `qualification/canic.toml` fleet declares two permanent services, `counter_a` and `counter_b`, built from the same package. Each installation receives its own service name and an explicitly approved workload driver. It can report ready before the other service exists. A channel operation resolves the opposite name from the local Canic directory. Before publication the caller can retry later. Relay retains the resolved peer and delivery sequences across upgrades.
From this repository, generate the two Canic service frames for the same driver. Supply each file to its corresponding ordinary Store-backed child plan with `--install-args`. Root remains controller of both children. The driver alone can queue, deliver or consume the demo increments.
```sh
canic app build --config qualification/canic.toml
cargo run --locked --manifest-path qualification/Cargo.toml --example service-install -- counter_a "$DRIVER" /tmp/counter_a.install
cargo run --locked --manifest-path qualification/Cargo.toml --example service-install -- counter_b "$DRIVER" /tmp/counter_b.install
```
With Root and Store already installed and the same counter Wasm published under each slot name, create each child plan against its retained publication. In this example the operator has set `ROOT_HASH`, `COUNTER_A_PUBLICATION` and `IDENTITY` for that local fleet. Review and apply the operation printed by the first command before planning the second service. If interrupted, resume the same approved operation.
```sh
canic fleet plan --config qualification/canic.toml --fleet local --canister counter_a --root-module "$ROOT_HASH" --release "$COUNTER_A_PUBLICATION" --install-args /tmp/counter_a.install --identity "$IDENTITY"
canic fleet apply "$OPERATION" --config qualification/canic.toml --fleet local --identity "$IDENTITY"
```
Repeat with `--canister counter_b`, the completed publication for `counter_b`, and `/tmp/counter_b.install`.
The PocketIC test installs A before B exists and verifies that A refuses a queue operation until publication. The test supplies directory deliveries as the installing canister, exercises the lost acknowledgement, upgrades both services and retries the retained frame. Run it with the prepared simulator:
```sh
POCKET_IC_BIN=/absolute/path/to/pocket-ic make demo
```