DEV Community

Cover image for The Blockchain Handbook
Gursimar Singh
Gursimar Singh

Posted on

The Blockchain Handbook

Blockchain is a new and exciting technology that underpins Bitcoin and other cryptocurrencies.

But there are many other applications for blockchain outside the world of cryptocurrency. And that’s what you’ll learn about here.

This article is going to be a divided into three parts:

  • Introduction to Blockchain

  • Embracing Blockchain

  • How does Blockchain work?

Introduction to Blockchain

In the first part, I’ll introduce you to blockchain: what it is, what it is not, and why it is attracting business interest.

At the end of this part, you should be able to understand:

  • The Purpose of Blockchain, and Why It Is Attracting Interest

  • On a Basic Level, How Blockchain Works

Why Blockchain Technology is Useful

Today, many transactions require a trusted third party as a ‘go-between’. For example, users often use banks or organizations, such as PayPal or Western Union, when transferring money to each other.

Such a third party is often required for trust: both parties want to be confident that they will not be cheated, and everything is above board. Sometimes it is also required because of cultural, border, or compliance issues.

However, these third parties usually require a payment for their services and using these third parties can slow down the transaction. Many people would be interested in a solution that does not require such a trusted third party.

Today, many financial transactions pass through some sort of central control or oversight. For example, governments control their currency: how much is produced or circulated. Similarly, governments often control financial transactions such as the purchase of gold or securities.

Many people would likely be interested in a currency or solution that was not controlled by a single authority or government.

There are many applications that could benefit from a database where existing data could never be updated or deleted — only added to.

For example, a solution that traced an aircraft part from manufacture, through testing, and finally onto an aircraft. If existing data could never be updated, anyone using this solution would be confident that they had the correct data, and it had not been changed, corrupted, or deleted.

Image description

Blockchain is a technology that has the potential to address these issues and opportunities. But before you can find out how, you need to learn a little about what blockchain is, and what it is not.

What is the Blockchain?

In essence, a blockchain is a way of storing data: it’s like a database. But databases are normally centralized: the data is managed by one process or computer.

Blockchain is a distributed database that is stored on many different computers in a peer-to-peer network. There is no single controlling computer or ‘boss’. This may not seem logical. But one of the most exciting features of blockchain is how it allows this to work. You will see how shortly.

Image description

With blockchain, all data–the entire blockchain–is usually stored on all computers. Every computer, or node, communicates with others, broadcasting database changes and other information.

Such a structure provides resilience and reliability advantages: there is no outage if one, or even several, nodes fail.

In a regular database, any data can be changed, updated, or deleted. Blockchain is different: only new information can be added. By its design, existing information can never be updated or deleted.

This may seem strange, but you will see soon how this is actually one of the key security features of blockchain.

So how can a database that cannot be changed or deleted work? Blockchain is often referred to as a ledger-based system. Some call it Distributed Ledger Technology. Changes are recorded as new information.

Distributed Ledger Technology: A technique of storing data on many synchronized, geographically separate computers with no central controlling process. Often used in relation to blockchain.

Let’s look at an example.

Suppose you have an application that tracks the number of widgets in a warehouse.

When the first order of widgets is received, a normal database would create a new record holding the number of widgets.

In Blockchain, a new record is also created, recording that 5 widgets have been received. So, you know you have 5 widgets.

Suppose 1 widget is removed. In a normal database, the record could be updated with the new number of widgets: 4.

In blockchain, a new record would be added, recording that 1 widget had been removed. So, you can find out that 4 widgets remain.

In a regular database, the record could be deleted if all widgets were removed.

In blockchain, a new entry setting the number of widgets to 0 performs a similar function.

What Does Trustless Mean?

The blockchain features we’ve covered so far allow the blockchain to work as a data management system without trusting any of the computers storing the blockchain.

These computers can even exist on public networks with no centralized security management. For example, anyone can create a Bitcoin node and participate in running Bitcoin.

Malicious users cannot change or corrupt the contents of the blockchain unless they can control updates to the blockchain. We’ll discuss this in more depth later in this tutorial.

Removing the need for trusted nodes allows us to implement exciting applications over the internet without a controlling organization. These are sometimes referred to as permissionless. Bitcoin is an example of a permissionless blockchain implementation.

Although blockchain applications can work with a permissionless network, many applications will not want this. They will want to retain some control over their blockchain: who can join, what they can do, and being able to verify their identity.

