In this book, you will understand how blockchain technology actually workes by learning to build your own blockchain and understanding the decentralized network. Toward the conclusion of the book, you will have a full-fledged blockchain prototype that is hosted on a decentralized network, and you'll have gained a great deal of knowledge and understanding as to how blockchains actually work under the hood.
The blockchain that we will create throughout this book will be able to carry out the following functionalities:Â
- Perform a proof of work to secure the blockchain
- Create new blocks through a mining process
- Create new, immutable transactions
- Validate the entire blockchain and all of the data within each block
- Retrieve address/transaction/block data
Along with these, the blockchain willÂ have many other important features. You'll get to explore those as you readÂ furtherÂ through the chapters.
Firstly, in this introductory chapter, let's try to understand what blockchain actually is. This will help you to become familiar with the concept of blockchain, as this is a prerequisite for the book. Then we'll move on to learn how to set up the project to create our own blockchain.Â
So, let's get started!
In this section, let's go through a brief explanation of what a blockchain is. Simply put, a blockchain is an immutable, distributed ledger. Now, these words may seem quite complex, but when we try to break them down, it is very easy to understand them. Let's begin by exploring what a ledger actually is. A ledger is simply a collection of financial accounts or transactions (or in other words, a record of transactions that people have made).
Let's take a look at the following example to get a better understanding of ledgers. In this example, Kim paid Joe $30 and Kevin paid Jen $80. A ledger is simply a document that is used to keep track of these transactions. You can see this depicted in the following screenshot:
Now, what does it mean for a blockchain to be immutable? This means that it cannot be changedâever. Consequently, when a transaction is recorded, it cannot be undone. Other factors that cannot be changed include the amount of money that was sent or the people who took part in the transaction.Â Once a transaction is made, no aspects of that transaction can be changed because it is immutable.
In the world today, we see many applications, platforms, and networks that are all centralized. Take Facebook, for example. Everyone who uses Facebook has to trust this company is protecting their data and not abusing it. Compared to this, blockchain is different. Blockchain technology is not centralized like Facebook, Google, or most other entities. Instead, it is a distributed network, which means that any given blockchain network is not controlled by a single entity, but is run by normal, everyday people. Blockchains, such as Bitcoin, are supported and hosted by thousands of people worldwide. Consequently, all of our data, or the ledger in this case, is not at the mercy of a single company or entity. This proves to be a great benefit of blockchain technology because by being distributed, we do not have to trust a single company with our data. Instead, our data is persisted by the entire network of thousands of different people who are all acting independently.
Each individual who contributes to the blockchain network is called a node, and each node has theÂ exact same copy of the ledger. Therefore, the ledger data is hosted and synchronized across the entire network.
So, a blockchain is an immutable distributed ledger. This means that it is a ledger in which the transactions can never be changed and the blockchain itself is distributed across the network and run by thousands of independent people, groups, or nodes.
The blockchain is a very powerful technology which is still in its infancy, but its future is very exciting. There are many ways that blockchain technology can be applied to our world today to make certain industries more secure, efficient, and trustworthy.Â Some industries that could be transformed with the help ofÂ blockchain technology include financial services, healthcare,Â credit,Â governments, energy industries, and many others. Pretty much every industry out there could benefit from a more secure, distributed form of data management.Â You can observe that blockchain technology is at a very exciting stage right now, and many people are excited about what the future holds forÂ it.Â
Now that we're aware of what blockchain is, let's move onto setting up our project environment to build our blockchain.
This book will help you to gain a deeper understanding ofÂ blockchain technology by building your own blockchain from scratch. Blockchain is a fairly new technology, and while it can seem tough and slightly overwhelming to learnÂ at first, we're going to take a step-by-step approach and break it down in order to understand how it works under theÂ hood.Â By the time you finish this book, you will have a very solid understanding of how blockchain technologyÂ works, and you will have built your own entire blockchain as well.
In this book, we will start by building the blockchain itself. At this point,Â we will build a blockchain data structure that has the following abilities:Â
- Proofing workÂ
- Mining new blocks
- Creating transactions
- Validating the chain
- Retrieving address data and other functionalities
Thereafter, we will create an API or a server that will allow us to interact with our blockchain from the internet. Through our API,Â we will be able to use all of the functionality that we have built into our blockchain data structure.
Furthermore, you'll be learning to create a decentralized network.Â This means that we'll have multiple servers running and acting as separate nodes.Â We'll also make sure that all of the nodes interact with each other properly and share data with each otherÂ in the correct format. In addition, you'll learn how to synchronize the entire network by making sure that any new nodes or transactions that areÂ created are broadcast throughout the entire network.
We'll then move onto creating a consensus algorithm. This algorithm will be used to make sure that ourÂ entire blockchain stays synchronized and that this algorithm will be used to make sure that each node in our networkÂ has the correct blockchain data.
Finally, we will create a block explorer. This will be a user interface that will allowÂ us to explore our blockchain in a user-friendly manner, and it will also allow us to query ourÂ blockchainÂ for specific block transactions andÂ addresses.
Firstly, however, we need to set up our development environment.
Let's get started with building our blockchain project. TheÂ first thing we're going to do is open our terminal and create our blockchain directory by typing commands into the terminal, as seen in the following screenshot:Â
Let's begin by creating a folder called
programs. Inside this folder, let's create a directory called
blockchain. This directory is currently empty.Â Inside of thisÂ
blockchainÂ directory is where we're going to be doing all of our programming. We are going to be building our entire blockchain inside of thisÂ
blockchain directory is ready, and the first thing that we need to do is to add some folders and files into it. The first folder that we want to put into the directory will be called
dev, so we want to make sure that we are inside of theÂ
blockchainÂ directory, and then let's type the following command into the terminal:Â
devÂ directory is where we are going to be doing most of our coding. This is where we're going to build our blockchain data structure and create our API to interact with our blockchain, test it, and fulfill other similar tasks. Next, inside this
dev folder, let's create two files:Â
test.js.Â To do this,Â enter the following command:
cd dev touch blockchain.js test.js
touchÂ term in the preceding command line will help us in creating the mentioned files. The
blockchain.js file is where we will type our code to create the blockchain and theÂ
test.js file is where we will write code to test our blockchain.
Next, let's return back to our
blockchain directory by typing the following command in the terminal:
After running the preceding command, you will get some options on your terminal. To set up the project, you can just press Enter through those options.
So, this is pretty much all we need to do in order to set up our project folder structure. Now, if you go to ourÂ
blockchainÂ directory and open it with a text editor such as Sublime or Atom (or whatever you would like), you will get to see the file structure, as seen in the following screenshot:
blockchain directory consists of the
dev folder that we just created. Inside theÂ
dev folder, we can observe our
test.jsÂ files. Also, when we run theÂ
npm initÂ command, it creates theÂ
package.jsonÂ fileÂ for us. ThisÂ
.jsonÂ file will keep track of our project and any dependencies that we need, allowing us to run scripts.Â We'll be working more inside of thisÂ
package.jsonÂ file in further chapters, so you'll become more familiar with it as we progress through the book.
To summarize this introductory chapter, we began byÂ exploring what a blockchain actually is and understanding how it functions. Then we moved onto setting up our project to create our very own blockchain. We also had a quick overview of all of the topics you'll get to learn about in this book.Â
In the next chapter, we'll build our blockchain by learning about the constructor function, prototype object, block method, transaction method, and many more important concepts.Â