我正在研究区块链脚本。它可以运行,但是我遇到了正在打印的密钥的问题,有时它们似乎正在重用先前代码块中的密钥,而不是指“ prevhash”
代码:
import hashlib
import json
class Block():
def __init__(self, nonce, tstamp, transaction, prevhash=""):
self.nonce = nonce
self.tstamp = tstamp
self.transaction = transaction
self.prevhash = prevhash
self.hash = self.calcHash()
def calcHash(self):
block_string = json.dumps({
"nonce":self.nonce,
"tstamp":self.tstamp,
"transaction":self.transaction,
"prevhash":self.prevhash
},sort_keys=True).encode
# hashId = hashlib.sha256(block_string).hexdigest()
hashId = hashlib.sha256()
hashId.update(repr(block_string).encode('utf-8'))
hashId = hashId.hexdigest()
return hashId
def __str__(self):
string = "nonce: " + str(self.nonce) + "\n"
# string += "tstamp: " + str(self.tstamp) + "\n"
# string += "transaction: " + str(self.transaction) + "\n"
string += "phash: " + str(self.prevhash) + "\n"
string += "chash: " + str(self.hash)
return string
def printHashes(self):
print("phash: ",self.prevhash)
print("chash: ",self.hash)
class Blockchain():
def __init__(self):
self.chain = [self.generateGenesisBlock(),]
def generateGenesisBlock(self):
return Block(0, '01/01/2018', '1000000')
def getLastBlock(self):
return self.chain[-1]
def addBlock(self, newBlock):
newBlock.prevhash = self.getLastBlock().hash
newBlock.hash = newBlock.calcHash()
self.chain.append(newBlock)
def isChainValid(self):
for i in range(1,len(self.chain)):
prevBlock = self.chain[i-1]
currentBlock = self.chain[i]
if(currentBlock.hash != currentBlock.calcHash()):
return False
if(currentBlock.prevhash != prevBlock.hash):
return False
return True
BCoin = Blockchain()
BCoin.addBlock(Block(1, '01/02/2018', 100))
BCoin.addBlock(Block(2, '01/14/2018', 200))
BCoin.addBlock(Block(3, '01/15/2018', 15))
BCoin.addBlock(Block(4, '01/16/2018', 324))
BCoin.addBlock(Block(5, '01/02/2018', 100))
BCoin.addBlock(Block(6, '01/14/2018', 200))
BCoin.addBlock(Block(7, '01/15/2018', 15))
BCoin.addBlock(Block(8, '01/16/2018', 324))
BCoin.addBlock(Block(9, '01/02/2018', 100))
BCoin.addBlock(Block(10, '01/14/2018', 200))
for block in BCoin.chain:
print(block)
# print(BCoin.isChainValid())
输出:
phash:
chash: 19b00835428d29d63f2229933bf7067e7cec2057c19a90a51f22401c6571fd24
phash: 19b00835428d29d63f2229933bf7067e7cec2057c19a90a51f22401c6571fd24
chash: 687f54d1fbaabb7d43a009e87a9da34cd550a7e1a1c6499fb35a790bfbfd2bc4
phash: 687f54d1fbaabb7d43a009e87a9da34cd550a7e1a1c6499fb35a790bfbfd2bc4
chash: 687f54d1fbaabb7d43a009e87a9da34cd550a7e1a1c6499fb35a790bfbfd2bc4
phash: 687f54d1fbaabb7d43a009e87a9da34cd550a7e1a1c6499fb35a790bfbfd2bc4
chash: 6788d68d048817fd40a2c01e7f80a26efd23a3ae1f6fcb5fce87ae07f26cc18c
phash: 6788d68d048817fd40a2c01e7f80a26efd23a3ae1f6fcb5fce87ae07f26cc18c
chash: 687f54d1fbaabb7d43a009e87a9da34cd550a7e1a1c6499fb35a790bfbfd2bc4
注意重用先前区块(1,2,&4)中的(chash)键
部分问题与hashlib.sha256函数有关。原始代码给了我一个错误,替换代码应该解决了这个错误,但确实如此,但是后来我遇到了这个重新哈希问题。
# hashId = hashlib.sha256(block_string).hexdigest()
hashId = hashlib.sha256()
hashId.update(repr(block_string).encode('utf-8'))
hashId = hashId.hexdigest()
有人对导致这种情况的原因有任何想法吗?