Consensys
consensys-bg-mesh-light

Knowledge Base

How Does a Blockchain Work?

A deep dive into the workings of a blockchain, including mining, consensus algorithms, block times, wallets, nodes, and more.

Joe-Lubin-How-Ethereum-Differs-Bitcoin Short

BLOCKCHAIN EXPLAINED BY JOE LUBIN

How Ethereum Differs from Bitcoin


How the Bitcoin Blockchain Works

A Spreadsheet Analogy

Imagine a blockchain as a spreadsheet. Everyone has access to the data within each individual cell and saves a copy to their personal computer. The data within the spreadsheet is shared between multiple people. However, nobody can alter an individual cell or change the existing information. In the blockchain world, we call this “immutable”—the inability to change or modify something over time. Now, imagine you wanted to add a new cell to the spreadsheet. This would require approval from members who have access to the spreadsheet data. Once a majority of the spreadsheet owners approve the new cell, the data would then be added to the main spreadsheet.

Back to Bitcoin

As an aside, you’ll notice us use “Bitcoin” with an uppercase B to refer to the overall blockchain and “bitcoin” with a lowercase b to refer to the Bitcoin network’s cryptocurrency. Next, let’s apply the above concepts to further understand how the Bitcoin blockchain works. If Alice traded one bitcoin to Bob, that transaction would be recorded in the Bitcoin blockchain ledger. Viewing the ledger would show how much bitcoin was exchanged when the transaction occurred, and the corresponding bitcoin addresses of Alice and Bob that sent and received the transactions.

Bitcoin Miners

Once a transaction has been made, it must be verified by people called miners. When a transaction occurs—such as Alice’s transaction with Bob—it is grouped together in a mathematically protected “block” with other transactions that have happened in the same time frame. Miners then use computers with incredible computing power to solve the block mathematically. The first miners to solve the block and validate the transactions are rewarded with bitcoin. This is the only way that bitcoin can be created. Finally, each block is connected to the previously verified block, creating a chain of blocks, hence the name blockchain (we know, super creative). One more thing that you need to understand about the blockchain is that it is immutable, which means, once the data has been added into a block, it can never (like never ever) be changed. In a practical application, once Alice trades her bitcoin and the transaction is verified, she cannot get it back or cancel the trade. It’s gone. However, it is worth noting that this benefits individuals and businesses that deal with credit card transaction, which aren’t settled until later dates. A malicious individual, using a credit card could purchase an item and then cancel the transaction leaving an individual or business with lost revenue.

Consensus Algorithm

When Alice trades her bitcoin to Bob, the transaction is represented as a block, which needs to be broadcasted to the blockchain network. The blockchain network is comprised of nodes or participants of the network that validate and relay transactions to transmit information. All nodes are operated voluntarily and are used to verify the correct transactions on the blockchain. Nodes follow consensus rules, which are rules that are agreed upon by the community. A change in the consensus rules requires 95% of the community to approve, which makes it incredibly difficult for one group to change the Bitcoin software. Other consensus algorithms require a lower percentage to change the software protocol. Nodes relay block transactions and validations to other nodes so that the network remains updated. Nodes do not mine bitcoin. However, all miners typically run a full node to validate and relay bitcoin transactions effectively. Since both miners and non-miners execute validation and relay by operating nodes, they all participate in the consensus process.

Bitcoin Mining

Bitcoin miners verify legitimate transactions and create new bitcoin as a reward for their work. A transaction is considered verified once the miner solves a cryptographic (mathematical) puzzle. Bitcoin uses a protocol called proof of work, which has a broad goal to prevent cyber attacks from any single entity or group. More specifically, Bitcoin uses the Secure Hash Algorithm 256 bit (SHA-256). Computer chips can run the SHA-256 algorithm to generate an output, which is referred to as a “hash.” Hashing, the process of creating multiple hashes, is used to solve the mathematical problem, in which the end answer is the known and expected hash value.

A Simplified Example

  • To solve the problem, the hash value has to begin with three zeros: “000”

  • The input then varies every number finding a match for a “Solution”

  • It takes 6,518 tries to find a value that has “000” in the first three digits.

Solution 1 ———088djldkh2h5h3kjhk24gd5h2h5h3kjhk24gd5kh2h5h

Solution 2 ———73485jfljroi5635h3kjhk24gd5we94ee356h2hkh2h5h

Solution 3 ———d89sdf8sge9nxc894opl8qjroi5635h3kjhk24gd5we94

……

