Introduction
When you first heard about Bitcoin mining, you might've had an image like this in your head. However, reality is far different. Put your hard hat on as we dig around and find out what Bitcoin mining actually is, and how it's key to Bitcoin's function and security.
How does Bitcoin work?
First, a quick recap on how Bitcoin works and where mining comes into the picture. Bitcoin is a cryptocurrency that operates on a decentralized blockchain powered by globally distributed nodes. Whenever a transaction is made, specialized nodes called 'miners' are responsible for validating and grouping multiple transactions together into a 'block', which is then cryptographically linked to the block before it - hence the term blockchain. The miner which successfully adds the block is then rewarded some bitcoin for their work. Therefore, 'mining' is the process by which new blocks are added onto the blockchain, and is also how new bitcoin enter into circulation via the block reward awarded to miners.
How does mining Bitcoin actually work?
But how does mining actually work? You may have heard that mining is about solving complex mathematical puzzles. But it's more accurate to call it a lottery, as miners are actually guessing the right number to win the block reward. To understand this analogy, here's the mining process in more detail.
Each block contains a block header, which contains data such as a timestamp, a reference to the previous block, and a number called a nonce. Miners would guess a nonce, which could be any number really, then put that along with all the data in the block header through a cryptographic hash function to create a hash. In cryptography, a hash is simply a string of characters of a fixed length created by putting data through a mathematical function, and is easy for a computer to do. The real work in mining thus comes from the sheer number of guessing and hashing needed to reach a valid output. For reference, the current Bitcoin network hashrate is over 500EH/s, which is an astronomical figure.
Why mining is so difficult?
So why is it so difficult to guess the correct nonce? Well, the thing is that roughly every 2 weeks, the bitcoin network sets a "target hash", and will only accept output hashes that are equal or smaller in value than the target hash. That may sound counterintuitive, but imagine having to roll a 1000-sided dice; it would be more difficult to have to roll a number smaller than 10 than, say, 250. The target hash thus determines the Bitcoin network's "difficulty"; the higher the difficulty, the more guesses needed on average for a miner to find the correct nonce. The first miner to output a valid hash successfully 'mines' the block and wins the reward, and the guessing game starts all over again.
This difficulty imposed on miners would also adjust up or down automatically based on factors such as total network hashrate and miner participation. For example, if more miners join and network hashrate increases, the average time needed to find the valid hash would reduce, as there would be more computational power put into the guessing game. The difficulty would then be adjusted upwards. The reason the Bitcoin network does this is to ensure that new blocks and new bitcoin are created roughly every 10 minutes on average, in accordance with its programmed issuance schedule.
Proof-of-work in mining
So, mining is essentially a guessing game, but why this seemingly random task? This mechanism, also known as 'proof-of-work', ensures that miners have verifiably expended some effort and cost in order to mine a block. In this case, computational power and electricity. Therefore, aside from adding blocks and issuing new bitcoin, proof-of-work helps ensure the security of the network. As more participants join and the value of the Bitcoin network increases, it becomes increasingly more difficult and expensive to attempt fraudulent transactions or tamper with the blockchain.
Outro
In case you're wondering if all this energy consumption is bad for the environment, check out this video!
