Blockchain A-Z: Build a Blockchain, a Crypto
This post distills key insights from my study of "Blockchain A-Z: Build a Blockchain, a Crypto" by Hadelin de Ponteves, Kirill Eremenko, SuperDataScience Team and Ligency Team.
Blockchain Intuition
- What is a Blockchain?
Is a continuously growing list of records, called blocks, which are linked and secured using cryptography.
Concepts: Block Data, Genesis Block, Blockchain, Hash
Additional Reading: "How to Time-Stamp a Digital Document" By Stuart Haber & W.Scott Stornetta (1991)
- Understanding SHA256 Hash
SHA256 is a cryptographic hash function that takes an input and produces a fixed-size string of bytes.
The SHA256 has been developed by the NSA.
Hash Algorithms requirements:
- Has to be One-Way
- Has to be Deterministic (Same Document will generate the same hash output)
- Fast Computations
- The Avalanche Effect (Same document with any change, the hash will be completely different)
- Must withstand collisions (Pigeon hole principle)
Note: The pigeonhole principle states that if n items are put into m containers, with n > m, then at least one container must contain more than one item.
- Immutable Ledger
On a Traditional Ledger we had who owns what.
The blockchain is basically a ledger that is immutable, meaning that once a block is added to the chain, it cannot be changed.
Basically an immutable ledger means that the data that is stored in the blockchain cannot be changed.
- Distributed P2P Network
Blockchain is a distributed network, meaning that it is shared among all the nodes in the network.
It is a peer-to-peer network, meaning that all the nodes in the network are equal.
There is no central authority that can control the blockchain.
It is decentralized, meaning that all the nodes in the network have a copy of the blockchain.
Since the network is constantly checking their pier's work, it is very secure, because even if one node is compromised, the rest of the network will still be able to verify the transactions.
- How Mining Works (Nonce & Cryptographic puzzle)
Fields of a Block: Block Number, Data, Prev.Hash, Hash, Nonce
Nonce is a number that is used only once in a cryptographic communication.
The hash is gonna use the nonce and all of the previous fields to create a hash that is gonna be less than a certain number.
It is a cryptographic puzzle that the miners have to solve.
A Hash is a hexadecimal Number.
In essence, the blockchain system will set a target, and the miners have to find a hash that is less than that target.
It is a probabilistic process, meaning that the miners have to try many different nonces to find the right hash, that is less than the target (Example: Hash must have the target as a 4 leading zero values).
Once the miner finds the right hash, it will broadcast it to the network, and the network will verify that the hash is less than the target.
Once the hash is verified, the block is added to the blockchain.
Then the miners will start working on the next block.
Nonce means "Number used once".
- Byzantine Fault Tolerance
It is a problem that is related to distributed systems, where the nodes in the network have to agree on a value.
Imagine that you have a general that has to send a message to his lieutenants, and he has to make sure that all the lieutenants receive the same message, and that they all agree on the same message.
If one of the lieutenants is a traitor, he can send a different message to the other lieutenants, and they will not be able to agree on the same message.
Byzantine Fault Tolerance is a way to solve this problem, by having the nodes in the network agree on a value, even if some of the nodes are malicious.
Even if the general is a traitor and says to attack, the lieutenants can device to not attack, and they will all agree on the same message.
It is a way to reach consensus in a distributed system.
This algorithm to work, it has to have no more than 1/3 of the nodes in the network being malicious.
You can read more at the official papel: "The Byzantine Generals Problem" by Georgios Konstantopoulos, 2017.
- Consensus Protocol (Defense against attackers, competing chains)
Regarding Defense against attackers, the consensus protocol has to solve two main challenges:
- Attackers
- Competing Chains
Imagine the descovery of a new block at the same time by two different miners in different parts of the globe.
Both miners will broadcast their blocks to the network, and the network will have to decide which block to add to the blockchain.
It would be a fork in the blockchain, and the network will have to decide which fork to follow.
It is a consensus protocol that will decide which fork to follow.
How to solve the problem of competing chains in this case?
It is a probabilistic process, meaning that the network will choose the longest chain, because it is the chain that has the most work put into it.
Imagine a scenario with 6 nodes, and 4 of them have the same chain, and 2 of them have a different chain, basically they will wait for the next block to be added to the chain, and they will choose the longest chain.
It is a way to solve the problem of competing chains.
Soo what happens to the Orphaned block? It is a block that is not part of the longest chain, and it is not added to the blockchain, so it is discarded.
The are various types of consensus protocols such as:
- Proof of Work (Bitcoin)
- Proof of Stake (Ethereum)
- (Other)
The proof of work is the most popular consensus protocol, and it is used by Bitcoin, and it is the one that we talked about above regarding the cryptographic puzzle.
Proof of Stake is a different consensus protocol, where the nodes in the network have to put up a stake, and the nodes that have the most stake will have the most power in the network.
There are other consensus protocols, such as Proof of Authority, Proof of Space, Proof of Burn, etc.
Every single node before the block gets added to the blockchain, it has to pass in a various number of checks, such as:
- Check syntatic correctness
- Reject if duplicate of block we have in any of the tree categories
- Transaction list must be non-empty
- Block hash must satify claimed nBits proof of work
- Block timestamp must not be more than two hours in the future
- First transaction is coinbase (i.e. only 1 input, no prev out), the rest must not be
- etc...
Note: Cryptographic puzzles are hard to solve, but easy to verify.
Additional Reading: "Bitcoin P2P e-cash paper" by Satoshi Nakamoto (2008)
Cryptocurrency Intuition
What is Bitcoin Deepdown
Basically bitcoin is a protocol just like Ethereum, Neo, Ripple, that uses the technology of blockchain.
Protocols contain within them the coin, which is used to reward people for mining, to pay for transactions, etc.
While coins are Layer 2, on layer 3 we have the Tokens.
Tokens are used to represent assets, and they are built on top of the coins.
If we invest in a coin, we are investing in the coin protocol, while if we invest in a token, we are investing in tokens, we are investing in an idea, a project, etc.
Bitcoin was invented by Satoshi Nakamoto in 2008, and it was the first cryptocurrency.
It is a decentralized digital currency, without a central bank or single administrator, that can be sent from user to user on the peer-to-peer bitcoin network without the need for intermediaries.
It is a way to transfer value from one person to another, without the need for a bank.
The bitcoin ecosystem is composed of:
- Nodes
- Miners
- Large Mines
- Mining Pools
Additional Reading: "Bitcoin: A peer-to-peer Eletronic Cash System" by Satoshi Nakamoto (2008)
Bitcoin Monetary Policy
Bitcoin has a fixed supply of 21 million coins.
Bitcoin monetary policy consists of two main components:
- Block Frequency
- Halving
The Halving states that the number of bitcoins released in each block is halved every 210,000 blocks to half the reward.
After all the blocks are mined, the miners will be rewarded with transaction fees with higher fees for the miners.
It is a way to incentivize the miners to keep mining, even after all the bitcoins are mined.
Regarding the block frequency it is the time that it takes to mine a block, and it is 10 minutes for Bitcoin.
How it states that needs 10 minutes? It is because the network will adjust the mining difficulty of the cryptographic puzzle, so that it takes 10 minutes to mine a block.
This is automatically adjusted every 2016 blocks, and it is a way to control the supply of bitcoins.
It is a way to control the supply of bitcoins, and it is a way to control the inflation of bitcoins.
Additional Reading: "This time is Different part 2: What Bitcoin really Is" by Mark E. Jeftovic
Understanding Mining Difficulty
What is the Current Target and how does that feel?
- The target is expressed as an Target of leading zeros.
Example:
- Target: 18 zeros
-
Estimations:
- Total possible 64-digit hexadecimal numbers = 10^77
- Total valid hashes ( with 18 leading zeros) = 2*(10^55)
- Probability that a randomly picked hash is valid = 2*10^(-22) = 0.0000000000000000000002%
How is the "Mining Difficulty" calculated?
- Difficulty = current target / max target
- Max target when bitcoin started was: 00000000FFFF000000000000...
Difficulty is adjusted every 2016 blocks(2 weeks)
Difficulty is adjusted so that a block is released every 10 minutes, increasing or decreasing the number of leading zeros.
Mining Pools
Consists of a group of miners that work together to mine a block.
They share the reward of the block, and they share the reward of the transaction fees.
It is a way to increase the chances of mining a block, and it is a way to increase the chances of getting the reward.
The work is distributed in a way to ensure that is not doing the same work twice.
The contribution reward is based on the hash rate of the miner.
Mining pools remove the headache of the miners to know what is a blockchain, how to mine a block, etc., and they just have to mine provide the computational power.
Nonce Range
Nonce is the number that is used once to try to solve the cryptographic puzzle.
Basically a modest miner does 100 MH/s, that's 100 Million hashes, and the maxium is 4Billion hashes, so that means it would take 40 seconds to find the right hash.
But hashing requires a new field too caled the Timestamp, and the timestamp is always updating every second.
That means we will only be able to verify a limited number of nounces given the time frame
So the hashing is limited by the Nonce and timestamp.
What happens is that mining pools will distribute the work to the miners, and they will give the miners a range of nonces to try.
It is a way to increase the chances of finding the right hash.
How Miners Pick Transactions
So the transactions come from the mempool, which are attached to every miner.
While blocks are added every 10 minutes, transactions are added to the mempool every second.
Basically while this transactions are in the mempool, the are still pending, and they are not added to the blockchain.
Every transaction has a fee attached to it, and the miners will pick the transactions that have the highest fees.
The fees are specified by the sender of the transaction.
Miners will pick the transactions that have the highest fees, because they want to maximize their reward.
They will also pick the transactions that have the highest priority, such as the transactions that have been waiting for a long time.
But lets see another point, since the miners pick the transactions from the mempool and attach them to the block, the miners can also choose to not include a transaction in the block.
It is a way to censor transactions, and it is a way to control the transactions that are added to the blockchain.
If the transaction is not picked up by the miners, it will stay in the mempool waiting for a duration of 72 hours, and then it will be removed from the mempool and the transaction will be canceled.
The max block size is 1MB and usually transactions count vary between 1-2k transactions.
Additional Reading: "How Bitcoin Mining Works" by coindesk
ASIC vs GPU vs CPU
ASIC is a specialized hardware that is used exclusively for the hashing process.
The ASIC is much faster than the GPU and the CPU, and it is much more efficient in terms of power consumption.
- ASIC: 1 TH/s, 1000 GH/s, 1,000,000 MH/s
- GPU: 1 GH/s, 1000 MH/s
- CPU: 1 MH/s
ASICs can be used on bitcoin mining or other type of protocols that uses the SHA256 hash function.
How do Mempools work?
Imagine Susan that wants to do a transaction. What happens?
Susan has a node so the transaction is added to the mempool of the node.
Then the transaction is broadcasted to the network, and it is added to the mempool of the other nodes in the network.
Then the miners will pick the transactions from the mempool and attach them to the block while mining.
Once the block is mined, the block is added to the blockchain, and the transactions are removed from the mempool.
Additional Reading: "An in-depth guide into how the mempool works" By Marion Deneuville (2016)
The 51% Attack
What the 51% attack is not:
- Is not an attack designer to tamper with an individual or select blocks
What the 51% attack is:
- A group of miners join the network and don't broadcast their blocks to the network
- Then they isolate themselves and they mine their own blocks and they create a longer chain than the rest of the network
- After this, they broadcast their blocks to the network, and the network will choose the longest chain
This attack can cause a double spend problem. Imagine that in the normal blockchain you spent millions buying assets and then the network gets overriden by the 51% attack, and the attacker can spend the same coins again.
This because the transactions are not added to the blockchain, and the attacker can spend the same coins again.
Basically the 51% attack is all about the hash power, and the attacker has to have more than 51% of the hash power of the network.
This could happen if a mining pool has more than 51% of the hash power of the network.
Cryptocurrency Transactions Intuition
Transactions and UTXOs
Imagine the following transactions:
From | To | BTC Value |
---|---|---|
Mark | Me | 0.1 BTC |
Hadelin | Me | 0.3 BTC |
Helen | Me | 0.6 BTC |
Susan | Me | 0.7 BTC |
UTXO stands for Unspent Transaction Output.
It is a way to keep track of the transactions in the blockchain.
Baically every row in the table is a UTXO.
Imagine that i want want to buy a bicycle for 0.5BTC.
What happens is that i will pick the UTXOs which is closed to the value, for example 0.6 BTC from elen, and the output would look like this:
From | To | BTC Value |
---|---|---|
Me | Bikeshop | 0.5 BTC (UTXO for the bike shop) |
Me | Me | 0.1 BTC (UTXO for me) |
This UTXO (0.1BTC) will be available for me to spend in the future.
After this, i could say that i want to buy another bicycle for 1.1 BTC, then the trasaction input will be the 0.3BTC from Hadelin, 0.7BTC from Susan and 0.1 BTC from Me, and the output will only be the 1.1BTC to the bike shop
Where does transaction fees come from?
Transaction fees are everything that we do not account for on the output UTXOs.
Basically the transaction fees are the difference between the input UTXOs and the output UTXOs.
Imagining an input of 0.9BTC and an output of 0.88BTC, it means that the fee is 0.02BTC for the miner
How Wallets work
How a wallet comes from a balance value?
Basically it iterates over all the blocks in the blockchain, and calculates the total balance of the UTXOs.
Signatures: Private & Public Keys
Soo how does the blockchain ensures privacy and security?
We have the private key which we can generate the public key, and the public key is like the number of the bank
So imagining a transaction from Me to Mark, and the private key is used to sign the Message, and the public key is used to verify the signature.
In Blockchain there's a verification function that takes the public key, the message and the signature, and it verifies if the signature is valid.
What is Segregated Witness (SegWit)?
Bitcoin has a limit of 1MB per block, and Segregated Witness (SegWit) is a method to increase the number of transactions in a block without technically increasing the block size limit.
In a typical Bitcoin transaction, the signature and public key (part of the scriptSig) can occupy a significant portion of the block—sometimes up to 60% of the space.
The scriptSig contains the signature and public key, which are used to verify the transaction, while the scriptPubKey is used to determine who can spend the output of the transaction.
SegWit works by moving the signature data (the witness) from the main part of the transaction into a separate structure, effectively freeing up space in the main block. This allows for more transactions to fit within the 1MB block size limit, increasing the effective block size without changing the block size limit itself.
In this way, SegWit helps reduce the size of each transaction, thereby increasing the number of transactions that can be included in each block.
Additional Reading: "Understanding Segwit Block Size" By Jimmy Song (2017)
Public Key vs Bitcoin Address
A public key is part of a cryptographic key pair used in Bitcoin and other cryptocurrencies. It is used to verify digital signatures and to encrypt data that only the corresponding private key can decrypt. In Bitcoin, the public key is used to generate a Bitcoin address, but it is not directly involved in decryption.
A Bitcoin address is a shorter, more user-friendly representation derived from the public key. It is a hash of the public key, typically using a combination of the SHA-256 and RIPEMD-160 hashing algorithms, followed by a checksum and encoded in Base58. The Bitcoin address is what users share to receive Bitcoin.
Why should we avoid using the Public Key directly when we can? The main reason is security. If someone were to somehow discover a method to derive the private key from the public key, it would compromise the associated Bitcoin. By using the Bitcoin address (a hash of the public key) instead of the public key itself, we add an extra layer of protection. The hashing process makes it significantly harder for an attacker to reverse-engineer the public key back to the private key, thereby enhancing security.
Hierarchically Deterministic (HD) Wallets
Hierarchically Deterministic (HD) wallets are a type of cryptocurrency wallet that generates all of its keys and addresses from a single seed, known as a master seed. This seed is a random string of data that is typically represented as a series of 12 or 24 words, known as a mnemonic phrase.
From this master seed, an HD wallet can generate an entire tree of private and public keys in a deterministic way. This means that the same seed will always generate the same sequence of keys and addresses, allowing users to recover their entire wallet (including all addresses and funds) using just the seed phrase.
One of the key benefits of HD wallets is that they allow users to create multiple addresses from a single seed, which enhances privacy. Instead of reusing the same address, a user can generate a new address for each transaction, making it more difficult for outsiders to track the user's transactions on the blockchain.
Another advantage is the ease of backup and recovery. Since all keys are derived from a single seed, users only need to back up the seed phrase to secure access to their entire wallet. This makes HD wallets both convenient and secure.
Smart Contract Intuition
What is Ethereum?
Ethereum is a decentralized, open-source blockchain platform that enables developers to build and deploy smart contracts and decentralized applications (dApps). The idea behind Ethereum is to create a global "supercomputer," where a network of nodes (computers) works together to execute code and store data in a decentralized manner, without the need for a central authority.
Unlike Bitcoin, which is primarily focused on being a digital currency, Ethereum is designed to be a versatile platform that can decentralize a wide range of applications, from financial services to supply chains and beyond. This allows developers to create applications that run exactly as programmed, without the risk of downtime, censorship, fraud, or third-party interference.
What is a Smart Contract?
Smart contracts are self-executing programs that run on the Ethereum blockchain. These contracts have the terms of the agreement between buyer and seller directly encoded into lines of code. When predefined conditions are met, the contract automatically executes the agreement, eliminating the need for intermediaries.
In the cryptocurrency world, different platforms use different languages for their contracts. For instance, Bitcoin uses Bitcoin Script, while Ethereum uses Solidity.
So, what is Bitcoin Script? It is a simple, stack-based programming language used to define the conditions under which a Bitcoin transaction output can be spent. However, Bitcoin Script is not Turing complete, meaning it is limited in its functionality and cannot perform complex computations.
On the other hand, Solidity is a high-level, contract-oriented programming language used to write smart contracts on the Ethereum platform. Solidity is designed to be easy to learn and use, with a syntax similar to JavaScript. Unlike Bitcoin Script, Solidity is Turing complete, meaning it can perform any computation that a computer can execute.
To further clarify, a system is described as Turing complete if it can perform any computation that can be executed by a computer. Essentially, a Turing complete system can simulate any other Turing complete system, including complex computations and loops.
Each Node has:
- A History of all smart contracts
- A History of all transactions
- A Current State of all smart contracts
Decentralized Applications (Dapps)
A Dapp consists of a frontend and a backend, where the backend is a smart contract.
It is a method for creating applications that are decentralized and operate on the blockchain.
Ethereum Virtual Machine & Gas
Security threats:
- Viruses and Unauthorized Access: Smart contracts cannot access private files or compromise our system's security.
- Infinite Loops & Heavy Calculations: These can lead to excessive resource consumption.
When we participate in the Ethereum network, we receive access to the Ethereum Virtual Machine (EVM), a virtual environment running on our computer but fully isolated. This ensures that nothing within the virtual machine can affect our local system. As a result, smart contracts are sandboxed and have no access to our personal files or sensitive information.
Gas is the fee paid to the network for executing smart contracts. It's a critical mechanism designed to prevent inefficient code and potential abuses like infinite loops or resource-intensive calculations. Each operation within the EVM has a predefined gas cost, as detailed in Ethereum's Yellow Paper. For instance, simple operations like addition or multiplication might cost 5 gas.
Gas also serves to encourage developers to write optimized, efficient code. The reason we use gas instead of ether is due to ether's price volatility. Gas, on the other hand, has a more stable cost, ensuring consistent pricing for computation.
While Bitcoin focuses primarily on transactions and cryptocurrency, Ethereum is designed for computation, enabling the development and execution of decentralized applications on the blockchain.
Decentralized Autonomous Organizations (DAOs)
What are organizations today?
Traditional organizations typically have a hierarchy and follow established protocols and procedures. This hierarchy can be replaced by smart contracts.
DAOs are organizations that are governed by smart contracts, making them decentralized and autonomous.
They are decentralized because they are managed by a network, and autonomous because their operations are controlled by smart contracts.
DAOs offer a way to create organizations that are both decentralized, managed by the network, and autonomous, governed by smart contracts.
Additional Reading: "DAOs, DACS, DAs and More: An Incomplete Terminology Guide" by Vitalik Buterin (2014)
The DAO Attack
This attack occurred in 2016 on the Ethereum network.
The DAO was an investor-directed venture capital fund, created with the noble goal of funding projects initiated by its members.
This DAO was decentralized, meaning it was a smart contract running on the Ethereum blockchain.
They raised $150,000,000 through crowdfunding.
Unfortunately, there was an error in the code, and the DAO was attacked and hacked, resulting in the loss of $50,000,000 due to a flaw in the code.
There was a fail-safe mechanism that locked the funds for 28 days, preventing the attacker from immediately withdrawing them.
However, the attacker found a way to split the DAO into a child DAO, allowing them to withdraw the funds from the child DAO.
After this, the Ethereum community decided to perform a hard fork of the blockchain, creating a new chain where the funds were returned to the investors.
This resulted in the creation of Ethereum (ETH) and Ethereum Classic (ETC).
Soft and Hard Forks
While the DAO attack resulted in a hard fork, there are two types of forks in the blockchain world: soft forks and hard forks.
Soft Fork: A soft fork is a change to the blockchain protocol that is backward-compatible, meaning that nodes that have not upgraded to the new protocol can still participate in the network. Soft forks are typically used to implement minor changes or updates to the protocol.
Hard Fork: A hard fork is a change to the blockchain protocol that is not backward-compatible, meaning that nodes that have not upgraded to the new protocol will not be able to participate in the network. Hard forks are typically used to implement major changes or updates to the protocol.
In Bitcoin, with the introduction of SegWit (Segregated Witness), it was a soft fork, while the DAO attack on Ethereum resulted in both a soft fork and a hard fork.
Initial Coin Offerings (ICOs)
Initial Coin Offerings (ICOs) are a method for companies to raise capital by issuing tokens on the blockchain. These tokens can represent various assets, such as equity in the company, access to a product or service, or voting rights within the organization. ICOs operate on the token layer of the blockchain, but they can also involve protocol layer changes when new coins are issued.
In an ICO, the company creates a whitepaper that outlines the details of the offering, including the purpose of the tokens and the project's goals. Investors then purchase these tokens directly from the company during the ICO period.
How do ICOs compare to IPOs?
An IPO, or Initial Public Offering, is the first sale of stock by a company to the public. It allows a company to raise capital by selling shares of its stock, typically through an investment bank that underwrites the offering and facilitates the sale.
In contrast, an ICO involves issuing tokens rather than shares. While an IPO gives investors ownership stakes and voting rights in the company, ICO tokens can provide various benefits, such as access to a service or the potential for appreciation in value. Unlike IPOs, ICOs often do not involve traditional financial intermediaries.
ICOs have become a popular fundraising method for blockchain projects, offering a decentralized way to raise capital. However, they come with risks. ICOs are frequently unregulated and can be susceptible to fraud and scams. As a result, it is crucial for investors to conduct thorough research and understand the project's whitepaper before investing.
Typically, the tokens acquired in an ICO can be used within the project's ecosystem to access products or services. Unlike in an IPO, where the public receives shares and potential voting rights, ICO participants hold tokens which may gain value over time or provide utility within the project’s platform. Additionally, companies conducting ICOs usually retain control over their shares and may not distribute equity as in an IPO.
Additional Reading: "Wtf is an ICO" By Alex Wilhelm (2017)
Other Papers
Additional Reading: "How Crypto Tokens Will Enable the Disruption of Businesses like Uber and Airbnb" By Andrew Finn (2018)
Additional Reading: "Why the Web 3.0 Matters and you should know about it" By Matteo Gianpietro Zago (2018)