These solutions will operate in a trusted network, with a security management system maintained by some kind of centralized authority. These are sometimes referred to as “permissioned.” Many blockchain frameworks such as Ethereum and Hyperledger are permissioned.

What does Permissioned / Trusted mean?

  • Access limited

  • Centralized security

  • Generally used on an intranet

  • Cannot be anonymous

What does Permissionless / Trustless mean?

  • Anyone can join

  • No centralized security

  • Can use internet

  • Maybe anonymous

Why has blockchain become such a hot topic?

In the past, commercial transactions have been controlled by a central, trusted authority. For example, banks, exchanges, and governments.

Blockchain offers an alternative where a trusted central authority is no longer required to manage or monitor transactions, even for commercial transactions or currency. Also, boundaries and borders are no longer an issue as blockchain applications can be used over the internet.

The first blockchain was proposed by an unknown computer scientist using the name Satoshi Nakamoto in a paper proposing a cryptocurrency, which is now known as Bitcoin.

Since then many different applications, including various cryptocurrencies, have been created based on this technology.

Image description

  • Blockchain is a decentralized, distributed data management system. Data can only be added to a blockchain. It cannot be changed or removed.

  • Blockchain offers excellent reliability and resilience.

  • Blockchain can operate over a public untrusted network, and maintain integrity.

  • Blockchain applications can perform functions such as commercial transactions without requiring a trusted third party or ‘boss.’ This is one of the major benefits of blockchain.

  • Blockchain was created in 2008, and first implemented for the cryptocurrency Bitcoin.

How Does Blockchain Work?

Blockchain is a very new technology. As such, there are few rules, standards, or best practices to follow when using blockchain. This is exciting, as new applications are developed to use blockchain technology in different ways.

So, the best way to see how blockchain works is to look at current examples. The best known implementation of blockchain is the cryptocurrency Bitcoin.

Clients and nodes:

Bitcoin users are connected over the internet, and are divided into two groups, clients and nodes. Clients are the users of Bitcoin. Nodes are the computers where the Bitcoin blockchain resides, and which participate in the execution of Bitcoin.

Note: A node can also be a client.

Wallet software:

Suppose a user, Jane, wants to transfer one Bitcoin to another user, Brian.

Jane will have a Bitcoin client program executing on her computer. This is called a wallet, and there are several to choose from, including Bither, Electrum, and Mycelium.

Jane will have previously used this wallet to open a Bitcoin account and obtain bitcoins. The wallet software records the bitcoins that Jane has in her possession.

Note that the wallet is not used by the blockchain network to store data. Records in the blockchain are used to determine how many bitcoins can be spent by Jane.

To transfer a Bitcoin, Jane will use the wallet software, specifying the amount — one Bitcoin, and the address of the recipient — Brian.

Every Bitcoin user has a unique address that is created when they create their Bitcoin account. So, Brian will also have a wallet, and will have opened a Bitcoin account.

Jane will send the transfer request. The wallet software will send this request to one or more Bitcoin nodes. The wallet will also sign this transaction with a private cryptographic key that only Jane’s wallet knows.

The transaction will be transmitted to all other nodes. Each node will validate the transaction. This validation will include checking the signature of the transaction with a public key that Jane’s wallet transmits to everyone. Anyone can validate a signature, but only Jane can sign a transaction.

Every node will combine this transaction with others into a block. Periodically, around every 10 minutes or so, every node will try to add this block of transactions to the blockchain.

Blockchain processing will select one node to add newly blocked transactions to the blockchain. How this node is selected can vary between different blockchain technologies. However, this is usually a different node every time.

This winning node will notify all other nodes in the blockchain that it is adding a block of new transactions to the blockchain.

Every node will add this new block to its local copy of the blockchain.

When Jane’s transaction has been added to the blockchain, it is confirmed. Brian’s wallet is notified that he has received one Bitcoin, and Jane’s wallet is notified that she has sent one Bitcoin.

Now we will look at the security of blockchain. All transactions are signed using a private key that only Jane knows. So, like car keys, providing Jane keeps this private key a secret, others cannot perform transactions in her name. No one can transfer bitcoins from her account.

Many nodes validate every transaction. A single node cannot fake transactions, or pass a transaction that is not valid.

Existing blocks of transactions cannot be altered. A node cannot change or corrupt existing transaction data.

