Proof of Work

 Proof of Work: In proportion to computing power. 

                      POW   ∝ computing power

Proof of Work (PoW) algorithm was developed as a mechanism to achieve consensus.

Pow direct impacts on climate change although in order to achieve consensus in a decentralized manner and to prevent bad actors from overtaking the network.

Idea: Allow nodes to compete with each other using their computing power that implies the nodes automatically being picked in that proportion. A node can propose and validate transactions and perform mining to facilitate consensus and secure the blockchain.

POW property 1: Difficult to compute.

As of Feb 2022 the bitcoin difficulty is 26.69 x 10^12 hashes It requires approximately 2.7 x 10^15 hashes to create one.

BITCOIN only some nodes bother to compete - minors.

POW property 2: Parameterizable cost

Nodes automatically re-calculate the target every two weeks Goal average time between blocks = 10 minutes Each 2016-block interval is known as a difficulty epoch. At the beginning of every epoch the Bitcoin network recalculates the current Target. If you put a fixed amount of H/W for mining the rate at which you find the block depends upon the total computer power available with others.

Python Example:

Let's suppose three participants in this case Ryder, Dora and Shin-chan. But there are other kinds of participants as well. In blockchain lingo, the miner is the one who writes into the blockchain. The miner must first put in some work in order to add the transactions to the blockchain.

We have 3 blocks blockA, blockB, blockC, and additional blockD says candidate block. let's add into the blockchain as follows.

blockD = Block()

blockD.id = 4

blockD.history = 'Parent likes Ryder and Dora'

blockD.parent_id = blockC.id

Output:

PS D:\KnowledgeHunt\blockchain\ch1> python .\upgradelinklist.py

b'{"id": 4, "history": "Parent likes Ryder and Dora", "parent_id": 3}'

b'{"id": 3, "history": "She likes Ryder", "parent_id": 2, "parent_hash": "79be004af591f9724f09d47ce0fc9bdb7565f748e56f81447a694a3cfe0393ee"}'

For the complete code, please see the previous article.

Before addition the BlockD to the blockchain we have to require the miner to do some puzzle work . Now we will serialize that block and ask the miner to apply an extra string , which appended to the serialize string of the block that will show the hash output with the five zeros in the front.

In the early days, the computing power used for mining was minimal so the difficulty to produce a block was relatively easy, and only a few zeros were required for a valid block.

Ex. 0000079be004af591f9724f09d47ce0fc9bdb7565f748e56f81447a694a3cfe0393ee

So puzzle is something like as below:

string serialization + answer + hash output with at least five leading zeros

string serialization + answer+hash output with at least five leading zeros 

answer=#

input=b'{"id": 4, "history": "Parent likes Ryder and Dora", "parent_id": 3}'

output=hashlib.sha256(json.dumps(input)).hexdigest()

output needs to be 

00000########################################################

We can use brute force to figure out how a miner would solve this problem:

import hashlib

payloadString = '{"id": 4, "history": "Parent likes Ryder and Dora", "parent_id": 3}'

payloadBytes = bytes(payloadString, 'utf-8')

for i in range(1000000):

    nonce = str(i).encode('utf-8')

    result = hashlib.sha256(payloadBytes + nonce).hexdigest()

    if result[0:5] == '00000':

        print("The answer to puzzle:= " + str(i))

        print()

        print("Input to hash is:= " + payloadString + str(i))

        print()

        print("Output hash which has 5 leading zeros:= " + result)

        break

Output:

 D:\KnowledgeHunt\blockchain\ch1> python .\pow.py

The answer to puzzle:= 515950

Input to hash is:= {"id": 4, "history": "Parent likes Ryder and Dora", "parent_id": 3}515950

Output hash which has 5 leading zeros:= 00000d618014bf14f964a075a843ec93427e64c3c67c786bacf7b1ed709e8196


Comments