Most public cryptocurrency blockchains today are open and free networks. This means that users are free to send funds whenever they want to and are free to include or not include transaction fees with the funds. Miners are free to create blocks of the blockchains or not create them. On every step, there are incentives that function based on the principles of free markets. For example, while users do not have to include a transaction fee with the funds they are sending, miners are free not to include transactions into the blocks of the blockchain that they are mining. If there are a lot of transactions, then miners because of the obvious self-interest would be including transactions with fees first because miners get to keep ether the entire fee (on the Bitcoin network) or a large percentage of the fee. Miners are free to leave a network and stop mining, but then they would stop getting rewards from the network for creating blocks of the blockchain.
Because most blockchains aim to be creating blocks at certain intervals of time, they needed to solve the issue of having too many or too few miners. Obviously, if a network such as Bitcoin wants to be creating blocks every 10 minutes and it has tons of transactions and not enough miners, it would be taking it longer to create the blocks. On the other hand, if there are a lot of miners, then it would be creating the blocks faster and would still miss its goal.
The networks solve this issue by using the parameter of difficulty. The Bitcoin network adjusts this parameter every 2016 blocks. It aims to create a block in 10 minutes, which means 6 blocks in a hour and 144 blocks in a day. 2016 blocks divided by 144 blocks a day is 14 days, which means that the Bitcoin network adjusts the parameter of difficulty every two weeks. If in the preceding two weeks the miners created 2016 blocks faster than in 10 minutes per block on average, it means that there are more miners than the network needs and the difficulty goes up as a disincentive to miners. If it took miners over two weeks to create 2016 blocks on the Bitcoin network, it means that the difficulty is too high and there are not enough miners and the difficulty goes down. The math and the logic work in the same way on other blockchains, too. The hard numbers are different because different networks have different block creation speeds, but the approach is identical.
Practically speaking, difficulty means that a hash for a block needs to correspond to certain conditions. On the Bitcoin network, this condition is the number of zeros in the beginning of a hash. This is why if you pay attention to hashes of individual Bitcoin blockchain blocks on the official Bitcoin blockchain explorer, you will notice that they start with a lot of zeros. For example, this is the hash for block #525125 of the Bitcoin blockchain (note all the zeros in the beginning): 0000000000000000001b4e6bf72320488135f1012eb8f49ca0f6a885121a4e05. Take another block and you will see a similar picture. Here is the hash for the block #432222: 000000000000000002551fb6b50854925284133b88fe5e9d25a6da42d9cbb5cb.
How can miners create different hashes using the same cryptography algorithm if they use the same data about the same transactions and a set of data can only have one hash in one cryptography algorithm? They do so by adding a random number to these sets of data. This random number is called nonce, which is short for number used once. Nonce is also what makes blockchain network immutable: it is easy to create a hash for a set of data. It is not easy to come up with a nonce that generates a hash that matches difficulty of the network. Therefore, what miners are doing on blockchain networks can be summarized in the following steps:
- Compile transactions into a set
- Generate a random number (nonce)
- Add the number from step #2 to the data set from step #1 and create a new set of data.
- Generate the hash for the resulting set of data in step #3.
- If the hash from step #4 matches the conditions, the miner gets to create a block of the blockchain and gets a reward. If the hash doesn’t match the conditions, the miner repeats the process starting from step #2.
Once a miner generates a winning hash, both the hash and the nonce (the number that the miner added to the data about transactions to generate the winning hash) become a part of the block, the next block and the blockchain. For example, block #432222 of the Bitcoin blockchain contains not just its own hash, but also 0000000000000000018d3101a85a08de0f85ae4370d7addb68be06d42b8ca7df, which is the hash for the previous block. You can see this information on the official Bitcoin blockchain explorer here: https://blockchain.info/block/000000000000000002551fb6b50854925284133b88fe5e9d25a6da42d9cbb5cb