Mining; A deep dive
We all want to know the magic behind blockchain security, if you want to know what really makes them tick, this piece is for you.
I’ll explain the intricacies of mining, if you want something more simple, find that here.
What is Mining?
Mining is what it sounds like — it’s ‘finding’ new coins. It’s more like making new coins but same difference.
To be precise, mining is solving complex mathematical puzzles to attach a group of transactions (a block) to the blockchain.
So why Mining?
It’s a matter of security.
In the digital world of blockchains, there’s one supreme rule; the longest blockchain is valid.
Imagine I buy a PS 5 with bitcoin, my transaction is approved and added to the blockchain, then I propose a new blockchain that doesn’t have the block that contains my transaction.
Both chains are valid but one censors my transaction, hence the rule.
But human beings are relentless, this rule is not enough.
Imagine I buy another PS 5 and I once again pay in Bitcoin, but this time I propose a new longer valid blockchain that doesn’t have my transaction. Not the entire block, just my transaction.
I once again censor my transaction so I get my money and a PS 5.
Mining solves this problem and you’ll see how soon.
How does Mining Work?
First, you need a mining permit from the government, next you buy a drill…wrong mining.
The foundation for digital mining is cryptography, precisely hashing so let’s start there.
Hashes
Hashing is a way of representing data.
No matter the input ( photographs, audio, text, etc), a hashing algorithm takes the data outputs a fixed size text output.
It doesn’t matter if it’s a 1 Byte character like ‘A’, or a 25 GB video, a hashing algorithm produces a hash of the same size.
Most blockchains use the SHA-256 algorithm. SHA-256 gives out a 256 bit hash, the hash written in hexadecimal which gives us 64 characters.
Another interesting thing about hashes is that they’re one-way functions.
This means if you’re given a hash you can’t decode it to find the original data.
The SHA-256 hash for A is; 559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd.
The SHA-256 hash for a is; ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb.
The only similarity between the hashes for A and a is that they’re numbers.
Every block on a blockchain has one of these hashes.
The hash for Bitcoin block 715,105 is 0000000000000000000579895ab86ebf34dbe6ea0ddc766e1edd917f25b88445.
(Lots of zeros, remember this.)
How hashes relate to mining
We use hashes to create puzzles.
Yup, we have supercomputer level rigs solving puzzles.
Imagine all the transactions are written on a page, the block header is the page title.
There are 4 parts to a block header;
- The Previous Block Hash
The Hash of the previous block (it links the current block to the previous block forming a chain of blocks). - The transaction root hash
This is the hash that results from hashing the roots of all transactions in a block, if one transaction is changed, the root hash changes. - The NONCE
A number only used once hence NONCE. - The Current Block Hash
A miners job is to find the current block hash and here’s where the energy consuming magic happens.
Miners take the previous block hash and the transaction root hash, and then they have to find a string of numbers or by some ungodly luck one number (the NONCE), which when concatenated with the previous block hash and transaction root hash, produces a hash with with the first X characters being 0.
That’s a huge word salad, try re-eating it.
Concatenate means writinglikethis i.e without spacing.
If you remeber, hashes can’t be reverse engineered so miners have to guess the NONCE by brute force i.e trying numbers until they get a suitable hash.
What if the block hash can’t be found with a number?
That’s impossible. The reason being there are ‘only’ 2²⁵⁶ possible SHA-256 hashes i.e 115792089237316195423570985008687907853269984665640564039457584007913129639936.
A stupid amount, but there’s a limit to the hashes, numbers on the other hand are infinite so there will always be string of numbers that satisfies the condition, it could just end up taking a large amount of time to find.
The first block took over 5 days to mine.
How are block times maintained
If finding the hash takes too much or too little time, how is there order.
The answer; Hash difficulty can be scaled up or down.
How? By increasing or reducing X (the number of required zeroes)
If X is 40, then on average it will take 2⁴⁰ tries - 1,099,511,627,776 to find the correct NONCE
If X is 19, 2¹⁹ tries - 524,288 to find the correct NONCE.
So we can reduce or increase block times by deciding how many zeros we want.
If that went over your head, a good analogy is flipping a coin, if you want 40 heads in a row, you’ll have to flip more times than 19 heads in a row.
Sometimes the coin will just keep fucking with you and you might be there forever and sometimes you get lucky and get 40 heads very fast.
But on average it’s safe to say it’ll take waaaay more time for 19 than 40.
The algorithm regularly adjusts the number of zeros based on time required for previous blocks.
Ok, About the Electricity?
No human is going to guess the correct NONCE in good time, so we turn to computers, and they guess, and guess, and guess, and guess.
The current guessing speed of all nodes on the Bitcoin network alone is at 87 Million Trillion hashses per second.
I kid you not, 87 million Trillion. With 87.62 Million Trillion hashes per second being the lowest in 13 months.
The energy costs should be self explanatory now.
If they’re not, stop and think about that for a second.
Swoosh – 87 M Trillion hashes have been tried.
And that will go on for 10 minutes until the correct NONCE is found.
Can’t disobey the laws of thermodynamics.
What’s in it for Miners?
The only good thing about mining is pay day.
It’s July 2021 and each block is currently worth 6.25 BTC ($206k)
It’s currently 6.25 BTC because the mining rewards per block are halved every 4 years.
So sometime in 2024 it’ll be 3.125 BTC per block and in 2028 it’ll be 1.5625 per block.
Block rewards are also independent of the transaction fees (as high as 300 BTC per day).
Mining can be very lucrative.
Anyways to stop this energy monster from melting greenland?
Yep, but that’s another article, I talk about it here
Glossary and Other Stuff
Hashes may seem completely random but if you run the SHA 256 alogrithm with the same text it gives the same result.
Million Trillion isn’t a made up number, but it’s not the right term either. The correct term would be 87.2 Exahashes.
A Node/Miner: is a computer running the network protocol.
Disclaimer: The photos used do not belong to me I got them from www.istockphoto.com
You can also play around with the SHA-256 algorithm here