区块链nonce值随负载变化

2024-10-06 15:45:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究一个具有基本nonce值的区块链,但是每次应用程序重启,nonce值都会发生变化,我不知道为什么

我尝试了各种方法来检查哈希是否以“0”开头,例如while not循环

def hash_block(self):
        sha = hashlib.sha512()
        sha.update( (str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode("utf-8") + str(self.nonce).encode() )
        valid = False
        if(sha.hexdigest().startswith('0'*self.difficulty)):
            valid = True
        while not valid:
            self.nonce += 1
            sha.update( (str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode("utf-8") + str(self.nonce).encode() )
            if(sha.hexdigest().startswith('0'*self.difficulty)):
                valid = True
        return sha.hexdigest()

我希望nonce在应用程序重启前后都保持静态,但是当它再次加载时,值似乎会改变,我已经从JSON输出文件中检查了这一点


Tags: self应用程序dataindexnotupdatehashtimestamp