The Blockchain is a technological revolution at the heart of the success of Bitcoin and other crypto currencies. For some, the Blockchain is a revolution comparable to what the Internet was in its day. In this article, we propose you to create your own Blockchain in 30 minutes by using the Java programming language. It will allow you to better discover how the Blockchain works internally.

This article is divided in 3 parts :

  1. Design and Create the Blocks for the Blockchain
  2. Creating the Blockchain by using the Blocks
  3. Put our Blockchain in action !

You can also discover this tutorial in video on YouTube :

 

Design and Create the Blocks for the Blockchain

First, we are going to design the Blocks for the Blockchain. Basically, a block contains the following information :

  • Index
  • Timestamp to store the creation date of the Block
  • Hash of the previous Block
  • Data stored in the Block. For the Bitcoin and the other crypto currencies, data are transactions
  • Hash of the current Block to ensure integrity of its content

This gives us the following code for the properties of the Block :

 

SHA-256 Cryptographic Hash Function

The hash of the blocks of our Blockchain will be calculated by relying on the cryptographic hashing algorithm SHA-256 which you will find all the details on wikipedia : https://en.wikipedia.org/wiki/SHA-2 . Luckily, the Java language SDK offers a standard implementation of this algorithm. It will not be necessary to code it ourselves. The cryptography algorithms offered by Java are recoverable via the MessageDigest class, which allows you to retrieve the instance of an algorithm by entering the name of the getInstance() method.

It then remains to enter in parameter of the digest() method a textual representation of the blocks’s content to hash for getting the result of its hashing via the SHA-256 algorithm in the form of an array of bytes. To finish, we transform this array of bytes into a string of characters and we return this last function output :

 

Mining the Blocks

Our block is practically functional. It only remains to add a method to carry out its mining. The mining process will allow us to solve the enigma posed by the famous “Proof of Work”. Given some difficulty passed as input, we will have to find a hash for the block starting with a given number of zeros :

The attentive reader will have noticed the presence of the static method zeros of the class Utils in this code. This method returns a string containing the number of zeros passed as an input parameter. As for the nonce property, it will contain the number of tests performed before solving the proof of work during the block mining.

 

Complete code for the Block class

To end this first part, you will find the complete code for the Block class just below :

 

Creating the Blockchain by using the Blocks

In this second part of the tutorial, we are going to create the Blockchain object by using the Block object created previously.

Designing the Blockchain

The Blockchain is a distributed database ensuring the integrity of its data. The Blockchain will have a property letting to define the level of difficulty for the mining of the blocks during the solving of the “Proof of Work”. Then, the Blockchain will store its blocks within a List object of the Java language which gives us the following declaration start for the Blockchain object :

 

Creating and Mining new Blocks

Our Blockchain must allow the creation of new blocks and their mining taking into account the difficulty associated with it. So, we add a newBlock method to generate a new block associated with the last block known of the Blockchain. The block’s mining is done while adding the block to the Blockchain within a dedicated method addBlock :

 

Checking the Blockchain’s validity

The Blockchain must guarantee the integrity of the block data within it in order to remain valid. To do this, we will add different methods within our Blockchain to verify that :

  • The first block is valid
  • A new block is valid with the previous Blockchain block
  • Blockchain is valid

In order to implement these methods, it is necessary to define the validity criteria that we must consider. Thus, the first block is valid if its index is equal to 0, it does not have a previous previousHash and finally if its hash is correct. A new block is valid next to the previous block of the Blockchain if its index is incremented by 1 compared to the index of the previous block, its previousHash field is filled with the hash of the previous block and finally if its hash is itself coherent. Finally, a Blockchain is valid and the integrity of its data is guaranteed when the first block is valid and each block that composes it is valid next to the block that precedes it. All this gives us the following code :

 

Complete code of the Blockchain

Our Blockchain class is almost complete. All we have to do is to override its toString() method in order to be able to return a textual representation facilitating the visualization of its contents. We obtain the following Java code :

 

Put our Blockchain in action !

In this last part, we are going to put our Blockchain in action ! We are going to create some blocks for our Blockchain and then checking its validity.

Adding some blocks and checking the Blockchain’s integrity

We create our Blockchain by instantiating the Blockchain object and specifying in input a difficulty of 4 for the block mining. Next, we will add three blocks before checking the validity of the Blockchain and displaying the data it contains on the screen. This is done with the following Java code :

 

Executing this code using the Java virtual machine will give us the following console output on which we can visually see the validity of our Blockchain :


Corruption of our Blockchain

In order to verify that our method of validating the Blockchain is working properly, it may be interesting to try to corrupt it by integrating a corrupted block. Once this addition is done, we will be able to verify that our method detects that the Blockchain is not valid :

 

Executing this code using the Java virtual machine will give us the following console output on which we can see that our Blockchain detects a problem of validity with the corrupted block we added :


Go further

Our implementation of a basic Blockchain in Java is fully functional. It will have allowed us to highlight the internal functioning of a Blockchain and especially the part related to block mining that will be even faster than the computer or computers realizing it are powerful.

To go further, we should now implement features to peer-to-peer (P2P) our Blockchain. This could be done in a future tutorial if the subject seems interesting to you. Feel free to leave me your comments if you want to see these P2P features implemented in a future article.

For French readers of the SSaurel’s Blog, you can read a French version of this tutorial just here on Tout sur le Bitcoin : “Créez votre propre Blockchain en Java”.