What is mining?
Bitcoin mining is the procedure where miners verify the transactions and add them to a new candidate block. When a nonce which makes the blockhash matching the required pattern is found, the block is validated and propagated across the whole network and added to the chain. As collateral effect new bitcoin are issued by the coinbase transaction in favor of the miner discovering the right nonce creating an economic incentive for mining.
As today, bitcoin mining is performed using bitcoin mining pools. These pools are coordinating a high amount of computing units on the network in order to find the solution for the math problem and thus provide the proof of work, becoming so eligible to add the new block to the chain.
Finding a valid nonce
The mining process consists in finding a valid nonce for the block, therefore testing billions and billions of possibilities per second. The nonce is simply a very large random number, it is not secret, it’s calculated by all the miners (every one is calculating its nonces independently).
The space for the nonce into the block is 32bits (which means 4 billions possible combinations). There is also an additional space into the block (the coinbase script) which is called “extra nonce“. This allows to expand the nonce space to much more than 32bits, allowing a lot of combinations. So in coinbase transaction since there is no unlocking script, this field is replaced by coinbase data which is between 2 and 100 bytes. Except for the beginning gew bytes the other bytes of coinbase data can be used by miners for any purpose: it’s free data space. Infact normally miners include extra nonce values.
The purpose of trying many times the nonce is to add it to the block header into the specific field and then calculate the block header’s hash.
The nonce must be in such a way that the sha256 hash of the header (calculated by the miner) is lower than the current target for the block, in order to be accepted by the network. The lower is the target and the more difficult is to find the suitable nonce. The difficulty is automatically adjusted by the network based on the available total hashrate.
The block header hash
When you put a nonce into the header and then hash the header itself, you get a 256bits hex number (as a result of sha256 hash). The resulting number should start with a certain number of zeros (depending of the difficulty). If the resulting hash does not match this requirement, therefore the nonce is changed and the hash is recalculated again and again until a correct hash is resulting.
The only part of the block header miner is going to change, infact, is the nonce.
Building the block
So the miner builds the candidate block, puts the transactions from the mempool inside the block, adds additional informations (timestamp, etc). Then it adds a nonce and calculates the header hash and checks if the desired pattern is matching (the target). The chances that the resulting hash is starting with zeros is decreasing the more zeroes must be present at the beginning of the number; this is due to the difficulty.
if the number must start with 1 zero, the chance is 1/2; if the number must start with 2 zeros, the chance is 1/4; with 3 zeros, the chance is 1/8; with 4 zeros, 1/16 and so on.
So the difficulty increases and you have to try many many possibilities for the nonce to match the required pattern by difficulty target. One of the miners will succeed in finding the right nonce and therefore constructing the correct header.
When the solution is found, the miner is then propagating the block to the network. As soon as this block is checked valid and accepted by the nodes in the network, it is added to the chain and therefore the coinbase transaction will become totally valid issuing new bitcoin and assigning them to the address of the miner (the pool in this case).
The funds inside the coinbase transaction can be spent only after 100 blocks, and this is to avoid the miner to try to make re-organizations on the chain.