Understanding Blockchain Fundamentals

Web3 is often described as the potential next phase of the internet. Although it is still developing, the core promise of Web3 is a more decentralized approach to computing. Whether you are enthusiastic or skeptical, grasping the technical building blocks of Web3 is crucial for any engineer or scientist. At the heart of this ecosystem are blockchains, which underpin systems like Bitcoin and Ethereum by offering a cryptographically secure and tamper-proof data layer.

The aim of this guide is to build an intuitive grasp of blockchains, complete with practical tools and clear examples, to prepare you for exploring more complex Web3 concepts, particularly Ethereum. We will center our discussion on Bitcoin's blockchain because its design is relatively straightforward. This will establish a solid foundation for your learning.

Blockchains can seem daunting because they integrate ideas from diverse disciplines such as computer science, cryptography, distributed systems, and economics. The challenge is compounded by the significant hype and misinformation surrounding the topic, which often leads to it being perceived as some form of digital magic. However, this is a misconception. As we will discover, blockchains are clever but can be demystified by approaching them logically, one step at a time.

Helpful tool: Seeing these concepts in action is invaluable for comprehension. Anders Brownworth has developed an interactive demonstration that is perfect for experimentation. I will refer to this tool throughout our discussion and highly recommend you try it out.

Background and Motivation

The initial goal of Bitcoin was to create a "peer-to-peer version of electronic cash…requiring no trusted third party to prevent double spending".

In simple terms, Double Spending is an attempt to use the same digital currency in two separate transactions with different recipients at the same time.

The original Bitcoin whitepaper provides a comprehensive abstract that effectively introduces and justifies the technology. Reading it, you will likely observe two key points:

  1. The primary objective of Bitcoin is to eliminate the risk of "double-spending" without relying on a trusted intermediary.
  2. The term “blockchain” is not mentioned anywhere in the document.

Bitcoin functions as a digital currency that handles payment transactions in a manner that avoids the need for a trusted central party. The security of these transactions is ensured through public key cryptography, where users sign their transactions with their private keys. This principle is a consistent theme across various blockchain technologies.

An Introduction to Blockchain — figure 1

An illustration of signed transactions from the Bitcoin whitepaper.

Core Technical Ideas

This section provides a high-level summary of the essential components that make blockchains like Bitcoin operational. These are the key concepts we will explore in depth:

  • Cryptographic hash functions, such as SHA256. These are essential for generating account addresses and for creating the public/private key pairs used in digital signatures for transactions.
  • Transactions represent data entries that alter the blockchain's state, like transferring 10 bitcoin from Alice to Bob. These transactions are secured with digital signatures (from a private key) to prove authenticity, preventing Bob from fabricating a transaction from Alice.
  • Blocks are containers for a collection of transactions. Grouping transactions improves network efficiency by processing many at once.
  • The blockchain itself is a data structure that securely links these blocks together. It can be thought of as a singly-linked list enhanced with hash functions – a simple yet powerful way to visualize it.
  • A peer-to-peer network is formed by nodes. These nodes share transactions and blocks with each other to agree on the current state and update the blockchain accordingly.
  • A distributed consensus mechanism is the process for deciding which new blocks get added. This is critical as it decentralizes control, forcing nodes to cooperate according to rules and making it hard for malicious actors to inject fake data or attack the system.
  • Cryptoeconomics applies game theory to incentivize nodes to contribute computing power honestly. This is typically done by rewarding them with the blockchain's native cryptocurrency, like bitcoin.
  • An open-source software client is the application that implements all these features, allowing anyone to download it and join the network as a node.

Before we get into the technical specifics, it's beneficial to consider the rationale behind decentralization.

The Principle of Decentralization

In a centralized setup, a single trusted authority has exclusive control over reads and writes to a data store. Your bank, for instance, is the sole entity that can update your account balance in its system, and you trust it to do so accurately. The idea of your bank opening its database for the public to download and modify would be alarming.

This is precisely what Bitcoin does, though. It operates as a decentralized and trustless system, meaning no single authority governs updates. Bitcoin consists of a distributed network of computers, known as "nodes," that run its software. This software holds a full copy of the blockchain and has the capability to append updates, as well as to communicate with other nodes. Anyone can download this software and become a participant.

