python挖礦算力測試程序詳解
談到比特幣,我們都知道挖礦,有些人并不太明白挖礦的含義。這里的挖礦其實(shí)就是哈希的碰撞,舉個簡單例子:
import hashlib x = 11 y = 1 #這里可以調(diào)節(jié)挖礦難度,也就是哈希的長度 while hashlib.sha256(f'{x*y}'.encode("utf-8")).hexdigest()[5:7]!="00": print(x*y) y +=1 print("找到了:",(x*y))
結(jié)果如下:
當(dāng)然比特幣的挖礦要比這個復(fù)雜太多,但是原理差不多,有個大概的認(rèn)知。
關(guān)于節(jié)點(diǎn)的同步,是取整個節(jié)點(diǎn)中最長的區(qū)塊鏈進(jìn)行同步,如圖所示:
有了以上內(nèi)容鋪墊,代碼實(shí)現(xiàn)和理解就容易了,代碼如下:
#挖礦原理與網(wǎng)絡(luò)共識 import datetime import hashlib import json import requests class Blockchain2: def __init__(self): self.chain = [] #區(qū)塊鏈列表 self.nodes = set() #節(jié)點(diǎn)集合 self.current_tranactions = [] #交易列表 self.new_block(proof=100,preHash=1) #創(chuàng)建第一個區(qū)塊 #新建一個區(qū)塊,需要計(jì)算,才能追加 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共識機(jī)制 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 #校驗(yàn)工作量 @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" #可以調(diào)整計(jì)算難度 #區(qū)塊一致性,同步算法, def resolve_conflicts(self): neighbours = self.nodes new_chain = None max_length = len(self.chain) #遍歷所有節(jié)點(diǎn),找出最長的鏈 for node in neighbours: #獲取節(jié)點(diǎn)區(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 #校驗(yàn)區(qū)塊鏈的合法性 def valid_chain(self,chain): last_block = chain[0] current_index = 1 #校驗(yàn)每一個區(qū)塊的prehash,proof合法性 while current_index <len(chain): block = chain[current_index] #校驗(yàn)哈希的合法性 if block["preHash"] != self.hash(last_block): return False #校驗(yàn)算力的合法性 if not self.valid_proof(last_block["proof"],block["proof"],block["preHash"]): return False last_block = block current_index +=1 return True
算力校驗(yàn)和pow共識基本實(shí)現(xiàn)了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python抓取網(wǎng)頁圖片示例(python爬蟲)
這篇文章主要介紹了python抓取網(wǎng)頁圖片示例(python爬蟲),需要的朋友可以參考下2014-04-04python中__new__和__init__的實(shí)現(xiàn)
在Python中,每個對象都有兩個特殊的方法__new__和__init__,本文主要介紹了python中__new__和__init__的實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-05-05YOLOv8訓(xùn)練自己的數(shù)據(jù)集(詳細(xì)教程)
YOLO是一種基于圖像全局信息進(jìn)行預(yù)測的目標(biāo)檢測系統(tǒng),YOLOv8 是ultralytics公司在2023年1月10號開源的YOLOv5的下一個重大更新版本,這篇文章主要給大家介紹了關(guān)于YOLOv8訓(xùn)練自己的數(shù)據(jù)集的相關(guān)資料,需要的朋友可以參考下2023-01-01pandas apply 函數(shù) 實(shí)現(xiàn)多進(jìn)程的示例講解
下面小編就為大家分享一篇pandas apply 函數(shù) 實(shí)現(xiàn)多進(jìn)程的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python讀取大量Excel文件并跨文件批量計(jì)算平均值的方法
這篇文章主要介紹了Python讀取大量Excel文件并跨文件批量計(jì)算平均值,介紹基于Python語言,實(shí)現(xiàn)對多個不同Excel文件進(jìn)行數(shù)據(jù)讀取與平均值計(jì)算的方法,需要的朋友可以參考下2023-02-02Python PyQt5干貨滿滿小項(xiàng)目輕松實(shí)現(xiàn)高效摳圖去背景
PyQt5以一套Python模塊的形式來實(shí)現(xiàn)功能。它包含了超過620個類,600個方法和函數(shù)。本篇文章手把手帶你用PyQt5輕松實(shí)現(xiàn)圖片扣除背景,大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11python多進(jìn)程及通信實(shí)現(xiàn)異步任務(wù)的方法
這篇文章主要介紹了python多進(jìn)程及通信實(shí)現(xiàn)異步任務(wù)需求,本人也是很少接觸多進(jìn)程的場景,對于python多進(jìn)程的使用也是比較陌生的。在接觸了一些多進(jìn)程的業(yè)務(wù)場景下,對python多進(jìn)程的使用進(jìn)行了學(xué)習(xí),覺得很有必要進(jìn)行一個梳理總結(jié),感興趣的朋友一起看看吧2022-05-05