How mining works — Week 2 of Learning.

How mining works — Week 2 of Learning.

In this blog, we will dive deeper into blockchain and one of its fundamental components is mining. Mining is a process of adding new blocks to the chain.

Side context : This week 2 of exploring and diving deep into Blockchain, Protocols, Cryptography, Tokens, Smart Contracts and a lot more. Stay tuned!

People all around the world use blockchain networks to digitally timestamp various stuff like transactions, art works, applications, etc. As blockchain provides immutability it builds trust among people.

  • Several chunks of data combine to form a block. If you were a miner which data would you pick? Of course the one which pays the best fees, and you remember it was called GAS Fees. A miner picks the transaction from Mempool (fancy name for list of pending transactions) based on GAS Fees.

  • A miner is given a problem (now this depends on consensus protocol, we will discuss in the next blog) to solve. We will discuss this process today.

  • When a miner mines a block, every node of the network i.e. other machines verifies and validates the block and then adds it to their copy of the chain.

  • When this process is completed, the miner receives the GAS fees as payment and some tokens, i.e. coins of the network as reward. (Do you know every 4 years, the reward coins in BTC network halves? We will look into this while learning about Bitcoin.)

This is an overview of the process. Let's dive deeper into understanding the essential elements.

In the last blog, I lied to you. HASH of the block does not only depend on block’s data and previous block’s HASH, there are 3 more things : Block no., Nonce & Timestamp.

The main process is generating a HASH of the block below a certain target. This target is also a HASH and keeps changing after every 2 weeks. The Block no. is self explanatory and cannot be changed so there is no need to further explain.

Nonce : This is a 32 bit positive number i.e. 2³²-1 possible numbers which is approximately 4 x 10⁹ (4 Billion) numbers. So the miners have to try every permutation of nonce to generate a HASH below Target HASH.

As a HASH is 16 bit x 64 characters long, total possibilities of HASHES = 16⁶⁴ ~ 10⁷⁷. So only 4 x 10⁹ hashes can be generated using these nonces which is very very less than all possibilities i.e. 10⁷⁷ hashes. So there might be some hashes which can never be generated. Therefore there exists a target, and miner needs to generate a HASH below the given target. This eases the complexity of mining.

So every miner uses either CPU, GPU or ASIC miner. Simply explained these are hardware which are used to generate HASHES so these have Hashrate. Hashrate is the number of HASHES produced every second, for CPU it is around 100K ~ 10⁵ H/s, for GPU around 80M ~ 80 x 10⁶ H/s and lastly God of Hashrate ASIC miners have around 80T ~ 80 x 10¹² H/s.

A modest miner has a Hashrate of 10⁸ H/s so it will exhaust all the possible nonce in 40seconds. What to do now ?

Now comes Timestamp into the picture. This timestamp is UNIX time i.e. how much time from when the first UNIX system was setup & run(i.e. 1st January 1970 UTC) up to now. It is in seconds and the same for everyone across the globe.

So after every second the nonce range gets reset and you can start over again.

But there are miners who can exhaust these range of nonce in fractions of seconds. So what for them ? Do they sit idle after exhausting the range ?

Now Mempool plays the most important part. Supposing there are 10 transactions in the Mempool arranged in order of Highest to Lowest GAS fees and assuming a block will take only 6 transactions. Understand the process now :

  1. The miner took top 6 transactions as they pay highest. Our miner has a very high Hashrate such that it exhausts all the possible nonces in 1/4th of second. We know the timestamp will change after a second, so do we sit idle for the next 3/4th of second ? No.

  2. The miner will swap the 6th transaction with 7th (next highest paying one) and repeat the process.

  3. This process is looped until the timestamp changes.

Now since not all the miners have such high mining power, they join Mining Pools.

These mining pools prevent institutional miners from cashing all the profits. Mining pools are a group of miners who share their mining powers and each member is rewarded based upon its contribution of mining power to the pool.

See you in the next week! Signing off. Keep Learning!

Source : Code Eater