Block Chain Mining — Proof Work & Stake

Venkatesh Narayanan
8 min readFeb 21, 2018

--

What does it takes to add a new transaction / record / block to the blockchain ?

When you read any article on Bitcoin or Blockchain, you will come across this word called “Mining”. Lets demystify that in this article and go deep to understand better the need for mining.

When a new node is added to the blockchain, it needs to be validated for its correctness. Mining is the process that takes place to validate a block of transaction and add it to the blockchain. As part of the mining process a set of computers utilize their resources to validate the transaction and add it to the blockchain.

To participate in this process you need computing resources. Anyone in the network can declare themselves as miner and can create the blocks in the blockchain. Every miner would have to solve a mathematical proof for submitting to the blockchain. The proof acts as a guarantee as to whether the block is valid or not. Any miner participating in this process has to be faster than the other miners and by this the miner who solves the mathematical proof correctly first would be rewarded. This is the incentive built in the system to reward the miner and also to ensure that the blockchain is having the right transactions.

Some of the mathematical approach used in validating the transaction are “Proof Of Work” and “Proof of Stake”.

Proof Of Work

The Proof Of work is an approach to prove that you have done some significant computation and so ready to validate the transaction. You could think of the Proof of Work as some kind of puzzle that you have to solve. The puzzle is difficult to solve and requires significant computation power. On the other hand it would be easy to verify them. Hence producing the Proof of work will be a random trial and error process and requires significant computation before a valid proof of work is generated.

Bitcoin uses a Proof Of work algorithm called Hashcash. Hashcash was an algorithm invented for email spam detection, requiring a proof of work on the email contents on every mail. As part of the Proof Of Work, the miners would have to generate a hash that has to satisfy a certain conditions. Say a hash with certain number of digits and a certain number of zeros in the hash.

The mining owners have to figure out a hash such that the hash of the whole block satisfies the specified condition. In case of bitcoin the Sha-256 hash of the block would have a certain number of leading zeros. Since the hashes are one way functions there is no easy way to generate the right nonce without trying out randomly. The miners would have to try with different nonces to generate this hash that satisfies the specified condition.

In the above diagram, what you have is a challenge C which is the input block with its transactions. It also includes the hash of its previous block. Now as part of the Proof Of Work, you need to come up with a Proof Response R which is some random number (called nonce), so that the hash of (challenge + proof response) would generate a hash with certain number of zeros and also less than a target value.

As a miner you have a list of valid transactions and you need to calculate the cryptographic hash that satisfies the requirement for adding to the blockchain. But then the hash that was computed based on the block transactions does not meet the required criteria. So the only way to get a different hash is to use a slightly different data to compute the hash. But then we cannot mess up the transaction data. So the only way to generate a different hash is to change the nonce which is a random number that can be changed any number of times.

So as a miner you are generating millions of hashes by changing the nonce to something else. After changing the nonce, the hash of the whole block is computed again. The same process is repeated again and again until we can generate a hash of the (block transactions and the nonce) together which satisfy the given criteria. One of the pitfalls of the Proof of Work is that it requires tons of computing power. Without huge computing power it won’t be possible to solve the Proof Of Work puzzle.

In summary, the blockchain miners are trying to find a random nonce (random data) that goes into a block and makes the block hash (SHA256) starts with certain number of 0’s. More zeros meant that there is more challenge in generating the hash. Since the hash outcome is not predictable, you would have to try a lot of times to find a good nonce that satisfy the given criteria. The amount of zeroes are based on how difficult it is supposed to be to find a block. In Bitcoin it is adjusted to add a new block every 10 minutes (on average, given the rate at which previous blocks are found).

