Nodes on a canister's subnet can read its state, so no authority rests
on swiss numbers. fetch("home") answers the caller principal's own
durable store of named objects (init, get), found through Loam's name
index and made on first use; fetch("makeCounter") answers the public
maker. Homes, counters, and the CapTP identity live in Loam over the
stable-memory log, reopened in post_upgrade; sessions end with the heap.
The canister installs from 32 bytes of entropy passed as its argument
and refuses the anonymous principal. The client takes a key file, so a
caller can come back to its home, and sends string and reference
arguments.
Generated-by: Claude Code (claude-opus-5-5)
77 lines
5.0 KiB
Markdown
77 lines
5.0 KiB
Markdown
# A Counter in a canister, over OCapN
|
||
|
||
An Internet Computer canister hosts counters as an [OCapN](https://github.com/ocapn/ocapn) peer: a client makes one, calls `incr` and `decr` on it over CapTP, pipelined, and keeps it in a home that survives a canister upgrade. Both peers are [glowcap](https://github.com/glowcap-org/glowcap) compiled to WebAssembly; this is an exploratory prototype for [vattr97#5](https://github.com/dckc/vattr97/issues/5), canisters as vats.
|
||
|
||
## Getting started
|
||
|
||
You need [Nix](https://nixos.org/) with flakes; it supplies Zig 0.17.1 and Node.
|
||
|
||
```sh
|
||
nix develop
|
||
(cd client && npm ci --ignore-scripts)
|
||
export DO_NOT_TRACK=1 # icp-cli reports usage by default
|
||
icp() { npx --prefix client icp "$@"; }
|
||
icp network start -d
|
||
icp deploy -y counter --args "(blob \"$(head -c32 /dev/urandom | od -An -tx1 | tr -d ' \n' | sed 's/../\\&/g')\")"
|
||
ID=$(icp canister status counter --id-only)
|
||
node client/counter.mjs http://127.0.0.1:4960 $ID alice.key init c1
|
||
icp canister install counter --mode upgrade -y
|
||
node client/counter.mjs http://127.0.0.1:4960 $ID alice.key use c1 incr value
|
||
```
|
||
|
||
The output shows each update call and each answer; after the upgrade, Alice's counter picks up where it left off:
|
||
|
||
```
|
||
start 329B -> 345B in 319ms
|
||
session open
|
||
deliver 551B -> 807B in 258ms
|
||
incr 1
|
||
incr 2
|
||
decr 1
|
||
deliver 180B -> 120B in 247ms
|
||
kept as c1
|
||
Canister counter installed successfully
|
||
start 329B -> 345B in 295ms
|
||
session open
|
||
deliver 363B -> 538B in 261ms
|
||
incr 2
|
||
value 2
|
||
```
|
||
|
||
With another key file, `use c1` breaks: that home is someone else's. `icp network stop` stops the local network. `zig build test` runs the same story in memory.
|
||
|
||
## Homes, not swiss numbers
|
||
|
||
Nodes on a canister's subnet can read its state, so a canister can't keep a swiss number secret. Authority here starts instead with the caller principal, whose signature the Internet Computer checks:
|
||
|
||
- `fetch("home")` answers the caller's own home, a durable store of named objects: `init(name, object)` and `get(name)`. It's made the first time the caller asks.
|
||
- `fetch("makeCounter")` answers the one public maker; `make()` answers a fresh counter at zero.
|
||
- The anonymous principal is refused, since everyone who uses it would share one home.
|
||
|
||
Homes, counters, and the canister's CapTP identity live in a [Loam](https://github.com/glowcap-org/glowcap) store whose engine ([stable_log.zig](src/stable_log.zig)) appends each committed transaction to stable memory, the part of a canister's state that survives an upgrade; `post_upgrade` replays it. CapTP sessions live in the heap, so an upgrade ends them, and a client starts a new one and fetches its home again.
|
||
|
||
## One update call per round trip
|
||
|
||
- **The wire.** The canister's Candid interface is two update methods, `start` and `deliver`, each taking and returning a `blob` of CapTP frames in Syrup byte-string envelopes, and an install argument of 32 bytes of entropy ([counter.did](counter.did)). The OCapN locator transport is `icp`, its designator the canister's principal.
|
||
- **Sessions.** The canister keeps one CapTP session per caller principal, so the Internet Computer's request signature is what tells sessions apart. `start` carries the caller's `op:start-session`.
|
||
- **Pipelining.** The client sends to an answer before it settles, so the handshake is one call and making a counter plus every send to it is another: about 2.6 s on mainnet, 0.6 s on icp-cli's local network. Keeping it with `init` takes a third, since the client passes only settled references as arguments.
|
||
- **Answer releases** (`op:gc-answer`) ride ahead of the client's next message rather than costing a call of their own.
|
||
- **No server push.** The canister can only answer what a call delivers; promise resolutions it learns of later would need a query to poll.
|
||
|
||
## Testnets
|
||
|
||
| Where | How | Update call | Notes |
|
||
| --- | --- | --- | --- |
|
||
| icp-cli local network | `icp network start -d` | ~250 ms | [icp-cli](https://cli.internetcomputer.org/) 1.8 succeeds dfx; state in `.icp/` |
|
||
| dfx local replica | `dfx start` (project-local network in [dfx.json](dfx.json)) | ~1.25 s | dfx 0.30 is deprecated; not in nixpkgs |
|
||
| Playground (mainnet) | `dfx deploy --playground` | ~1.2 s | free; canister reclaimed after 20 min; icp-cli has no equivalent |
|
||
| Mainnet | `icp deploy -e ic` | ~1.2 s | budget 1–2T cycles (about 1 XDR per T) per canister |
|
||
|
||
## Known gaps
|
||
|
||
- **The entropy is not secret** from the subnet's nodes, nor from whoever installs; nothing rests on it but the CapTP keys. A real deployment would draw it from `raw_rand` in a first call after install.
|
||
- **One session per principal.** A second `start` from the same caller traps until an upgrade clears sessions; a client that loses its session can't reconnect with the same key.
|
||
- **No removal or collection.** A home holds 8 names and can't drop one; Loam garbage collection never runs.
|
||
- **The canister pays for every call** and admits any signed caller; it should refuse strangers before doing work (`canister_inspect_message`).
|
||
- **Third-party handoffs** need gift ids from entropy and dials to other peers; the canister refuses both.
|