Bitcoin mining is the mechanism for creating new bitcoins. But it is much more than that. It is the invention that makes Bitcoin special and the decentralized security mechanism that is the basis for peer-to-peer money.
New bitcoins are created each time a new block is added to the blockchain. Each block, generated approximately every 10 minutes, contains new bitcoins, created from scratch, but which obey immutable mathematical rules and defined since the creation of the network. Every 210,000 blocks, approximately 4 years, the level of currency creation is reduced by 50%. During the first 4 years, 50 new bitcoins were created in each block. The currency creation rate has already been reduced twice and will be reduced again in May 2020. After this halving, known as halving, 6.25 bitcoins will be "minted" for each block. In total, there will be 32 halvings, and after the last one, no more bitcoins will be created.
The last halving will take place in the year 2140. Thereafter, the only reward that miners will receive will be transaction fees. Satoshi designed the system to have greater rewards in the first blocks to make this activity attractive right from the start. As miners are attracted to support the network, it will make it stronger. As the network strengthens and there are more users, your currencies will appreciate and from there you can reduce the reward by keeping it attractive. This process is designed to be positively reinforced by the network effect: with an increase in network users, demand increases and the currency appreciates, attracting a new wave of users and so on.
Mining is seen as the reward given to miners for the work they do to keep the network online and synchronized. In fact, this is the incentive for miners to make their resources available to the network. In return, they receive their precious bitcoins. This scheme serves to align the interests of miners with the security of the network, while simultaneously providing the minting of currency. In addition, mining achieves something much more important. It allows consensus to emerge over the network without a central authority.
Satoshi's most important invention was the decentralized mechanism for emerging consensus.
Emerging, because consensus is not reached at a specific time or in an election. The network consensus emerges from the asynchronous interaction of thousands of nodes, all following simple rules. All Bitcoin properties - currency, transactions, payments and security model - are derived from this invention.
How does mining work?
Miners validate new transactions and record them in the global ledger. Every 10 minutes a new block is "mined" where transactions that took place during that time are included.
Until it is published, validated and added to the oldest Bitcoin chain it is called a candidate block. To join it to the blockchain, the miner consults his own copy of the blockchain, sees the number of the last block and assigns him the next number. It remains to be seen that the block has a valid Proof-of-Work. The block only becomes valid after a miner confirms this key step.
Transactions added to a block that is on the blockchain are considered “confirmed”, and from here the new owners of the bitcoins sent can spend them in whatever way they want.
The miner who succeeds in adding a block to the chain is rewarded in two ways: new currencies and transaction fees.
The first transaction in each block is a transaction different from all others. This transaction is built by the node that creates the block and contains its reward for the work spent. It is known as the coinbase transaction. It will include the bitcoins created again by the miner and the transaction fees related to the transactions included in that block. This transaction is special because its input is not an unspent output from a previous transaction, but new currencies created based on the rules defined by the system. It is also different because there is only one output to be paid to the miner's own address.
The coinbase transaction will be added to the Merkle tree. The miner will build the header for that block, and include six fields in it: the version of the Bitcoin client that you use, the reference to the previous block, the Merkle root where all transactions are included, the stamp with the block's creation date, the target used by the Proof-of-Work algorithm and the nonce - the variable - used to find the solution for the algorithm.
The solution to the algorithm must be found by the miner in competition with all the other miners in the network who try to solve a cryptographic mathematical problem, the solution of which is known as Proof-of-Work. This Proof-of-Work is included in the block and acts as proof that the miner has spent significant computing resources, and therefore deserves the reward.
After having the block built and validated by the network, the miner gains the right to add the block to the blockchain and receive the reward that he included in his block. As the winner of the competition, it is your block that is added to the chain making the transaction where you address the reward to yourself be accepted and validated by the network. Thus making him the rightful owner of those bitcoins. A dishonest node that tries to increase its reward will see that transaction invalidated by the other nodes and with that the block itself will be invalid. You will then have wasted your computing resources in vain. That's why this competition is the foundation of Bitcoin's security model.
Each node that enters this competition increases the security and the value of the network. The blockchain is not created by a central authority but by each node, independently. The brilliance of this network allows each node, working alone and based on information transmitted over insecure connections, to reach the same conclusions and create an identical copy of the blockchain.