08-04

Chapter 08 · Messages in a world that loses them

Alternating bit

  • 45 min core
  • Core track
  • Lab: Trace Theater
  • Skills: hostile-env, counterexamples

00Recall

One card from 08-03 on what a set does to equal messages, one from 08-02 on why loss is free, and one from 03-02 on why traces are shortest. The last one decides how you read today's trace.

08-03-a from Queues versus sets

A sender puts the same value into a set network twice before either is received. What does the set hold?

Show answer

One copy. A set cannot hold an element twice, so the second send changes nothing; use IDs, a bag or a sequence if copies must count.

08-02-b from Loss and duplication for free

Why does adding an explicit Lose action never make an invariant about the sender and receiver fail?

Show answer

Any behavior with a lost message matches one where the message stays in the set and is never taken, with the same endpoint states. Loss matters for a property that mentions the network, and once fairness says messages must arrive.

03-02-b from Your first counterexample

Why are the course's TLC traces always shortest ones?

Show answer

The course runs TLC with one worker, so the search is strictly breadth-first and the first violation found is at the smallest depth.

Skip recall Recall is never required.

01Commit

You want to send messages 1, 2, 3, ... over a channel that loses and duplicates. The classic answer is stop-and-wait: send message k, resend it until an acknowledgement (ack) comes back, then move on. Resending creates copies, so the receiver must tell a new message from a copy of the last one. The alternating bit protocol tags each message with one bit, 0 or 1, flipped for every new message. The receiver delivers a message only if its bit is the one it expects next, then expects the other bit. It acks the bit it saw either way.

specs/AlternatingBit.tla runs this over two queues that keep order but lose and duplicate in place (a copy sits right behind its original). TLC finds no violation of InOrder (the application gets 1, 2, 3, ... with nothing skipped or repeated) in 93 distinct states with N = 3. specs/AlternatingBitOverSet.tla changes one thing: both networks are sets, as in 08-01 and 08-02.

Commit a

TLC checks InOrder on AlternatingBitOverSet with N = 2: same sender, same receiver, set networks. What will it report?

  1. No violation: the bit already handles lost and duplicated messages.
  2. No violation, because N = 2 is too small for the bit to come back to 0.
  3. A violation: the receiver delivers an old message a second time.

Answer A violation: the receiver delivers an old message a second time.

Commit b

How many steps (not states) does TLC's trace take?

Your answer is a whole number.

Answer 6

02Lab

Lab · Trace TheaterWhat you’re looking at: TLC's trace for InOrder over set networks. Focus on dataNet and rbit: which message does the last Receive take, and which bit does the receiver expect?

InOrder violated

● 1● 2● 3● 4● 5● 6● 7

This is a teaching view. Real checking happens with TLC on your machine.

Trace

  1. State 1 Initial state ackNet = {} next = 1 dataNet = {} sbit = 0 rbit = 0 delivered = <<>>
  2. State 2 Send dataNet {} becomes {<<1, 0>>}
  3. State 3 Receive(<<1, 0>>) ackNet {} becomes {0} rbit 0 becomes 1 delivered <<>> becomes <<1>>
  4. State 4 GetAck(0) next 1 becomes 2 sbit 0 becomes 1
  5. State 5 Send dataNet {<<1, 0>>} becomes {<<1, 0>>, <<2, 1>>}
  6. State 6 Receive(<<2, 1>>) ackNet {0} becomes {0, 1} rbit 1 becomes 0 delivered <<1>> becomes <<1, 2>>
  7. State 7 Receive(<<1, 0>>) rbit 0 becomes 1 delivered <<1, 2>> becomes <<1, 2, 1>>
