For a long time, the concept of "bitcoin mining" was very mysterious to me. People would have their devices "compute" some things and receive some "Bitcoin" as a reward.
What is this mining? And why is it valuable? Is it magic?
Warning: I will simplify a lot. I will leave things out. I am not an expert. I just want to share my understanding with people that share the same questions.
Bitcoin is a "digital currency". Like money, but it only exists digitally. Bitcoin money is moved around through digital transactions. Much like the money of your bank account, which is also not physical. Bitcoin are, as Homer Simpson says, "exchanged for goods and services". You can also buy and sell bitcoin with and for "regular" money. That's why it has value.
Each user has a "wallet".
How much bitcoin each wallet contains is known by the system as a whole, through its "shared ledger" of transactions. A bank also has a ledger, but the bank can put in the ledger what it wants. In bitcoin, nobody can really tamper with the ledger.
In bitcoin, this ledger exists in the form of a "block chain". Simplified, this is a list of transactions. A transaction could look like this: "Alice transferred 1 bitcoin to Bob's account."
All bitcoin users have a copy of this list. They broadcast new transactions, and all other users add them to their copy of the list.
That's a bit too easy of course. There would be a problem if everyone could just broadcast random transactions everywhere.
Before people add transactions to their blockchain. They validate if the transactions make sense. For example, if Alice sends Bob 100 bitcoin even though she doesn't have that much in her wallet, the transaction will be ignored.
By looking through the entire chain, it's possible to reconstruct how much bitcoin is in which wallet.
After a user has agreed to add the transaction to the blockchain, it can send the validated transaction to other users.
Now a blockchain looks like this:
... Alice sends 1 bitcoin to Bob Bob sends 0.5 bitcoin to Bill Bob sends 0.5 bitcoin to Carla
Everyone has a copy of that same list, so everyone knows how much is in every existing bitcoin wallet.
This is how the system as a whole knows how much bitcoin everyone owns.
The longest chain
In bitcoin, the network agrees which list of transactions is the authentic one. It does this by convention: the longest known list of transactions is the legitimate one.
It wouldn't make much sense if everyone could generate super long alternative lists of transactions.
So bitcoin has a way to make it harder to generate a list of transactions. For this, some information is added to each transaction.
A single transaction looks more like this:
Alice sends 1 bitcoin to Bob Nonce: 2 Hash: 0f3b6cbb2b10a4812373599563943a223fd9238775b73cbee9941733b9984f66
The hash is a hash of the transaction itself and a nonce. The "nonce" is really just a number that's changed to influence the result of the hash.
In the example above "2". Before sending a transaction to other users, a user must prove they found a nonce that creates a hash with specific properties.
For example: "find a nonce so that the hash of the transaction is less than some target value".
A hash is a very big, seemingly random, number. Created based on some input. Given some input, it always generates the same result. A small change in the input (the nonce for example) generates a completely different hash, so it is unpredictable.
To generate a full blockchain, one has to construct each transaction and for each transaction find the appropriate "nonce" so that the hash matches.
Now, it's still easy to change a single transaction, find the hash and call it a day. So there is an additional condition.
This is more what an actual transaction looks like:
Alice sends 1 bitcoin to Bob Nonce: 7 Previous hash: 057803e3a42c2ac4e666e420beb06229bf73234e36a44d7839661612c5ae4fbb Hash: 088235bb31175d30c1a3fc0461cfa410b05c5d1f74437ecac131e98f9412956b
The transaction also contains the hash of the previous transaction in the list. This means that if you want to change a transaction is the block chain, you have to calculate the nonce for that transaction and every transaction that comes afterwards.
If you change the transaction before this one shown above, the "Previous hash" would have a different value. So our hash will also change, and a different nonce will create a hash lower than the target value. Then our transaction changes and the next one must also recalculate the hash, etc ...
So you cannot just change one transaction in an entire chain.
It proves also, that you have put in the work of calculating the nonces. And to the block chain that is worth something. And for that, you receive some bitcoin. And this is what mining is:
Set nonce Calculate hash of transaction Does hash match criteria needed? Yes: broadcast No: increase nonce by 1 and repeat all steps above
And that's where the longest chain comes in again. Suppose you are building an alternative chain. And you want to pass it to others as the longest. Everyone else is still busy finding nonces and appending transactions.
The chance that you ever generate the longest chain, that can overrule the one used by all other users, is very small. You really need more compute power than all the other bitcoin miners together. Since by the time your list is ready, bitcoin users have moved on and your list is no longer the longest.
That's why the blockhain is said to be immutable. Because nobody can come up with a longer chain fast enough to overrule the one everyone is using.
Why is it called a block chain and not a transaction chain, you may ask.
In real life, a bunch of transactions are grouped in "blocks" of a bigger size (for example 1 MB). And the hash calculations are done on those blocks and not individual transactions. But in principle it's the same.
The "target" value, that must be bigger than the calculated hash changes over time.
This is to ensure a new block is "mined" about every 10 minutes. Each comes with a timestamp. This way, the time between blocks can be calculated and the target will be adjusted if needed:
- If new blocks are generated more slowly, the target will be higher so that it is easier to find a hash
- If new blocks are generated too quickly, the target will lower.
Over time, the reward for mining a block decreases. The more bitcoin are created, the lower the reward is.
At first you received 50 bitcoin per block mined, currently this is 12.5. Every 210.000 blocks, the reward is halved.
There is a lot more to bitcoin than what I just discussed. For example, the concept of wallets and how you can prove you own that wallet.
But for me, at least, this demystifies "bitcoin mining".
By itself, the mining excercise is quite pointless and wasteful with energy. Calculate a hash, increment a counter, calculate a hash, increment a counter etc ...
Its value lies in the fact that it ensures that it's hard to broadcast a falsely constructed alternative block chain. The more people are mining, the more computing power is needed for a bad actor to fabricate (part of) the block chain.
And this is why bitcoin miners are rewarded.