read
  • According to Wikipedia, a blockchain is:

A distributed database that is used to maintain a continuously growing list of records, called blocks.

BlockChain in itself is a simple concept, a very simple data structure at its core, cryptocurrencies are complicated but blockchain is not. Lets start with What is a hash? In simple words a hash is the digital signature of data e.g

public static void main(String args[]) throws Exception {
		String[] list1 = {"a","b","c"};
		String[] list2 = {"a","b","c"};
		System.out.println(Arrays.hashCode(list1));
		System.out.println(Arrays.hashCode(list2));
	}

Output
126145
126145

If we try to change anything in the list1 or list2 we will get completely different digital signatures, and that is the foundation of blockchains because a blockchain is a list(chain) of blocks and each block holds the digital signature of the previous block and digital signature of the next block is based upon the digital signature of the currenct block, So they are all based together, if we change anything back in the past it will break all of the signatures they will look completely different.

Now as I have explained blockchain, Let's get into our block chain coding. First thing we are going to talk about is block structure.

The hash of the previous block must be found in the block to preserve the chain integrity

Lets create our Block class in java:

public class Block {
    private int index;
    private int hash;
    private int previousHash;
    private String[] transactions;

    public Block(int previousHash,int previousIndex, String[] transactions) {
      this.previousHash = previousHash;
      this.transactions = transactions;
      Object[] contents = {Arrays.hashCode(transactions),previousHash};
      this.hash = Arrays.hashCode(contents);
      this.index = previousIndex+1;
    }	

    public int getPreviousHash() {
    	return this.previousHash;
    }

    public String[] getTransactions() {
    	return this.transactions;
    }

    public String[] getHash() {
    	return this.hash;
    }

    public int getIndex() {
    	return this.index;
    }
  }

In the above code to keep things simple I took simple array of transaction strings, but in real world scenario this can be objects of another class or actions. Now that we have created our block class lets create block chain of it in out main class

public class Main {
ArrayList<Block> blockchain = new Arrayist<>();
public static void main(String args[]) throws Exception {
        String[] genesisTransaction = {"A sent B 10 coins", "C sent B 10 coins"};
		Block genesisBlock = new Block(previousHash: 0,previousIndex: 0, transactions: genesisTransaction);
		System.out.println(genesisBlock.getHash());
	}
}

Output
-67799281
In the above code snippet genesis block is defined it is the first block in block chain from which all the other blocks are linked together. C receives coins from both A and B, now suppose we want to cheat the system and change the genesisTransaction to be

    String[] genesisTransaction = {"A sent B 1000000 coins", "C sent B 10 coins"};

Now if we run our main we will see that digital signature of the block is completely different, and this is very important if there are chain of blocks and whenever a single block is mutated or attacked all the other block hashes will change. In the block chain technology there are nodes or computer systems which checks for the integrity and correctness of new blocks. An essential part of a node is to share and sync the blockchain with other nodes. The following rules are used to keep the network in sync.

  • When a node generates a new block, it broadcasts it to the network
  • When a node connects to a new peer it querys for the latest block
  • When a node encounters a block that has an index larger than the current known block, it either adds the block the its current chain or querys for the full blockchain.

Conclusions

Now we have seen how block chain works, there are many areas where block chains can be used to reduce time and securing the system, from bank insurance to real estate where security of data is to be guaranteed, and it takes more time to verify the correctness of data, blockchain can esily be implemented to record the actions and maintain the security.

Blog Logo

Rahul Yadav


Published

Image

Hot Cocoa Software

Blogs written by our developers.

Back to Overview