In this era of modern communication and knowledge sharing, it’s almost not necessary to discuss how blockchain works. There is no need of reinventing the wheel. And as I am trying to write my journey in the blockchain, Let’s share my point of view and understanding about this technology. Also, you can make comments and make my knowledge more solid by challenging me with the items inside.
Now let’s jump right into the blog, shall we?
I will try to give you my understanding in a very visual way. A blockchain is simply a chain of block connected with each other.
- Block: It’s a number. It starts from 1 in every block chain. The number 1 block is called the genesis block. Within smart contracts, block numbers are represented as a 256-bit unsigned integer, so the maximum block number is 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,4.
I will discuss more on smart contract in another writing.
- Nonce: Another number. Just remember that I will be discussing this item in later part of the article.
- Data: Mainly transactional data or any immutable data your blockchain is designed to hold.
- Prev: It holds the hash of the previous block. For this example, I am using the genesis block. That means it doesn’t have a previous block. That’s why it’s 000000…000.
- Hash: this is the hash generated for the current block.
To understand how the hash works, we must understand how the hash is generated and what role it plays in the blockchain. This hash is generated using SHA-256 cryptography algorithm. You can call it the fingerprint of some digital data(in this case: information in Data section).
An empty data set might look like this:
Which can be changed if I type anything in the box. For example, I am typing “brain station 23 Ltd.
See, how the hash changed. Now it looks pretty straight forward. But, if you try to make a small change such as a letter “Brain station 23 Ltd.”, see what happens:
The algorithm gave us a completely different hash. If you change it back to “brain station 23 Ltd.”, You will see the previous hash again.
So, there are two important things to remember about this concept. First- if you change anything, the algorithm will generate a completely new hash. Second, for unchanged information, the algorithm will generate the same hash. This behavior is called avalanche effect. A key aspect of cryptographic hash functions is their collision resistance: no one should be able to find two different input values that result in the same hash output. SHA-256 generates an almost-unique 256-bit (32-byte) signature for a text. For this unique property, in the hash section of a block, sha-256 is used.
Now, the promise I made earlier, the nonce concept. I am using a demo I found in internet to demonstrate what I understood. Look at the image below:
The first block is genesis block which started with 000…00 hash. On the right side, you can see the prev section is showing the hash of the previous genesis block. Why it started with four zeros (0000). The possibility is higher that this blocks are already mined in the system or the miner chose only the hash which started with leading 4 zeros. Let’s try to change the Nonce and see what happens.
The block became red and the leading 4 zeros are gone. For the nonce value= 7 and data available in the block, this unique hash was generated. It became red in the demo because it was indicating that the block is not mined and didn’t meet the criteria to mine it. If I try to change it again to 8, 9 10, it gives me hash with no leading four zeros. To generate the right hash, the demo creator made the button mine. But the question is what is happening with the nonce. As a miner, we can’t change the data inside the block. If we change it, it won’t mean anything as the data is all encrypted with sha-256 as well. So, miner changes the nonce only. The miner’s function runs into the machine iterates through the nonce to generate a hash which meets the requirement to be part of the hash chain. If we click mine, we will see this:
As you can see, the hash is starting with 4 leading 0 which has met the requirement to be part of this blockchain. Look at the nonce, it is 35230. Simply we can say, the miner machine had to iterate for over 35k times to generate this hash and mine it. I think you may imagine how much machine power is needed for iterate millions of times a second. There are some questions can come to your mind, feel free to ask.
Coming soon: As we can mine a block whenever we can, how the security is maintained! I will answer this question in my next writing briefly and I will start guiding how you can set up your pc to start working on the blockchain.
Until next time, Enjoy.