Trusted by millions

Blockchain Development part 4: Double-Spend Problem, Cryptographic Puzzle & Mining

There was a big problem with digital currency called the double-spend problem. Let’s take an example to understand this problem

Consider Mr. Johns has 3 coins in this system. He decides to buy a cell phone and some books from online stores. Cell phones cost 2 coins and books cost 2 coins.

At the same time he signed coins and made two transactions for two online stores. The  first shop  receives an order and delivers the phone the same for the second shop also receives an order and delivers the required product. Mr johns has only 3 coins how he bought products which cost equals 4 coins. It is due to two transactions being made at same timestamps. This is called the “Double-spend” problem.

In this scenario both shopkeepers are considering that they have 2 coins but one of them must have only one coin and he will know when he wants to spend this coin. 
Now the question is how to solve this problem in a peer to peer network. Because there is always a speed variation in the network. So there is no global ordering in peer to peer networks. For this problem the father of bitcoin Mr. Satoshi Nakamoto comes with a solution called Bitcoin Protocol. He provides the concept of cryptographic puzzles. In Cryptographic puzzles the message is combined with computational work. This computational work is called cryptographic puzzles, very hard and no one can fake these solutions.

Proof of Work

This whitepaper provides proof of work for his solution. You have to do some work before sending a message. The next person who finds the solution of this puzzle will get this message. The puzzle is to find a nonce. Nonce is a hash which is combined with your original message to make it a specific number of zeros starting such as 5 zeros starting  hash 4 zeros starting hash.

In today’s topic we are generating a puzzle message and solving this puzzle. For this purpose make a file named proof_of_work.rb in your code and add following code in it

Hash Method

This method is to create hash of your required string as shown below

Is_valid_nonce Method
This method checks if nonce is present with a message. For this purpose we will check the number of zeros the message and nonce hash starts with.

Nonce Method
This is the basic method to find and check the nonce occurrence.

For this hello world it performs 49758 iterations and tries every single letter with the hash to check if it is valid or not.  

If we now make a hash of  “HELP I’M TRAPPED IN A NONCE FACWKHS” + “hello world” it should have 4 starting zeros. So let’s check it in this way.

The puzzle is that you need to find the crazy nonce which combined with this message to start with four zeros. 

This whole process is called mining in which  machines or machines are involved to solve a single  puzzles problem. When this puzzle is resolved the value is added to the network called blockchain network. In this blockchain network every block has the address of the previous block. And this makes its ordering.

Conclusion

Here you can download complete source code of each step