add verify command

This commit is contained in:
Aurelien Rebourg 2023-04-27 23:49:44 +02:00
parent a66fb691e4
commit 8c4ec3ebc1
Signed by: Aurelien
GPG Key ID: F02826677ABB98C1
2 changed files with 23 additions and 0 deletions

View File

@ -179,3 +179,15 @@ class Blockchain:
self.writeBlocks()
return True
def isChainValid(self):
"""
Method to verify integrity of blockchain
"""
for i in range(1, len(self.chain)):
eachBlock = self.chain[i]
if eachBlock.hash != eachBlock.calculateHash():
return i
if eachBlock.previousHash != self.chain[i-1].hash:
return i
return 0

View File

@ -27,6 +27,7 @@ SUPPORTED_COMMANDS = [
'getblock',
'help',
'modify',
'verify',
]
def printLogo():
@ -161,6 +162,16 @@ def modify(args):
else:
throwError("Block modification failed!")
def verify(cmd):
"""
Method to verify the validity of blockchain.
"""
chain_valid = coin.isChainValid()
if chain_valid == 0:
printSuccess("Blockchain is valid!")
else:
throwError(f"Blockchain is not valid! Error on block {chain_valid}. Altered Chain!")
def throwError(msg):
"""
Method to throw an error from Blockshell.