Understand Blockchain using Java (part 3)

Hi All, so we are back again on track with the third part of Blockchain. So far so good, we discussed about the mining of Blockchain.  Enough of theory in first 2 parts. Let’s understand the concepts and basic building blocks using Java.  Here we will represent the same concepts of blockchain by different classes (Block [which consists of index, timestamp, previous block’s hash, data of the block], Blockchain [chain or list of blocks] and a demo class to test the same).

Let’s understand the concepts using a small java program.

Before going through the details of coding, let me first tell you what this code is not:

This is not a Blockchain or a network or a distributed ledger.

 

A basic block of a distributed ledger is represented via a class as below:

package com.example.blockchain;

import java.nio.charset.StandardCharsets;

import java.util.Date;

import com.google.common.hash.Hashing;

/*

* Index

* Time Stamp

* Previous Hash

* Hash

* Data

*/

public class Block {

private int index;

private Date timeStamp;

private String previousHash;

private String hash;

private String data;

// constructor for blockchain values insertion

public Block(Date timeStamp, String previousHash, String data) {

index = 0;

this.timeStamp = timeStamp;

this.previousHash = previousHash;

this.data = data;

this.hash = calculateBlockHash();

}

public String calculateBlockHash() {

String sha256Hash = Hashing.sha256().hashString(data, StandardCharsets.UTF_8).toString();

sha256Hash += timeStamp.toString();

return sha256Hash;

}

public int getIndex() {

return index;

}

public void setIndex(int index) {

this.index = index;

}

 

public Date getTimeStamp() {

return timeStamp;

}

public void setTimeStamp(Date timeStamp) {

this.timeStamp = timeStamp;

}

public String getPreviousHash() {

return previousHash;

}

public void setPreviousHash(String previousHash) {

this.previousHash = previousHash;

}

public String getHash() {

return hash;

}

public void setHash(String hash) {

this.hash = hash;

}

public String getData() {

return data;

}

public void setData(String data) {

this.data = data;

}

}

 

Using an ArrayList lets create a class which will create a list of blocks, initialize the blocks and add genesis and further blocks:

package com.example.blockchain;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

publicclass BlockChain {

private List<Block> blockChain;

public BlockChain() {

initializeChain();

addGenesisBlock();

}

privatevoid addGenesisBlock() {

blockChain.add(createGenesisBlock());

}

private Block createGenesisBlock() {

returnnew Block(new Date(), null, “{}”);

}

privatevoid initializeChain() {

blockChain = new ArrayList<Block>();

}

public List<Block> getBlockChain() {

return blockChain;

}

publicvoid addBlock(Block block) {

Block latestBlock = getLatestBlock();

block.setIndex(latestBlock.getIndex() + 1);

block.setPreviousHash(latestBlock.getHash());

block.setHash(block.calculateBlockHash());

blockChain.add(block);

}

public Block getLatestBlock() {

return blockChain.get(blockChain.size() – 1);

}

publicvoid setBlockChain(List<Block> blockChain) {

this.blockChain = blockChain;

}

publicvoid blockDisplay() {

for (int i = 0; i < blockChain.size(); i++) {

Block currentBlock = blockChain.get(i);

// Block previousBlock = blockChain.get(i-1);

System.out.println(“Block Values are::::” + “index: ” + currentBlock.getIndex() + “, timeStamp: ”

+ currentBlock.getTimeStamp() + “, previous Hash: ” + currentBlock.getPreviousHash() + “, Hash: ”

+ currentBlock.getHash() + “, data: ” + currentBlock.getData());

}

}

}

Test the class using a demo class with main method:

package com.example.blockchain;

import java.util.Date;

publicclass BlockChainDemoMain {

publicstaticvoid main(String[] args) {

BlockChain bitCoin = new BlockChain();

bitCoin.addBlock(new Block(new Date(), null, “{sender:Deb,receiver:Ari,amount:1500}”));

bitCoin.addBlock(new Block(new Date(), null, “{sender:Ari,receiver:Joy,amount:100}”));

bitCoin.addBlock(new Block(new Date(), null, “{sender:Joy,receiver:Deb,amount:2500}”));

bitCoin.blockDisplay();

}

}

 

The program uses internal JVM memory so the blockchain lasts till the program is run.

If the same program is run in different nodes of a system which interact through a peer to peer network and which follows other properties of a blockchain network, then the same can be implemented easily as a blockchain.

 

To be contd..

Digiprove sealCopyright secured by Digiprove © 2019 Geeks 18

Be the first to comment

Leave a Reply

Your email address will not be published.


*