Essentially you need to understand what is blockchain and how it works? the blockchain is distributed system where there is no central node to handle all transaction. All transaction handled by the miner. The blockchain is the combination of blocks. Each block contains the hash value of the previous block and current block. If previous block hash code changed, current block hash code has to change too. Please see code below to understand simple how blocks interconnected and importance of hash code.
public class Block {
private int previoushash;
private String [] transactions;
private int blockhash;
public Block(int previoushash, String[] transactions) {
this.previoushash = previoushash;
this.transactions = transactions;
Object[] contents = {Arrays.hashCode(transactions), previoushash};
this.blockhash = Arrays.hashCode(contents);
}
public int getPrevioushash() {
return previoushash;
}
public String[] getTransactions() {
return transactions;
}
public int getBlockhash() {
return blockhash;
}
}
Main.java
public class Main {
ArrayList<Block> blockchain = new ArrayList<>();
public static void main(String [] args) {
/*
hash= digital signature
Each block contains:
-list of transactions
-previous hash
-hash
* */
System.out.println("Blochchain test initialized:\n");
String [] genesisTransactions = {"a send 10 bitcoin to elnur", "b send 3 bitcoin to elnur" , "c send 5 bitcoin to elnur"};
Block genesisBlock = new Block(0, genesisTransactions);
System.out.println("Hash of genesis block:");
System.out.println(genesisBlock.getBlockhash());
String [] block2transactions = {"elnur send 2 bitcoin to ibm","elnur send 6 bitcoin to sap","elnur send 12 bitcoin to amazon"};
Block block2 = new Block(genesisBlock.getBlockhash(),block2transactions);
System.out.println("Hash of block 2:");
System.out.println(block2.getBlockhash());
String [] block3transactions = {"elnur send 2 bitcoin to satoshi","satoshi send 1 bitcoin to starbuck"};
Block block3 = new Block(block2.getBlockhash(),block3transactions);
System.out.println("Hash of block 3:");
System.out.println(block3.getBlockhash());
}
}