Blockchain Technology

What is Blockchain?

Ledger

What is Ledger?

Repository of all the transactional information happening in the system

What is Distributed Ledger

Distributed repository of all the transactional information i.e. multiple copies of same ledger distributed over network

Block in Blockchain

Java Implementation

A basic implementation of block looks like

public class Block {

  private int previousHash; // <- HASH (Unique Identification Number of a block) of previous block
  private List<Transaction> transactionList; // <- Current transaction information

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Block block = (Block) o;

    if (previousHash != block.previousHash) {
      return false;
    }
    return transactionList.equals(block.transactionList);
  }

  @Override
  public int hashCode() {
    int result = previousHash;
    result = 31 * result + transactionList.hashCode();
    return result;
  }
}

Transaction Class

public class Transaction {

  private String source;
  private String destination;
  private Long sum;

}

Sample block creation

public class BlockChain {

  public static void main(String[] args) {

    //Transaction transaction1 = new Transaction("Kamran", "Palash", 100L);
    Transaction transaction1 = new Transaction("Kamran", "Palash", 1000L);
    Transaction transaction2 = new Transaction("Palash", "Prakhar", 110L);
    Transaction transaction3 = new Transaction("Prakhar", "Mayank", 120L);

    Block block1 = new Block(0, Arrays.asList(transaction1, transaction2));

  /*
   Block2 has hash of previous block.
   */

    Block block2 = new Block(block1.hashCode(), Arrays.asList(transaction3));

    // for Transaction transaction1 = new Transaction("Kamran", "Palash", 100L);
    System.out.println(block1.hashCode());  // 354690732
    System.out.println(block2.hashCode());  // 662422422

    // for Transaction transaction1 = new Transaction("Kamran", "Palash", 1000L);
    System.out.println(block1.hashCode());  // 354718632
    System.out.println(block2.hashCode());  // 663287322

  }
}

Every block is tightly coupled with each other. So if we want to modify one block we need to go through entire chain.

Summary

Blockchain works on the concept of distributed ledger and uses blocks that are related by hashes.Every ledger validates the block in that peer to peer chain for a particular transaction to verify that particular information is correct.
If more than 50% of the ledger denies the transaction than the whole transaction will be cancelled.

References

What is Blockchain? | How does Blockchain Technology work? | Tech Primers
Let’s create custom BlockChain in Java | Create Block Chain Technology in Java

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...