Blockchain is a budding technology that has tremendous scope in the coming years. In this tutorial, we will briefly cover the basic concepts of Blockchain. We'll also create a basic Blockchain program in Java to understand how it works in the programming world.
What is Blockchain?
Blockchain is a continuously growing list of records, known as blocks, which are linked and secured using cryptography.
Blockchain is the modern technology that stores data in the form of block data connected through cryptography and cryptocurrencies such as Bitcoin. It was introduced by Stuart Haber and W. Scott Tornetta in 1991. It is a linked list where the nodes are the blocks in the Blockchain, and the references are hashes of the previous block in the chain. References are cryptographic hashes when dealing with link lists. The references are just basically objects. So every single node will store another node variable, and it will be the reference to the next node. In this case, the references are cryptographic hashes.
Blockchain uses hash pointers to reference the previous node in a long list. We assign a hash to every single node because this is how we can identify them.
To generate a hash, the SHA256 algorithm is used. One of the most important advantages of this algorithm is that it is generic, so it is a generic cryptographic hash function wherein the input can be anything.
Blockchain Block Diagram
Let's understand the Blockchain concept with the help of a diagram
So basically, this is the underlying data structure. We have various blocks that store data, such as transactions when dealing with cryptocurrencies. Every single block has two hashes, i.e., the hash of the previous node and the hash value of the actual block. And these hash values are going to be the references of the previous blocks.
Implementing Blockchain in Java
Though we are implementing the Blockchain in Java, you can create the code in your preferred OOPs (Object Oriented Programming) language.
Step 1: Defining the constants
First of all, let's take a look at the Constants. In this program, we will create a private constructor to prevent this class from being instantiated as we will store public, static and final variables such as difficulty, minor reward and the previous hash value of the first block, which is called the Genesis block.
Refer to the given below program to create the constants in Java:
STEP 2: Generating Hashes:
In this step, we will implement the SHA256Helper. It is pretty convenient to implement this procedure in Java because we rely on the built methods and built-in classes of Java security.
Firstly we will import the MessageDigest, and further will generate the hash for our Blockchain.
Question: How many SHA256 hashes are there?
Answer: One hash takes up 256 bits in memory with binary values 0 and 1. Therefore, we can conclude that the total number of hashes is 2256. If you are dealing with hexadecimal values, there are 64 hexadecimal characters; therefore, it yields 1664 hash values.
Refer to the given below program to generate the Blockchain hashes in Java:
STEP 3: Creating Blocks
The block is the fundamental building block of the Blockchain. These blocks are cryptographically linked together based on the hash values. For creating a block, a Block class is implemented, and further in that class, several variables are defined, such as the id of the block, the timestamp, the hash of the block, the previous hash in the Blockchain, the transactions, and the nonce.
Refer to the given below program to create the block in Java:
Step 4: Implementing Blockchain
In this program, we will store the generated blocks in an ArrayList.
Step 5: Miner Program
Mining is the most important concept in Blockchain as well as in cryptocurrencies (such as Bitcoin). Mining means finding the right hash to avail you for the given block. But there are some constraints as far as difficulty is concerned. Nevertheless, every miner is going to get a reward because they are validating the given transactions.
In the Constant class, we have already defined the difficulty as '5', which means there have to be five leading zeros at the beginning of every hash. So miners will generate hash values until they find the right hash.
Question: In a decentralized system, who will handle the transactions?
Answer: In a decentralized system, Miners will handle all the transactions.
Refer to the given below program to initiate the mining code in Java:
Step 6: Main Program
Refer to the given below program to run the main Blockchain program in Java:
Run the main program to get the following output with hashes and reward values.