重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍python中区块及区块链开发的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
成都创新互联公司于2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元林周做网站,已为上家服务,为林周各地企业和个人服务,联系电话:13518219792首先我们要明白一个区块,需要的内容,包括交易记录集合,时间戳,哈希,上一个区块的哈希。明白了这个,下面就容易代码开发了。
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中区块及区块链开发的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!