DISCLAIMER: In this article I will explain the working of blockchain in a very abstract level. This article is not meant to explain every aspect of blockchain. I will reference some sources in the end for better knowledge.
So, what’s up with this new buzzword “Blockchain” and how it powers the cryptocurrency… Bitcoin, Ethereum, Dogecoin?. And can you make one? YES. But read the full article.🛑
Blockchain is a decentralized distributed digital ledger.
Ok, why do I care? Blockchain may be powering the cryptocurrency but it’s not just that.
To maintain data integrity of healthcare systems, Original content creation, NFT’s, and so on.
Now how the blockchain achieves this many use cases?
Let’s try to understand the working of a simple blockchain,
A blockchain consists of blocks that are linked with chains (If you are a CS student, this is like a linked list). Each block will contain its previous block’s hash (we call it a chain) and the current block will generate hash from the blocks content including the previous block’s hash and this creates a chain of blocks.
A block contains a timestamp, hash of the previous block, data, nonce, and current blocks hash. But if no one has ever used the blockchain how will it have a previous block?
The first block is called the Genesis block which will be created before any of the block is created and the others are the block that we can use to store data such as money transfers, digital artworks, or anything that can be represented as digital data. The genesis block will contain nothing(might have data related to genesis) and its previous hash will be set to 0 and every consecutive block will use the genesis blocks hash to maintain the integrity.
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes
Simply put if you pass data to a hash function it will give a string that might look randomized but if you pass the same data to the hash function it will always produce the same result and by even changing a single bit in the input data will transform the output hash different making it quite hard to do analytical attacks. We can use the hash to verify if a file has been modified so it is used in many chat applications and file download services. Some download services will show the file hash so that after downloading you can verify that the file has not tampered with on transit.
How blockchain is considered secure?
It’s secure simply by eliminating the central point of failure. Traditional systems will store data in a central database and when the database is compromised all the data is open for change. And even if the database had the hash of each data now the attacker can recalculate the hash of each data and the clients will not know anything because the site will now show the tampered file and its hash, not the original hash.
Blockchain eliminates this by using peer-to-peer networks. So there is no such thing as a central database, each client in the blockchain network is a database or everyone in the blockchain network will have a copy of the chain. Meaning every time someone tries to add a block they will have to broadcast that block to all the individual in the network to sync so that everybody has the same copy. But what if someone doesn't broadcast? Simply the block will be rejected and will not be added to the blockchain.
In order for a block to be added to the blockchain, at least 50% of the nodes in the network needs to verify and accept the addition of the block.
So the attacker cannot attack the network as the network is peer-to-peer and the only way he can attack is to join the network, but what if he can change the blocks after joining. Assume Bob owes you 50$,
Let’s say you(Alice) sent 10$ to bob. (Block 1)
And bob sends 20$ back to you. (Block 2)
A new user(Sally) joins the network and transfers 100$ to bob as a gift (Block 3)
Sally transfers 100$ to Alice as a gift (Block 4)
But you owe bob 50$ but he says that he transferred 50$ already, but you’ve only got 20$. If only Bob could have altered the chain this wouldn’t be possible. If he altered the data in Block 2, he is in trouble… because each block contains the hash of the previous block so if bob changes Block 2 he has to change the Block 3 hash and it will invalidate Block 4 and so on.
Let’s say he was able to recalculate all the block hash and cheated the network but still you can go to the blockchain network and see that he only sent 20$ and everyone in the network can see that he only transferred 20$ because bob could only change his copy of the blockchain and as the blockchain is distributed so everyone in the network will have the copy. Once the network detects there is a tampered version of the copy the tampered version will be replaced with the legit copy.
So bob was caught red-handed🥰.
This is an oversimplification as real working blockchain networks will have stronger hash functions and longer chains and further more security standards so that it is near impossible to recalculate all the hash to tamper the network.
There are so many other cool features of blockchain but how about building one.
We will be building a blockchain to store every day activities, like a notebook for writing down about your day
This is optional but it’s efficient to understand the concept, I am gonna program it in python for easier understanding and you can try to implement it in other languages.
We need to import two libraries, SHA1 from Crypto library for hashing the data and datetime for timestamps.
Now we will define the structure of the block.
Now we have the block but we need a structure where we can store our blocks, so we will now define the blockchain class.
I tried to make the code self-explanatory but I will just go through the verifyIntegrity function. This function will iterate through each blocks and check its stored hash and compare it with the original hash by regenerating the hash for that particular block.
How to verify the blocks?
Of course, we have verifyIntegrity function but still a attacker can recalculate the hashes for all the blocks. To prevent this we have a protocol called “Proof of Work”.
Proof of Work
It is a method of making sure that the block is a consensus algorithm which verifies the block before it gets added to the chain. Yeah the block wont directly be appended rather it has to go through a puzzle game where the block has to generate a hash which will have preceding N number of zeros.
This puzzle is a cryptographic puzzle where a group of people tries to solve the problem and once the problem is solved (ie. the particular block has a hash which precedes with N number of zeros) then the one who solved the problem will send the block to be added to the main blockchain.
Now, as I already mentioned the hash function will produce the same output when it is provided with the same input data then how can we expect a block to produce a different hash(with preceding N zeros) while having the same data? For this reason, another property will be in the block called the nonce. This will be a counter variable, so every time we create a new hash for it to have preceding N zeros, we increment the nonce by 1.
So now the hash will differ every time because we increment the nonce every try. So why do we need to do this?, How does this make the blockchain secure?.
There is no magic algorithm to predict the golden nonce value that can produce the hash with preceding N zeros so we have to brute-force one by one until we get the desired hash. For this reason the bigger the N is, the longer the time it takes to add the block to the chain which also means if an attacker tries to change a block, then the attacker needs to spend an equal amount of time to produce the hash with preceding N zeros so it’s near impossible for the attacker to calculate all the block’s hash. So now the hash will differ every time because we increment the nonce every try. So why do we need to do this?, How does this make the blockchain secure?.
Who does the Proof of Work?
These cryptographic puzzles are very computationally intensive and the bigger the N and more advanced hash functions this puzzle can be very hard for a laptop to compute. So we need high-performance hardware’s but not everyone in the network can afford to verify each copy of the blockchain, what else can we do?
Miners. Miners are a group of people or an individual trying to solve these puzzles using their own hardware’s. These hardware’s are generally cryptographic specific processors which are fine-tuned to calculate millions of hashes per second. Great powers come with greater responsibilities, so this hardware still needs electricity to run and the electricity bills may skyrocket if you try to do it on your own. Good luck👍.
When a new block is being added it is broadcasted. The miners listen for the broadcast and each of the miners will try to find the correct nonce for the particular block and the one who completes first will send the block with the new nonce and the new hash. Only then the block can be added to the blockchain. But why do miners spend their money to make the blockchain secure? Because when a miner successfully solves a puzzle then he is rewarded with a certain amount of cryptocurrency. Or people in the network can voluntarily add some amount of cryptocurrency for the miner as a reward.
This is why RTX 30 series card’s are scarce 😡
JK, miners are a reason but also scalpers too…
Alright, now how to implement this part in code:
We are going to import the time library for measuring the time taken to mine.
We just need to add a nonce property in the block class.
In Blockchain class we just need to update the addBlock function and add a new property called difficulty.
So the constructor of Blockchain will look like this:
Now we need a miner, so we will define the miner class.
That’s it, now all the blocks will be verified before adding. You can change the difficulty to any number (Bigger the better and slower).
Now all these are fine but there is still a problem. How are going to continue the Proof of Work if it is using more energy to operate?. Environmental issues are a problem and as the more computational power, someone has they can solve more problems and get more rewards. So this raised a new problem called Miner pools, miner pool is a group of miners sharing their resources to solve the problem and share the rewards among the pool members. You might think this isn’t a big deal but this centralizes the mining process which is not good for a decentralized system and also this can lead to fraudulent blocks being added to the blockchain. This is because if the combined miners can make up to 51% of the network then they have the power to approve the fraudulent blocks because to add a single block at least 50% of nodes need to agree to the addition of the block.
Full code on gist: Simple blockchain implementation with python (github.com). This gist is old and will not contain the same code as above screenshot as I have updated some code afterwards. Use this instead: hiruthicShaSS/simple-blockchain: Simple implementation of blockchain in python for medium article (github.com)
Blockchain is not just for transferring cryptos, you can store any data in blockchain and the longer the chain is the stronger the network is to break. Blockchain do have some vulnerabilities but so far it is far more better than traditional methods.
This story is just part one, in next part we will create the structure to store our daily activities and storing multiple activities in a single block and more. Stay tuned…
I tried my best to explain what I know, please feel free to comment if you think there is a mistake, I will try to revise the post as soon as possible.
Parts of the above code was inspired by Simply Explained on YouTube. I really encourage you to watch his video for better understanding.
(3) But how does bitcoin actually work? — YouTube
If you have reached this far I hope you now got a basic knowledge of blockchain and hopefully I didn't over complicate anything😅.