The distributed consensus problem: Each agent in a system proposes a value; the problem is to develop an algorithm by which all agents agree on a single value chosen among the proposed values. The algorithm has the following requirements:
Achieving consensus is the key to many distributed algorithms including those that manage cryptocurrency. Without consensus, an agent may buy multiple items with the same coin. Consensus requires that all agents agree on who owns a coin in each state; all agents agree that an agent X has the coin in a state, and after X uses that coin to buy an item from agent Y, then all agents agree that Y has the coin.
The FLP theorem Fischer, Lynch and Paterson (FLP) proved that there is no deterministic algorithm that solves distributed consensus in every execution, if even one agent may halt.
In later classes we will study two consensus algorithms: the Paxos algorithm and blockchain algorithms. Paxos guarantees agreement and validity, but not termination. Blockchain algorithms do not guarantee agreement. These algorithms work for most practical situations.
A fully connected network The network has N agents. Each agent has a N inboxes and N outboxes. Each outbox is connected to exactly one inbox. So, for any pair of agents X, and Y: X has exactly one outbox connected to exactly one of Y's inboxes, and that inbox of Y is not connected to any other outbox of any agent.
The agents are more powerful than in the models that we used earlier in the course in the sense that an agent can process messages from any of its inboxes. For simplicity, assume that an agent reads a null message from an empty inbox. So, in every state, every agent can read from any of its nonempty inboxes.
The agents are more powerful in another way: an agent can change its state without receiving any message. This state change is represented by the agent receiving a null message. We can think of a null message as a message that is always in the agent's connection to itself.
The fairness requirement If message m is in inbox w of agent p in state S, then in all infinite computations starting in S, p eventually gets m from w.
Event An event e is an agent p getting a message m from the front of one of its (nonempty) inboxes, w. It can be specified by the trip let (p, w, m). The state after event e is executed in a state S is denoted by e(S). A computation is a sequence of events, and the state after executing a computation c in a state S is denoted by c(S). A computation consisting of c followed by an event e is denoted by c;e, and the state after executing c;e in a state S is denoted by (c;e)(S). The fairness requirement is that if an event e is enabled in a state S, then e occurs in all infinite computations starting in S.
The proof of the FLP theorem is more difficult with the more powerful agents. The theorem for the more general model applies to our model as well.
Associated with each agent Y is a boolean variable, Y.finished, which signals whether Y has finished computing the consensus value. Initially Y.finished is false, and after Y computes the consensus value Y.finished becomes true and remains true thereafter.
Associated with each agent Y is a constant, Y.proposal, which is the value that Y proposes to the other agents as a consensus value. The simplest case is where Y.proposal can take on only two values, say 0 and 1, or "red" and "blue". Each agent Y has a variable, Y.consensus, which is the consensus value that Y computes. The three parts of the specification are:
A simple algorithm is as follows. Let N be the number of agents. Each agent sends a message containing its id and its proposal to all agents. After receiving proposals from all other agents, an agent Y sets Y.finished to true, and sets Y.consensus to one of the proposals chosen by a function of the proposals. Examples of functions are:
The proof that the algorithm is correct is straightforward.
In the UNITY model of distributed systems, each component of the system (agents, outboxes, connections) executes its actions infinitely often. We modify the UNITY model slightly to produce a fault model that allows agents to halt. An agent halts at a point in a computation by executing no further actions. So, an agent that halts executes its action a finite number of times, whereas agents that don't halt execute their actions infinitely often. The specification is that the agents that do not halt satisfy the requirements of termination, validity and agreement. There are no requirements of the agent that halts.
Let's look at the two algorithms for consensus for the no-fault case and determine if they will work for the case where an agent may halt.
Consider a computation in which the agent with the highest id halts without sending messages. Let's look at a system with N agents where Z is the agent with the highest id.
An algorithm in which an agent waits to receive messages from all other N-1 agents will not terminate because the message from Z never arrives. So, this algorithm violates the requirement of termination.
An algorithm that does terminate is as follows. Each agent determines its consensus after receiving proposals from N−2 other agents, so each agent can determine its consensus without ever hearing from Z. This algorithm terminates. Does it satisfy the agreement requirement?
Consider a computation in which all agents other than Z propose red while Z proposes blue. An agent that receives proposals from all the agents other than Z determines that the consensus is red, while an agent that receives Z's proposal, and then receives other proposals, determines that the consensus is blue.
Look at an example of 1001 agents, and let us call one of the agents Z. 500 of the agents other than Z propose red while the other 500 propose blue. So Z's single proposal decides the majority: with it the vote is 501 to 500 one way or the other, and without it the vote is tied. If the agents wait for Z for ever then the algorithm does not terminate.
An algorithm that terminates is one in which agents determine consensus after receiving proposals from 1000 agents. Assume that agents that receive an equal number of red and blue proposals determine that the consensus is red. Consider a computation in which an agent X has received no message from Z and has received 500 red and 500 blue proposals. So X determines the consensus to be red. In the same computation, an agent Y receives a proposal from Z, and if that proposal is blue then Y makes its decision based on 499 red and 501 blue proposals. So Y determines that the consensus is blue. This algorithm violates the agreement requirement.
Assume that proposals are either 0 or 1. Assume that initially each agent has one message in its inbox which is that agent's proposal.
Valence Associated with each state of the system is a valence which can be 0, 1, or bi. The valence of a state is 0 exactly when all computations that start in that state terminate with all non-halted agents reaching a consensus value of 0. Likewise, the valence of a state is 1 exactly when all computations that start in that state terminate with all non-halted agents reaching a consensus value of 1. So, the valence of a state, S, is bi exactly when some computation from S reaches a consensus of 0, and some computation from S reaches a consensus of 1.
Proof Proof by contradiction. Suppose no initial state is bivalent. Then every initial state is either 0-valent or 1-valent. There is no need for consensus if all initial states are 0-valent because then validity guarantees that the only consensus is 0. Similarly, we don't consider the case where all initial states are 1-valent. Now, we deal with the case where there exists at least one 0-valent and at least one 1-valent initial state.
Sublemma There exist initial states X and Y, where X is 0-valent and Y is 1-valent, and where X and Y differ from each other by the input of exactly one agent.
Proof of sublemma Let U be any 0-valent initial state and V be any 1-valent initial state. Construct a sequence of distinct initial states S[0], S[1], S[2],..., S[n] as follows. S[0] is U and the difference between S[i] and V decreases for increasing i. For all i: if S[i] is not V then obtain S[i+1] as follows. Change the input of any one agent b of S[i] where the input of b is different in S[i] and V. If S[i] is 0-valent and S[i+1] is 1-valent then the result holds with X = S[i] and Y=S[i+1]. There exists such an i because V is 1-valent. This proves the sublemma.
Back to the lemma which states: There exists an initial bivalent state. Let X and Y be a 0-valent and 1-valent initial states, where X and Y differ only by the input of agent b.
Consider any computation C_0 starting in state X where agent u executes no steps (see top diagram in figure below where agent u has proposal 0 in state X -- the initial state of the computation). We can construct an identical computation C_1 starting in state Y where agent u executes no steps (see lower diagram in figure below where agent u has proposal 1 in state Y). If computation C_0 terminates with all agents agreeing on a consensus value 0 then the identical computation C_1 also terminates with all agents agreeing on a consensus value of 0. This contradicts the assumption that all computations from Y terminate with consensus value 1. Therefore X and Y are bivalent states. This completes the proof of the lemma that there exists an initial bivalent state.
The lemma For every bivalent state, S, and every event e, there is a computation, c, in which p executes no action, such that e(c(S)) is bivalent.
Let R be the set of states that are reachable from S by executing a computation c in which agent p takes no action. Let e be the event: p gets a message from inbox w. Let m be the message at the front of inbox w in state S. Because p takes no action in c, it follows that message m is in the front of inbox w of p in all states in R. Therefore, p can read m from w in all states in R. The lemma says that there exists a state S* in R such that if p gets a message (m) from inbox w in S* the next state S' is bivalent.
We will prove that the following assumption leads to a contradiction.
Assumption that leads to a contradiction For all S* in R, e(S*) is not bivalent.
The assumption is equivalent to: for all S* in R, e(S*) is 0-valent or 1-valent. State S is in R, and from the assumption e(S) is 0-valent or 1-valent. Assume without loss of generality that e(S) is 0-valent. We will prove (next lemma) that there exists a state S* in R such that e(S*) is 1-valent.
Sublemma 1 There exists a state S* in R such that e(S*) is 1-valent.
Proof of sublemma 1 Because S is bivalent, there exists a computation, c*, such that c*(S) is 1-valent. Consider two cases: (1) p executes no action in c* and (2) p executes an action in c* - these actions are reading inboxes other than w because c* is a computation in which p does not read inbox w.
Case 1 If p executes no action in c* then c*(S) is in R. Because c*(s) is 1-valent, e(c*(S)) is 1-valent. Therefore the sublemma holds with S* set to c*(S).
Case 2 Every infinite computation starting in state S has e eventually. Let c* be a computation [c_pre; e; c_post], where event e does not appear in c_pre. It is possible that agent p executes actions in c_pre however it does not read inbox w in c_pre. c_post is arbitrary.
c*(S) = c_post(e(c_pre(S)))
Because c*(S) is 1-valent, e(c_pre(S)) is not 0-valent. From the assumption, e(c_pre(S)) is not bivalent. Therefore e(c_pre(S)) is 1-valent. So the lemma holds with S* set to c_pre(S). This completes proof of the sublemma.
Sublemma 2 There exists a state E and event e' such that E is reachable from S, and e(E) is 0-valent and e(e'(E)) is 1-valent.
Proof of sublemma 2 From the previous sublemma, there exists an S* such that e(S*) is 1-valent. We assumed e(S) is 0-valent. State S* is reached from S by a computation c that does not include event e. Because e(S) is 0-valent and e(c(S)) is 1-valent, there is some k such that for the first k states X along computation c, e(X) is 0-valent, and for the remaining states Y in c, e(Y) is 1-valent. Let e' be the event that takes the last state X to the first state Y. Then e(E) is 0-valent and e(e'(E)) is 1-valent. This proves sublemma 2.
Now we prove the critical event lemma. Here is the theorem again: For every bivalent state, S, and every event e, there is a computation, c, in which p executes no action, such that e(c(S)) is bivalent.
From sublemma 2, there exists a state E and event e′ such that E is reachable from S, and e(E) is 0-valent and e(e′(E)) is 1-valent. Consider two cases: (1) e′ is an event at an agent other than p, and (2) e′ is an event at p.
Case 1 e′ is an event at an agent other than p. Events at different agents can be executed in arbitrary order. So e′(e(E)) = e(e′(E)). We have a contradiction: e(e′(E)) is 1-valent and e′ is 0-valent.
Case 2 e′ is an action by p. Consider a computation c* from S to E after which agent p halts. All agents other than p decide on a consensus value in a finite number of steps. Let the consensus value be v.
Because c* has no actions by p, the events in c* are independent of actions by p. Therefore, there exist computations [e, c*] and [e′ ; e ; c*] from E. e(E) is 0-valent and so c*(e(E)) is also 0-valent, and therefore the consensus value v is 0. However e′(E) is 1-valent, and therefore c*(e(e′(E))) is also 1-valent, and therefore the consensus value is 1, a contradiction. Therefore, the lemma holds.
The first lemma gives a bivalent initial state. The second says that any message waiting to be received can be received without the system ceasing to be bivalent. Together they build a computation that never decides.
Construct it in stages. Begin in a bivalent initial state. At each stage, take the message that has been waiting in an inbox longest, and apply the critical event lemma to it: there is a computation, ending with the receipt of that message, that ends in a bivalent state. Append that computation and begin the next stage.
The result is an infinite computation in which every agent that has not halted executes its actions infinitely often, and every message sent is eventually received — because a message that is waiting becomes, after finitely many stages, the message that has been waiting longest. So it is a legal computation of the system. And no agent ever decides in it, because the state after every stage is bivalent, and a state in which an agent has decided is not.
So the algorithm we assumed to exist has a legal computation in which no agent decides, which violates termination. There is no such algorithm.