A traditional database would be untenable for Bitcoin in this scenario. If any node could make changes freely, the system would be rife with conflicts, fraudulent transactions, and inconsistent data across nodes – rendering it useless. Giving write access only to specific nodes would solve the immediate problem but would violate the principle of being trustless, as the network would have to trust those privileged nodes. Reliance on trust brings back centralization, which Bitcoin aims to avoid.

Consequently, Bitcoin uses a blockchain for data storage and a consensus mechanism known as Proof-of-Work to control updates, rather than a conventional database. It is common in distributed networks to have several competing versions of the blockchain at any given moment. The network nodes are programmed to accept the longest chain as the valid one, as this chain represents the greatest cumulative “proof-of-work.” We will revisit the reasoning behind this shortly.

It's important to clarify that simply having a blockchain ledger is not a novel concept. The real challenge is for a distributed network to agree on the correct updates to that ledger. This issue has been a central problem in distributed systems for many years. The term Byzantine Fault Tolerant (BFT) is often used in this context. BFT describes consensus mechanisms that are effective even when participants may not act honestly. Consider Alice broadcasting a transaction to pay Bob 10 bitcoin. How can both Alice and Bob confirm that this payment has been recorded by the other nodes? And how can the network be assured the transaction is valid, ensuring Alice hasn't already spent those same 10 bitcoin elsewhere (a double spend)?

Bitcoin's breakthrough was devising a simple method for a distributed network to achieve consensus on the ledger's state without needing a trusted party. This is the Proof-of-Work mechanism, also called the Nakamoto Consensus Algorithm. In essence, it states that among all versions of the blockchain circulating in the network, the genuine one is the longest, because it has had the most computational effort invested in it. If we assume that honest nodes control at least 51% of the network's computing power, then the longest chain must be the real one. If this assumption were broken, for instance if a group controlled more than half the network's power, they could attempt to double spend their coins, a scenario known as a 51% attack.

In the following sections, we will examine the technical workings behind this consensus.

Cryptographic Hashing Explained

Hashing is a fundamental concept in blockchain technology. Bitcoin employs the SHA256 cryptographic hash function, which has two crucial properties:

  1. The input cannot be derived from the output; the function is non-reversible.
  2. A minor change in the input results in a dramatically different output. This unpredictability prevents attackers from finding patterns in the output that could help them reverse-engineer the input.

To illustrate this, consider the interactive example below. You will notice that every change to the input data yields a completely new hash value. Try hashing a simple entry like {from: “alice”, to: “bob”, value: 10} and observe how the hash is altered when you adjust the transaction details, such as the value.

An Introduction to Blockchain — figure 2

The hash value shifts whenever the input data is modified.

Structure of a Block

Transaction data is aggregated into blocks for storage. Since adding to the blockchain is time-intensive, batching transactions into a single block is more efficient. Each block contains the transaction data and a header with metadata. For our purposes, the focus is on two key metadata fields: the nonce and the hash.

We've seen that the hash is a product of the block's data. Adding a block to the chain requires solving a specific problem: a nonce value must be guessed so that the SHA256 hash of the nonce combined with the block's data fields results in a hash beginning with a certain number of zeros. This target number of leading zeros is known as the block's difficulty level. The more zeros required, the more difficult and time-consuming it is to find a valid nonce. But why is that?

Let's break it down. A SHA256 hash is 256 bits long, represented as 64 hexadecimal characters (since each hex char is 4 bits). Each hex character has 16 possible values, making the total number of possible hashes 16^64. If we need a hash with 20 leading zeros, we have fixed 20 of the 64 characters, leaving 16^(64-20), or 16^44, valid hashes.

An Introduction to Blockchain — figure 3

The probability of randomly finding such a hash is minimal. A block difficulty of 20, therefore, implies a very lengthy mining process to find a suitable nonce. In our simplified example below, we search for hashes with only 4 leading zeros, which makes finding a correct nonce significantly easier.

An Introduction to Blockchain — figure 4

Altering the data even slightly changes the hash and invalidates the block.

Clicking the “Mine” button simulates the process of trying different nonces. When a valid nonce is found, the block turns green, indicating the hash has four leading zeros. This search for the right nonce is the essence of “mining”, evoking the image of a prospector seeking gold.