Solution 6517 ———088djldkh2h5h3kjhk24gdjroi5635h3kjhk24gd5we

Solution 6518 ———00088djldkh2h5h3kjhk24gdjhk24gd5h2hk24g4f4

Given the input of “Solution 6518,” any miner could verify that this undeniably creates a hash with three zeros in the first three digits. The first miner to solve this problem would broadcast their answer to the network and be rewarded with bitcoin. The block would then be considered verified, and it would be added to the blockchain. This puzzle is a representation of the type of puzzle a Bitcoin miner would need to solve and not a real puzzle.

Confirmation Difficulty

A standard Bitcoin block requires millions of hashes to solve the puzzle. In the Bitcoin blockchain, the complexity of the puzzle changes every 2,016 blocks to ensure that the average block confirmation time takes ten minutes. Therefore, it is possible for the problem to become easier to solve if confirmations are consistently taking longer than ten minutes. Although, usually the problems become more challenging to solve, which has made it very expensive and highly time intensive to confirm blocks. Initially, bitcoin could be mined using consumer-grade computing chips. However, due to this increase in difficulty, bitcoin mining currently requires enormous hashing power, and the industry utilizes application-specific integrated circuit (ASIC) chips. ASICs are customized specifically for bitcoin mining, rather than general-purpose functions. These chips are also incredibly expensive, which has resulted in a select group or pool of miners that dominate the lion’s share of the bitcoin mining activity.

Block by Block

Another important aspect is that each block contains the solved hash from the parent or previous block. Each new block requires the parent block’s hash, ensuring a chronological block order. For instance, block 30’s hash is necessary for block 31. Block 31 cannot exist until block 30 has been verified and added to the blockchain. This guarantees that each block is connected all the way back until the first block, which is commonly referred to as the “genesis block.” To change any single block would require changing every preceding block, simultaneously. Lastly, each block is immutable, which again, means that once a transaction occurs, it cannot be reversed. This further ensures that once added the blocks within the blockchain cannot be altered.

Supply

The supply of Bitcoin is limited to 21 million bitcoins, which is estimated to occur around the year 2140. The amount of bitcoin rewarded to miners is halved every 210,000 blocks. Therefore, the reward will continue to split until the twenty-one millionth bitcoin is created, at which point miners will only receive transaction fees.

Why Don’t Miners Cheat?

Bitcoin is deeply rooted in cryptography, computer science, economics, and various other disciplines. One of those disciplines happens to be game theory, which uses mathematical models to predict how rational players will react when given individual choices or situations. Bitcoin utilizes game theory concepts to incentivize the desired action of miners and users.

How the Ethereum Blockchain Works

The Ethereum blockchain functions similarly to the Bitcoin blockchain, but with several important differences.

Accounts: Wallet Addresses

There are two types of Ethereum accounts:

  • Contract Accounts

  • Externally Owned Accounts (EOAs)

Externally Owned Accounts are similar to Bitcoin addresses and are controlled by private keys. Ethereum accounts also have both public and private addresses for users to interact with to transfer ether. Contract accounts, which are used to communicate with smart contracts, are controlled by their contract code and are activated through an EOA. Ether is required for every transaction on the network (contract accounts and EOAs) to prevent malicious attacks, such as network spamming.

Transactions

The Ethereum network requires users to pay a transaction fee when using the blockchain. This protects the Ethereum blockchain from frivolous or malicious computational tasks, such as users spamming the network with an overload of useless transactions. The more complex the transaction (more lines of code), the more expensive the fees. These fees are paid in ether (ETH) and are referred to as the “gas price.”

Ethereum Network

Like bitcoin, Ethereum utilizes nodes that are operated voluntarily in order to verify the correct transactions on the blockchain. Nodes hold information such as:

  • the entire Ethereum transaction history

  • the most recent information about the state of smart contracts

  • the balances of accounts

  • and much more

There Are Two Types of Nodes

  • A full node synchronizes the blockchain by downloading the whole chain, from the genesis block (first block) to the current block.

  • A light node does not download the entire chain, but can still effectively verify transactions.

Nodes do not mine the Ethereum blockchain. However, all miners typically run a full node to validate and relay Ethereum transactions effectively. Since both miners and non-miners execute validation and relay by operating nodes, they all participate in the consensus process.

The Ethereum Virtual Machine

