Lesson 5 of 9 · 2 min 35 sec

Endpoints: Typed Messages, Refusals and Await

Chapters

  1. What crosses the boundary
  2. Who may call and what comes back
  3. Call another canister
Transcript
Download transcript

What crosses the boundary

Clients receive typed messages. The Shop declares an Item message with a price and a stock. It derives Serialize, Deserialize and Wire Type. The query returns an optional Item, and the build produces a matching client interface from these declarations. Two rules come with a message type. The order of the fields is the message format, so reordering them changes what clients must send and read. And integers have a fixed width, like u sixty-four and u thirty-two. A pointer-sized integer is not allowed in a message. The Shop has two Item types on purpose. One is the message. The other is the saved item inside Inventory. The service copies fields from the saved one into the message. Each can now change on its own schedule.

Who may call and what comes back

Access checks have two homes. A guard on the attribute runs before the arguments are decoded, and the SDK ships one for controllers. When the rule depends on saved data, the check goes inside the method. This endpoint from a larger app compares the caller with a saved owner and answers false. For an expected refusal, return false, None or an error before making any changes. This synchronous test method changes a counter and then traps. The Internet Computer rolls back that message's writes, so the counter is back where it was.

Call another canister

Roundtrip is an async update. It first records that a call has started. That write happens inside root with, and the scope ends on the same line. Nothing borrowed from the app is still held. Now the call leaves. With arg encodes the value as a typed message, and await suspends this method until the peer replies. Other calls can run in the meantime. The reply is decoded into a u sixty-four. A failed call or a bad reply becomes an error value that the method owns. After the reply, the method asks for write access again. This is a new scope. Whatever the first one read may have changed while the call was out, so the code reads again if it needs to. It records the outcome and returns the answer. A native test can model the two scopes as two separate calls. The real suspension, the real peer and the trap are checked on PocketIC, where roundtrip returns eleven and the counters match.