Image source: Davidstankiewicz CC BY-SA 4.0

Blockchain I - Creating and Understanding your very first own Blockchain

Felix Klauke
FelixKlauke

--

You ever wanted to create your own blockchain but you are still unsure, what a blockchain actually is and how do you implement it? In the following you will learn, how you can create your own sweet little blockchain, meow.

What is a Blockchain?

At the end a blockchain is nothing other than a sequence of units starting with the so called genesis block, the root of all evil. The chain aspect is gained from the data of a single unit, the so called Block. This data usually contains the hash of the previous block, a timestamp and the content we want to manage: Transactions. As every unit holds a unique reference on its predecessor you can easily imagine a chain.

Understanding Transactions

The easiest definition of a transaction is the same like known from databases or similar, where a Transaction is a self contained set of instructions that should be executed as a whole without any issues or not at all. An example would be the transfer of money from person A to person B. To complete a transaction we have to withdraw the money from A and deposit it on B. Never ever only one step should be executed.
Now A wants to send money to B. The transaction starts, money is withdrawn from a and moved to B. Transaction complete. This transaction has to be saved and authenticated. To achieve that, we save the transaction in a block and append the block at the end of the blockchain.

Why does saving a transaction in a blockchain make it authentic or why is a blockchain safe?

The trick is that a block, once it is in the blockchain, can’t be altered. The transactions in the block are part of the blocks hash and changing the transactions would mean changing the hash. As the hashes actually hold the chain together this step would break the chain from the point where a hash changed. As soon as a blockchain loses its integrity this way you can say that all transaction from a certain point are bullshit.

Modeling in Java I

Until now we can clearly identify three entities: The Blockchain, The Block and The Transaction. It’s easy to create a simple model now:

Mining a new Block

This would already be a working blockchain. But there is still one thing unclear: Can everyone just come and append a Block? Well, we will have to add an aspect that confirms a new block, the so called consensus. There are several ideas about how a consensus can be created. The most popular algorithm is the Proof of Work for example used by Bitcoin or Ethereum. Creating this proof is a main task when mining and appending a new block.

The proof of Work

The proof of work can be seen as a payment. You need to do some work before you’re worthy appending a new block. Bitcoin implemented an algorithm where you have to find a valid hash consisting of the last proof of work and a newly created number, where the hash has to begin with a certain number of leading zeros. The amount of needed zeros defines the difficulty.

Modeling in Java II

For implementing the proof of work we have to extend our current model by a proof work work property in the Block entity and add an algorithm for finding and validating proofs of work.

The new Block entity:

The algorithm for finding and validating a proof of work:

And how do I hash a Block?

For hashing a block we will join all its properties with a delimiter and then hash the resulting String using SHA256 like before with the proof of work.

Putting all the things together

You now know everything you need for creating your own blockchain. With some changes and additions your Code could look like the following code. In the next part we will talk about how we can create a network of nodes working on the blockchain.

--

--

Felix Klauke
FelixKlauke

25, oving infrastructure and backend services and networking, devops by night, privatizing the world peace, only doing the extravagant jobs