You will need to read our article about hash functions in order for this article to make sense. It will help a lot with understanding all this. As you might have picked up in our article on Consensus Mechanisms, not every blockchain has miners. Miners are only in Proof-of-Work Blockchains. They work to secure the blockchain against attacks and protect the history recorded up to that point against changes. Let’s get back to how protection is provided in a minute. You have probably heard about the miners trying to solve a “puzzle” or doing a “computationally expensive task”. In this article, we want to finally tell you what this task actually is.

What Does the Miner Do?

The main job of a miner is to collect all transactions broadcast to the network in the order they received them in. The miner verifies if the transactions are valid according to the protocol and then places them in a data container (the block) that we have talked about when describing the blockchain as a data structure. The first transaction in a block is special, the coinbase transaction. In the coinbase transaction, the miner rewards his own address with the block reward, 12.5 BTC or 12.5 ZEN that did not exist before (actually 7.5 ZEN, as 10% of block rewards go to Secure Node and Super Node operators respectively, 20% to the Horizen Treasury). Usually, one of the criteria that is used to check the validity of a transaction is if the sender has sufficient funds in the address he is sending the transaction from. The coinbase transaction rules are slightly different. The protocol allows this first transaction to have no input, which means having no sender in this context. This is how the miners get to reward themselves for their work and how new coins are created.

After the miner includes the coinbase transaction he then collects the transactions that he received from other nodes and places them in the block he is working on. Usually, the miner places the transactions in the order they arrived in. In times of high network activity and full blocks (a block has a maximum data size, the block limit) the miner might choose to include only the transactions with the highest transaction fees attached. That’s why transaction fees skyrocketed in December 2017. People were trying to outbid each other to get their transactions included in the next block first. It’s also the reason why you will sometimes see the transaction fee denominated in ZEN per kb. It is the metric the miners use to calculate which transactions will give the most bang for the buck.

Many miners are working on the next block simultaneously. Each miner might have a slightly different block, either because he received the transactions in a different order than his competitors, or because he chose to include a different set of transactions based on the attached transaction fees. The miner that solves the puzzle first, gets to extend the blockchain with his block, including the coinbase transaction that rewards him the newly created coins.

Miner Miner

Difficulty

So what is the puzzle that miners are trying to solve? As we said in the introduction, reading up on hash functions if you are not already familiar with them will help you understand what comes next.

One of the important properties of cryptographic hash functions is being pseudorandom. You must not be able to predict a change in the output that results from a change of the input. As an example: If you are hashing the input “1” and get the output “00002” and hash the input “2” afterward, the second output better not be “00004” but something like “73968”, a seemingly random (pseudorandom) number.

The mining protocol requires every block to hit a certain target. This target is a measure of the difficulty to solve a block. This means if you hash the entire block with all the data contained within it the output has to start with a given amount of zeros. The difficulty describes the number of leading zeros required.

Imagine you were to calculate a hash for a random input that needs to start with one leading zero. If there were only digits in the output the chance of getting it right the first time would be 1 in 10. If the difficulty required the output to start with two leading zeros, your chance of getting it right the first time would be 1 in 100. And if the target was a difficulty of 6, chances would be 1 in 1,000,000. With increasing difficulty, solving this challenge evolves into real work, especially as there is no shortcut to trying out a bunch of different inputs. If you would like to play around with a hash function you can do so here.

There is a special data field in each block header, the nonce, that doesn’t carry any important information. Its sole purpose is to be a variable (a number used once), that the miner can put different values in, to change the output of the hash function. What follows is a trial and error approach of getting the block hash right. When a miner first attempts to solve the block, which means calculating a valid block hash (with the required amount of leading zeros), they will put a random value in the nonce data field.

For the sake of simplicity, let’s say the nonce value the miner starts with is “0” and the difficulty is 1, meaning one leading zero is required for the block hash to be valid. Let’s also assume the output consists of numerical characters only.

  • First, the miner will use the hash function to calculate the hash value of the block. In the example below the output doesn’t satisfy the condition in a first attempt.
  • The miner now increments the nonce by one and tries again. The resulting hash still doesn’t satisfy the difficulty condition so he keeps going.
  • Finally, when the miner uses the value 8 as a nonce they get the desired result: the block hash starts with a leading zero and the miner solved the block.

Mining nonce Mining nonce

