DEV Community

Pedro Matias de Araujo
Pedro Matias de Araujo

Posted on • Originally published at Medium on

Creating a blockchain with less 100 code lines

Creating a blockchain with less than 100 code lines

Photo by Thought Catalog on Unsplash

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) or also blockchain+IOT(Internet of things), and this is just the start for the technology.

One simply image about the blockchain’s concept

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

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.

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:

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

In this version of our blockchain, we not implemented the Proof of Work, 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, 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.

See you soon!

Top comments (0)