Creating a blockchain with less 100 code lines
Tags: crypto, cryptography, blockchain, python
Published: 2019-04-03 02:23:08

---
title: Creating a blockchain with less 100 code lines
published: true
tags: crypto,cryptography,blockchain,python
canonical_url: https://medium.com/@pemtajo/creating-a-blockchain-with-less-100-code-lines-7168849d687d
---

### Creating a blockchain with less than 100 code lines

![](https://cdn-images-1.medium.com/max/1024/0*QADifoe26ax-QP_9)<figcaption>Photo by <a href="https://unsplash.com/@thoughtcatalog?utm_source=medium&amp;utm_medium=referral">Thought Catalog</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

The basic concept of blockchain is very simple: _a distributed database that maintains a continuously growing list of ordered records._

The blockchain is a term normally associated with Bitcoin and/or Ethereum, but blockchain is more than that, blockchain is the technology behind these, and behind any other cryptocurrency.

There is a lot of other’s uses for blockchain, per example, games ([CryptoKitties](https://www.cryptokitties.co/)) or also blockchain+IOT(Internet of things), and this is just the start for the technology.

![](https://cdn-images-1.medium.com/max/805/1*TV_02Syq-SWRChiXigN8Rw.png)<figcaption>One simply image about the blockchain’s concept</figcaption>

The blockchain is, like the name said, one _chain of blocks,_ so, we have the first class, the Block.

![](https://cdn-images-1.medium.com/max/1024/1*0D3Uc7yIyR7Kv-n7Fq6vXA.png)

In this stage my block has the attributes:

- index — the block index the position in the chain
- timestamp — date and time that the block was added in Blockchain
- data — the data value, in other words, what you want to save
- previous hash — the hash of the block index -1
- hash — the hash of the block

If you don’t know what’s hash, I did explain in my last post [here](https://dev.to/pemtajo/an-overview-about-hash-functions-theory-and-security-139e).

You will see some things interesting in the picture and here I will explain a little bit:

- let more OOP the function _isValid_ is for each block respond if he is valid
- The constructor defines all the things in the block
- the function “update” is to update the _dict_ when reading from a file, this is for saving the data for future
- calculating the hash for previously saved files, to convert always to the same encode, because, differents encodes have different characters and different characters produce different hashes.

So this is a chain valid, if the block was changed, the current block will know, and make yourself invalid, and if any previous block was changed, the chain will know, and all the chain will be invalid. That is the concept that makes the data save in Blockchain immutable.

So looking in our second class, Blockchain, it looks like:

![](https://cdn-images-1.medium.com/max/1024/1*RKxCwUFAebYgfkMjvJCeZA.png)

So, the blockchain class create the blocks and look for any problem in the chain, and this class is responsible for saving in a simple JSON file and read from him. Our first version blockchain is ready!! \o/

All the code is below, you can execute and see the output

{% gist https://gist.github.com/pemtajo/18d0f598033757f7390b514ded6f75b4 %}

In this version of our blockchain, we not implemented the [Proof of Work](https://en.wikipedia.org/wiki/Proof-of-work_system), the idea first is created the blockchain and guaranteed the chain’s integrated. That will be my next step.

I created a project that you can follow in [GitHub](https://github.com/pemtajo/blockchain), I will increment more and more my blockchain, if you have interesting just follow me, I will write some posts about all the process.

_P.S: I am not an expert in the blockchain, so any problem, code fix, or tips, this is will be very welcome to comment below and will help some peoples too. Or you can talk to me privately in the_ [_LinkedIn_](https://www.linkedin.com/in/pedromaraujo/)_._

See you soon!