The “work” in "proof-of-work" refers to the computational effort of guessing nonces to produce a valid hash. Our example only requires four leading zeros, but real Bitcoin blocks presently require 17. A block explorer for Bitcoin's main network shows this, as all hash IDs start with multiple zeros. As previously explained, a higher required number of leading zeros means a more time-consuming mining process.

What is the purpose of making mining so demanding? The answer lies in how blocks are linked together in the chain. This difficulty is a defense mechanism, making it extremely challenging for an attacker to create a convincing false version of the blockchain.

Linking Blocks into a Chain

Besides the nonce and hash, a block's header includes the hash of the preceding block. This pattern repeats, with each block referencing the one before it, forming a continuous chain – hence, the “blockchain”.

An Introduction to Blockchain — figure 5

A diagram illustrating the chain of blocks from the Bitcoin whitepaper.

The example below shows a chain of five green blocks, each with hashes beginning with '0000'. These are all considered valid, “mined” blocks under our simplified rules.

An Introduction to Blockchain — figure 6

In this simulation, each block's hash must start with four zeros, setting the difficulty at "4".

Now, imagine an attacker, Alice, who has 10 bitcoin and wants to double spend it by paying both Bob and George. She alters the data in block 2 of her local copy of the blockchain. This change causes the hash of block 2 to change. Since block 3 contains the hash of block 2, block 3's hash is now also invalid, and this effect cascades to blocks 4 and 5. All blocks from block 2 onwards turn red, indicating they are invalid in this new chain.

An Introduction to Blockchain — figure 7

Modifying block 2 causes blocks 2, 3, 4, and 5 to become invalid.

If Alice were to submit this altered chain to the network, it would be rejected immediately as invalid because the hashes no longer have the required four leading zeros. There is no “proof of work” to substantiate these blocks' validity. The network will always choose the chain with the greatest amount of verified work. Alice would need to re-mine every block from block 2 onwards to make the chain valid again and then submit it for consensus.
However, as we established, mining requires substantial computational effort and time, as demonstrated in the previous step.

An Introduction to Blockchain — figure 8

Re-validating blocks by turning them green is a time-intensive process.

While Alice would be spending time re-mining her modified blocks, the rest of the network would be adding new blocks to the legitimate chain. According to probability, unless Alice controls more than 51% of the network's computing power, her chances of catching up and successfully executing her attack decrease exponentially with every new block added to the main chain. Nodes will always prefer the longest valid chain, as it holds the most proof-of-work. Under the assumption that the majority of computing power is in the hands of honest nodes, the longest chain is the canonical one. This is the underlying security mechanism of Bitcoin's blockchain within a public, decentralized, and trustless environment.

Proof-of-Work, while the first successful implementation, is not without its flaws, such as being slow and computationally demanding, and it relies on the honesty of the majority. There are other mechanisms like Proof-of-Stake that address these issues, and will be explored in another article.

Economic Incentives and Cryptoeconomics

A significant question remains: if mining is so resource-intensive, why do individuals bother to do it? The electricity and specialized hardware are costly, not to mention the environmental concerns. This brings us to game theory and the field of cryptoeconomics, which focuses on designing incentives for honest participation in a network like this.

An interesting fact is that in Bitcoin, mining is the only method for creating new bitcoin. Each time a miner successfully adds a block, they can include a special transaction that awards them a set amount of bitcoin (6.25 as of 2022, halving every few years). This is called a miner reward, and combined with transaction fees, it provides the primary financial incentive to cover mining costs. Other blockchains like Ethereum also provide rewards in their native currency, ether. Using a native currency for rewards is logical. If rewards were in a fiat currency like USD or JPY, the system would depend on off-chain authorities, contradicting Bitcoin's core philosophy. The reward is also strategically set high enough to make honest mining more profitable than attempting to attack the network. If you had the resources to take over the network, you would likely find it more lucrative to mine legitimate blocks instead.

Conclusion and Next Steps

If you've followed along, you now have a solid grasp of blockchain basics. For those interested in more, a list of resources is provided below. It's important to remember that a deep understanding of Bitcoin's blockchain and its consensus mechanism provides an excellent foundation for learning about Ethereum and developing Web3 applications. Even if Web3 isn't of interest to you, this subject is a fantastic way to learn core principles in computer science and economics. Thank you for reading.

Further Reading and Resources