Blockchain Mechanics#
Hashes#
Hash functions (such as SHA-256) take a block of data and generate a valid random fixed-size integer. Any change to the data block will affect the hash value.
Hash-based Proof of Work#
- Cannot compute the input from the output
- To find a hash value with N leading zeros, 2N attempts are needed. Nonce is a number that can only be used once in cryptographic communication. Calculation - proof of computational work.
- If we hash an incrementing "nonce" as the hash input, (Nonce is a number that can only be used once in cryptographic communication), we can look for zeros:
The chain race#
- Parameter N sets the difficulty of the game. (Hash value has N leading zeros)
- Players get a list of blocks, with:
- A block number
- A winner number
- A nonce value
- A hash of the previous block with N zeros
- Players accumulate points by creating blocks
– Hash the previous block
– Find a hash of the new block with enough zeros
– They then transmit this block to everyone
Set N=3, the goal is to find a nonce that, combined with the winner, parent hash, etc., results in a generated hash value that meets the requirement of having N leading zeros.
The algorithm to make a new block:
- Verify the hashes of all the previous blocks
- Build a new block with a random nonce
- Hash the new block. Does it have N zeros?
– No? Go back to Step 2
– Yes? Send your new block to everyone!
For N zeros, since the SHA-256 output is essentially random, achieving N zeros = the same as flipping a coin and getting N heads in a row. For N zeros, you must try 2N/2 random numbers. N=1, try 1 random number; N=16, try 32,768 random numbers; N=32, try 2 billion nonces.
cheaters#
One cheating method: fabricate a fake hash.
Then:
- Step 1 in the algorithm will fail for all the other players.
- Other players will not use your block, making it not part of the chain.
Signatures#
Bitcoin Account and Signature#
Make player ID = public key
We can now make trades by signing messages and sending them to everyone
Signed trades are:
• Unalterable
• Verifiable by anyone
• From key to key, not tied to a “real” identity
The Race with Trades#
We cannot guarantee that public_key1 actually has 50 points, which leads to overtrading. The solution is to put transaction information into blocks.
The "overtrading" problem is difficult to solve because there is no absolute immutable time source. In preventing overtrading, timestamps are a common means used to determine the order and time limits of transactions. However, without an absolutely trustworthy and immutable time source, players may exploit time loopholes to bypass restrictions on overtrading. For example, players might conduct a number of transactions far exceeding the allowed amount in a short period by manipulating local time or exploiting uncertainties in time records, while the system struggles to effectively identify and prevent such overtrading due to a lack of reliable time references.
• Players expend effort to get points
• Players can trade points securely
– Signatures prevent alteration of trades
– Signatures authenticate the origin of trades
• Players can detect overtrading
– Players will decline to extend the game on blocks with overtrades
– If they do, they are wasting effort, since other players will not extend the game on their blocks
Each block contains a timestamp to record when that block was created. When transactions are packed into blocks, they are recorded in chronological order. This allows the system to clearly see the order of transactions and determine whether there is excessive trading within a short period. In the blockchain network, when miners pack transactions into blocks, those blocks need to be validated by other nodes. Nodes will check the legality of transactions, including whether the signatures are correct and whether the sender has sufficient balance. For transactions attempting to overtrade (i.e., transaction amounts exceeding their actual points or balance), nodes will refuse to include them in valid blocks. Only when all nodes validate successfully can the block be added to the blockchain, ensuring the legality and authenticity of transactions and preventing overtrading.
Why bother putting transactions in your block?
• Let’s solve this by adding fees to transactions
– Incentivize players to add transactions by giving them points for each additional transaction
– Two ways to earn points!
• Why restrict player trading?
– Allow players to send points to anyone with a public key....
– This is now a global trading system
Simplified Bitcoin#
• Players = “miners”, points = “Bitcoins”
• Transactions send value (bitcoins) from key to key
• The chain race game (blockchain) prevents overspending without a central authority
• Game rules = Bitcoin node code, changes by miner consensus
• Player consensus replaces authority
– Number of coins (limit to 21 million)
– Reward per block
– How difficulty grows
Note that players/miners can interact with non-players
• Once points are created, recipients can create transactions to any public key • Now can extend to transactions with non-miners/players
• All points still come from some block/miner
Like a bankbook.
Attacks#
- The rules of Bitcoin (or any blockchain) are determined by miner nodes
The core role of miner nodes: In Bitcoin or other blockchain systems, miner nodes play a crucial role. Miner nodes are responsible for verifying transactions, packaging blocks, and adding new blocks to the blockchain through consensus mechanisms. The rules of the blockchain are actually followed and enforced by these miner nodes. For example, in the Bitcoin network, miner nodes need to compete for the right to record transactions according to specific algorithms (such as proof of work), and only blocks that comply with the rules can be accepted and added to the blockchain by other nodes. - Majority decision principle: “Whatever 51% of the miners are running will win” This phrase embodies the majority decision principle in blockchain. In many blockchain consensus mechanisms, when more than 51% of miner nodes reach a consensus, the rules and blocks they recognize will be accepted by the entire network. This is because blockchain is a decentralized system without a single authority to decide the rules, relying instead on the consensus of the majority of nodes to ensure the consistency and security of the system.
- What happens when the majority of participants defect
51% attack: When more than 51% of miner nodes in a blockchain network defect or are maliciously controlled, a 51% attack may occur. In this case, attackers can use their majority computing power to extend a malicious blockchain branch (“extend bad blocks”). Attackers can disrupt the normal operation of the blockchain by rearranging transaction orders, reversing confirmed transactions, etc. For example, after completing a transaction, an attacker could reverse that transaction through a 51% attack and then transfer the same asset to another address, achieving double spending. - Impact on the system: A 51% attack severely undermines the security and trust mechanism of the blockchain. Users will be unable to trust the transaction records on the blockchain because attackers can arbitrarily tamper with them. This could lead to the collapse of the entire blockchain system, loss of user confidence in that blockchain, and subsequently affect its value and applications.
- Proportion of nodes required for defection
- Differences between networks: Not all blockchain networks require 51% of nodes to defect to be attacked. Depending on the design and consensus mechanism of different blockchain networks, the actual proportion of nodes needed for defection may be as low as 30% or even less. Some new consensus algorithms are designed to reduce reliance on the majority of nodes to enhance the security and attack resistance of the system. However, even with a lower proportion of defecting nodes, attackers can still cause significant damage to the blockchain network.
- Sybil attack: A Sybil attack is a type of attack in the field of computer security targeting reputation systems in peer-to-peer networks. In a blockchain network, attackers can control part of the computing power or voting rights in the network by fabricating a large number of fake node identities (Sybil nodes). These fake nodes can work together to simulate the opinions of the majority of nodes, thereby disrupting the consensus process of the blockchain and executing attacks on the blockchain network. For example, attackers can use Sybil nodes to gain more accounting rights in proof-of-stake (PoS) consensus mechanisms, thereby carrying out malicious actions.
Operational Realities#
- Assume cheap storage and network conditions
- Nodes store all transaction records: The blockchain system assumes that storage and network costs are relatively low, under this premise, each node can store every transaction that has occurred on the blockchain. This design is intended to ensure the decentralization of the blockchain and the integrity of the data. Each node has a complete copy of the ledger, allowing any node to independently verify the legality of transactions and the state of the blockchain. For example, in the Bitcoin network, full nodes store all transaction information since the genesis block, which helps maintain the security and transparency of the network.
- Transaction and block broadcasting: When new transactions occur or new blocks are created, this information is broadcast across the entire network. Each node receives these broadcast messages and verifies them. Transaction broadcasting ensures that all nodes are promptly informed of new transaction requests, while block broadcasting allows new blocks to be added to the blockchain. However, this broadcasting mechanism may impose certain limitations on the scalability of the network. As the number of transactions on the blockchain continues to increase, the network bandwidth and time required to broadcast and synchronize this information will also increase, potentially leading to network congestion and affecting the system's performance and scalability.
- Slow transaction speed
- Reliance on public block verification: In blockchain, to verify whether a transaction is valid, one must wait for that transaction to be packed into a public block and confirmed a certain number of times. Due to the consensus mechanism of the blockchain (such as proof of work), it takes a certain amount of time to complete, making the transaction confirmation process relatively slow. For example, in the Bitcoin network, a new block is generated approximately every 10 minutes, and to ensure the security of transactions, it is usually necessary to wait for 6 block confirmations, meaning a transaction may take about 1 hour to receive a relatively reliable confirmation. This delay can become a significant issue in scenarios that require high transaction speeds (such as everyday shopping payments).
- Control of private keys is crucial
- Losing a private key renders funds unusable: The private key is the key for users to control their blockchain assets. The private key is a randomly generated number, and only the person who possesses the private key can operate the assets in the corresponding address. If a user loses their private key, it means losing control over the funds in that address, rendering those funds unspendable. Due to the nature of private keys being unrecoverable (unless backed up), once lost, users face permanent asset loss. For example, early Bitcoin holders may have lost access to large amounts of Bitcoin due to forgetting the storage location of their private key or due to storage device failure.
- Theft of private keys leads to asset theft: The security of private keys directly relates to the safety of users' assets. If a private key is stolen, attackers can use that private key to sign transactions, transferring funds from the user's address to their own. Hackers may obtain users' private keys through various means, such as network attacks, malware infections, etc. Therefore, users must take strict security measures to protect their private keys, such as using hardware wallets, setting strong passwords, and regularly backing up.
Bitcoin Wallet and Address#
- A user can get multiple wallets like bank accounts
- Each wallet can have many addresses
The role of addresses: Bitcoin addresses are similar to bank account numbers, serving as specific identifiers for users to receive Bitcoin. Each wallet can generate multiple different addresses, all associated with that wallet but independent of each other.
Meeting diverse needs: Multiple addresses can satisfy users' needs in different scenarios. For example, when users conduct transactions across different trading platforms or with different partners, they can use different addresses to receive Bitcoin. This makes it easier for users to distinguish and manage funds from different sources. - A user uses an address to receive bitcoins
Transaction process: When a user wants to receive Bitcoin, they simply provide their Bitcoin address to the sender. The sender, when initiating the transaction, sends Bitcoin to the account corresponding to that address on the blockchain. Once the transaction is confirmed by the network and recorded on the blockchain, the user successfully receives the Bitcoin.
Uniqueness of addresses: Each Bitcoin address is unique, ensuring that Bitcoin can be accurately sent to the specified user account. Additionally, the generation of addresses is based on cryptographic algorithms, providing high security and ensuring the reliability of transactions. - Each address is generally used only once
Enhancing privacy and security: To improve the privacy and security of transactions, it is generally recommended to use Bitcoin addresses only once. If an address is used multiple times, attackers can analyze transaction records on the blockchain to more easily track the transaction behavior and fund flows of the address owner, thereby exposing users' private information. Using a new address each time to receive Bitcoin can make transaction records more dispersed, increasing the difficulty of tracking and protecting users' privacy.
Preventing address marking: In the blockchain network, certain addresses may be marked due to involvement in specific types of transactions (such as illegal transactions). If a user continuously uses the same address, they may be affected by these markings, leading to restrictions or investigations of their account. Using one-time addresses can avoid this risk. - Using addresses to receive bitcoins makes trading
more private
Reducing information exposure: An important feature of Bitcoin transactions is their relative anonymity. When using an address to receive Bitcoin, the true identities of the sender and receiver are not directly exposed in the transaction records; only the address information is recorded on the blockchain. Moreover, since each address is typically used only once, this further enhances the privacy of transactions, making it difficult for outsiders to directly associate blockchain data with specific user identities.
Protecting business secrets: For enterprises or merchants, using Bitcoin addresses to receive payments can protect their business secrets. Transaction counterparts cannot easily learn about the financial status, transaction scale, and other sensitive information of the enterprise through transaction records, helping to maintain the competitive advantage and business interests of the enterprise.
Beyond bitcoin#
- Transactions don’t have to just be transactions
- Transactions can contain:
- –Executable code
- In fact, BTC transactions are scripts
- Scripts specify when outputs can be spent–Contracts
- Set conditions for allowing outputs to move–Random data to be timestamped
Private Chains#
- Change the game to require signed blocks
- Limit miners to some authorized set
- Useful for adding other rules or preventing block “takeovers”
- Approach being used to trade securities on a blockchain
- Same crypto physics apply…
Blockchain Properties#
Transactions#
• As with enterprise transactions today, Blockchain is a
historical archive of decisions and actions taken.
• Proof of history provides provenance.
Immutable Ledgers#
- Blockchain saves data through transactions
- Commonality with traditional databases: Like existing databases, blockchain is also a way of storing and managing data, preserving data by recording transactions. Transactions are the basic carriers of data in the blockchain, each containing specific information, such as asset transfers, contract executions, etc. These transaction data are organized and stored in an orderly manner in the blockchain, forming a complete dataset, similar to various business records stored in traditional databases.
- Immutability of blockchain data after writing
- Difficulty of modification: Once data is written to the blockchain in the form of blocks, it becomes extremely difficult to change. This is because the blockchain uses a chain structure, where each block contains the hash value of the previous block. The hash value is a fixed-length string obtained by hashing all the data within the block, which has uniqueness and sensitivity; any slight change in the data within the block will result in a completely different hash value. To modify data in a block, one must simultaneously modify that block and the hash values of all subsequent blocks, as the hash values of subsequent blocks are calculated based on the contents of the preceding blocks.
- Consensus is required: In addition to recalculating a large number of hash values, modifying data in the blockchain also requires the consensus of the majority of nodes in the network. In the blockchain network, nodes verify and confirm the legality of blocks through consensus mechanisms (such as proof of work, proof of stake, etc.). To modify an existing block, one must persuade more than a certain proportion of nodes in the network to accept this modification, which is very difficult in a decentralized network, as nodes are often independent and have different interests, making it hard to reach a consensus to modify already confirmed transaction records.
- Immutability of transactions
- Transaction records are permanently preserved: “The transaction is immutable, or indelible” emphasizes the unchangeability and permanence of blockchain transactions. Once a transaction is recorded on the blockchain, it becomes an indelible historical record that cannot be tampered with or deleted. This feature ensures the authenticity and credibility of transactions, making blockchain valuable in fields such as finance and supply chain management, as it can provide a reliable and traceable transaction history.
- Blockchain is a read-only and write-only database
- Read-write characteristics: From the perspective of databases, blockchain can be viewed as a read-only and write-only database. “Write-only” means that new transaction data can continuously be added to the blockchain, and once written, it cannot be modified; “read-only” means that users can only read transaction records that already exist on the blockchain and cannot modify or delete them. This characteristic differs from traditional databases, which typically allow for updates, deletions, and other operations on data, while blockchain design focuses more on ensuring data security and immutability.
- Handling errors
- Similar to ledger correction: Just like a ledger written in ink, if an error occurs in a transaction recorded on the blockchain, the original record cannot be directly erased or modified. Instead, a new transaction needs to be created to correct the error. For example, if there is an error in the amount of an asset transfer transaction, a new transaction can be initiated to adjust the asset quantity, restoring the account balance to the correct state. This method of handling errors ensures the integrity and traceability of blockchain transaction records, with all operations being traceable.
Decentralized peers#
Encryption Process#
Standard encryption practices
- Some Blockchains allow for “BYOE” (Bring Your Own Encryption)
- All blocks are encrypted
- Some Blockchains are public, some are private
- Public Blockchains are still encrypted, but are viewable to the public, e.g. https://www.blockchain.com/explorer
- Private Blockchains employ user rights for visibility, e.g.
- Customer – Writes and views all data
- Auditors – View all transactions
- Supplier A – Writes and views Partner A data
- Supplier B – Writes and views Partner B data
We can view the hash value as a real number, set a target bar, and find hash values that are less than the bar. The bar can be raised to decrease difficulty.
Consensus Mechanisms#
- Ensures that the next block in a blockchain is the one and only version of the truth
- Keeps powerful adversaries from derailing the system and successfully forking the chain
- Many Consensus mechanisms, each with pros and cons
Proof of Work#
Proof of Stake#
- Stakeholders invest their coins into POS mechanism
- Faster and friendlier to the environment than PoW
- More coins -> Higher chance of creating the next block
- A transaction fee is given to the miner that creates a block
Proof of Elapsed Time#
• Each node generates a random wait time
• Each node waits for the duration of its randomly determined period before attempting to propose a new block.
• After the waiting period elapses, the node that has waited the longest is eligible to propose the next block.
• One of the fairest blockchain consensus algorithms
Smart Contract#
- Computer code
- Smart Contract
- Provides business logic layer prior to block submission