If you’ve ever seen a news piece covering Bitcoin what you’re most likely seeing is just one side of it, and that’s bitcoin mining. You’re usually shown pictures of massive warehouses showing hundreds of pieces of equipment that is very loud and has cables running everywhere, or a very simplistic picture of a pickaxe hitting a rock and bitcoins flying out of it. This article covers what those pieces of machinery are actually doing, how mining works, and where it fits in the Bitcoin protocol.
What Is Bitcoin Mining
Bitcoin mining is the act of using computer processing power to execute a special formula that calculates a number. The special formula is called a hashing function. The hashing function that Bitcoin uses is called SHA256, and it takes any kind of text as input and results in a hash. Here’s an example:
SHA256("This is a test") = c7be1ed902fb8dd4d48997c6452f5d7e509fbcdbe2808b16bcf4edce4c07d14e
Miners use their computers to first collect the transactions they’re going to include in the block, the header from the previous block (to preserve the connection of the blocks in the blockchain), and a random piece of text called a “nonce” all in a text value. They then hash the entire thing in SHA256 and look at the result value. If the value falls within the network’s limits of being a valid block, the miner submits that found block to the network for bitcoin nodes to validate as true. The special value the network is looking for is any text value that has minimum number of zeros in the hashed value. Here’s an example of that number from block id 540677:
0000000000000000000b2be42297654db25290e69d1499c8250938c0eb813223
Currently, a successful hash must start with approximately 17 zeros, so only one out of 1.4×1020 hashes will be successful. In other words, finding a successful hash is harder than finding a particular grain of sand out of all the grains of sand on Earth.
http://www.righto.com/2014/09/mining-bitcoin-with-pencil-and-paper.html
These zeros in front help ensure blocks are found about every ten minutes, and the network increases or decreases the number of zeros needed depending on how many people are trying to mine for bitcoin. When a miner finds a block they are awarded new bitcoins and any fees that were present on the transactions that they decided to include in the block. As such, miners will include transactions in the order of highest fee to lowest fee in order to gain the most money on their work put into finding a block. Since blocks are limited to 1mb in size, this means if you as a bitcoin user want to transfer value from one address to another address you must include a higher than normal fee in order to make sure it gets into the very next block.
How It Fits In The Bitcoin Protocol
Once the miner finds a valid block it needs to be proven that it’s valid by the network. The miner does not award itself with bitcoins and fees from the block, the bitcoin node does. The miner will submit the block to a node along with the hash found and the bitcoin address where the awarded coins should be going to.
The node takes the data submitted and confirms that the data contains a reference to the previous block in order to preserve the blockchain’s chain, and that the resulting SHA256 on all of the block’s data matches the hash submitted by the bitcoin miner. If all of these are true, then the miner is awarded the block reward (currently at 12.5 bitcoins) and any fees associated with all the transactions in the block. As a last step, the node then broadcasts this new block to every node (and resulting miners) which will then begin work on the next block referencing the new block’s header, and continues on.
Bitcoin Mining Cheaters
It might follow then that to get free coins one just needs to take a valid hash already used and submit it to the network to trick it into giving you free coins. This will not work for a number of reasons, but the two primary ones are the nature of hashing functions along with how the hash is calculated.
Hashing is a one-way function. You can not take a hash and somehow calculate what the text was that was used to create the hash on complex values. People have created rainbow tables which are large databases of text values containing the unhashed text value and the hashed value, however these are used to crack passwords so the complexity of the data in a bitcoin hash makes this an impossibility.
Finally the bitcoin nodes themselves that are validating these blocks will compute the hash themselves, so any submitted data along with the hash is checked for validity before including them in the blockchain and awarding the miner with coins. In a nutshell that’s how bitcoin mining works, from a top-level non-math intense view.