Blockchain explained by simple Java code

Following my problem with KFC system, let’s see another real world problem: The food shop chain with a central hotline/database couldn’t work sometimes because of single point that needs to be always up. If the order database could be distributed, the high availability of the system could be increased. It’s when blockchain shows its value by all distributed shops store the copy of single order database.

Blockchain is combination of block and chain. Block is unique and stores the data/information, chain stores a sequence of blocks that each block points to the previous one.

This article describes the simple blockchain that’s used to store the orders of all food shops in the system.

Let’s start with Block with a simple version of Order.

One Block has the unique ID, called hash, with time the block is generated, called timeStamp, and hash of the previous block, called previousHash. Block has order that store the food order data.

The first block that doesn’t have previous one, called genesis block, should be hard-coded.

A BlockChain has a list of block with some add-in methods like validateBlock() or validate() the block chain.

The reason of class BlockChain implementing interface Cloneable should be describe later.

Now each food shop, called Node, has a single blockchain. To make sure all nodes store the same blockchain, we must firstly make sure all nodes can ‘know’ others. Following:

  • If the node is started without connectedNode, it’s the first node
  • If the node is started with connectedNode, it asks the connectedNode for the list of nodes in the network, calling to getNodes(). Then the node tells those existing nodes that it wants to connect to the network, calling to registerToNetwork()

To make sure blockchain consistently, when a node mines a block, it needs to notify all nodes in the network by method mine(). In this problem, it means a food shop creates order for a customer.

It is. A blockchain is ready. Class Store and FoodBlockChainTest are for testing: it creates 3 stores (kfc, burger, pho) and registers them to the network, each store simulates creating the orders and add them to blockchain. You should see the result in your console. This example is just simulated by distributing the system in threads, that’s why I used Cloneable to get it looked like ‘distributed’ without references. In the real blockchain, methods like getNodes()getBlockChain()register()… should be called by REST or RPC.. and each node has it owned isolated data. But networking isn’t our focus here, you can easily write some extra code.

It’s of course not a real blockchain that you can use now. There are many things that this article didn’t mention: chain conflict, Proof of Work, sub chain, confirmation… But I hope you can get the idea of blockchain and its value through a simple code. It just a start, I will get back with another topic later.

Full code should be found here:

 5,089 total views,  1 views today