In reality, the difficulty is much higher and it takes an incredible amount of computations to calculate a valid block hash. At the time of writing the hash rate of the bitcoin network is roughly 41 million TH/s (tera hashes per second). This means that all miners combined try 41,000,000,000,000,000,000 different values for the nonce every second. You can check the current difficulty for the Horizen blockchain here. The hash rate on the Horizen network is 208.68 Mh/s at the time of writing and you can check the current value here.

All this works because of the properties of cryptographic hash functions. Remember this graphic from our Hash Function article?

Hash function Hash function

There is no way to calculate a valid nonce from the desired output because the hash function is a one-way function. It’s also infeasible to approximate a valid nonce from prior outputs because the hash function is pseudo-random and you can’t predict changes in the output. Every participant on the network can verify if the solution, once found, is valid because the hash function is deterministic and will produce the same result for every node that verifies the block.

Block Time

Every Proof-of-Work protocol defines a block time, an interval within which a new block should be created. With Horizen this is 2.5 minutes, with Bitcoin 10 minutes. When a miner solves a block he broadcasts it to the network immediately, because he wants to collect his block reward. This is why it doesn’t take exactly 10 minutes to create a new block. 10 minutes is the average time that it takes to find a valid nonce and in turn a valid block hash. Some blocks are solved quicker and some take a bit longer. If more miners join the network and the hash rate increases, they will solve the block faster on average. The protocol evaluates the average block time and adjusts the difficulty accordingly every 2016 blocks. If it takes less time on average to solve a block the difficulty will increase, if it takes longer the difficulty will decrease.

Hardware

When Bitcoin released in 2009, people were using their PC’s CPU, the central processing unit, to mine bitcoin. It is the most versatile processing unit in a computer but not the most efficient. After a while, people switched to using GPU’s (Graphical Processing Unit) for mining. GPU’s are more specialized and efficient for certain tasks, at the cost of being less versatile. Nowadays the mining industry is running on ASIC’s (Application Specific Integrated Circuits). It is the most efficient hardware to mine cryptocurrencies because it is designed with one goal in mind, calculating hash values with a specific hash function. In computing hardware, versatility and efficiency have their trade-offs. If you want something that can perform many tasks, it will not be very efficient. If you want something that is super efficient at one task, it will not be very versatile. You can find a great article on mining hardware here, written by David Vorick, lead developer of Sia.

How Does This Protect the Network?

By now you know that it requires a huge amount of work to solve a single block in a blockchain. You also know that each block references the preceding block by including its block hash, therefore chaining them together. You also know, that changing one little piece of information in a block, will alter its block hash.

If an attacker wanted to tamper with any record on the blockchain, they would have to find a valid nonce for the block they edited, as well as a valid nonce for all the following blocks. The attacker would have to do all this by themselves, and at a faster rate than the honest network is performing the mining process. The longest chain rule that we talked about in our last article on consensus mechanisms determines which branch of the blockchain is the valid one in case a fork occurs. An attacker would need to outperform all the other miners combined to make their edited chain the longer and therefore valid chain. This is called a 51% attack and we have dedicated the next article entirely to attack scenarios.

This attack would be very expensive, so expensive that even most nation-states don’t have the means to perform such an attack on the Bitcoin network. The likelihood of success is also rather low. This is why people trust the blockchain. Miners prove that they put in a lot of work to secure the network. This work consumes resources outside the system (electricity), which makes it unforgeable. It is fascinating how something out in the open and completely transparent can be that resilient to attacks.

A great tool to understand the tamper-proof character of the blockchain is this Blockchain Demo. We encourage you to play around with it. It will help you internalize the concepts explained throughout this chapter.

Summary

The miners of a Proof-of-Work blockchain secure the network with their computational power. Adding a block to the blockchain requires a large number of random guesses to find a nonce that will produce a valid block hash. The block hash must satisfy the current difficulty requirement on the network. This makes it extremely difficult to tamper with data on the blockchain. An attacker has to redo the work of finding a nonce that results in a valid block hash all by themselves. Not only does the attacker have to do this for the block they wish to manipulate, but also to all the subsequent blocks until the malicious chain is the longest chain and will be recognized as valid by the entire network. Because this type of attack is so incredibly difficult, blockchains are considered to be the most secure data structure that we have at our disposal today.

This was the last article of our chapter on how a blockchain works. Next, we will talk about Wallets, a tool that helps you manage your keys and create transactions. We hope you learned something (or possibly even a lot) in this chapter and are looking forward to hearing some feedback. Did we miss an important concept or should we clarify a section? Let us now at [email protected]!