Definition
A finite-state machine (FSM) describes a system using a finite set of states and rules, called transitions, for moving between them. In the deterministic protocol machine used here, a message and the current state determine what the device does next. For example, the device can accept a commit message after verification and reject the same message while data are still arriving. This protocol explicitly rejects inputs it cannot handle. Other machines may ignore an input or take an internal transition without waiting for another message.
The idea
A device receives a file in three kinds of message: BEGIN announces a transfer and its size, DATA carries a piece of the file, and COMMIT asks the device to install what it received. Each message can be perfectly well formed and still be wrong when it arrives: a COMMIT before all the pieces have arrived would install half a file. Whether a message is acceptable depends on what came before it.
One way to track that history is a set of yes-or-no flags, such as receiving, complete and verified. Three flags give combinations, and some of them mean nothing, such as verified set while complete is not; code that never considered such a combination behaves unpredictably in it. A finite-state machine names the meaningful stages instead, here Idle, Receiving, Complete, Verified and Committed, and lists for each stage exactly which inputs it accepts and where each one leads.
A board game is a fair picture: the square a token stands on decides which moves are legal next, and the path that led to that square no longer matters. The picture stops at one point. A real program also holds data beside its stage, such as the number of bytes received so far, and its rules test that data too. The stages are finite; the data beside them need not be.
A reset adds one more problem. The current state normally lives in a variable in RAM, and a reset restarts the program, which sets its variables afresh; a loss of power erases RAM altogether. After restarting, the program must work out its state again from what survived in persistent storage, such as flash memory §6.6, which keeps its content without power. That reconstruction is part of the machine’s design, not an afterthought.
How it works
To build a finite-state machine, write down five things.
States: the stages, each with a name and a meaning. For the transfer: Idle (no transfer under way), Receiving (a transfer has begun and some bytes have arrived), Complete (all announced bytes have arrived), Verified (the received file passed its checks) and Committed (the file has been installed).
Inputs: everything that can happen, not only what a correct peer sends. Here:
BEGIN,DATA,COMMIT, the result of a check, a dropped connection, a timeout §9.34 and a reset.The start state: Idle.
The transition table: for every pair of a state and an input, either the next state with its action, or “reject”. A pair with no entry is a case nobody designed. A machine with at most one transition for each pair is deterministic: the current state and the input fully decide what happens next.
Guards: each transition can carry a state transition guard §8.34, a condition on data that must also hold, such as “this piece starts exactly where the previous one ended and does not run past the announced size”.
Outputs can be attached to transitions, which gives a Mealy machine, or to states, which gives a Moore machine; the two forms can be converted into each other.
Then decide what each state means in persistent storage, and write a recovery function: code that runs at start-up, reads and checks the stored facts, and returns the state in which the machine resumes. For the transfer, one consistent rule is:
a valid commit record §8.21 (a small stored record that names the installed copy) that names a complete copy passing its integrity §1.9 check, a test that the stored bytes are undamaged and are the bytes intended, means Committed, with the new version in force;
anything else means Idle, with the old version in force; a partial or uncommitted copy is erased.
The recovery function derives the state from these facts. If the program also stores the state’s name, recovery treats that name as a summary and checks it against the facts, because a reset can fall between writing the facts and writing the name, and the name is then wrong.
Worked example
Take the five states and a file of bytes. Let be the number of bytes received so far. The table gives the response to each message in each state; “add” means store the piece and increase by its length.
| State | BEGIN |
DATA |
COMMIT |
|---|---|---|---|
| Idle | Receiving, | reject | reject |
| Receiving | reject | add; Complete if | reject |
| Complete | reject | reject | reject |
| Verified | reject | reject | install; Committed |
| Committed | Receiving, | reject | reject |
All cells are filled. Inputs that do not come from the peer complete the table: a passed check moves Complete to Verified, and a failed check, a dropped connection or a timeout before Committed moves the machine to Idle and erases the received bytes.
Run it.
In Idle,
BEGINannouncing 12 bytes moves to Receiving with .DATAwith 6 bytes at position 0 passes the guard: it starts at , and . Now , and since the state stays Receiving.COMMITarrives. The cell (Receiving,COMMIT) says reject, so nothing is installed. The message was well formed; the state made it wrong.DATAwith 6 bytes at position 6 passes the guard, since . Now , and the state becomes Complete.The check passes, and the state becomes Verified.
COMMITnow finds the cell (Verified,COMMIT): install, then Committed.
Now let power fail between steps 5 and 6. RAM said Verified. After the restart, recovery finds 12 stored bytes and no valid commit record, so by the rule above it resumes in Idle: the old version stays installed and the copy is erased. A design that wants to spare the peer a full resend could instead resume in Complete and run the check again. It must never resume in Verified, because the result of the check lived only in RAM.
Stated precisely
A deterministic finite-state machine is a tuple, an ordered list of four components, . Here is a finite set of states, (capital sigma) is a finite set of inputs, is the start state, and (delta) is the transition function, which maps a state and an input to the next state . For this protocol, an undefined means that input is rejected in state . A machine with outputs adds a set of outputs and a function that gives an output for each state and input (Mealy) or for each state (Moore). In the example, is undefined, while : one input, two results, decided by the state.
A state is reachable if some sequence of inputs leads to it from . A condition on states, such as “exactly one installed version is in force, and it passed its check”, is an invariant if it holds in every reachable state §3.21. It is proved by induction: show that it holds in , and that every defined transition from a state where it holds leads to a state where it holds.
A reset is a second way into the states. Let be the set of contents that persistent storage can hold after a reset at any moment, and let (rho) be the recovery function. The invariant must also hold in for every . A proof that covers only the transitions misses every state that recovery can produce.
Limits and common mistakes
A diagram of the expected path is not the machine. Dropped connections, timeouts, resets, repeated messages and messages out of order are inputs too, and each needs an entry in every state.
Finite states do not mean bounded resources. Receiving accepts DATA without end unless a guard limits the total to the announced size and the announced size to a maximum.
A finite-state machine cannot count without limit. It cannot, for example, check that brackets are balanced to any depth, because that needs memory that grows with the input. Programs add counters beside the states for such tasks, and the set of all combinations of state and counter values is then far too large to list by hand.
Combining machines multiplies their states. A device that runs one machine with 5 states and another with 4 can be in any of combined states, and the table of each machine alone does not show how the two interact.
The model assumes that one input is handled at a time. If two threads, or a thread and an interrupt handler §6.12, can change the state at once, the program can make transitions that the table forbids; the guard and the state change must form one indivisible step.
A stored state name is a summary, not evidence. If it disagrees with the facts it summarizes, recovery follows the facts, or chooses the state that keeps the old version in force, never the name alone.
Questions answered
Which persistent facts determine the state after reboot?
For a protocol that must survive power loss, recovery derives its state from facts that survive the loss and can be checked at boot. An ordinary RAM variable offers no such guarantee. Some hardware retains selected memory across a warm reset; a design that uses it must establish which resets preserve it and how to validate its contents. For an update or file-transfer protocol, the usual facts are:
whether a complete received copy exists and passes its integrity check;
whether a valid commit record names that copy as the installed one;
the version high-water mark §8.27, the highest version accepted so far, below which versions are refused;
whether a newly installed version has been confirmed as working, in a design that reverts unconfirmed versions;
recovery counters, such as the number of failed start attempts.
The recovery code reads and checks these facts and computes the state from them. A stored state name counts only when it agrees with the facts, because a reset between writing the facts and writing the name leaves the name wrong. A check whose result lived only in RAM must be run again.
Related entries: State transition guard (§8.34), Protocol state machine (§7.37), Security invariant (§1.23), Commit record and selector (§8.21), Reachability (§3.21).