Reader small image

You're reading from  Learn Blockchain Programming with JavaScript

Product typeBook
Published inNov 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789618822
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Eric Traub
Eric Traub
author image
Eric Traub

Eric Traub currently works as a software engineer in New York City. He has extensive experience working as a teacher and instructing people in a variety of different subjects. He changed his career from teaching to software engineering because of the excitement it brings to him and the passion that he has for it. He is now lucky enough to have the opportunity to combine both of these passions - software engineering and teaching!
Read more about Eric Traub

Right arrow

Creating a Decentralized Blockchain Network

In this chapter, let's focus on building a decentralized blockchain network. The way that our blockchain works right now is that we have a single blockchain, and the only way to access it is through the API: our single server. This server is very centralized, which is not beneficial because the API is in total control of the blockchain and the data that gets added to it.

In the real world, all blockchain technology is hosted across a decentralized network. In this chapter, that's what we're going to focus on building. We'll build a decentralized blockchain network by creating various instances of the API. Each of these instances of the API are going to be a network node in our blockchain network. All of these nodes will work together to host our blockchain.

In this way, it's not just a single network node that...

Creating multiple nodes

Let's begin by building the decentralized network:

  1. The first thing that we'll have to do to create our decentralized blockchain network is make some modifications to our api.js file.
  2. In our decentralized network, we're going to have multiple instances of our API, and each one of them will act as a network node. Since we'll be dealing with multiple network nodes, it will be better to rename our api.js file to networkNode.js for easy reference.
  3. To set up the decentralized network, we'll have to run the networkNode.js file multiple times. Each time we run the file, we want it to act as a different network node. Let's do this by running the file on different ports every time we run it. To have a different ports value every time, we'll have to make port a variable. To do this, add the following line at the start of the code...

Adding the currentNodeUrl

After testing our nodes, the next thing that we're going to do is alter the commands in our package.json slightly. The reason that we're going to do this is because we want each of our network nodes to be aware of what URL they are currently on. For example, they could be on http://localhost:3001, localhost:3002, localhost:3003, and so on. Therefore, we want each node to be aware of the URL that it is being hosted on.

In our package.json, as a third parameter to each of our commands, we are going to add the node's URL. Therefore, our first node's URL will simply be http://localhost:3001. It is likely that for our second node it will be http://localhost:3002. Similarly, you can add URLs for the remaining nodes, as shown in the following screenshot:

After adding the URLs, save the file. Now we have the URL of each node being passed...

New endpoints outline

In our blockchain, we would now like to create a network and have a way to register all of the different nodes that we have with it. Therefore, let's make a couple more endpoints that will make it possible to register nodes with our network.

Defining the /register-and-broadcast-node endpoint

The first endpoint that we create will be /register-and-broadcast-node, and this is defined as follows:

app.post('/register-and-broadcast-node', function (req, res) {

});

The preceding endpoint will register a node and broadcast that node to the whole network. It will do this by passing the URL of the node we want to register on the req body. Therefore, type the following inside the preceding endpoint...

Building the /register-and-broadcast-node endpoint

Let's start building our register and broadcast node endpoint. The function of this endpoint will be to register the new node with itself and then broadcast the new node to all the other nodes that are already present in the network. So, let's get started with building the endpoint:

  1. From the preceding sections, in the dev/networkNode.js file, we already have the following code:
app.post('/register-and-broadcast-node', function(req, res) {
const newNodeUrl = req.body.newNodeUrl;

Here, we defined a variable called newNodeUrl, and this newNodeUrl data will be passed onto the request body, similar to how we have transaction data being passed into the transaction endpoint. With access to the newNodeUrl, the first thing that we want to do is register the node with the node's register-and-broadcast...

Building the /register-node endpoint

Now that we have built the /register-and-broadcast-node endpoint, it's time we move on to some things that are a little less complex. In this section, let's begin building the register-node endpoint. This is going to be very straightforward compared to the endpoint that we built in the previous section.

This register-node endpoint is where every node in the network is going to receive the broadcast that is sent out by our register-and-broadcast-node endpoint. The only thing that this register-node endpoint has to do is register the new node with the node that receives the request for it.

To begin building the register-node endpoint, follow these steps:

  1. The first thing that we'll have to do is define the newNodeUrl; therefore, add the following highlighted line of code:
// register a node with the network
app.post('...

Building the /register-nodes-bulk endpoint

The next endpoint that we are going to build is our register-nodes-bulk endpoint; this is the final endpoint that we need to build. These three endpoints that we have been working on will all work together to create our decentralized blockchain network.

Before we start building the endpoint, let's try to understand what the register-nodes-bulk endpoint does. Whenever a new node gets broadcast to all the other nodes inside of the network, we want to take all of the nodes that are already inside of the network and send that data back to our new node so that the new node can register and recognize all of the nodes that are already present inside of the network.

The register-nodes-bulk endpoint will be accepting data that contains the URLs of every node that is already present in the network. Then, we're simply going to register...

Testing all of the network endpoints

From what we have learned in the preceding section, we know that our register-node route and that the register-nodes-bulk route are both working correctly. So, in this section, let's put it all together and test our register-and-broadcast-node route, which uses the both the register-node route and the register-nodes-bulk route.

The register-and-broadcast-node endpoint will allow us to build a decentralized blockchain network by allowing us to create a network and add new nodes to it. Let's jump right into our first example to get a better understanding of it. To understand how the register-and-broadcast-node route works, we'll make use of Postman.

In the Postman application, we want to make a post request to register and broadcast the node on localhost:3001. However, before we do that, just make sure that all four nodes are...

Summary

We have now finished creating our decentralized network. In this chapter, we learned about a lot of new concepts. We began our journey by learning about how to create the multiple instances of our API and how to use them to set up our decentralized network. We then defined various endpoints such as register-and-broadcast-node, register-node, and register-nodes-bulk. After this, we built these endpoints and tested them.

In the next chapter, we will learn how to synchronize the network.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn Blockchain Programming with JavaScript
Published in: Nov 2018Publisher: PacktISBN-13: 9781789618822
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Eric Traub

Eric Traub currently works as a software engineer in New York City. He has extensive experience working as a teacher and instructing people in a variety of different subjects. He changed his career from teaching to software engineering because of the excitement it brings to him and the passion that he has for it. He is now lucky enough to have the opportunity to combine both of these passions - software engineering and teaching!
Read more about Eric Traub