5. Nodes, state and contexts

5.1 Node types and running

5.2 Node state

Taught in 4. First node

Node has
  set Heartbeat to 0
  set Known to {}
  set peers to {}

5.3 Receivers and start

Taught in 4. First node

Node code names its node with a receiver: a hole of a node type before the head of a definition, or after when.

define [n: Node] Version
  result ((n.Generation times 1000000) plus n.Heartbeat)

when [n: Node] (net.Received digest from sender)
  if (sender is not in n.peers)
    set n.peers to (lists.Append sender to n.peers)
define [n: Node] start
  set n.Generation to ((disk.Read "generation" on n or 0) plus 1)
  disk.Write n.Generation at "generation" on n
  time.Every 1s
    set n.Heartbeat to (n.Heartbeat plus 1)

5.4 The four contexts

Taught in 1. Hello, 4. First node, 11. Invariants, 12. Looks, 13. Story

Every statement runs in exactly one context. A context starts at an entry, and everything that code runs keeps it: nested blocks, after blocks, block arguments, and phrases without a receiver called from it.

Context Starts at Reads Writes May use
node start; when [n: Node] on node events; receiver phrases the receiver's state; identity of any node the receiver's state; locals net.Send, disk.*, after (simulated), emit … by n
main define main any node's state, through Node and p.Node values any node's state (setup and faults); locals deliver, after (simulated), emit … by sim.Nobody, sim.* setup and fault phrases, net.* configuration
check always any node's state locals only nothing with effects
look when on trace events exported state of any node, as it was after that trace event; a lower-case field is not readable, even of the same package, and neither is a lower-case field of a record (context) locals and shapes only draw, std/look, after (presentation)

Rules that follow:

5.5 Node events vs trace events

when takes an event phrase. Event phrases are native and come in two kinds:

A receiver on a trace-event when, or a node-event when without one, is a bad-receiver error.

5.6 Matching events

Taught in 5. Messages, 12. Looks

In when [n: Node] (net.Received Prepare from proposer):

5.7 Invariants

Taught in 11. Invariants

define Decisions [ns: list of Node]
  set values to {}
  each n in ns
    if (n.Decided is not in values)
      set values to (lists.Append n.Decided to values)
  result values

always ((lists.Size (Decisions (sim.NodesRunning paxos))) at most 1)

always takes a bool. It is evaluated after every simulation step (see spec/07) in check context. When it becomes false the trace records sim.Broken, once per transition from true to false; the run continues.