我一直在研究一个具有基本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输出文件中检查了这一点
在休息一晚并再次解决问题后,我发现问题出在使用sha.update,而没有清除以前的结果
相关问题 更多 >
编程相关推荐