微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError: '_hashlib.HASH' 对象没有属性 'hex'

如何解决AttributeError: '_hashlib.HASH' 对象没有属性 'hex'

我正在做一个关于区块链的课程,终点工作正常,但矿块没有,我试图改变烧瓶的版本,但它不会改变结果,如果我改变了 hex()到 hexdigest() 我什至无法得到回应。有人可以帮我吗?

import hashlib
import json
import flask

from flask import Flask,jsonify

# Parte 1,criar um Blockchain

class Blockchain:
    def __init__(self):
        self.chain = []
        self.create_block(proof = 1,prevIoUs_hash = '0')
        
    def create_block(self,proof,prevIoUs_hash):
        block = {'index' : len(self.chain) + 1,'timestamp' : str(datetime.datetime.Now()),'proof' : proof,'prevIoUs_hash' : prevIoUs_hash}
        self.chain.append(block)
        return block
    
    def get_prevIoUs_block(self):
        return self.chain[-1]
    
    def proof_or_work(self,prevIoUs_proof):
        new_proof = 1
        check_proof = False
        
        while check_proof is False:
            hash_operation = hashlib.sha256(str(new_proof**2 - prevIoUs_proof**2).encode()).hexdigest()
            if hash_operation[:4] == '0000':
                check_proof == True
            else:
                new_proof += 1
        return new_proof
    
    def hash(self,block):
        encoded_block = json.dumps(block,sort_keys=True).enconde()
        return hashlib.sha256(encoded_block).hexdigest()
    
    def is_chain_valid(self,chain):
        prevIoUs_block = chain[0]
        block_index = 1
        
        while block_index < len(chain):
            block = chain[block_index]
            if block['prevIoUs_hash'] != self.hash(prevIoUs_block):
                return False
            prevIoUs_proof = prevIoUs_block['proof']
            proof = block['proof']
            hash_operation = hashlib.sha256(str(proof**2 - prevIoUs_proof**2).encode()).hexdigest()
            if hash_operation[:4] != '0000':
                return False
            prevIoUs_block = block
            block_index += 1
        return True
        

app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False


blockchain = Blockchain()

@app.route('/mine_block',methods=['GET'])
def mine_block():
    prevIoUs_block = blockchain.get_prevIoUs_block()
    prevIoUs_proof = prevIoUs_block['proof']
    proof = blockchain.proof_or_work(prevIoUs_proof)
    prevIoUs_hash = blockchain.hash(prevIoUs_block)
    
    block = blockchain.create_block(proof,prevIoUs_hash)
    
    response = {'message' : 'Parabens voce minerou um bloco!','index': block['index'],'timestamp': block['timestamp'],'proof' : block['proof'],'prevIoUs_hash' : block['prevIoUs_hash']}
    return jsonify(response),200;

@app.route('/get_chain',methods=['GET'])
def get_chain():
    response = {'chain' : blockchain.chain,'lenght' : len(blockchain.chain)}
    return jsonify(response),200;


app.run(host= '0.0.0.0',port=5000)
  • http://0.0.0.0:5000/ 上运行(按 CTRL+C 退出) [2021-02-18 18:38:06,039] 应用程序错误:/mine_block [GET] 上的异常 回溯(最近一次调用最后一次): 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask\app.py”,第 1982 行,在 wsgi_app 中 响应 = self.full_dispatch_request() 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask\app.py”,第 1614 行,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask\app.py”,第 1517 行,在 handle_user_exception 再加注(exc_type,exc_value,tb) 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask_compat.py”,第 33 行,重新加注 提升价值 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask\app.py”,第 1612 行,在 full_dispatch_request 中 rv = self.dispatch_request() 文件“C:\Users\Leslye\anaconda3\lib\site-packages\flask\app.py”,第 1598 行,在 dispatch_request 中 返回 self.view_functionsrule.endpoint 文件“C:\Users\Leslye\Desktop\Workspace\Blockchain\Modulo 1 - Criar Blockchain\blockchain.py”,第 70 行,在 mine_block 证明 = blockchain.proof_or_work(prevIoUs_proof) 文件“C:\Users\Leslye\Desktop\Workspace\Blockchain\Modulo 1 - Criar Blockchain\blockchain.py”,第 31 行,在 proof_or_work hash_operation = hashlib.sha256(str(new_proof2 - prevIoUs_proof2).encode()).hex() AttributeError: '_hashlib.HASH' 对象没有属性 'hex' 127.0.0.1 - - [18/Feb/2021 18:38:06] "GET /mine_block HTTP/1.1" 500 -

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。