Trusted by millions

Blockchain Development Part 2

In previous part we have developed a simple coin named Howcoin in our blockchain development and do some basic transactions on it. At the previous stage we have to face some problems such as

  • No authentication system
    Coins are open for everyone there is no authentication system we have developed yet.
  • Availability
    What if our server goes down? All the created users will be lost and all of their money will also destroy. 
  • Security
    If anyone successfully enters the system he can steal all the money easily. We haven’t implemented any security measurements yet.

Centralized System into Gossip Network
Our developed system is now a centralized network. we need to make it GOSSIP Network. In GOSSIP protocol every node acts like a server as shown below:

In this tutorial we are now starting from converting a centralized system into Gossip. Where every node is directly connected to all others within this system. If a new node wants to join the network he need to directly communicate with a single node of network and this node introduce this new node to all nodes of the network

For this purpose create a file named “gossip.rb” in our previous system. In this system we are going to distribute coins loaded in a file between all nodes and check them particularly. The tricky part is that the coin may be changed by transferring nodes among different nodes.

In gossip.rb file add following code

1- In this file we are using two ports to start the system. One my own port to start and second is for other nodes by whom I am communicating. 

2- Save the state so everyone can save the record about how many coins a node has. After the above code add the following hashmap

3- For updation purposes we need a helper to update or edit coins status of nodes. We will define this helper later here we need to call the methods of this helper. After the above line add the following code

4- We need  to select coins from a text file as shown below

5- Here we need to set coins and update network for us as shown below

6-  Now we are going to render state method of helper after every 3 seconds to check the status

7- Here is code for your helper to update and render the state

 let’s check and run this code once again before proceeding further. For this purpose type command 

ruby gossip.rb

Program now provides me 115 coins and after every 3 seconds it is now rendering the state of the system. 

We are now going to change the state or coins after 8 seconds and let’s see what happens after this code. Add this code after every(3.seconds) statement. 

Run this code again and you will get the following output.

In which your state or coins are now updating after every 8 seconds. Currently our system is not a Gossip network. We need to make it gossip, we need to send messages to all the other nodes as well. Here we need the version number to check the latest message. As you can see in our code, on every update version number is also updating. 

To do gossip networking we need to connect with other nodes and after every few seconds we will ask these nodes about other node states like. If it is updated we will update our record according to its state. For this purpose inside point 6 code add following code

In the above code we are checking if the port is not mine own port and after that we are making a gossip network with our client’s method written below. We are now getting responses to this gossip call and updating our stored  state according to this call. State should be sent in the form of a json string to avoid network errors. After every time of gossiping we need to render the state again. And if someone leaves the network we will simply put in a rescue block and delete its port to save from errors.

Now let’s run this code on different console windows and see the magic of gossip.

We had made 4 different console windows and in each window we are now running gossip with different ports as shown.

In above we can consider that there are 4 nodes of the network and every node has its own coins continuously updating. 

In the gossip network every one is saving the states so password for authentication will not work here. And for authentication we will use cryptography here. Cryptography has public and private keys for encryption and decryption.  Private keys should be saved privately in the digital world. Incase of losing a private key means you will lose your password. We will make a deep look at it in the next part of this tutorial.

Conclusion

Here you can download complete source code of each step