Understanding Block Chain Concept using Simple Java Program


# Are you a Block Chain geek ?



The Block Chain is a decentralized and distributed digital ledger that is used to record transactions across many computers so that the record cannot be altered retroactively without the alteration of all subsequent blocks and the collusion of the network.

Here is the easy way of understanding Block Chain concept using a simple java program for you.

A Java program to understand the simple BLOCKCHAIN network.

( how blocks are added with previous hash & current hash)

---------------- BlockChainNetwork.java ------------------------------------------------------

package com.dharani.blockchain.simplenetwork;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

public class BlockChainNetwork {

public static Set <Block> blockChain;
public BlockChainNetwork() {
blockChain = new LinkedHashSet<Block>();
blockChain.add(createTheGenesisBlock());
}
    
public Block createTheGenesisBlock(){
// The Genesis block is the first block created while creating the blockchain network.
String timestamp =new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS").format(new Date());
int index=0;
String previousHash="null"; // consider previous hash of Genesis block is zero
String data = "This is the Genesis Block";
return new Block(index,timestamp,previousHash,data);
}
public Block getTheLatestBlock(){
return (Block) blockChain.toArray()[blockChain.size()-1]; 
}
public void addNewBlock(Block newBlock){
Block latestBlock = getTheLatestBlock();
newBlock.setIndex(latestBlock.getIndex()+1);
newBlock.setTimeStamp(getCurrentTime());
newBlock.setPreviousBlockHash(latestBlock.getCurrentBlockHash());
newBlock.setCurrentBlockHash(newBlock.calculateHash(newBlock.getBlockData()));
/*
 To make the hash more stronger, we can calculate it along with index, time stamp and previous hash. 
 i.e., this.calculateHash(timeStamp+previousBlockHash+blockData)
*/
blockChain.add(newBlock);
}

public static String getCurrentTime(){
return new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS").format(new Date());
}

}

--------------- BlockChainNetworkTest.java ------------------------------------------------------

package com.dharani.blockchain.simplenetwork;


public class BlockChainNetworkTest {
public static void main(String[] args) {
System.out.println("******** Welcome to the demonstration of simple BlockChain Network ******** \n");
BlockChainNetwork blockChainNetwork = new BlockChainNetwork();
blockChainNetwork.addNewBlock(new Block("{Operations : Credit}"));
blockChainNetwork.addNewBlock(new Block("{Operations : Debit}"));
blockChainNetwork.addNewBlock(new Block("{Operations : Transfer}"));

for(Block blocks : BlockChainNetwork.blockChain){
System.out.println(blocks.toString());
}
}
}

-------------------------------------------------------------------------------------------------------

Get the complete source code here,

-------------------------------------------------------------------------------------------------------

Follow the next post to understand the Block chain network validation concept using the java program.

Post Link : Block Chain Network validation using simple java program