Jane’s transaction has been completed securely, without trusting all of the nodes that perform Bitcoin processing. There is no central computer or controlling process — all processing is distributed among the nodes that perform Bitcoin processing.

Anyone in Bitcoin can create a node and start Bitcoin processing over the internet.

Steps:

  • A transfer request is sent to the blockchain nodes

  • The transaction is validated by the nodes

  • Once validated, the transaction will be combined with others to form a block

  • After a period of time, each node attempts to add the block to the blockchain

  • Once successful, every node adds the new block to its copy of the blockchain

  • The receiver is notified of the successful transfer

We have seen one example which is bitcoin. There are many more such examples to explore.

In the next part, we’ll explore blockchain’s effect on the world and how blockchain can be implemented.

Embracing Blockchain

As we’ve been learning so far, blockchain is a new and exciting technology that underpins the Bitcoin digital currency ecosystem and other cryptocurrencies.

Many organizations are only now just beginning to recognize blockchain’s potential and are starting to identify how they can benefit from this technology.

In the previous part, we learnt the basics of blockchain with the example of bitcoin. Now, we’ll explore blockchain’s effect on the world and how blockchain can be implemented.

We’ve just discussed how blockchain offers an alternative to the past where commercial transactions were performed using a trusted third party: a bank, a government, or a broker.

You also saw that blockchain technology can work over the internet, bypassing boundary and border issues. It also can be used for applications that benefit from data that cannot be removed or updated.

Many of the early blockchain applications were cryptocurrencies: online currencies that are not controlled by any country, bank, or similar entity. Bitcoin was the first cryptocurrency, but others include Ripple (XRP), Peercoin, Litecoin, and Ether, which is used by Ethereum.

Cryptocurrencies provide an alternative way for people and organizations to pay for goods and services: one that is not controlled by any central government, bank, or organization.

Cryptocurrencies have also demonstrated that a blockchain application using trustless computers over the internet can provide a secure commercial framework in the real world.

What is a Smart Contract?

Another area of interest in blockchain applications are smart contracts. Not a contract in the normal sense, smart contracts are a set of business rules that are stored and implemented on a computer system. They allow transactions to be automatically enacted without needing a third party, such as a lawyer or bank.

Smart Contract: A set of business rules stored in a computer system that is executed automatically by that computer system based on a set of logic or clauses. Can allow contracts to be agreed and enacted without a middleman such as a lawyer.

Let’s explore with an example:

Suppose a person, Charlotte, is about to rent an apartment for two weeks from a second person: Jackson. Suppose they use a blockchain application that has been created to manage apartment rentals as a smart contract.

The terms of the rental agreement are coded in the smart contract: rent, start date and other details.

Image description

Charlotte pays the rent in a cryptocurrency such as Bitcoin. Jackson provides the code to access the apartment in the smart contract.

At a defined date and time, the smart contract automatically verifies that all terms of the contract have been met: rent has been received, and apartment entry code provided.

Once all terms are met, the smart contract automatically forwards the rent payment to Jackson, and the entry code to Charlotte.

If any terms of the contract are not met — for example, the rent has not been received — the contract is not enacted. No entry code is forwarded to Charlotte, no rent is forwarded to Jackson.

The contract is stored on many computers, and cannot be deleted or changed. Only new information can be added. Jackson and Charlotte are confident of the integrity of the information in the contract, and that it can be trusted as a valid document.

This is all done without a trusted central authority, such as a rental agency, managing the contract.

Applications of the Blockchain

Blockchain is still a relatively new technology, but there are some interesting applications that have already been implemented across many industries.

Bitspark: Bitspark is a remittance platform. It allows people to transfer money to each other. There are several similar applications including Abra and Veem.

IBM Food Trust: IBM Food Trust is a blockchain-based solution to track and manage food products as they pass from source to customer. This allows retailers, wholesalers, and suppliers to improve efficiency, trace origins of products that may not match requirements, and reduce food loss and waste.

Boeing: Boeing has used blockchain technologies with the Internet of Things (IoT) for tracking and maintenance of aircraft parts. This allows them to predict maintenance events, optimize production operations, and extend the lifecycle of components.

Arcade City: Arcade City is a peer-to-peer ridesharing application that uses blockchain.

Storj: Storj is a cloud storage solution. Users can rent out unused space on their local disks, or rent cloud based storage using these local disks.

