The beginner guide to blockchain.
If you are probably new to blockchain, brace yourself, after this story, you will have complete idea of blockchain and how it works.
Before You get started, I recommend you to check out the previous story here “The philosophy of blockchain” that helps understanding the big picture.
The Block chain technology is relatively simpler than other technologies in CS as it has a lot of understandable concepts and consensus, so you got nothing to be scared when it comes to learning Blockchain so Relax….
Let me start with a simple question = what’s blockchain??
Blockchain = A chain of blocks cryptographically linked together which are immutable & impossible to change after attached to the chain.
Each block has a finite number data parameters which are Block number, Nonce, Data payload, Previous block hash and current block hash.
The first block in chain is called “Genesis block”.
Let’s first understand each of these parameters one by one.
Block number : → As you can see, it’s just the index of the block.
Block hash: → A unique identifier to identify a block, it is generated by a cryptographic algorithm called “SHA256” ( will speak how it works later)
Previous block hash : → As you guess, it’s the hash of the previous block, As you can see the first block doesn’t point to any previous block so the previous hash the first block would be “0000000…”
Nonce : → A 32 bit unsigned integer ( say a number between 0 to 4 billion).
Data : → Contains the actual data that we want to store in the blockchain, for example , transaction data, logistics data, message data etc…
The exact data field contains some metadata of the actual data & Timestamp
Timestamp : → Unix Timestamp since January 1, 1970 (UTC) in total secs ( 1617697097)
As you can see, the data could be transactional data like A → sends B $100 ( transaction 1), B → sends C $50 ( transaction 2) and so on..
Each block has some fixed capacity to store the data in it called generally Block size.
Sounds like Linked list, Doesn’t it??
Yes you can say that at this moment, but it has more things.
so let me add a little spice to it.
Blockchain Network
As you know, a network is a collection of computers or Nodes that are connected together inorder to share the information.
Blockchain network = A collection of computers or Nodes maintain the same blockchain independently and share the information of the chain to anyone in the network.
Two points here.
- The same chain is copied and mainted by every node.
- A node can share the entire blockchain information to any other node that is connected to the network.
so clearly that means that there is no one person or entity that controls or manages the entire chain, it’s everybody who stores, maintains and runs it.
That’s what we call “Distributed ledger”
So we can say, A blockchain is an immutable & distributed ledger that is maintained by everybody in the network.
So far so good right.
now let’s talk about the “immutability”
immutability → Being unable to be changed.
As I said in the beginning, once a block is attached to the chain, it’s not possible to change that block ( well that is just how it should be)
But technically we can change/tamper any block as we want, to prevent this, it’s where the cryptographic algorithm “SHA 256” comes to rescue.
SHA 256
→ It generates a cryptographic hash, that is an almost unique 256-bit signature for any input data ( in this case, the entire block)
→ The hash can’t be decrypted back to the original data and fixed sized for any source of data.
→ The same hash for the same data → if A = 5, A will always be 5.
→ A small change in the data will yield complete new results.
As you can see, the two hashes are completely different even a small change occurred.
The block hash gets calculated by taking all the parameters in the block
Block hash = SHA 256 (Block number + Prev hash + Data + Nonce)
so if one or more parameters change, the current block has gets changed.
so when a block in the chain gets tampered or changed, the block hash will also be changed thus the next block’s previous hash will no longer point to the current tampered block.
The attacker has to change the next block prev hash to fix this, but
if the next block’s prev block hash gets changed , then the next block’s current hash also changed and so on…
so inorder to resolve this, the attacker has to change the hash for all the subsequent blocks in the chain to make the change unoticed.
That seems like possible right?? Yeah it’s possible but wait.
He can do that in a single chain but remember we have the network of independent chains so the attacker has to do that for all the nodes in the network or atleast 51% of nodes in the blockchain network in order to do that job.
As you may predict, its nearly impossible. ( not 100% impossible )
The bigger the chain = the better the security.
But what if the attacker wants to tamper the latest block , inorder to do that , the attacker has to control all nodes or 51% nodes in the network which is relatively easier ( I will let you wonder a bit ).
Now let’s talk about who adds the blocks to the network and how everybody gets or updates the same network.
Since Blockchain is a distributed ledger , running and maintaining by everyone in the netwok, anyone in the network can add a block to their chain first, then they propagate to the network for all others to verify the block & confirm and add the block to their chain.
Block chain is a concept so there are some high level rules those must be followed by everybody in the network.
for example, How to create valid blocks, how to verify them, how to agree the new block, how to maintain, etc..
And also, the blockchain itself has some automation of verification of blocks and security.
One important thing in blockchain is “Consensus” let’s talk about that.
Consensus
→ Consensus = A general agreement of the state of the block chain.
When a new block is added by someone in the network to their chain, then they propagate this new block to everybody in the network,
When someone receives a new block, they do a series of checks on that new block depending upon the blockchain network rules. If valid then they agree the block else they reject the block.
The consensus protocol should take voting into consideration, if more people accepted, then add the block, if more reject the block, the network rejects the block.
The consensus protocol should also be responsible for various cases, like what happens when someone puts a malicious block at the end ?, and what happens if two people add two valid blocks simultaneously to the chain ? etc..
We need to design this consensus protocol according our data & problem.
Bitcoin uses blockchain and has “Proof of work” as it’s consensus protocol. ( will talk more on that later)
at a high level, the consensus protocol’s job is to maintain the secure & valid chain all the time for everybody in the network.
Any block chain network should have strong consensus protocols and strong automation of tasks ( should be hard coded in the network )
Q: Why would anyone do this verifying/adding block job???, should not we just Netflix and Chill.
Alright, now let’s talk about the “Incentives” or usually you may have heard “Mining”.
in 2009, some random dude named “satoshi nakamoto” created a crypto currency called “Bitcoin” which uses blockchain and it’s principles
Bitcoin = “Cryptographically Verifiable digital currency”
The use of blockchain involves here is to store the Transactions of bitcoin as data and maintenance of the network securely by everybody in the network.
So inorder to maintain the network securely , the people in the network should add/verify/reject blocks in the network which of course needs some “Incentives ” this is where the concept of mining comes into the picture.
Mining as an incentive.
→ Everyone who is connected to the block chain is called “Nodes”, who can read & have access to the entire chain data ( of course what’s needed to be seen)
→ But some nodes among them want to create new blocks and verify block are called “Miners” by solving a small cryptographic puzzle when it comes to added blocks and get some incentives if successful everytime.
→ Miners are the one’s who maintain the network of course with the internal automation of the blockchain network to keep it secure.
so what does a miner do??
to understand this first we need to take an application of the blockchain tech, as we speak Bitcoin first uses blockchain so let’s speak about the bitcoin blockchain principals at a high level
The bitcoin blockchain network is created with certain rules internally coded about variuos things of consensus, mining, security, etc… ( will talk about the bitcoin blockchain deeply in coming stories, now let’s only focus on few pieces).
when it comes to mining, the bitcoin network propses a reward to a miner who successfully adds a block to the network by solving a puzzle.
We call “Block reward”, in every 4 years or after every 210,000 blocks are mined, the reward gets reduced by half (called “Bitcoin halving”)
→ So for that reward the miners will compete with each other to solve “that untold cryptographic puzzle” for new blocks.
→ The mining of bitcoin requires huge power and heavy computation equipment
→ New chips are invented to do that computation.
Now let’s talk about that cryptographic puzzle.
Brace yourself.
The cryptographic puzzle
It’s simpler than you think it is.
As we know the sha256 is a 256 bit binary value and it’s represented in hexadecimals, each hexadecimal takes 4 bits of binary values,
the sha256 string will have 64 hexadecimal values ( 64*4 = 256 binary bits)
Now let’s a think of all possible values ( the entire space) that would be betweeen all 0's and all F’s
0000000000……0000 → FFFFFFFFF…….FFFF
The bitcoin network sets a target for each new block, as you can see there is a target, which means any hash that is generated within the target is valid, the others are invalid.
The miners job is to find this valid hash ( given the parameters of the block like block number, data, etc.. we talked above)
so the miner should not be allowed to change some parameters, eg : Data, prev block hash, block number,
The only parameter that miner is allowed to change is Nonce
so the question is to figure out what Nonce value ( including the data) gives a hash that is within the target boundary.
so the miner tries all the Nonce values ( 0 to 4 billion) to figure that hash
As you can see , the first nonce combining with all params gives an invalid hash, the second nonce combining with all params gives a Valid hash.
There is no way we can understand to manipute the Nonce other than trying randomly billions of times due to SHA 256 characteristics (avalanche effect)
The calculation of sha 256 requires heavy computation depending upon the taget difficulty and luck.
A general CPU can calculate 10 Million hashes / sec
A specialized GPU can calculate 1B / sec
An ASIC (Application specified IC) can calculate 1 Trillion hashs/sec
so the miners use this application speacific IC’s which are designed to calculate SHA256 hashs faster.
The mining diffculty depends upon the target boundary, the bigger space = the more easy , the smaller space = difficult.
The more trailling zeros = the more complex
The bitcoin blockchain network does not accept any new blocks whose hash is not within the target space so this is the puzzle, the miners trying to solve.
so once an valid hash is found, then the blockchain allows that block, the miner adds that block to the chain and propagates this block to the network to veriy and attach to their chains.
Of course, the people in the network can reject it if it’s invalid or some tampered happend in the transactions data.
It all depends the block chain rules.
As we spoke about, the network takes voting into consideration and automatically add/reject that block to the network and updates the chain to everybody other miners + non miners.
so there are so many problems we can point at this time like
what happens if two miners add two different blocks at the same time and propagate them to the network??
What happens if someones attacks the network ??
what happens if no miner is there to verify and confirm the data for a while?
I would say that , all the problems depend upon the blockchain rules and how one specific blockchain network handles those ( like bitcoin blockchain)
I will cover in detail in the next story (Bitcoin blockchain),in this story I wanna give you a high level idea of it.
In the next story, I will talk about all the issues that can happen in blockchain, of course the solutions, possible attacks and some interesting things and complete guide to understand the Bitcoin blockchain.
Conclusion
I spoke about Blockchain, Blockchain network, SHA 256, Consensus, and Mining.
That’s all for this story, I will see you in the next story ( Bitcoin blockchain in detail).
For any discussions/ideas/thoughts/ feel free to reach out to me.
Reach out via
Twitter: https://twitter.com/mady_ai
Email : madhusanjeevi.ai@gmail.com
Linkedin : https://www.linkedin.com/in/madhusanjeeviai/
Until then have a great day/night.