Blockchain Proof of Work

Blockchain Proof of Work

Role in the entire blockchain

  • New blocks rely on Proof of Work (PoW) | Proof Of Work to construct

understand

  • The goal of PoW is to find a number that meets certain conditions, which is difficult to calculate but easy to verify. This is the core idea of ​​Proof of Work.

sample code

from hashlib import sha256    
import time

def Pow_fun(x=5, y =0):
    '''Pow算法函数
    param: x=5 为原始信息
    param: y=0 工作量初始值,通常为0
    
    return : y  值越大,工作量也会越大
            during_time 
    
    '''
    strat_time =  time.time()
    while sha256('{0}*{1}'.format(x, y).encode()).hexdigest()[-4:0] != "0000": #预设条件:当0的位数越多,理论上,CPU在进行枚举时所需要耗费的时间要更长,工作量也会越大
        y += 1          #用来判断Pow的量
    end_time = time.time()
    
    during_time =  end_time - start_time
    print(sha256('{0}*{1}'.format(x, y).encode()).hexdigest())
    print('The solution is y = {}'.format(y)) 
    print ("Working time:{}".format(during_time))
    

if __name__ == "__main__":
    Pow_fun(x=5,y=0)

The ingredients played by Pow - illustrated in Bitcoin

A Bitcoin wallet is a database of private and public keys. Bitcoin itself is stored on the blockchain. Users sign transactions with their private keys, thus proving they own the transaction. When you sign a transaction with your private key, the bitcoins mentioned in those transactions will have records, and these records can be queried by everyone.

  • Miners are responsible for validating the transaction and also charge some fees, a process called Proof of Work (POW, Proof of Work).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161035&siteId=291194637