您好,登錄后才能下訂單哦!
這篇文章主要介紹python中區塊及區塊鏈開發的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
首先我們要明白一個區塊,需要的內容,包括交易記錄集合,時間戳,哈希,上一個區塊的哈希。明白了這個,下面就容易代碼開發了。
import datetime import hashlib from Message import DaDaMessage, InvalidMessage from Transaction import Transaction class Block: #不定參數,*args,區塊集合 def __init__(self, *args): self.messagelist = [] # 存儲多個交易記錄 self.timestamp = None # 存儲多個記錄最終鎖定時間 self.hash = None self.preHash = None if args: for arg in args: self.add_message(arg) def add_message(self, message): # 添加 交易信息 # 區分第一條與后面多條,是否需要鏈接 if len(self.messagelist) > 0: message.link(self.messagelist[-1]) # 鏈接最后一個 message.seal() # 密封 message.validate() # 校驗 self.messagelist.append(message) def link(self, block): # 區塊鏈接 self.preHash = block.hash def seal(self): # 密封當前hash self.timestamp = datetime.datetime.now() self.hash = self.hash_block() def hash_block(self): # 密封上一塊哈希,時間戳,交易記錄的最后一個 return hashlib.sha512( (str(self.timestamp) + str(self.preHash) + str(self.messagelist[-1].hash)).encode("utf-8")).hexdigest() def validate(self): # 校驗 for i, message in enumerate(self.messagelist): # 每個交易記錄校驗一下 message.validate() #每一條交易記錄校驗 if i > 0 and message.prev_hash != self.messagelist[i - 1].hash: raise InvalidBlock("無效的block,交易記錄被修改為在第{}條記錄".format(i)+str(self)) return str(self) + "Ok" def __repr__(self): return "money block= hash:{},prehash:{},len:{},time:{}".format(self.hash, self.preHash, len(self.messagelist), self.timestamp)
自定義異常:
class InvalidBlock(Exception): def __init__(self,*args,**kwargs): super(Exception,self).__init__(*args,**kwargs)
編寫測試模塊:
if __name__=="__main__": t1 = Transaction("yicheng", "ddd1", 100) t2 = Transaction("yicheng", "ddd2", 200) t3 = Transaction("yicheng", "ddd3", 300) t4 = Transaction("yicheng", "ddd4", 400) m1 = DaDaMessage(t1) m2 = DaDaMessage(t2) m3 = DaDaMessage(t3) m4 = DaDaMessage(t4) try: block1 = Block(m1, m2, m3) block1.seal() #測試篡改數據 #m3.data = "你妹的直播" #block1.messagelist[0] = m3 print(block1.validate()) except InvalidMessage as e: #交易記錄被修改 print(e) except InvalidBlock as e: #區塊被修改 print(e)
測試結果如下,為了打印需要,我改成了md5格式下的結果:
篡改區塊信息的結果,可能結果不一樣,因為修改的內容不一樣,報的錯誤也不一樣:
至此,已經完成了:交易記錄,區塊的開發,現在進行區塊鏈的開發就比較容易了。實現代碼如下:
from Block import InvalidBlock, Block from Message import InvalidMessage, DaDaMessage from Transaction import Transaction # 區塊鏈 class Dada_BlockCoin: def __init__(self): self.blocklist = [] # 裝載所有區塊 def validate(self):#校驗所有區塊 for i, block in enumerate(self.blocklist): try: block.validate() except InvalidBlockCoin as e: raise InvalidBlockCoin("區塊校驗錯誤,區塊索引{}".format(i)) def add_block(self, block): # 增加區塊 if len(self.blocklist) > 0: block.link(self.blocklist[-1]) #連接區塊 block.seal()#密封 block.validate()#校驗 self.blocklist.append(block)#添加到區塊鏈中 def __repr__(self): return "Dada_BlockCoin:{}".format(len(self.blocklist))
自定義異常:
class InvalidBlockCoin(Exception): def __init__(self, *args, **kwargs): super(Exception, self).__init__(*args, **kwargs)
編寫測試模塊:
if __name__ == "__main__": t1 = Transaction("yicheng", "ddd1", 100) t2 = Transaction("yicheng", "ddd2", 200) t3 = Transaction("yicheng", "ddd3", 300) t4 = Transaction("yicheng", "ddd4", 400) t5 = Transaction("yicheng", "ddd5", 500) t6 = Transaction("yicheng", "ddd6", 600) m1 = DaDaMessage(t1) m2 = DaDaMessage(t2) m3 = DaDaMessage(t3) m4 = DaDaMessage(t4) m5 = DaDaMessage(t5) m6 = DaDaMessage(t6) try: yin1 = Block(m1, m2) yin1.seal() yin2 = Block(m3, m4) yin2.seal() yin3 = Block(m5, m6) yin3.seal() # 篡改區塊 #yin3.messagelist.append(m1) coin = Dada_BlockCoin() # 區塊鏈 coin.add_block(yin1) coin.add_block(yin2) coin.add_block(yin3) coin.validate() print(coin) except InvalidMessage as e: print(e) except InvalidBlock as e: print(e) except InvalidBlockCoin as e: print(e)
測試結果如下:
篡改區塊鏈,測試模塊區塊鏈的內容,可以任意篡改,測試結果如下:
這里已經完成了數據層的部分開發,其余部分后續會完善。
以上是“python中區塊及區塊鏈開發的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。