Lesson 2 of 8 · 2 min 34 sec

Who Owns and Who Calls

Chapters

  1. Owning is not calling
  2. Check everything, then write
  3. A call that leaves the canister
Transcript
Download transcript

Owning is not calling

The shelf is one module of several. Policy holds the rules. Members holds who is borrowing. Loans holds the loan book. Branches holds the shelves. Each has its own sealed memory, and all of it hangs from one root. This is the owning tree. It says where memory lives. Lending needs all of them at once. So we add Desk. Desk has public functions and code, and no memory of its own. It is not in the tree at all. Desk calls the public functions of Policy, Members, Branches and Loans. This is the second drawing, the call graph. It is wired once at startup, in the root. The two drawings are separate. Owning decides where memory lives and when it is removed. Calling decides who talks to whom.

Check everything, then write

A member borrows a drill. The public canister method checks who's allowed to make the request and supplies today's date. Then it opens a write frame on the root and calls Desk. Desk and the modules it calls share that frame. Desk asks before it changes anything. Policy allows three loans per member. Members says five-oh-one holds two. Branches has a drill on the shelf. Loans has room for another entry. Every check passes before the first write. Now the writes. Each one is a call to a public function, and each module changes only its own memory. The shelf gives up a drill. The member's count goes up. The loan book opens loan seven, due on day one-fourteen. The same member asks again. Desk asks Members first. Three is the limit. Desk stops there and answers with the reason. No write was made, so there is nothing to undo.

A call that leaves the canister

One feature leaves the canister. On day one-twenty, loan five is fifteen days late, and the fee is charged by a payments canister. First frame: Loans marks loan five as charging and hands back plain values. The frame closes. Then the call leaves, and we wait. When the answer returns, a new frame opens and Loans records the result. Nothing borrowed from the first frame is carried across the wait.