• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

麻烦各位大神帮忙看看哪错了 AttributeError: 'Blockchain' obje

区块链实战课程的内容
前面已经定义了 self.chain 为什么会报错?
class Blockchain:
def int(self):
self.current_trainsactions = []
self.chain=[]
self.new_block(proof=100,previous_hash=1) # 12.定义完成后我们首先创建一个创世纪区块,也就是链头区块,调用new_block,工作量证明是任意的,因为他是不需要计算的.定义上一个区块哈希值=1
def 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  # 交易信息返回到该信息所在区块的下一个区块的索引里@staticmethoddef 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转化成字符串,返回摘要信息@propertydef 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 proofdef 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’,metho


ds=[‘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

注意代码缩进

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!