On crypto currency and mining (a “Bit” more than) gold
Ahem Ahem!!! Let”s discuss mining..No not the conventional gold or coal mining but bitcoin mining.
Even after studying a lot about crypto currency and blockchain, there was one thing related to it that I was really not well aware of and that is mining. And back then I didn’t even explore about it because my nature is such that I need a trigger or for our Bollywood fans, I need a kick to do something in life. And same was the case with crypto currency mining. So one fine morning as I was sitting in my office I heard (actually overheard) an earnest conversation about mining a crypto currency going on at the subsequent bay. And that was all I needed to begin my research on the topic.
Let me ask you a question. What can be more rewarding than mining gold? Well you can tell me coal or iron but for me that is mining bitcoin. Let me put this in perspective. To mine a single block in the bitcoin blockchain you are rewarded with 12.5 bitcoins plus something else that I will tell you later. And we all know the current price of 1 bitcoin. So the natural question that comes to our mind is why isn’t everyone doing it? And the simple answer is because it is very resource intensive and difficult thing to do. So without any further ado lets discuss bitcoin mining.
BEWARE!!! SOME TECHNICAL STUFF AHEAD. TECHNICAL DISCRETION REQUIRED
When I was very new to the topic I thought that every transaction that is happening is equivalent to one block in the blockchain. Little did I know that I was completely wrong in my understanding. Let’s backup a little and talk about transaction. So once a bitcoin transaction happens and is verified it is added in the transaction pool. And miners who are competing to add a block in the blockchain for the simple reason that they are paid with newly created bitcoins take a specific number of transactions from the transaction pool and try to form a block. The size of the block should be less than a specific MAX_BLOCK size which is defined by the bitcoin protocol. Each transaction has a size and the sum of the sizes of all the transactions picked up by the miner should be less than the MAX_BLOCK size.
Now suppose you are a miner and you are competing to add a block in the blockchain to be rewarded. In the meantime you are also collecting transactions to form the next block and the next block which you are creating to add to the blockchain is called a candidate block. There are a lot of transactions in the transaction pool and they are picked up using a priority metric picking the highest priority transactions first. There is a simple formula to calculate the prority of a transaction which is the sum of the value and age of the inputs divided by the transaction size. In the above formula it is easy to understand value and transaction size parameters but what does age refer to? Suppose you have been added in the transaction pool and then you have not been picked up for say 3 blocks then your age will increase and so will your priority. Hence, when miners build a block the first 50 kb is reserved for highest priority transactions even if there is no processing fee added in these transactions which means that these transactions are added in the block in any case. Then they fill the remaining space with transactions having a minimum transaction fee attached to them prioritising the transactions with maximum fee per kb. Any transactions that are remaining are left in the pool to be picked up in the next block increasing their age and in turn their priority.
Once all the transactions have been picked up the process of forming the block is started. The first transaction added in the block is the generation transaction which is the transaction of transferring the bitcoin reward plus the sum of the fees from all the transactions that are picked up to the miner. To construct the block header mining node needs to have six fields. The list of the fields includes Version, Previous block hash, Merkle root, Timestamp, Difficulty target and Nonce. I will try to elaborate each and every field.
Version is simply the current version of the blockchain structure going on. Previous block hash is the hash of the previous block added in the blockchain. The next step is to calculate the merkle root. This basically follows the merkle tree data structure. In this step all the transaction pick-ups are hashed in pairs and then the process is repeated until we have reached the root of the tree. The only condition being that the number of transactions added in the block is even. So suppose you have 150 transactions and you add 1 more generation transaction, so the total number of transactions to be hashed is 151 but we need to have even number of transactions to calculate the merkle root. To do so we create a duplicate of the last transaction. The root of the merkle tree summarises all the transactions into a 32 bit value. In the next step mining node adds a 4 byte timestamp which is based on the number of seconds elapsed from January 1 1970. The node then fills in the difficulty target which is required to make this a valid block. The difficulty target is required to implement the proof of work concept of the protocol. The nonce value is initialised to 0.
Now the header is constructed so that the mining can begin. The main difficulty involved in mining is finding a nonce value which results in the blockchain header to be less than a difficulty target. I understand that the above explanation is contrite so now I will try to elaborate it.
Basically the main process of mining is to find a hash by hashing the above values using SHA-256 which is less than the value specified in the difficulty target. SHA-256 is basically a hashing technique. All the values in the header are constant except for the nonce. So the miners adjust the value of the nonce to create a new hash with the hope that the hash generated is less than the specified difficulty target. To better understand the concept I will take the help of an example. Suppose you have a pack of 20 cards with distinct numbers written on them in the range 1 to 20 on each card. Now the game is to pick two cards and you will win the game if the sum of numbers written on them is less than 40. And now if i want to increase the difficulty of the game i would ask you to do the same thing but this time for you to win the sum of numbers written on the cards should be less than 20. The same type of thing is achieved in case of bitcoin mining by adjusting the difficulty target. The main purpose of adjusting the difficulty is to make sure that the generation of new blocks takes 10 minutes. If the number of nodes participating in the blockchain increases the difficulty is adjusted so that it still takes the same amount of time. So the process of mining is to find a hash using SHA-256 by inputting the block header so that its value is between 0 and the difficulty target. The difficulty is adjusted after every 2016 blocks are generated. So if the average time taken to create a block is less than 10 minutes then the difficulty is increased and vice versa. To better understand this let’s say the difficulty target is a 256 bit hash value having 60 zeroes which means the miner has to find a number between 0 and the difficulty target and to do so the network needs to do at least 150 quadrillion hash calculations to find a valid hash. And if you remember correctly the only thing that can be varied is the nonce in the block header.
If you are the first one to successfully mine, you are rewarded with newly created bitcoins and the sum of all the transaction fees. This is automatically done as the first transaction added in the block is the generation transaction. Once the block is created it is verified and added to the block chain, and the race for the mining of next block starts. The main purpose of mining is to safeguard the block chain from hacks and miners are rewarded for the same reason.
One more thing before I sign off: The limit of number of bitcoins that can be created is 21 million before 2140. To achieve this the reward for mining is decreased to half every 4 years. When it all started 50 bitcoins were given as reward for every successful block that was mined which was decreased to 25 in 2012. Currently if you mine a block you are rewarded 12.5 bitcoins. After 2140 there will be no reward to mine and the miners will only receive the transactions fees as reward. Who knows perhaps we’ll be doing something entirely different and more rewarding then. For that’s more than a century ahead and the human race has brought revolutions in just decades. Your wild imaginations already begun? Good! but don’t stop exploring crypto currency and mining as long as they are rewarding for a bird in hand is worth two in the bush.
Through this post I have tried to explain the concept of bitcoin mining. This is just to give you an idea on how the mining of crypto currencies take place. I hope I was able to explain the concept of mining. If you have any questions, you can ask me in the comments section. Please feel free to connect. Signing off…