Text view: every state and step
States and their successors
StatenextsbitrbitdelivereddataNetackNetSteps (action → state)
initial 1100<<>>{}{}Send → 2
2100<<>>{<<1, 0>>}{}Send → self; Receive(<<1, 0>>) → 3
3101<<1>>{<<1, 0>>}{0}Send → self; Receive(<<1, 0>>) → self; GetAck(0) → 4
4211<<1>>{<<1, 0>>}{0}Send → 5; Receive(<<1, 0>>) → self; GetAck(0) → self
5211<<1>>{<<1, 0>>, <<2, 1>>}{0}Send → self; Receive(<<1, 0>>) → self; Receive(<<2, 1>>) → 6; GetAck(0) → self
6210<<1, 2>>{<<1, 0>>, <<2, 1>>}{0, 1}Send → self; Receive(<<1, 0>>) → 7
7211<<1, 2, 1>>{<<1, 0>>, <<2, 1>>}{0, 1}

Before the last card, say what delivered should become. Then look at what the receiver delivers, and find where that message entered the network.

Or in the terminal:

shell
./tla check chapters/08-messages/specs/AlternatingBitOverSet.cfg
Expected output 08-messages/specs/AlternatingBitOverSet
08-messages/specs/AlternatingBitOverSet  AlternatingBitOverSet.cfg  (N=2)
parse ok
model check: invariant InOrder violated; trace has 7 states (6 steps); 7 distinct states
expected outcome met: violation InOrder
trace (changed variables only; ./tla trace --full shows all):
  1  Init     next = 1, sbit = 0, rbit = 0, delivered = <<>>, dataNet = {}, ackNet = {}
  2  Send     dataNet: {} -> {<<1, 0>>}
  3  Receive  rbit: 0 -> 1, delivered: <<>> -> <<1>>, ackNet: {} -> {0}
  4  GetAck   next: 1 -> 2, sbit: 0 -> 1
  5  Send     dataNet: {<<1, 0>>} -> {<<1, 0>>, <<2, 1>>}
  6  Receive  rbit: 1 -> 0, delivered: <<1>> -> <<1, 2>>, ackNet: {0} -> {0, 1}
  7  Receive  rbit: 0 -> 1, delivered: <<1, 2>> -> <<1, 2, 1>>

Reveal for commit a

InOrder is violated: delivered ends as <<1, 2, 1>>. Message 1 was sent with bit 0 and delivered; message 2 was sent with bit 1 and delivered. The receiver then expects bit 0 again. The set still holds <<1, 0>>, the very first message, and the receiver takes it as new data.

Reveal for commit b

The trace has 6 steps and 7 states. The last step takes a message that has been in the set since the first step.

03Reason

If you picked "no violation", you knew what the alternating bit is for: it survives loss and duplication. That is true. It survives them in order. Here is the argument the protocol rests on, so you can see where order enters.

Over a queue that keeps order, think about what can be in the data channel while the sender is on message k. The sender sends message k only after an ack for message k − 1, and the receiver acks k − 1 only after it delivered k − 1. So the channel holds copies of message k, and possibly older copies of k − 1 queued in front of them. Nothing older: everything before k − 1 left the queue ahead of those copies. Two candidates, two bits.

The same argument applies to the ack channel. Over a queue, the sender only ever sees acks for k − 1 and k, so a stale ack cannot move it on. The protocol needs both channels to keep order, not only the data channel.

Write something before openingwhere exactly does the argument above use the fact that the channel keeps order?

In "nothing older". In a queue, a copy of message k − 2 was in front of every copy of k − 1, so it was received or lost before the receiver saw k − 1. In a set, nothing is in front of anything. A copy of message 1 can wait while messages 2, 3 and 4 go through, then arrive when its bit happens to match.

Here is the trace over sets:

  1. State 1 Initial state next = 1 sbit = 0 rbit = 0 delivered = <<>> dataNet = {} ackNet = {}
  2. State 2 Send dataNet {} becomes {<<1, 0>>}
  3. State 3 Receive rbit 0 becomes 1 delivered <<>> becomes <<1>> ackNet {} becomes {0}
  4. State 4 GetAck next 1 becomes 2 sbit 0 becomes 1
  5. State 5 Send dataNet {<<1, 0>>} becomes {<<1, 0>>, <<2, 1>>}
  6. State 6 Receive rbit 1 becomes 0 delivered <<1>> becomes <<1, 2>> ackNet {0} becomes {0, 1}
  7. State 7 Receive rbit 0 becomes 1 delivered <<1, 2>> becomes <<1, 2, 1>>

