shop.mjs runs the ERTP story step by step as the principal of a key file; connect.js holds the session setup it shares with counter.mjs. The client stages any number of string, integer, and reference arguments, and reports why an answer broke. A new start from a caller closes that caller's old session, so a client can come back with the same key. Generated-by: Claude Code (claude-opus-5-5)
106 lines
6.4 KiB
Markdown
106 lines
6.4 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.
|
||
|
||
## Alice buys a widget from Bob
|
||
|
||
[ertp.zig](src/ertp.zig) is a small [ERTP](https://docs.agoric.com/guides/ertp/): mints, issuers, purses, payments, and a shop that sells one unit of stock for an exact price. A sale moves the payment, the proceeds, and the stock in one Loam transaction. With the network and canister from above:
|
||
|
||
```sh
|
||
shop() { node client/shop.mjs http://127.0.0.1:4960 $ID "$@"; }
|
||
shop alice.key mint # moola: 10 into Alice's wallet; issuer on the board
|
||
shop bob.key open # widgets: 1 in stock; shop on the board at 3 moola
|
||
shop alice.key buy
|
||
shop alice.key buy # sold out: the payment goes back to the wallet
|
||
icp canister install counter --mode upgrade -y
|
||
shop alice.key balances
|
||
shop bob.key balances
|
||
```
|
||
|
||
```
|
||
bought 1 widget into closet
|
||
refunded 3 moola to wallet
|
||
Error: answer 5 broke: SoldOut
|
||
Canister counter installed successfully
|
||
wallet 7
|
||
closet 1
|
||
proceeds 3
|
||
```
|
||
|
||
(Update-call lines omitted. Alice's `balances` also reports `proceeds none`, and Bob's reports no wallet or closet: each home holds only its own.)
|
||
|
||
## 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("board")` answers a home every caller shares, where Alice and Bob post issuers and the shop.
|
||
- `fetch("makeCounter")`, `fetch("makeIssuerKit")`, and `fetch("makeShop")` answer public makers; each one's `make(...)` answers what it makes.
|
||
- 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 new `start` from a caller closes the caller's old session.
|
||
- **Not offer-safe.** A buyer hands over a payment and trusts the shop's code, which is right here because it's in the same vat; across vats this needs Zoe's escrow. The board is init-only, but anyone may post any name first.
|
||
- **No removal or collection.** A home holds 8 names and can't drop one; spent payments keep their records; 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.
|