02 Blockchain
02 Blockchain
Link to article
Outline
• Blockchain conceptually: the layers
• The consensus mechanism on the blockchain
• Signatures
• Miners
• How is data stored on the blockchain: Hash functions and merkle trees
• Proof of work, proof of stake, and delegated proof of stake
• Blockhain interoperability
• Mining pools and the network
• Recap
What is a blockchain?
First, tell me what you think…
Focus of this unit
• Short answer:
• A blockchain provides coordination between many parties, when there is no single trusted
party (e.g. a bank). 区块链提供多方的协调
• One application is ownership: It allows asset ownership to be recorded and transferred with
full transparency and verifiability of ownership.
• There are several advantages:
• The ledger is automated and therefore not as prone to human error. The design is however still man-
made.
• Distributed ledgers are less costly than centralized authorities (lower transaction fees).
• 分布式记账成本低于中央集权
• It’s very easy to audit
The blockchain: The beginning
1970s: Using double-key cryptography to make secure transfers of assets from one wallet to
another (British intelligence)
• Private key/public key cryptography:
• Private key: Password that releases coins from your wallet
• Public key: Address of the wallet, known by everyone, bank account number.
• Elliptical curve cryptography: Provide correct private key, can combine with public key as correct or not correct
• Almost impossible to reverse engineer a private key from a public key: Approximately 100 years with a supercomputer
1976: Recording new data sequentially in a write-only, indelible ledger, the “blockchain” (IBM)
• Each new entry is connected to the entry before it automatically (mathematically)
1991: Decentralizing the ledger to provide transparency of data to all users and interested third
parties (Bell Labs)
• Create security with a third party, not a trusted party (that needs to be incredibly secure)
• Everyone is the third party. Everyone one is an auditor
• The security comes from transparency (not secrecy)
• Cryptographer at bell labs came up with it
2008: Validating new data by cryptographic “consensus” proof, in recurring 10-minute open
competitions, instead of relying on a trusted third party (Nakamoto)
• Anyone can be a miner and compete to validate blocks for rewards
• The rewards keep everyone honest
• Many applications beyond payments
• Whoever built bitcoin understood micro economics and game theory very well and how to make a market reach an interesting equilibrium
Blockchain contains four layers
Buy TX Miners
Alice Pizza Mempool
Pizza
Reward
Buy
TX Reward
Drugs
Bob
Drugs
A B C
• Blockchain can contain other data than simple transactions (e.g., real estate ownership, birth certificate etc.)
• The data is then encrypted with hash codes which is extremely resilient to replication and reverse engineering
(“hashed”): For example, “Tx[1]” is the hash of transaction 1
• Each block contains two entries: hash code of inbound data, Tx(n), hash code of prior block, S(n - 1).
• They’re “chained”: For example, block 2 contains new information, and inbound information from block 1
• Every block hash is 64 characters long regardless of the size of the information
• The hash of the pervious block and the current block combined are 128 characters and then hashed to 64
characters so that it summarizes the current and past block’s information.
• Therefore, unlike a conventional ledger, if you remove or change one block it’s going to affect all future data:
The change will affect the current 64 code, which will ripple forward into all future 64 character codes
• Blockchains therefore can be changed, but it’s easy to detect. Auditing the blockchain is therefore an easy task.
Logic of a blockchain
• Cryptography: Making and breaking codes by finding patterns (similar inputs leads to similar outputs)
• Cryptographic method won’t work for the blockchain as the inputs are different every time: Contains the previous
blocks hash + the new transactions hash.
• Even if Tx(1) = Tx(2), we will have S(1) ≠ S(2), making it impossible to recover the raw data
• The methods is so powerful that the US defense department uses it for battlefield orders
Ok, so where do we stand?
Step 2: We also know that miners verify the shift in ownership. They
act as an alternative to a centralized authority (e.g., a bank)
Signatures
Accept/reject
Verifier
Signing
algorithm
Accept/reject
Buy
TX 6.25 BTC
Drugs
Bob
Drugs
+Sig
• A “digital fingerprint”
• You cannot reverse the hash into the original content, just like you can’t say anything
about a person from their fingerprint.
• A person can prove their identity by matching their fingerprint with one stored in a
database
• In an airport they can check you fingerprint against database of fingerprints, but they can’t tell your
nationality, age, sex…
• Someone who breaks into the database and steals the fingerprint cannot use it to re-
create the person, or even tell what the person looks like
Input to a hash function
• Anything that can be stored in digital form can be hashed (and verified)
• Text
• Data
• Video, music, photographs
• Fingerprints, irises
• Etc.
• Limit: 2.09 exabytes
• So large it would take 220 years just to read in
• New York times made hash codes of their entire photo album so that they can’t be
doctored. Josef Stalin removed people from photo for example as if they never
existed (chilling)
What does a hash function
do with the input?
• Input is converted to a hexidecimal “hash” by scrambling
it in a way that is impractical to invert
• For instance, “Take every third digit in the file, multiply that
number by 7, add the digits together and divide the total by every
fourth number in the file. Append every number not used in the
previous calculation to the number you have, etc. . .”
• Even if everyone knows the hash code (the process), they can’t
invert it and they can easily be made more complicated
• They are usually updated at NSA (smartest cryptographers in the
world) for example for nuclear launch codes
Hash function complexity (have a go)
Input Output
14f962560929788a1c2a1f8c4f6b1789
2 5 6) 2b0ea0750330872361104c2bce0cf2d6
A -
(SH
“I love Digital Finance” (SHA3-2
56) 2bce9285b12afe39bcbbdadb5c44528d
(SH
A3 8a6cac9c3a15fa427f7ec0c0f0c3c145
- 51
2)
261d1917af20347c92c4eba78b6473e8
637a85cde226636ccc9046211930e945
c76e630c09f4bc8ef704dc46d374b954
a5b6b573ead00523333a0877f0f653bc
Merkle tree
H1234567
910910
910910
H12345678 H910910
910910
H1 H2 H3 H4 H5 H6 H7 H8 H9 H10
Tx Tx Tx Tx Tx Tx Tx Tx Tx Tx1
1 2 3 4 5 6 7 8 9 0
Merkle tree
Blockchain
Tx1 Tx2 Tx3 … Tx1 Tx2 Tx3 … Tx1 Tx2 Tx3 … Tx1 Tx2 Tx3 …
Txn Txn Txn Txn
Storing large amounts of data:
bundle it into blocks using Merkle trees
Each block contains 1 MB worth of data equal to 2000 transactions. We hash the
blocks and condense them into a merkle tree.
In the tree we hash each transaction, combine them, hash again etc. With every hash we
create a 64 alphamerical string. Finally, the merkle root is the the final hash.
The Nonce
• This is where the competition happens
• Miners compete to find a nonce that when added to the other three elements and hashed falls below a certain cutoff value
• Right now the nonce begins with 18 0s
Number of guesses:
For one leading 0?
If I need 1 0 at the start of the hash then I’d need to do 16 guesses since there are 16 possible entries into each cell (hexadecimal: 0-9 and a-f)
For two leading 0s?
If I need two 0’s in a row I’d need 16^2 power
For 18 leading 0s?
If I need 18 zeroes then I need 16^18 power
How do miners reach consensus?
Step 2: We also know that miners verify the shift in ownership. They
act as an alternative to a centralized authority (e.g., a bank)
A blockchain with “proof of work”
Nakamoto (2008)
You computer needs to guess random numbers very fast and hash them just as fast
“Hash rate”: How many trillions of hashes your computer can do in one second
The reward was originally 50 btc and halves every four years
Today its 6.125 btc
2140 it will disappear
What will incentivize mining then?
Today, 19,101,800 out 21 million bitcoin have been awarded. Every bitcoin in the network was awarded to a miner at some point in time
The value of mining a block is (today) 32,795 * 6.125 = $200,869
They are rewarding people who can play the “lottery” very quickly
Blocks
The blocks come about 10 minutes apart and you can’t guarantee how long it will take for someone to solve a block (random)
The difficulty of the problem is adjusted every 2016 blocks (two weeks roughly) depending on how long it takes for people to solve a
Proof of work (PoW)
May the best Thanks, here’s a
I solved it! reward
CPU win!
Verify block
lved
ine r that so
M
puzzle
Block reward
• https://andersbrownworth.com/blockchain/blockchain
Miners verify the blockchain via consensus
mechanisms…
• Proof of work (PoW)
• Proof of stake (PoS)
• Delegated proof of stake (Dpos)
Proof of stake (PoS)
Let’s get Won’t let you
Yay! to work! down!
Thanks, here’s a
Pick me! Pick me! Pick me!
reward
Pick me! Pick me! Pick me!
• Validators “stake” a specific amount (32 eth in Ethereum blockchain) of their cryptocurrency as
collateral for a chance to validate transactions (they have a horse in the race)
• Miners are then selected randomly from the volunteers
• Validators can lose their stake if they perform the validation task poorly (penalty). If the
validator for instance fail to validate (by going offline) or
• First proposed on the “bitcointalk” forum in 2011.
• Ethereum is moving to proof of stake (from proof of work).
PoS increasing in size
Delegated proof of stake (Dpos)
Let’s get Won’t let you
Yay! to work! down!
Vote! Thanks, here’s a
Vote! Vote!
reward, share with
your delegates
Vote! Vote! Vote!
20-100 delegates
Transaction fee
selected
https://blockchain.info/charts/avg-block-size
Block size
7-day moving average
In the beginning the blockchain had unused capacity, now there is so much demand that there’s backlog
2017: Crisis, blockchain full, average block size up to 1 mb limit
Nakamoto added the 1MB block limit in secret back in 2017 (in and upgrade) to avoid Denial of service attacks (DOS)
where users fill the blockchain with spam (transactions) to crash it
In Aug 2017, the segregated witness was implemented but this was just patch to truncate the data (now blocks exceed
1MB by a bit)
After implementation the block size did grow a bit beyond 1 MB
Day of the week effects: Monday and Friday much more mempool pressure
During regular business hours in the US (most of the demand is American) its also higher (US daylight hours)
When middle of the night in US there’s no no backlog
How long does it take for the median transaction to be include in a block (confirmed): 5-10 minute
Lately its gone up: 8-9-10 minutes
https://blockchain.info/charts/median-confirmation-time
User fees and confirmation speed
February 24, 2019
• Fees work as an extra incentive for the miner to use your
transaction in their block (opposite to stock markets who
work FIFO)
• To find out how much to tip you can look at what others are
tipping (you don’t want to tip too much)
• Fees will increase in the future as block rewards shrink
• Separating equilibrium: The tip based on their patience.
• Your transaction can only stay in the mempool for 72 hours
• Out of this pool miners won’t pick the same transactions.
This means that no two miners will have the same root to the
tree
• Incentives may be off. Maybe the tip should be set by the
receiver (miner)?
Impact of
congestion:
customers paying
for liquidity
Likely a user
error
(irreversible)
https://blockchain.info/charts/transaction-fees-usd
Mining fees vs. block rewards
Another user
error, but the
Why tips miner gave back
this far the money
back?
Example: on February 18, 2017, hash target value was reduced from
0000000000000000029ab9000000000000000000000000000000000000000000
to
0000000000000000027e93000000000000000000000000000000000000000000
https://blockchain.info/charts/difficulty
Recalibrating time to mine each block
• As the mining difficulty changes during a 2,016-block cycle
(blue series), difficulty is re-set for the next 2,016 blocks (red
series) such that the new expected block time equals 600
seconds. It then could drift upward or downward.
Goal
https://bitcoinwisdom.com/bitcoin/difficulty
Mining technology
• Central Processing Units (CPUs) of a high-end laptop or desktop, circa 2009
• 14 million SHA-256 hashes per second
• Balancing capital cost (hardware) against operating cost (energy), real estate cost, and
maintenance/IT personnel cost
• Replacing hardware and re-selling units into the secondary market for used miners
https://www.buybitcoinworldwide.com/mining/pools/
Maybe crypto mining is the new gold mining?
Maybe crypto mining is the new gold mining?
1. Exploration
2. Development
Complex
3. Mining
4. Initial processing
5. Refining
Choosing the location
• Costs average US$1000 to US$1500 per ounce • Bitcoin mining requires an up-front cost for
of gold the GPU and then variable costs in electricity.
• Main costs tied up with physical mining and • The cost of a miner is closely related to its
processing. Refining only 1% of costs. profits (accounting for electricity costs).
Supply uncertainty
• Internal governance
• Defection by members
• Possible market entry by Amazon, Microsoft, and other cloud computing providers
Botnet miners
Other miners:
• Sometimes professor hijack computing power to mine crypto
• Government computing facilities can also do it
• Soviet nuclear engineers where mining crypto instead of
designing weapons with the mainframe.
• People at Langley have also been arrested for doing it
• The incentives are great
2%
https://blockchain.info/charts/cost-per-transaction-percent
Recap questions
• Q1: What are the four layers of the blockchain and what are their functions?
• A1: Consensus-, Compute-, Application-, and User facing layer.
• Consensus layer: parties agree on the state of the chain
• Compute layer: changes the state of the chain
• Application layer: allows for applications to function on the chain
• User facing tool: allows for the user to send messages to the blockchain
• Q2: What are the three consensus protocols (that we discussed) and how do they work?
• A2: PoW, PoS, DPoS
• PoW: An “arms race” with multiple participants that all try to mine a block
• PoS: Only a few individuals with a stake mine blocks
• DPoS: Network choses a few “delegates” that mine blocks
• How many guesses do you need for a nonce with a limit value of 3 leading 0s?
Recap questions
• Q3: How do blockchain communicate with each other? Explain the process chat
一下
• Q4: How are miners remunerated? chat 一下
• Q5: Name three strengths of the bitcoin blockchain
• A5
1. Transparency: The program is public to everyone
2. Verifiability: Everyone can verify that state transitions
3. Trustless: We don’t need to trust third parties as everything is public
• Q6: Can a hacker change something in the blockchain without anyone noticing?
• Q7: Why is it hard (impossible) to hack the bitcoin network?