Learn blockchain by creating one.
Blockchain will bring a lot of disruption in the IT-sphere but how precisely do you implement one. This article will take you through the entire completion of a simple blockchain project in python.
Blockchain is a public ledger being shared between all the nodes in a peer-to-peer network. It is composed of a multitude of blocks being each cryptographically linked together and that is ruled by a protocol.
If you want to get in more depth in Blockchain technology you can go see my article on the subject.
Blockchain has been kind of a buzz word lately the media and different type of companies used the word for pretty much…
Begin with the block:
As we stated above the blockchain is a cryptographically linked chain of blocks so it is logical to begin by implementing a block class. This structure is quite similar to the linked list one this is why we will use it as a base for our blockchain project.
We will begin by creating a Block class. Let’s take a look at our constructor function and explain all its different components.
Let’s take a look at our constructor function and explain all its different components.
- Index: The index tell the number of the block (i.e is position in the blockchain)
- Nonce: The nonce is number that is used in the proof of work algorithm that we will explain later in this article.
- Timestamp: The timestamp tell when the block was created.
- Block_hash: The block_hash hold the hash of the block we will takl about hash and hashing function after.
- Previous_hash: As we stated above all blocks in the blockchain are linked together this is the part that do the linking by holding the hash of the block before it.
- Transaction: Transaction is used to hold the data that you whant to put in the blockchain.
Hash and hashing function.
A hash is a digital signature of a block it is made with the help of a hash function. A hash function is a mathematical function that turn any amount of data into an alphanumeric string with a fixed length. Any change even the smallest into the input will result in a totally different hash. This makes it almost impossible to find the input of a hash function giving the output.
As we can state from the code above just a small change into the input will give a totally different hash.
Now that we understand hash and hash function better we can implement the method that will hash our block.
Blockchain and all its componant.
After creating our Block class we will implement a Blockchain class that will hold all the functionlity our Blockchain will have.
Let begin by creating the constructor.
Our constructor will be pretty simple because it wil only have two things a list called unconfirmed_transactions and a list called chain.
I think the named of the list are pretty self-explanatory but just to be sure the unconfirmed_transactions will hold all the transaction that are not in a block but that we want to put into one, and the chain one will actually hold all of our Blocks. Now let look at how to implement it.
Create the genesis block.
The first block in a blockchain is named the genesis block there is two way of creating a genesis block. You can either hardcoded it into the blockchain or do it manually. For or project we will hardcoded it into our Blockchain class. Here how we will do it.
Adding transaction into the blockchain.
The next step will be to implement a function that will store transaction before we put them into a block. The function below will do the task.
Proof of work and adding block to the blockchain.
Proof-of-work is one of the protocols used in Blockchain this is one special kind of protocol named a consensus protocol.
Why use a proof of work ?
One of the things that make the blockchain so interesting is its decentralization. But one problem emerges when a system is decentralized, and this problem is consensus. In other words, how do you make a decentralized system come to an agreement? In the case of blockchain, this agreement is deciding which block will be added to the blockchain.
Proof-of-work solve this problem easily, it is a consensus algorithm that allows the node in the blockchain to make an agreement.
A consensus protocol allows a decentralized system to achieve an agreement. In the case of the blockchain this agreement consists of choosing which block will be added to the blockchain.
As a matter of fact, you can only add a new block in the blockchain after a consensus between the participants in the network, and once this is done the block can never be erased or change.
But, how does a proof-of-work work?
In proof-of-works nodes in the computer compete against each other to hash the block until they achieved a certain result that is chosen by the protocol for example the giving hash should begin with a certain number of zeros. To do so they tweak a parameter that is in the block to get the right hash usually the parameter tweak is the nonce that we explain in a previous part of this article.
So lets implement our proof-of-work algorithm and a function to add block to the blockchain. This part will be longer than the other one but it will also be the more important.
Mining a block.
The action of solving the proof-of-work and adding a block to the blockchain is called mining. So lets implement a mining method.
Creating an interface and putting it all together.
The next step consists of creating a simple interface for our project. The implementation of the interface will be simple and we will use Flask to create it.
The code is below ,and the only thing it does is giving us each block in our blockchain.
To make your project first run your python code by typing python [file of your project] and then go to http://127.0.0.1:8000/.
You can see all the source code in my GitHub repository here.