python挖礦算力測試程序詳解
談到比特幣,我們都知道挖礦,有些人并不太明白挖礦的含義。這里的挖礦其實就是哈希的碰撞,舉個簡單例子:
import hashlib x = 11 y = 1 #這里可以調節(jié)挖礦難度,也就是哈希的長度 while hashlib.sha256(f'{x*y}'.encode("utf-8")).hexdigest()[5:7]!="00": print(x*y) y +=1 print("找到了:",(x*y))
結果如下:
當然比特幣的挖礦要比這個復雜太多,但是原理差不多,有個大概的認知。
關于節(jié)點的同步,是取整個節(jié)點中最長的區(qū)塊鏈進行同步,如圖所示:
有了以上內容鋪墊,代碼實現(xiàn)和理解就容易了,代碼如下:
#挖礦原理與網(wǎng)絡共識 import datetime import hashlib import json import requests class Blockchain2: def __init__(self): self.chain = [] #區(qū)塊鏈列表 self.nodes = set() #節(jié)點集合 self.current_tranactions = [] #交易列表 self.new_block(proof=100,preHash=1) #創(chuàng)建第一個區(qū)塊 #新建一個區(qū)塊,需要計算,才能追加 def new_block(self,proof,preHash = None): block={ "index":len(self.chain)+1,#區(qū)塊索引 "timestamp":datetime.datetiem.now(),#區(qū)塊時間戳 "transactions":self.current_tranactions,#區(qū)塊交易記錄集合 "proof":proof,#算力憑證 "preHash":preHash or self.hash(self.chain[-1]), #上一塊的哈希 } self.current_tranactions = [] #開辟新的區(qū)塊,初始化區(qū)塊交易記錄 self.chain.append(block) @staticmethod def hash(block): #處理為json字符串格式的哈希 block_str = json.dumps(block,sort_keys=True).encode("utf-8") return hashlib.sha256(block_str).hexdigest() #新增交易記錄 def new_transaction(self,sender,receiver,amount): transaction ={ "sender":sender, "receiver":receiver, "amount":amount, } self.current_tranactions.append(transaction) return self.last_block["index"]+1 @property def last_block(self): return self.chain[-1] #挖礦,依賴上一個模塊,獲取工作量證明,即POW共識機制 def proof_of_work(self,last_block): last_proof = last_block["proof"] last_hash = self.hash(last_block) proof = 0 while self.valid_proof(last_proof,proof,last_hash) is False: proof +=1 return proof #校驗工作量 @staticmethod def valid_proof(last_proof,proof,last_hash): guess = f'{last_proof}{proof}{last_hash}'.encode("utf-8") guess_hash = hashlib.sha256(guess).hexdigest() return guess_hash[:6] =="000000" #可以調整計算難度 #區(qū)塊一致性,同步算法, def resolve_conflicts(self): neighbours = self.nodes new_chain = None max_length = len(self.chain) #遍歷所有節(jié)點,找出最長的鏈 for node in neighbours: #獲取節(jié)點區(qū)塊鏈信息 response = requests.get(f'http://{node}/chain') if response.status_code ==200: length = response.json()["length"] chain = response.json()["chain"] if length>max_length and self.valid_chain(chain): max_length = length new_chain = chain if new_chain: self.chain = new_chain return True else: return False #校驗區(qū)塊鏈的合法性 def valid_chain(self,chain): last_block = chain[0] current_index = 1 #校驗每一個區(qū)塊的prehash,proof合法性 while current_index <len(chain): block = chain[current_index] #校驗哈希的合法性 if block["preHash"] != self.hash(last_block): return False #校驗算力的合法性 if not self.valid_proof(last_block["proof"],block["proof"],block["preHash"]): return False last_block = block current_index +=1 return True
算力校驗和pow共識基本實現(xiàn)了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python抓取網(wǎng)頁圖片示例(python爬蟲)
這篇文章主要介紹了python抓取網(wǎng)頁圖片示例(python爬蟲),需要的朋友可以參考下2014-04-04python中__new__和__init__的實現(xiàn)
在Python中,每個對象都有兩個特殊的方法__new__和__init__,本文主要介紹了python中__new__和__init__的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-05-05pandas apply 函數(shù) 實現(xiàn)多進程的示例講解
下面小編就為大家分享一篇pandas apply 函數(shù) 實現(xiàn)多進程的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python讀取大量Excel文件并跨文件批量計算平均值的方法
這篇文章主要介紹了Python讀取大量Excel文件并跨文件批量計算平均值,介紹基于Python語言,實現(xiàn)對多個不同Excel文件進行數(shù)據(jù)讀取與平均值計算的方法,需要的朋友可以參考下2023-02-02Python PyQt5干貨滿滿小項目輕松實現(xiàn)高效摳圖去背景
PyQt5以一套Python模塊的形式來實現(xiàn)功能。它包含了超過620個類,600個方法和函數(shù)。本篇文章手把手帶你用PyQt5輕松實現(xiàn)圖片扣除背景,大家可以在過程中查缺補漏,提升水平2021-11-11