At the foundation of these programmable applications is the Ethereum Virtual Machine (EVM), which is the executable and trustless environment for smart contracts. The EVM executes a contract with whatever rules the developer initially programmed, such as sending money from Alice to Bob. The network is made up of thousands of computers processing the execution of the programs. Therefore, each node in the Ethereum network is actually running the EVM, and consequently, each node executes the same code. The EVM can execute these programs through a bytecode language. However, developers are also able to use the Ethereum programming language Solidity to write smart contracts and other more advanced applications.

How Does Ethereum Mining Work?

Ethereum miners verify legitimate transactions and create new ether as a reward for their work. A transaction is considered verified once the miner solves a cryptographic (mathematical) puzzle. Similar to Bitcoin, Ethereum uses a proof of work (PoW) protocol, which has a broad goal to prevent cyber attacks from any single entity or group. One notable difference between Bitcoin mining and Ethereum mining is the block confirmation time. While a Bitcoin block confirmation requires an average of 10 minutes, an Ethereum block is confirmed in about 14 seconds. And just like Bitcoin, the algorithm automatically adjusts the difficulty of the cryptographic problems so that the average block time is 14 seconds. With Bitcoin, miners often work to mine the same block and sometimes those blocks are confirmed at very similar times, in which case the first one is a reward and added to the existing chain, while the second confirmed block is called an “orphan.” In Bitcoin, the orphaned blocks are mostly useless and not part of the main chain. Ethereum operates using a similar concept called the GHOST (Greedy Heaviest Observed Subtree) protocol, which essentially means the chain with most computation done upon it (not necessarily the longest) is the main Ethereum blockchain. Ethereum’s fast block time results in an increase in the number of orphaned blocks, which are referred to as “uncles” on the Ethereum blockchain. The main difference is that Ethereum incentivizes mining uncles and provides a reward, as opposed to Bitcoin, which only rewards the first confirmed block.

Mining Uncles Provides Two Significant Benefits

  • It decreases centralization by incentivizing individual miners to mine orphan blocks meaning they don’t have to be part of a large mining pool because they can still receive a reward.

  • It increases the security of the chain because all blocks, including uncles, have to follow the same mining protocol, which increases the amount of work on the main (heaviest) chain.

Proof of Stake

The Ethereum network currently uses the proof of work algorithm and will be transitioning to using a proof of stake algorithm called Casper. Proof of stake (PoS) is a consensus algorithm that requires each miner to own a stake in the native currency, ether in this case. It is worth noting that not all PoS protocols are the same, but are similar in regards to the basic concept. Proof of work (PoW) protocols, such as Bitcoin, reward mining effort (validations) with individual bitcoins. The Proof of Stake Casper algorithm is a smart contract, in which the miners—now referred to as “validators”—must send a deposit or “stake” to the Casper contract. The validators or stakers now take turns proposing and voting on the next block, each having one vote per block. The votes are also weighted by the amount of stake, so a validator who stakes 100 ETH will have a more meaningful vote than a validator who stakes 40 ETH. If these validators produce anything that the Casper protocol considers invalid, then the validators lose their stake, which incentivizes validators to follow consensus.

What Is Ether?

Let’s say you want to buy ether, the cryptocurrency native to the Ethereum blockchain. The simplest way to do that would be to set up an account on one of many widely-used cryptocurrency exchanges, like Coinbase or Kraken. These exchanges allow you to set up an Ethereum wallet so you can buy, sell, send, and receive ether from anyone else who also has a blockchain wallet that can send and receive ether.When you purchase ether, your transaction becomes part of a block on the Ethereum blockchain. This block contains a record of the most recent Ethereum transactions that have taken place anywhere in the world by anyone running the Ethereum protocol. It also includes a cryptographic hash (a mathematical algorithm) record of the most recently validated block on the Ethereum blockchain.This block with your transaction record won’t become part of the Ethereum blockchain until one of the many computers running the Ethereum network solves or discovers the cryptographic hash matching the unique hash tied to that block. This solving process is commonly known as “mining.” When the hash is solved or discovered, the block with your transaction record is instantly added to the end of the blockchain that’s maintained by every one of those computers, and your transaction becomes part of the Ethereum blockchain’s permanent record.It’s worth noting that “ether” and “Ethereum” are often used interchangeably to refer to the cryptocurrency that operates on the Ethereum blockchain. This is not technically correct, as ether is the term for the cryptocurrency token used on the Ethereum blockchain platform. If we were to represent this relationship in terms of computer software (which it is), Ethereum would be the operating system, and ether would be the application. In a non-computer software analogy, Ethereum is comparable to a vehicle while ether acts as the fuel that powers and enables the machine to run efficiently.