feat: homes per caller and counters that survive upgrades

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)
This commit is contained in:
dckc committed 2026-11-18 06:21:31 -06:00
1 parent eb15a05668
commit b717ccaedf
11 files changed
+782 -233

No files matched your search

+36 -14
View File
@@ -1,6 +1,6 @@
# A Counter in a canister, over OCapN
An Internet Computer canister hosts a Counter as an [OCapN](https://github.com/ocapn/ocapn) peer, and a client calls `incr` and `decr` on it over CapTP, pipelined, in one update call. 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.
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
@@ -10,29 +10,51 @@ You need [Nix](https://nixos.org/) with flakes; it supplies Zig 0.17.1 and Node.
nix develop
(cd client && npm ci --ignore-scripts)
export DO_NOT_TRACK=1 # icp-cli reports usage by default
npx --prefix client icp network start -d
npx --prefix client icp deploy -y
node client/counter.mjs http://127.0.0.1:4960 "$(npx --prefix client icp canister status counter --id-only)"
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:
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 300ms
start 329B -> 345B in 319ms
session open
deliver 539B -> 725B in 271ms
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
```
`npx --prefix client icp network stop` stops the local network. `zig build test` runs the same exchange in memory.
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 ([counter.did](counter.did)). The OCapN locator transport is `icp`, its designator the canister's principal.
- **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 the fetch's answer before it settles, so the handshake is one call and the fetch plus every send is another: about 2.6 s on mainnet, 0.6 s on icp-cli's local network.
- **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.
@@ -47,8 +69,8 @@ decr 1
## Known gaps
- **Seeds are not secret.** The canister derives its CapTP keys from its id and install time. Nothing in a canister is secret from its subnet's nodes anyway, but a real deployment would draw from `raw_rand`.
- **Upgrades forget everything.** Sessions and the count live in the Wasm heap, which a code upgrade discards; nothing uses stable memory.
- **One session per principal.** A second `start` from the same caller traps; the Node client mints a fresh identity per run.
- **The canister pays for every call** and admits any caller; it should refuse strangers before doing work (`canister_inspect_message`).
- **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.