Notice the last step. ackNet does not change, because 0 is already in it, and dataNet does not change, because nothing is ever removed. Only delivered and rbit move. One step, and the application holds message 1 twice.

What would fix it? The receiver needs to tell message 1 from message 3, which one bit cannot do.

Write something before openingevery message already carries its number. What should the receiver compare that number with? And what should travel back to the sender, so that a stale ack cannot fool it?

Something that never repeats. The receiver knows how much it has delivered, so it knows which number it needs next, and anything else is an old copy. The sender has the same problem in the other direction: an ack that carries only a bit can be a stale one. The Core exercise has you write both halves, and its hints say what happens if you repair only one.

Numbers work over sets, and they cost a counter instead of a bit, which on a real wire means more header bytes and a counter that must not wrap while old copies can still arrive.

Write something before openingthe queue model has Cap = 2. Is a queue that holds at most two items an honest model of a network?

It is a bound, and every bound is a choice you must defend. With Cap = 2, a resend or a duplicate can sit behind the original, which is the situation the bit exists for. A larger Cap would let more copies pile up. In a queue they are all copies of the current or the previous message, so the argument above says nothing new can go wrong; TLC checked only Cap = 2. If a real network can delay a packet arbitrarily, that is reordering, and the right model is the set.

Write something before openingin the Warm-up's trace, the sender that forgets to flip its bit reaches message 3 while delivered is still <<1>>. Which invariant notices, InOrder or NoSkip, and why not the other?

NoSkip. The sender moves to message 2 still holding bit 0, so a duplicate of the ack for message 1 reads as the ack for message 2, and it moves to message 3. Message 2 was never sent. delivered stays <<1>>, which is still in order: InOrder is happy. Only an invariant that relates the sender's progress to the receiver's notices. A protocol needs a property for each side's beliefs, not only for the output.

The 1969 paper that introduced the alternating bit addressed a single half-duplex data link, where messages cannot overtake each other. On a link that cannot reorder, it is correct and cheap. Over IP with retries across connections, assume it is not.

The same idea is in TCP. TCP numbers every byte, and its receiver hands bytes to the application only in order, holding back early arrivals until the gap is filled, so it survives reordering inside the network. Its numbers must not wrap while an old segment can still arrive. The idea is the one you write in the Core exercise: when copies can arrive late, a tag must be able to tell any old message from new data, and one bit can only tell two apart.

04In your editor

The specs are chapters/08-messages/specs/AlternatingBit.tla and AlternatingBitOverSet.tla; they differ in the two network variables and the actions that use them. Check this lesson's exercises with:

shell
./tla check 08-04

The queue version prints:

Output 08-messages/specs/AlternatingBit
08-messages/specs/AlternatingBit  AlternatingBit.cfg  (N=3, Cap=2)
parse ok
model check: no violation found in 93 distinct states (428 generated, depth 14)
expected outcome met: pass
coverage: all 8 actions fired
witness AllDelivered reached (trace of 9 states)
witness AllAcked reached (trace of 10 states)

05Practice

Warm-up. The sender moves on to the next message but forgets to flip its bit. Complete the step, and read the trace of what the broken version does first.

Core. Repair the protocol over set networks by tagging messages and acks with numbers instead of trusting the bit.

08-04-ex1Warm-up5 min

08-04-ex1 · Flip the bit

Tier: warm-up. Time: about 5 minutes.

