Proof of Work (PoW) is one of blockchain's consensus mechanisms which secures the network. It uses the computational power to create new coins for that blockchain. Miners who are responsible for adding new block into the blockchain will compete to each other to solve mathematical problems. They will find the nonce value which creates a valid hash value. The process of validating transactions and adding a valid block into blockchain called mining. The process is described as followed:
The first thing that will happen when one transaction is submitted into a blockchain is that they will go through the transaction verification. All of computers which are called nodes will verify this transaction because each node in the network stores a copy of that blockchain.
Transaction verification includes two tasks: (i) it needs to check transaction authenticity to make sure you are the one who send the transaction, (ii) it has to make sure that your account balance is sufficient for that transaction.
(i) Transaction will signed with private key. From this private key we can know the sender's public key which is also the wallet address. If the public key which is derived from private key is the same with sender's wallet address, then the transaction authenticity is confirmed.
Figure 1: Proof of Work's transactional verification (i)
(ii) How nodes can verify whether you have sufficient amount of coin for that transaction? Does they access to your account and know your account balance? That is something happening in some centralized network, not in the decentralized one. In fact, because all transactions will store in the blockchain, nodes will check through all transactions to make sure all the input into your wallet match with all the output from your wallet. Then it will know your account balance. If you have sufficient amount, your transaction will move to memory pool. Otherwise, it will be discarded.
Figure 2: Proof of Work's transactional verification (ii)
Memory pool which is often called mempool contains many transactions. You can consider memory pool as a waiting room where all transactions are standing and waiting to be packed into a block by miners.
Block size has only a certain amount of storage to store transactions. For examples, in Bitcoin blockchain, each block can only store between 1500-3000 transactions. In the mempool, we have a lot of transactions waiting. How the miners can decide which transactions they will prioritize to pick? The answer is based on the transaction fee. Transactions with the higher fee can be picked first. However, transaction fee is optional and depends on the users. Low fee transactions will be picked also just with a little bit of longer time.
Figure 3: Packing Information
As we can see from Figure 3, transaction information along with time data and nonce number will be combined together as summary data.
This summary of data will come through hash function and become a unique fixed length string (see the Figure 4). There are many types of hash function. Two popular ones are MD5 and SHA256. SHA256 is better. Data after coming through hash function cannot be transferred back to the old data
Figure 4: Hash process
Miners will compete to each other to compute the hash value that matches with hash value of the transaction.
Hash has to be lower or equal than the target number and has to start with the 0s. The number of 0s in the beginning of the hash will define the level of difficulty of the mining. The more, the harder. Therefore, level of difficulty will increase overtime. Miners have to try so many times to get it right.
Figure 5: Information needed for the miner to find the matching hash with target number
From Figure 5 we can see that with Nonce 1, the first 8 characters of the hash is different than the target number. So miner has to increase the Nonce value until they can get the first 8 characters of hash with eight consecutive 0s in the beginning and the 9th number which has lower or equal than 5 (the target). The miner who obtains that Nonce value solved the problem, add that block to blockchain and will receive the rewards.
The process of Proof of Work algorithm will describe shortly in diagram below:
Figure 6: The whole process of Proof of Work consensus mechanism
You can try the blockchain demo here. It is the product of Anders Brownworth. You can experience the whole process of creating block, adding block to blockchain and even how to find the right nonce value.
We can stop the proof of work part 1 here. In the next part, I will continue to discuss about the importance of increasing difficulty level of mathematical problem, the longest chain rule and other aspects related to PoW mechanism. Stay tuned!
If you like the post, please share and subscribe. I will come back for Part 2 next week. As always, thank you for interest into math, technology and science.
Charlotte.
Charlotte