Press "Enter" to skip to content

Storing a blockchain inside another blockchain!

In between building servers and programming great big websites and data sets I explore the blockchain and what it can be used for. My latest blockchain is based in Java. The cool thing about a Java blockchain is I can store anything in it – even another blockchain, so you could have for instance a series of documents relating to a purchase of a house, or a transaction stored in their own blockchain, and because the blockchain cannot be changed, this would mean the documents could be locked away – note that I could have it so you could retrieve the documents and change them, but when you put them back you’d have to generate another entry in the blockchain.

But I’m storing ACTUAL DOCUMENTS, ACTUAL WORD FILES not just indexes to them. You could see a complete audit trail of changes to the document. It could be used for anything where there must be a trail of changes, what about a replacement for subversion?

Here’s a dump of a simple 3 blockchain. It’s been mined but look what’s interesting. Look at the data! I have a string, a number and a list object in the blocks. But the most fun one is where I stored the blockchain so far IN THE BLOCKCHAIN ITSELF!

The block chain:
 [
 {
 "hash": "00dc2439d7e2c98bd734ae34500a2e12337b527a65fece939bba8bddf994fec3",
 "previousHash": "0",
 "data": "A simple String Block",
 "timeStamp": 1520429829508,
 "nonce": 67
 },
 {
 "hash": "00e803769a934d0ed9d8cfc3c5770098368aef3f7ddab128af7ecac788aaa556",
 "previousHash": "00dc2439d7e2c98bd734ae34500a2e12337b527a65fece939bba8bddf994fec3",
 "data": 45,
 "timeStamp": 1520429829546,
 "nonce": 48
 },
 {
 "hash": "0008c3ca3bb6fbefd6d3eb1f3d8537a98015608c4b0f7ffc3ea22f02415bc81b",
 "previousHash": "00e803769a934d0ed9d8cfc3c5770098368aef3f7ddab128af7ecac788aaa556",
 "data": [
  "Item 1",
  "Item 2",
  "Item 3"
  ],
 "timeStamp": 1520429829551,
 "nonce": 534
 },
 {
 "hash": "00483c07382ee421e852638a6546896c61580d7b86e465265bc034ad200aa026",
 "previousHash": "0008c3ca3bb6fbefd6d3eb1f3d8537a98015608c4b0f7ffc3ea22f02415bc81b",
*** LOOK I'VE GOT A BLOCKCHAIN STORED IN A BLOCKCHAIN ****
  "data": [
  {
  "hash": "00dc2439d7e2c98bd734ae34500a2e12337b527a65fece939bba8bddf994fec3",
  "previousHash": "0",
  "data": "A simple String Block",
  "timeStamp": 1520429829508,
  "nonce": 67
  },
  {
  "hash": "00e803769a934d0ed9d8cfc3c5770098368aef3f7ddab128af7ecac788aaa556",
  "previousHash": "00dc2439d7e2c98bd734ae34500a2e12337b527a65fece939bba8bddf994fec3",
  "data": 45,
  "timeStamp": 1520429829546,
  "nonce": 48
  },
  {
  "hash": "0008c3ca3bb6fbefd6d3eb1f3d8537a98015608c4b0f7ffc3ea22f02415bc81b",
  "previousHash": "00e803769a934d0ed9d8cfc3c5770098368aef3f7ddab128af7ecac788aaa556",
  "data": [
  "Item 1",
  "Item 2",
  "Item 3"
  ],
  "timeStamp": 1520429829551,
  "nonce": 534
  }
  ],
 "timeStamp": 1520429829592,
 "nonce": 142
 }
 ]

 

Exciting huh?

That’s just the power of this kind of ledger.

Here’s the java code I use to create this – note I’ve not included all my blockchain code for security purposes. There are a few other classes involved.

//store a string

blockchain.add(new Block("A simple String Block", "0"));
 blockchain.get(0).mineBlock(difficulty);

// Store a number
blockchain.add(new Block(45, blockchain.get(blockchain.size() - 1).hash));
 blockchain.get(1).mineBlock(difficulty);

// Store a list of stuff
List x = new ArrayList();
 x.add("Item 1");
 x.add("Item 2");
 x.add("Item 3");

blockchain.add(new Block(x, blockchain.get(blockchain.size() - 1).hash));
 blockchain.get(2).mineBlock(difficulty);

// Make a copy of this blockchain but I could build a new blockchain
// basically y could be a new blockchain written into this blockchain
// I could even have blockchains within blockchains

Object y = blockchain.clone();

blockchain.add(new Block(y, blockchain.get(blockchain.size() - 1).hash));
 blockchain.get(3).mineBlock(difficulty);

System.out.println("\nBlockchain is Valid: " + isChainValid());

String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
 System.out.println("\nThe block chain: ");
 System.out.println(blockchainJson);

Is that awesome or what?!

Issues

Ok first issue is ofcourse once a block is written it cannot be changed. So in fact if you want to have a blockchain inside a blockchain the blockchain may become split up – so the solution of that is the first block in the “blockchain inside the blockchain” includes the data of the master block or 0.

This way we can have an endless number of blockchains within other blockchains. All you’d need to know is the main key. When you write the second blockchain into the blockchain the first C record tells you that this is a continuation of 1. So technically if you start mid-blockchain you could navigate backwards. The problem would be that you could not navigate forwards from b as the c record has not been written yet.

Alternatively you could store each blockchain in a simple indexed database so you could just add to them forever.

 

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.