Files
captoys/README.md
T
2026-11-18 05:53:42 -06:00

55 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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.
## 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
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)"
```
The output shows each update call and each answer:
```
start 329B -> 345B in 300ms
session open
deliver 539B -> 725B in 271ms
incr 1
incr 2
decr 1
```
`npx --prefix client icp network stop` stops the local network. `zig build test` runs the same exchange in memory.
## 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.
- **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.
- **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
- **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`).
- **Third-party handoffs** need gift ids from entropy and dials to other peers; the canister refuses both.