The two major features of blockchain — that it is distributed, and that data cannot be updated — provides many interesting opportunities for businesses. In many ways, blockchain technology can be used to decentralize and record almost any transaction or movement.

A couple more interesting examples include,

Publishing music: In 2015, the startup uj0 Music released the song Tiny Human by singer-songwriter Imogen Heap using a Proof of Concept blockchain-based distribution system. Using this system, users immediately downloaded and paid for the song. Royalties were sent using blockchain immediately to the songwriter and collaborators. This showed how music could be released without the current music industry labels and publishers.

Human trafficking: One-fifth of the world’s population does not have an official document to prove their identity. Without legal identification, these people are at risk of human trafficking.

The United Nations is exploring ways that blockchain solutions can resolve this. Blockchain solutions are also being developed for online identity management and verification.

There is no doubt that blockchain technology has generated more than just interest. Most large IT organizations, including IBM, have positioned blockchain as a strategic technology. Many large organizations are actively pursuing blockchain solutions and there are many startups working with blockchain.

Blockchain has captured the interest of many organizations, from small startups to large financial institutions.

Many claim that blockchain technology is about to revolutionize the way business is done, while others are more skeptical. But most technology commentators agree that blockchain is a valid and valuable technology that is here to stay.

Here are some comments from a couple big players:

Like any emerging technology, the potential implications of blockchain seem both vast and at the same time not fully comprehensible. Many in the popular press have speculated about blockchain-driven notions as diverse as the development of borderless global currencies to the creation of fraud-proof voting tools to help eliminate electoral corruption and fraud. We may not yet even fully comprehend blockchain’s “killer app.” For corporate finance professionals, however, the potential implications of blockchain are more tangible. — J P Morgan Chase

And,

Even though the initial hype surrounding blockchain, and the prolonged blockchain “winter” that followed, are beginning to settle, this is no time to rest on our laurels. IT leaders must prepare for the inevitable blockchain “spring” on the horizon, bringing with it core-enabling technologies and significant opportunities for digital business. — Gartner

Large organization projects:

Forbes has recently created the Forbes Blockchain 50: a list of 50 companies in the US with minimum revenues or valuations of one billion USD actively exploring blockchain.

  • Samsung: Samsung is using a blockchain-based solution to overhaul how its battery manufacturing subsidiary manages contracts.

  • Nestle: Swiss based Nestle have commenced working with IBM Food Trust to manage and trace the lifecycle of food products.

In summary so far,

  • Many of the early blockchain applications have been cryptocurrencies such as Bitcoin.

  • Smart contracts are another interesting feature of blockchain that is being investigated.

  • Although there is disagreement as to how blockchain will be implemented, and its long-term effect on the computing industry, most agree that blockchain is an important and valuable technology that is here to stay.

  • Existing blockchain applications include shared storage, cryptocurrencies, and payment processing systems.

  • Some examples of potential future blockchain applications include land registration, identity management, and tracking of physical items such as diamonds.

  • Large organizations are actively investigating and investing in blockchain.

How to Get Started with Blockchain

There are many blockchain frameworks and tools designed to allow organizations to create their own blockchain solutions without starting from scratch.

Ethereum: One of the first blockchain frameworks created in 2014. It’s an open-source framework including the cryptocurrency Ether. It’s currently one of the most popular frameworks.

Hyperledger: An umbrella project of open source blockchains. It was started in December 2015 by the Linux Foundation, with input from companies such as IBM and Intel.

Quorum: An open source private blockchain framework released by JPMorgan Chase & Co.

In this article, we will learn more in detail about Hyperledger.

Hyperledger Fabric is a framework for private blockchains. It is not designed to operate as an open, permissionless blockchain like Bitcoin.

To achieve this, all Hyperledger Fabric actors — including client applications, administrators, processes within the blockchain, and more — must have a valid digital identity: an X.509 digital certificate. One or more external Membership Service Providers (MSP) will create, manage, and authenticate these certificates. Hyperledger Fabric provides instructions on what is required from MSPs.

Each actor has a set of permissions defined: what they can and cannot do. The digital identity and permissions together are called principals. Hyperledger Fabric includes functionality to use and manage principals.

In addition to identity and permissions, Hyperledger Fabric provides rules on how to create a blockchain These rules are backed by code, APIs, and documentation.

  • How to identify a user

  • How to setup permissions

  • How to setup a network

  • How to create a smart contract

  • How to create and submit a transaction

  • How to respond to a transaction

  • How a client connects to the blockchain

  • How to handle errors

