为了账号安全,请及时绑定邮箱和手机立即绑定

麻烦各位大神帮忙看看哪错了 AttributeError: 'Blockchain' object has no attribute 'chain'

麻烦各位大神帮忙看看哪错了 AttributeError: 'Blockchain' object has no attribute 'chain'

Yhyyy 2018-11-12 09:28:36
区块链实战课程的内容前面已经定义了 self.chain 为什么会报错?class Blockchain:def int(self):self.current_trainsactions = []self.chain=[]self.new_block(proof=100,previous_hash=1) # 12.定义完成后我们首先创建一个创世纪区块,也就是链头区块,调用new_block,工作量证明是任意的,因为他是不需要计算的.定义上一个区块哈希值=1def new_block(self, proof, previous_hash=None):  # 4.接下来定义一个块     block = {         "index": len(self.chain) + 1,  # 5.其中包括索引(链长+1)         "timestamp": time(),  # 6.用time这个函数返回时间戳         "trainscations": self.current_trainsactions,  # 7.保存当前的交易信息         "proof": proof,  # 暂时当做一个参数加入,工作量证明         "previous_hash": previous_hash or self.hash(self.last_block)         # 8,.同样暂时作为一个参数加入,添加一个默认值为none(前一个参数),或者是计算出来的上一个计算出来的哈希值     }     self.current_trainsactions = []  # 9.接下来把当前交易信息清空,因为交易信息已经打包到区块当中了↑     self.chain.append(block)  # 10.接下来把这个区块加入到链条中     return block  # 11.然后返回这个区块中 def new_trainsactions(self, sender, recipient, amount) -> int:  # 1.添加交易信息,交易方法中要包括,发送者,接收者,和金额     self.current_trainsactions.append(  # 2.把新的交易信息要打包到交易列表中,这个信息需要加入到下一个新加入的区块当中.         {             "sender": sender,  # 3.key:值             "recipient": recipient,             "amount": amount         }     )     return self.last_block["index"] + 1  # 交易信息返回到该信息所在区块的下一个区块的索引里 @staticmethod def hash(blcok):  # 13.接下来定义一下哈希函数     block_string = json.dumps(blcok, sort_keys=True).encode()  # 返回编码     hashlib.sha256( block_string).hexdigest()  # 14.这里用到的hashlib的算法是sha256,sha256需要传入一个字符串编码后的字节数组,所以先把block转换成一个字节数组,因为block是一个对象(json)我们需要把json转化成字符串,返回摘要信息 @property def last_block(self):     return self.chain(-1) def proof_of_work(self, last_proof: int) -> int:  # 15.工作量证明的函数就是不停的在尝试一个proof值,当满足条件时,返回这个值     proof = 0     while self.valid_proof(last_proof, proof) is False:  # 16.验证方法就是用上一个块儿的工作量证明和这一个块儿的工作量计算看是否能返回一个四个零开头的哈希值         proof += 1     # print(proof)     return proof def valid_proof(self, last_proof: int, proof: int) -> bool:  # 17.定义验证工作量证明的方法,返回bool型     guess = F'{last_proof},{proof}'.encode()  # 18.将lastproof 和proof转换成字符串类型并做一个编码     guess_hash = hashlib.sha256(guess).hexdigest()     # sleep(1)  # 计算过程睡眠1秒,为了看清计算过程用     # print(guess_hash)     return guess_hash[0:4] == "0000"     # testPow=Blockchain()     # testPow.proof_of_work(100)                            #测试工作量证明app = Flask(name) # 19创建通信节点,Flask 提供一个轻量级的web服务blockchain = Blockchain()@app.route(’/index’,methods=[‘GET’])#通讯测试,添加几个路由def index(): # 此路由链接一个方法return “hello blockchain” #测试@app.route(’/transactions/new’,methods=[‘POST’]) #添加交易的路由def new_transaction():return “we’ll add a new transaction”@app.route(’/mine’,methods=[‘GET’]) #添加打包交易的路由(挖矿路由)def mine():return “we’ll mine a new blcok”@app.route(’/chain’,methods=[‘GET’]) #添加整个链的详细信息路由,将当前区块链信息返回给请求者def full_chain():response = {‘chain’:blockchain.chain, #chain及相关功能上面已经定义了’length’:len(blockchain.chain)}return jsonify(response),200 #将变量信息转化为字符串并返回if name == ‘main’: # 这里是提供一个运行的look 可视化app.run(host=‘0.0.0.0’, port=5000) # 20.将框架启动起来 (地址,端口)
查看完整描述

1 回答

?
pardon110

TA贡献1038条经验 获得超227个赞

注意代码缩进

查看完整回答
反对 回复 2018-11-17
  • 1 回答
  • 0 关注
  • 1503 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信