Let us take an example and see how it works. This is an example from bitcoin stackexchange:

  1. Hash of the latest mined block shortened to 30 characters — 00000000000001adf44c7d69767585
  2. Assume there are two valid transactions to be included in the new block. The hash of the two transactions are — 5572eca4dd4 and db7d0c0b845
  3. Since we are mining and if successful we need include a transaction to reward ourself 25 BTC. Let the hash for this transaction be — 916d849af76
  4. As we build the new block with the above set of transactions(hash of the previous block and hash of 3 transactions), it would be something like — 00000000000001adf44c7d69767585–5572eca4dd4-db7d0c0b845–916d849af76 —
  5. Now to mine the above block, we would need to generate a random nonce such that hash of the new block starts with 13 zeros. (Remember the number of zeros depends on the previous block difficulty level).
  6. Once we have the previous block hash and the transactions for the new block, let us get into the mining process. As part of mining process we would use the (previous block hash + new transactions hash) + Random Nonce. Remember we cannot change the hash of the transactions or the previous block hash. Only thing we can change is the random nonce.

Try with nonce = 1 and compute the hash of the block.

echo “00000000000001adf44c7d69767585–5572eca4dd4-db7d0c0b845–916d849af76–1” | md5sum 8b9b994dcf57f8f90194d82e234b72ac

As you see there is no zero in the hash that was computed.

As you try with different nonces, the first two zeros appears when the nonce = 208

echo “00000000000001adf44c7d69767585–5572eca4dd4-db7d0c0b845–916d849af76–208” | md5sum 0055e55df5758517c9bed0981b52ce4a

In the above process as we continue with different nonces once we get a hash with 13 leading zeros then we are the winner. Other miners will build upon your block and we get 25 BTC. As part of the mining process, we need to be fast in generating the Proof Of Work. If not in the mean time someone else with faster hardware would be able to mine before us and append to the blockchain. If that happens then we would have to start again from the beginning with the new block’s hash from that of the winner.

Again this whole explanation is very simplified example for understanding. What happens under the hoods is more complicated, but you can assume the process remains the same, where by you need to compute hashes fast enough that satisfies the given condition. Things like the block chain height and forks are not elaborate here for simplicity.

Here is a sample block with transactions that you can view in the Blockchain.info.

The mining process using Proof Of Work is an operation of inverse hashing where by we determine the nonce (a number) so that the cryptographic hash algorithm of block data results in less than a given threshold. The threshold is determined by difficulty and is what makes the mining competitive.

Proof Of Stake

Proof of Stake is another approach for validating transactions in the block chain. Unlike in the Proof of Work where the miners are rewarded for solving the mathematical proof, in this approach the creator of a new block is chosen by a deterministic approach based on the wealth, also called as the stake. As part of the Proof of Stake, any miner would have to possess a certain number cryptographic units.

The creator of a new block is chosen in a random way based on the user wealth (”stake”). In the Proof of Stake the blocks are forged or minted and not mined. The users who create new blocks in the block chain are called forgers. The forgers would have to create or own cryptographic units.

In order to validate transactions, the forgers have to put their own coins at stake. You can think of that the forgers are using their own cryptographic units as a stake. If they validate a fraudulent transaction, then they lose their holdings (stake) and their rights to participate in future. Since the forger has put their own money or cryptographic units in the mining process and they have staked their own money, they are now incentivized to validate only the right transactions.

It is important that in a Proof of Stake system, there has to be a fair way of selecting the user who would forge the next block in the blockchain. It is not possible to select the forger only by the size of the account balance since it would be favoring the rich forgers with more cryptographic units. Some approach for solving this problem is to use : Random Block selection and Coin Age based selection method. In the Random block selection the next forger is selected based on the combination of the lower hash value and size of their stake. In Coin Age based selection method, the forger is selected based on the size of their stake and coin age (number of coins staked X number of days the cryptographic coins held as stake). Users who have staked older and larger set of coins have greater chance of being assigned to forge a new block. Once they forge a block, their age would be reset to zero and they have to wait for sometime before being assigned a new block.

Proof of Stake approach rewards the forgers with a transaction fee for verifying transactions and creating new blocks.

Pictorial representation of Proof Of Work vs Proof Of Stake from blockgeeks

Proof of Stake systems are more efficient as the electricity and hardware costs are much lower than cost associated with mining in Proof of Work system. The Proof of Stake enables more people to participate since it is more affordable compared to Proof of Work.

--

--