For example, a key function of Hyperledger Fabric is the creation and operation of smart contracts. Hyperledger Fabric provides a set of rules for creating smart contracts.

Smart contracts are programs written in a supported chaincode.

Hyperledger Fabric provides libraries and APIs that can be called to define the smart contract.

package com.blog.chaincode.example

import java.util.ArrayList;
import java.util.List;

import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contact;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Default;
import org.hyperledger.fabric.contract.annotation.Info
import org.hyperledger.fabric.contract.annotation.License;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ledger.KeyValue;
import org.hyperledger.fabric.shim.ledger.QueryResultsIterator;

public class final Example implements ContractInterface {
...
Enter fullscreen mode Exit fullscreen mode

Hyperledger Fabric is designed for enterprise-level blockchains that handle a large number of transactions that must be processed quickly.

Unlike other frameworks, such as Ethereum, Hyperledger Fabric does not provide its own cryptocurrency.

What is Hyperledger Fabric comprised of?

Hyperledger Fabric provides libraries, APIs, and documentation that can be used to implement a blockchain solution. These resources provide services including commands, RESTful interfaces, and performance metrics.

  1. Hyperledger Fabric provides libraries that are installed onto each computer: clients and nodes.These can be on UNIX, MacOS, or Windows machines.

  2. Hyperledger Fabric provides a suite of APIs to assist in creating and running a private blockchain.These APIs are included in software development kits (SDKs) for programming languages includingGO, Node.js, Java, and Python.

  3. Hyperledger Fabric provides samples to allow users to learn about Hyperledger Fabric, and to get started in implementing it.

  4. Hyperledger Fabric provides documentation and tutorials for administrators, operators, and application developers.These explain the rules that Hyperledger Fabric has for implementing a blockchain solution.

  5. Hyperledger Fabric provides line commands that can be used to manage blockchain nodes and Hyperledger Fabric resources.

  6. Hyperledger Fabric executables provide an HTTP RESTful operations API. This is designed to be used for operations: not administrators or users.

  7. Hyperledger Fabric provides performance data that can be processed using Prometheus monitoring software.

Blockchain frameworks often include additional features not common in traditional blockchain solutions.

Hyperledger Fabric provides services allowing private, confidential transactions processed by the blockchain, to be hidden from some nodes, using private channels.

Hyperledger is an umbrella project of the Linux Foundation. It includes several blockchain-related projects, including five blockchain frameworks.

Image description

The features of each blockchain framework vary: some may have features that others do not.

There are tools available, including some within Hyperledger, that can be used with these blockchain frameworks.

Benchmark: Tools such as Hyperledger Caliper allow blockchains to be compared. These tools measure the performance of a blockchain implementation using pre-defined test cases.

Operation and management: Tools such as Hyperledger Cello and Truffe provide features to manage the lifecycle of a blockchain: creation, start, stop, removal. These often include monitoring, logging, health, and analytics features.

View and Query: Sometimes administrators must view individual blocks in a blockchain, and their associated transactions, network information, and more. Tools such as Hyperledger Explorer provide graphic interfaces to do this.

Performance: As networks get larger, blockchain solutions slow down. Some tools provide features to improve the performance of blockchain frameworks or solutions. For example, the Raiden Network attempts to improve the performance Of Ethereum blockchain solutions by avoiding consensus. This is done by performing some transactions outside of the blockchain.

Blockchains in the Cloud

There are some cloud-based blockchain frameworks available. Some of these are part of large cloud offerings, while others are standalone blockchain frameworks.

Some Blockchain as a service (BaaS) providers are:

  • Microsoft Azure

  • Oracle Blockchain Platform

  • Alibaba Cloud

  • IBM Blockchain Platform

  • Amazon Web Services

Summary so far:

  • There are currently no official standards or best practices for blockchain. But some are being developed by standards organizations such as ISO and W3C.

  • Although it is possible to build a blockchain solution from scratch, many will choose to use an existing framework such as Hyperledger Fabric or Ethereum.

  • There are other blockchain-related tools to do things such as manage blockchain solutions, improve their performance, view individual blocks and related resources, or benchmark their performance.

  • There are cloud-based blockchain frameworks: Blockchain-as-a-Service (BaaS).

