DEV Community

Hazem Allbabidi
Hazem Allbabidi

Posted on • Originally published at hazemhadi.com on

Proof-of-Work and Mining

Proof-of-Work and Mining

A blockchain, and I am mainly talking about Bitcoin here, seeks to benefit all the people using it, these include the people who make payments, the people who receive payments, and also the people who approve these transactions. But who does the approving and the actual moving of the money, or Bitcoins?

Proof-of-Work is the method used to ensure that transactions are not being altered or changed to cause double-payment, or reusing of the same money, and make sure everything runs smoothly. Proof-of-Work is a technical term that basically translates to a method of finding consensus using complex computations. The actual usage of it, especially in the blockchain, can be complicated, but I will try my best to explain it properly.

In order to understand Proof-of-Work, we need to understand hashes. A hash is a computational algorithm of a value that outputs the exact same result everytime we use the exact same input, it is used to ensure that two outputs are the same without necessarily showing the full input. For example, we could hash the value “hello world”, and we would get the output “b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9”. No matter how many times hash the “hello world” that same output will be produced, but if we change it up just a tiny bit, e.g. “helloo world”, the output would be completely different, like in here: “671f53df225ff0c84208b7059e465a21f704d1404a37a632715b4e2bfa113fa4”. The output was completely different even when the change was very tiny and even barely noticable for some people, but this is the power and benefit of a hash. Since it is an algorithm, it can have different forms or functions, the main method used by Bitcoin is called SHA256, which is a function that outputs 256 Bits as a result, no matter how big or small the input was. There are many other hashing functions as well, but SHA256 is the one used by Bitcoin. The benefit of a hash is that is not reversable, meaning if you only have the output, or the hash, it is impossible to reverse it and find out what the input was.

Now that we understand what a hash is, we can move on to understanding blocks and what they are. In the blockchain, we have what we refer to as Blocks. These blocks include all the transactions of the certain period, the previous block hash, a nonce, and the timestamp. Now who do all of these mean?

In Bitcoin, a new block is formed every 10 Minutes. That means that all the previous transactions performed in the past 10 Minutes will be saved in the new block. We also give that block a timestamp, which is the time it has been created. Then we need to give it the previous block hash. This is the method that we use in order to connect the blocks together. Let us say we have 3 Blocks, and we want to connect them to each other, the way we do it on the blockchain, is to take the hash of the previous block, and adding it to the data of the new block that comes right after that one. This produces a list of items linked to each other, making it identifiable which block came after a specific block. The way we derive a hash is by taking all of that data and adding it as input to a hashing function, such as the SHA256. But there is one more thing we need to understand in order to fully know how Proof-of-Work in Bitcoin works.

The last part of a block is what we call a “nonce”, and the use of it is a little bit more complicated. In order for the blockchain to ensure that a block was properly hashed, it ensures that the output fits a certain criteria. The main condition is for the output hash to have a set number of zeros at the beginning of the hash. For example, the criteria right now can be that we need four zeros at the beginning of the hash, for example, if the data we have in the block is just “hello world”, then we would keep hashing the value, until we have four zeros at the beginning. But if you recall, we previously mentioned that a hash always outputs the same result, given the same input, so how do we get new hashes everytime? That is where the “nonce” comes in. We perform a loop that hashes the data (i.e. “hello world”) along with the nonce, which is a number that starts as 1, and keeps going up until we find the hash that starts with four zeros. An example might help better undertstand. Assuming we have the data “hello world” and the nonce which starts at 1. We hash those two values: hash(“hello world” and 1) The output of this would be 385b541ea93b0b52527bd6ec106ea2897e9cfaefde8b503a3ead7eef962be72e, which does not start with four zeros. We increase the nonce from 1 to 2 and retry: hash(“hello world” and 2) The output of this would be f0baab7090353028efe61f2659b8c8fba366d02ef358be5edc2f667ba5b2612f. This does not start with four zeros either, so we keep increasing the nonce and running the hashing on the data until we get an output that fits the criteria. To get the correct result of this, we would have the data “hello world” and the nonce of 18014, which outputs the hash that fits the criteria: 0000d3d3702b7dce97cfd9f35705650ff9cfbc81025943525680241678594033. This is how the nonce works.

To briefly summarize the above. We take the money-transfer transactions, the previous block hash, and the nonce, and run them through a hash function to get the output we need with the specific requirements set by the blockchain. Once that block is fully completed, along with it’s hash being generated, we get to work on the new block.

Now the above explains the Proof-of-Work, which is the method used to mine cryptocurrency. Mining of Bitcoin is similar to the Mining of Gold. When you want to find new gold, you typically mine in a cave, right? Well, in Bitcoin, you mine Bitcoin by using hardware, such as a GPU or CPU, to run the Proof-of-Work to “find” new Bitcoins and also approve the transactions. Mining uses hardware to do the computations required by the Proof-of-Work to approve transactions and ensure that the blockchain is stable and cannot be attacked or hacked. The miner uses his PC or a device known as ASIC to mine new Bitcoins and acquire it for themselves because they are the ones that did the effort of mining them. Before we could buy Bitcoin from exchanges or from other people, users had to use their PCs in order to mine and find new Bitcoins.

Hopefully this article helped you understand what is Proof-of-Work and what is mining. For an amazing and simple explanation in the form of a video, check out the link below: https://www.youtube.com/watch?v=_160oMzblY8&ab_channel=AndersBrownworth

If you are interested in this topic, I recommend reading these two introductory books: This first one is a Chinese book that was translated to English. Some reviews on GoodReads wrote that they had difficulty understanding some of the English in it, but I personally had no problems. It is very simple and is filled with pictures, making it a fun and easy read: https://amzn.to/3HBCOSs

The second book explains lots of aspects in the Bitcoin and Blockchain technology, as well as many other technologies that use blockchain: https://amzn.to/3LgzGgW

Originally published at https://hazemhadi.com.

Top comments (0)