We discussed distributed consensus in the modules on
Paxos and
Byzantine Generals with
written and
oral messages.
The specification of consensus is weaker in
Bitcoin and the algorithm used to obtain consensus is different
from those used in Paxos and Byzantine Generals written and oral
algorithms. The Byzantine Generals algorithms assumes that the
algorithm operates in rounds, whereas the Bitcoin algorithm
doesn't require synchronous rounds in which all agents
participate. The Paxos algorithm does not guarantee progress
whereas Bitcoin requires progress with high
probability; you wouldn't want to use coins if you had to wait
a long time to buy anything.
No trusted agent
In an earlier module we described an algorithm that had many of
the features that we expect from a
cryptocurrency. That algorithm had, however, a
characteristic which is problematic to some: It relies on a
trusted agent.
The trusted agent could be the
Federal Reserve in the
US or a
central bank that manages a currency.
Two of many reasons given for mistrusting banks are that (1) people may
want to execute transactions in secret giving only their public
keys, and (2) central banks may be able to print money whereas
some cryptocurrencies, such as Bitcoin, limit the total amount of
coins that can ever exist.
The Bitcoin algorithm is a modification that eliminates the
trusted agent from the algorithm given in the previous module.
No assumptions about numbers of agents
The Byzantine Generals algorithm uses an upper bound on the number
of faulty agents. Paxos assumes that the total number of agents is known.
The Bitcoin algorithm makes no assumptions about numbers of faulty and
non-faulty agents other than that there are a large number of agents.
Incentives and transaction fees
When a currency is managed by a single trusted agent, such as a
bank, we assume that the bank gets some reward for its
service or is paid by a government to carry out this service.
The Bitcoin algorithm pays agents with Bitcoins for checking the validity of
transactions. This payment consists of new coins that are
"mined". More about mining later
A First Proposal for an Algorithm
How can we modify the algorithm we described earlier so that it works
without a trusted agent?
Let's try this modification:
Select random agents to play the role of the trusted manager.
A step of the algorithm is as follows:
A single agent is
chosen randomly to play the role of the trusted manager. This agent
receives and validates transactions, gathers some of the transactions
into a block, appends the block to the block chain, and
broadcasts the updated block chain. The
other agents update their copies of the block chain when they receive this
value. The system waits for all agents to update their copies and then
executes the next step.
Challenges of the Proposed Algorithm
This algorithm has several challenges.
Selecting a single agent.
How can the collection of agents select a single agent to add
a block to the block chain? The selection of a single agent requires
all agents to reach a consensus about which agent to
select. So solving the problem this way would require solving
another consensus problem. The Bitcoin algorithm does
not select a unique agent to add a block to the block chain;
however, it uses an ingenious mechanism to ensure that multiple agents
don't attempt to add blocks at about the same time.
Synchronization:
How can the collection of agents wait long enough to ensure that all
agents have updated their copies of the ledger to the most recent version before
the ledger is modified again?
Consider the following example scenario. All agents have the same
copy x of the ledger at some point t. Agent B, selected randomly to
act as the trusted agent, appends transaction y to the ledger at a
later point t' at which point B's copy is [x, y]. Then, agent C,
selected randomly to act as the trusted agent, appends transaction z
to the ledger at a later point t''. If C's copy is still [x],
because it has not as yet been updated to [x, y], then after C
appends z to the ledger, C's copy becomes [x, z].
A key property of the algorithm with the trusted agent is that two
copies of the ledger are either identical or the longer copy
consists of additional transactions appended to the
shorter copy.
With this property, two copies of the ledger are
either identical or the shorter copy can eventually "catch up" to the longer copy by
merely by appending more values.
In the example, B may receive
information about transaction z after receiving information about
transaction y, whereas C may receive this information in the reverse
order, which leaves B's copy as [x, y, z] and C's copy as [x, z,
y]. In this case, the copies remain different forever.
The Bitcoin algorithm does not guarantee synchronization; however,
its mechanism helps to make many agents append blocks in the same
order.
Incentives:
Why should an agent chosen to play the role of trusted agent agree
to play that role? What's the incentive? Why wouldn't the trusted
agent do nothing at all execute its step slowly?
Untrustworthy Agents:
The randomly-chosen trusted manager may not be trustworthy. You can
imagine what may go wrong in the previous algorithm if the bank was
dishonest.
Next, let's look at how the Bitcoin algorithm addresses challenges 1
and 2. We'll look at challenges 3 and 4 later.
Selecting a Single Random Agent
How can an arbitrary set of agents, some of whom may be
malicious, and where the size of the set is unknown, pick a random
agent?
Using Puzzles to Select a Single Agent
Let's look at a simple situation:
several people solve puzzles in the same room. When a
person solves her puzzle, she yells "I won!". When a
person hears that somebody else has won she stops solving her puzzle.
If everybody starts at the same instant and take the same time
then there will be collisions --- many will claim to win at the same time.
If, however, the time to solve
a puzzle is a random variable with a flat distribution then collisions
are unlikely. Bitcoin uses the puzzle-friendly property of cryptographic hash
functions discussed in the
previous module.
Now, instead of people being all in the same room, assume that they
are competing across a network. When a person solves a puzzle,
she broadcasts a "I won" message. When a person working on a puzzle
gets a "I won" message from somebody else, she stops working on her
puzzle.
Collisions are likely if the expected time to solve the puzzle
is small (say a millisecond) compared with the expected time (say a
minute) for a message broadcast by one person to reach others. Message
delays may cause multiple people to solve their puzzles before
receiving "I won" messages.
Collisions are unlikely when times to solve puzzles are much greater
than message delays.
We could attempt to use timestamps: When a person solves her puzzle
she broadcasts a "I won" message and the time at which she finished
solving the puzzle. If a person gets a "I won" message with an earlier
timestamp then she concedes. This approach is problematic because a
devious agent may not solve the puzzle, or may set her timestamp to an
earlier value.
Puzzles in Bitcoin
Next let's look at the puzzles used in Bitcoin.
Each agent has its own copy of the block chain.
An agent \(A\) collects a set \(trans\) of transactions that haven't as yet been added
to \(A\)'s copy of the block chain.
Agent \(A\) proposes
to append a block to the chain where the block
consists
of the set \(trans\) in the
following way.
Let \(ptr\) be the pointer to agent \(A\)'s copy of the block chain.
Agent \(A\) can add a block containing \(trans\) to the chain only if it
proves that it has solved the following problem --- the "puzzle."
Find a number, called \(nonce\), such that:
\(H(nonce + ptr + trans) < target \)
where \(+\) is the concatenation operator , and target is a
given value.
For the time being
assume that target is a constant; later, we'll see that it decreases very
slowly over time.
The smaller the value of target the
greater the expected time to solve the puzzle.
The time to solve a Bitcoin puzzle is a
random variable with a flat distribution. Each proposer
of a block is probably solving a different puzzle because the block of
transactions that it is aggregating is likely to be different from
that of other proposers. Agents have different amounts of
computing capacity, and the time to solve a puzzle decreases with
capacity.
Agents are unlikely to start solving their puzzles
at the same instant. For these reasons, it is possible, but unlikely, that many agents
will solve their puzzles at the same time.
Using puzzles to identify a single random agent leaves us with at least three challenges: (1) Collisions
will occur; (2) agents may be devious --- they may claim to have
solved puzzles when they haven't; and (3) agents with computing power
that far exceeds those of others will solve their puzzles faster than
others do --- and so though agents are selected randomly, those with
large computing power are likely to be selected more often.
Attempts at Synchronization
When an agent \(A\) appends a new block \(B\) to a block chain \(L\) it broadcasts
the new chain \(L + B\). This is analogous to a person shouting "I won" in the
example given earlier.
When a (non-devious) agent \(A'\), which proposes to extend chain
\(L\), gets a message saying that \(L\) has already been extended to
\(L + B\) then \(A'\)
stops attempting to append a block to \(L\).
Instead, \(A\) starts again with a
new set of transactions that it proposes to append to the
extended chain \(L + B\).
If the message delay between agents \(A\) and \(A'\) is small compared
to the time to solve puzzles, then a collision
between \(A\) and \(A'\) is unlikely, but still possible. So, it is
possible that \(A\) broadcasts \(L + B\) while \(A'\) broadcasts
\(L + B'\) at about the same time. So different agents may have
different copies of the block chain.
What is the equivalent of the "true" system-wide
block chain when different agents have different copies?
The Bitcoin algorithm does not use synchrony to deal with this
issue. The problem of different copies of the block chain
extant at the same time is handled in an ingenious asynchronous way that we
describe later.
Managing Concurrent Updates
A key step of the Bitcoin algorithm that updates local copies of block
chains is as follows. After an agent creates a block and appends the
newly created block to its local copy it broadcasts its copy of
the block chain.
When an agent \(A\) gets a message containing a copy
of another agent's block chain, agent \(A\) sets its local copy to the
block chain in the message if and only if the length of the
block chain in the message exceeds the length of \(A\)'s local copy.
Let's look at a scenario.
For this scenario, \(X\) and \(Y\) are single blocks.
Assume that agent \(A\)'s copy of the block chain
is \(L\) when \(A\) receives a message containing the block chain \(L
+ X\). Because the length of the block chain \(L + X\) is bigger than \(A\)'s
copy, \(A\) sets its copy to \(L + X\).
Now suppose agent \(A\) gets a
message containing the block chain \(L + Y\); what does \(A\) do?
Agent \(A\) ignores the message because the length of \(L + Y\) does not exceed
that of \(A\)'s current copy, \(L + X\).
Continuing Collisions
Let's continue the above scenario.
Can block \(Y\) become part of \(A\)'s
chain, or will it remain forever an "orphan" block
as far as \(A\) is concerned?
Here's a possible scenario.
An agent with a block
chain copy \(L +
X\), solves its puzzle and appends a block \(X_{1}\) to get a new copy
\(L + X + X_{1}\) of the block chain which the agent broadcasts.
At the same time, another agent with a block
chain copy \(L +
Y\), solves its puzzle and appends a block \(Y_{1}\) to get a new copy
\(L + Y + Y_{1}\) which is broadcast.
If \(A\) receives \(L + Y + Y_{1}\) before receiving \(L + X + X_{1}\)
then \(A\) will set its chain to \(L + Y + Y_{1}\), and then reject
\(L + X + X_{1}\).
You can construct a scenario with a sequence of collisions between
agents appending blocks \(X_{i}\) and other agents appending blocks
\(Y_{j}\) so that \(A\)'s chain switches back and forth between \(X\)
and \(Y\) values.
Long sequences of collisions are unlikely, and the longer the sequence
the less the probability of continuing collisions.
So how can an agent determine whether a block is in the chain? And so
how can an agent find out if a transaction has been executed? You
sell your used bicycle to somebody for coins, but how do you know if
that transaction becomes part of the block chain when different
agents have different copies? And so how do you know that you can
spend those coins that
you should have received for your bicycle?
Confidence that a Block is in the Chain
In this section we discuss the behavior of non-devious agents; we will
show how the algorithm handles devious agents later.
Suppose an agent's copy of the block chain is \(X + X_{1} + X_{2}
+ \ldots + X_{K}\), and another agent's copy is \(Y + ? + ? +
\ldots\), where \(?\) represents arbitrary values. What is the
likelihood that \(Y \neq X\)?
\(Y\) can be different from \(X\) only if there are a sequence of
\(K\) or more collisions. And the likelihood of a sequence of
collisions decreases with \(K\). Likewise, the likelihood that an
agent never receives a block chain containing \(X\) decreases with
time, and so decreases with \(K\). So, if \(K\) is large then with
very high probability, \(X\) is part of every agent's block chain.
Now suppose an agent's copy of the block chain is \(L + X + X_{1} + X_{2}
+ \ldots + X_{K}\). What is the likelihood
that another agent's copy is \(L + Y + ? + \ldots\) where \(L\) is an
arbitrary sequence? By the same
argument, when if \(K\) is large then with
very high probability, \(X\) is part of every block chain.
For practical purposes,
many agents assume that if \(K > 6\) then \(L + X\) is a prefix of
most agents' block chains.
See the Princeton Bitcoin book.
Suppose agent \(A\) gets \(N\) Bitcoins in transaction \(X\). If \(K =
0\) then an agent can't be confident that \(A\) ever received these coins
because this transaction may not persist in the block chain. As \(K\)
increases agents become more confident that the transaction is in the
block chain and that \(A\) did, indeed, receive these coins.
K. Mani Chandy,
Emeritus Simon Ramo Professor,
California Institute of Technology