  • There are many blockchain books and courses available. Open-source blockchain frameworks also provide documentation, and their source code can be viewed.

In the next and final part, we’ll be looking at what hashes and blocks are, and how they work in a blockchain. Also, we’ll discuss how multiple computers work together to store and manage a blockchain.

How Does Blockchain Work?

So far, I’ve introduced you to blockchain and described what it is, what it does, who uses it, and why it is fast becoming an important component in today’s business world.

This is the third and final part of the handbook where we’ll dive more into the technical details of how blockchain works.

Blockchain is an exciting technology that has the potential to revolutionize banking, electronic transactions, electronic contracts, and much more. It relies on technology to ensure that messages cannot be tampered with, are correct, and are sent by authorized people.

In this part, you will explore the actual technology in more detail.

By the end, you should be able to understand:

  • What Hashes and Blocks Are, and How They Work in a Blockchain

  • How Multiple Computers Work Together to Store and Manage a Blockchain

What are Hashes?

An essential tool used by blockchain is the hash. A hash is a string of numbers and letters that can uniquely identify some text or data. So, in many ways, it is like a fingerprint.

Hashes are sometimes called hash values, hash codes, digests, or simply hashes. There are several hashing algorithms, or ways of creating hashes.

Hash: An algorithm used to map data of variable size to a set of data of fixed size. Often used in computer applications for lookup tables, and in cryptography. SHA-256 is one of the algorithms.

Hashes used by blockchain are not simply data generated from other data. They have some distinctive properties that are very useful for blockchain.

Hash Properties:

  • Same length: A hash is the same length — regardless of the length of the original text. Even if hashing the entire Library of Congress, the hash will be the same length.

  • Different: Any change in the original data, no matter how small, must produce a completely different hash.

  • Unique: Like a fingerprint, a hash should be unique. This is not possible in practice, but hashing algorithms make it almost impossible for a collision — a case where a program will encounter the same hash for different data. The probability of a SHA-256 collision is 4.3 * 10–60: almost impossible.

  • Cannot get original data: It should not be possible for the original data to be reproduced from a hash value.

What are Blocks?

In blockchain, information is stored in blocks. Each block includes a hash of the block’s data or contents.

So, a receiver can confirm that the contents of the block are valid by recreating the hash from the block contents, and comparing it with the hash included in the block.

This is the first step in blockchain security.

A blockchain is exactly that: a chain of blocks, each with a hash of the block’s contents included.

This chain provides a second layer of security.

  • A block is the basic entity in a blockchain.

  • The number of blocks in a blockchain can be very large.

  • Every block in a chain includes the hash of the previous block.

  • The previous block’s hash is included in the data used when creating the hash for a block.

  • If the data in a block is modified by an unauthorized person, the block’s hash will no longer match it.

  • The unauthorized person could recreate the hash in the original block, however, this would not match the hash in the next block.

  • The unauthorized person could try to update the hash in the next block, but then the next block’s hash would not match.

  • Even if the unauthorized person updated the second block’s hash, it would not match the hash in the third.

  • Therefore, if an unauthorized person updated a block, they would also have to update every later block in the blockchain.

Because no block is ever updated or deleted from a blockchain, blockchains are often referred to as ledgers. This ledger system can be used as a database system.

For example, a banking solution could use a blockchain to record deposits and withdrawals from a bank account.

When referring to a block, blockchain solutions usually use the hash of the block. An alternative is to use the block height, sometimes called the block number.

Blocks in a blockchain can store anything required by the blockchain solution. For example, the cryptocurrency Bitcoin stores multiple transactions in a block.

For an unauthorized person to change data in a block, they would need to recreate the block holding the data, and every later block in the blockchain.

Blockchain solutions prevent this from happening by storing the blockchain on many different computers at the same time. So now an unauthorized person must change blocks on every computer storing the blockchain. Blockchain is a distributed solution, not a centralized one.

Many blockchain solutions use hundreds, or even thousands of different computers (referred to as nodes) to store the full blockchain. Each computer stores the entire blockchain. Because each node stores the entire blockchain, each must update its own copy of the blockchain every time a new block is added.

The nodes must work together, following the same rules. For example, when a block is added to the blockchain, all nodes must add the same block at the same height in the blockchain.

Blockchain implementations will all have a system or method that determines which node will add the next block to the blockchain. This prevents more than one node from adding a block at the same time.

One way of doing this is to slow down the rate at which blocks are added to a blockchain. For example, currently a new block is added to Bitcoin every 10 minutes, and Ethereum every 15 seconds.

To see how many computers work together in a blockchain, let’s look at an example: Bitcoin.

