L18 Blockchain
L18 Blockchain
2
Design Intuition
• Run a consensus protocol for a consistent view
of payment history
– The protocol guarantees the irreversibility
– Anyone can participate
– As long as a majority of servers are cooperative,
the system is safe
3
Distributed Payment Layer
• A stateful layer: state transition has constraints
– After a payment, the total sum of balance is unchanged
Payment Layer
Internet
4
Transaction Log
• Instead of directly storing key-value pairs, we store a list of
transactions: (idsrc, iddst, amount)
– This means idsrc sent iddst amount of BTC
– We can construct the balance by iterating along the list
(Alice, Bob, 1.5) (Bob, Cindy, 1) (Alice, Cindy, 1.2) (Alice, Bob, 1.4)
Timeline
5
Problem: Proof of Transaction
• Users need to prove they actually made the transaction
6
Intro to Cryptography Signature
7
Public-Key Cryptography
• Each party has (public key, secret key)
8
Combine Signature with Transaction
• We use public keys as identifiers
(pkAlice, pkBob, 1.5, sigAlice) (pkBob, pkCindy, 1, sigBob) (pkAlice, pkCindy, 1, sigAlice)
Timeline
9
Problem: How to Build Consensus
• Any entity can insert/delete transactions anytime
– Suppose Alice only has 1 BTC, she may be able to spend it several
times
• Alice pays Bob 1 BTC, and receives the product from Bob
• Later this transaction is deleted, so Alice has 1 BTC again
• And Alice still keeps the product
– This is called the double spending problem
10
Intro to Cryptography Hash
11
Cryptography Hash Functions
• Takes message m of arbitrary length and
produces fixed-size (short) number H(m)
• One-way function
– Efficient: Easy to compute H(m)
– Hiding property: Hard to find an m, given H(m)
– Collisions exist, but hard to find
• For SHA-1, finding any collision requires 280 tries.
Finding a specific collision requires 2160 tries.
12
Blockchain: Append-only Hash Chain
prev: H( ) prev: H( )
txn 6’ txn 7’
14
Key Idea: Proof of Work
prev: H( )
txn 6’
18
Why Consume All This Energy?
20
Design Detail: Transaction Format
21
Real Transaction Format
• Four fields
○ Hash: hash of this transaction
○ Inputs: hashes of one/more previous transactions
○ Outputs: one/more (amount, public key) pairs
○ Signatures: signatures by each input coin owners
• Example: Alice sends Bob 1 BTC. Alice’s 1 BTC is from
the previous transaction x.
Hash: h
Inputs: hx
Outputs: 1 -> pkBob
Signature: SigAlice
22
Real Transaction Format
• Each input must be fully spent
○ Then what if the owner only wants to spend part of
the coin? Make the owner a receiver for rest of the
coin
• Example: Alice uses a 3-BTC tx as input, and sends 1
BTC to Bob, and the rest to herself
Hash: h
Inputs: hx
Outputs: 1 -> pkBob, 2 -> pkAlice
Signature: SigAlice
23
Real Transaction Format
• The outputs amount is not necessarily equal to inputs
○ Unspent portion of inputs is transaction fee to miner
Hash: h
Inputs: hx
Outputs: 1 -> pkBob, 2 -> pkAlice
Signature: SigAlice
24
Bitcoin & Blockchain Intrinsically Linked
security of
blockchain
health of
value of
mining
currency
ecosystem
25
Performance Issue
• Our current version processes 1 transaction
every 10 min
– Extremely slow!
26
Batching Transactions into Blocks
27
Transactions Are Delayed
User submits a tx
Timeline
Miner finalizes Miner find hash for block x, Miner find hash
txs in block x and finalizes txs in block x+1 for block x+1
29
Storage / Verification Efficiency
• Merkle tree
– Binary tree of hashes
– Root hash “binds” leaves
given collision resistance
30
Storage / Verification Efficiency
• Merkle tree
– Binary tree of hashes
– Root hash “binds” leaves
given collision resistance
31
Bitcoin Protocol Analysis
• Safety:
– No. Only probabilistic. The deeper block, the
safer
• Liveness:
– Yes. You can always compute a hash in a finite
number of steps
32
Limitation of Scaling
• Scaling limitations
– 1 block = 1 MB max, ~ 2000 txns, ~ 10 min
• 3-4 txns / sec
• Visa payment system: typically 2,000 txns / sec
– The fundamental limitation on sequential consistency
• Remember in Lecture 12 we talked about PRAM
– read time + write time ≥ max delay
– Blockchain is designed to read on one node, write on all nodes
– Each block needs to propagate to all nodes around the world
– We cannot make packet travel faster than light
33
Summary
• Payment system
– Coins transfer/split between “addresses” (public keys)
• Blockchain: globally-ordered, append-only log of txns
– Reached through decentralized consensus
– Nodes incentivized to perform work and behave
correctly
• When “solving” a block, get block rewards + txn fees
• Only “keep” reward if block persists on main chain
34
What’s Going on This Area
• Bitcoin happened ~10 years ago
• There is exciting progress in the area in recent years
– Ethereum: Turing-complete Blockchain to support more
complex state transitions
• Run program (smart contract) on blockchain
• ICO, Stable coin (Tether, USDC, GUSD, PAX)
• Can implement financial derivatives like option contract, future contract…
• More main-chains are developing
– Layer 2: scale the blockchain by releasing sequential
consistency:
• State channel and side chain
– See optional slides for more information about state channel
35