AlternatingBit.tla with one line of GetAck wrong: when the current message is acknowledged, the sender moves on but keeps its bit.

  1. Run ./tla check 08-04-ex1 and read the trace to the end. Which invariant fails, and how did the sender get ahead of the receiver?
  2. Fill gap g1: the next message goes out with the other bit. The sender may read only its own variables (the comments mark them) and the acks it receives; the receiver's bit is not its to read. The check cannot see which variables an action reads: in this model, reading the receiver's bit reaches the same states as the right answer and passes, but it is not an answer, because a real sender cannot see the receiver's memory.
  3. Run the check until it passes with both witnesses, AllDelivered and AllAcked, and every action firing, including every lose and duplicate action.

In the starter's trace, find the duplicated acknowledgement. Would the bug still show up if the channel never duplicated? Answer before you look at hint 1.

Hints: HINTS.md, under "08-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/04-ex1-flip-the-bit/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 08-04-ex1 prints "goal met: pass".

./tla check 08-04-ex1

Repo chapters/08-messages/exercises/04-ex1-flip-the-bit/

Hints

Hint 1

Read the starter's trace: after the first message is acked, which bit does the second one carry, and which bit does the receiver expect? The duplicated acknowledgement is one way to hand the sender a second ack with the old bit. Ask what the receiver sends back when the second message arrives with that bit.

Hint 2

The sender's bit must change exactly when it moves on to the next message. The receiver flips rbit the same way when it delivers.

Hint 3 (nearly the answer)

Write sbit' as the other bit. The receiver's Receive has the same expression for rbit'.

08-04-ex2Core8 min

08-04-ex2 · Numbers, not bits

Tier: core. Time: about 8 minutes.

The alternating bit fails over set networks: a stale copy of message 1 arrives when the receiver expects its bit again. Repair it with numbers. Each message already carries its number as its data.

  1. Run ./tla check 08-04-ex2 and read the trace. With N = 3, message 3 carries the same bit as message 1, so after message 2 a stale copy of message 1 looks like message 3.
  2. Gap g1, in Receive: the bit cannot tell message 1 from message 3, but the message's number can. Decide what the receiver should compare it with, and what it should send back.
  3. Gap g2, in GetAck: once the receiver acknowledges numbers, what must an acknowledgement carry before the sender may move on? In both gaps, each side may read only its own variables (the comments mark them) and the network it receives from. The check cannot see which variables an action reads: a sender that consults the receiver's delivered reaches the same states as the right answer and passes, but it is not an answer, because a real sender cannot see the receiver's memory.
  4. Run the check until it passes with both witnesses, AllDelivered and AllAcked.

Before you write g2, predict what the sender does if the receiver acknowledges numbers but the sender still compares them with its bit.

Hints: HINTS.md, under "08-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/04-ex2-numbers-not-bits/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 08-04-ex2 prints "goal met: pass".

./tla check 08-04-ex2

Repo chapters/08-messages/exercises/04-ex2-numbers-not-bits/

Hints

Hint 1

Every message carries its number as its data, m[1]. What number does the receiver need next, given what it has delivered so far?

Hint 2

In g1, test m[1] instead of m[2], and put m[1] into ackNet. In g2, the sender compares an acknowledgement with the number of the message it is sending. If only the receiver changes, the sender compares numbers with its bit and never moves past message 1.

Hint 3 (nearly the answer)

g1: deliver when m[1] is one more than the length of delivered; acknowledge m[1]. g2: move on when b equals next. Keep the next <= N part of the test.

06Explain it

Open NOTES.md and answer the three prompts under 08-04, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "08-04 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "The receiver delivers a message only if its number is the next one it needs and acks the number; the sender moves on only for an ack with its current number. Stale copies carry old numbers, so they are acked and dropped, which works over a network that reorders. It assumes numbers never wrap while an old copy can still arrive. TLC checked N = 3 over set networks for safety only; it did not check that every message is eventually delivered."

A weak answer: "Numbers are better than bits, so it works now." It does not say why the bit failed, where order was assumed, or what the numbers themselves assume.

Done?