  1. When a Bitcoin node wants to submit a transaction, it transmits that transaction to all nodes in the network.

  2. Full Bitcoin nodes are nodes that hold a copy of the blockchain and are prepared to update it as necessary.

  3. Every full node validates the transaction. If the transaction is valid, every full node adds the transaction to a transaction pool, or memory pool. This is a group of ready transactions that have not been added to the blockchain.

  4. Every full node then adds transactions from the transaction pool into a candidate block, and attempts to add this block to the blockchain.

  5. All full nodes are trying to add the next block to the blockchain, but only one can be successful. So, all full nodes compete to add the next block — it is a race to create the next block.

  6. The node that wins the race adds the block to the end of its local copy of the blockchain. Transactions in this new block are removed from the transaction pool.

  7. All other full nodes are notified that there is a winner. Each adds this new block to their local copy of the blockchain. All transactions in this new block are removed from all local transaction pools.

This race between Bitcoin nodes prevents two nodes adding a block at the same time to the end of the blockchain. How blockchain determines who will add the next block is called consensus. Consensus also prevents the same transaction from being in two different blocks in the blockchain.

Bitcoin nodes compete using a system of consensus called proof of work.

With proof of work, every block includes a value that is not used for anything other than the proof of work validation. This is called a nonce, and is set to zero when a block is created.

Before a block can be submitted, the hash of the block must meet restrictions set by the network. Normally, this is a target value that the hash must be below.

If the hash is not below the target, the computer must try again. It does this by increasing the nonce by 1, recalculating the hash, and trying again. This is repeated until a hash is found that is less than the target. This process is called mining.

The computer has provided proof that it has done some work. Now the block will be added to the blockchain in all computers in the network.

Blockchain networks deliberately make it difficult for a computer to find a hash that meets the target. So, it can take a computer several minutes, or even hours or days, before a suitable hash is found.

Because it takes so long, it is almost impossible that two computers will attempt to add a block to a blockchain at exactly the same time. Computers in the blockchain network simply accept the blocks in the order they are received.

Using a nonce is not the only way proof of work can be used. The blockchain network can set any puzzle to be solved by a computer, with the aim of slowing down the rate at which a computer can add blocks.

A major criticism of proof of work is that it wastes computer processing power — forcing computers to do millions of calculations for no reason except to slow the rate that blocks are added.

There are alternative consensus algorithms. One is proof of stake. This method is used by the Peercoin cryptocurrency, and is being developed for Ethereum.

Every node has a share or stake in the network. Some will have more, others less. For example, cryptocurrencies could base this on coins unspent for a period of time: the more unspent coins, the larger the stake.

The computer submits a block to the network. In this block, the computer provides a key to identify itself, and its stake in the network. The network will have a mechanism whereby the next computer that can add a block is determined by all computers in the network.

This mechanism will determine how often blocks can be added. This mechanism will also determine the next node that can add a block. The bigger the stake, the more likely a computer can add a block.

A computer will try to add a block. If unsuccessful, it can try again. If it is this computer’s turn, the block is added.

Proof of work and proof of stake are consensus solutions for trustless blockchain networks: networks where there is no central control, and anyone can join. Bitcoin and Ethereum are two examples of trustless networks.

Achieving security using a network of trustless computers is one of the major strengths of blockchain.

Resolving consensus for internal, or trusted, blockchain networks is much easier. For example, a simple solution could be to give each computer a turn, one after the other — like a round-robin.

In practice, such a round-robin consensus is not practical. However, other examples such as Raft are possible in a permissioned blockchain.

How does Raft consensus work?

  1. Each node begins as a candidate: eligible to become the leader.

  2. Each node votes for one node to become the leader.

  3. One node wins and becomes the leader. All other nodes become followers.

  4. The leader adds all blocks.

  5. If the leader shuts down or fails, a new vote is taken to select a new leader.

Conclusion

We’ve finally come to the end of this handbook. I hope this has helped you understand and get started with blockchain.

I’m always open to suggestions and discussions on LinkedIn. Hit me up with direct messages.

If you’ve enjoyed my writing and want to keep me motivated, consider leaving starts on GitHub and endorse me for relevant skills on LinkedIn.

Till the next one, stay safe and keep learning.

Top comments (0)