diff --git a/Blockchain/blockchain_algorithm.py b/Blockchain/blockchain_algorithm.py index ff1c775ca..385c6c786 100644 --- a/Blockchain/blockchain_algorithm.py +++ b/Blockchain/blockchain_algorithm.py @@ -2,35 +2,52 @@ import time class block: - def __init__ (self, timestamp, data, previousHash = ' '): - self.timestamp = timestamp - self.data = data - self.previousHash = previousHash - self.hash = self.calculateHash() + def __init__ (self, timestamp, data, previousHash = ' '): + self.timestamp = timestamp + self.data = data + self.previousHash = previousHash + self.hash = self.calculateHash() - def calculateHash(self): - return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest() + def calculateHash(self): + return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest() class blockchain: - def __init__(self): - self.chain = [self.createGenesis()] + def __init__(self): + self.chain = [self.createGenesis()] - def createGenesis(self): - return block(time.ctime(), "genesisBlock", "00000") + def createGenesis(self): + return block(time.ctime(), "genesisBlock", "00000") - def mineBlock(self, data): - node = block(time.ctime(), data, self.chain[-1].hash) - # mining a new block to the blockchain - self.chain.append(node) + def mineBlock(self, data): + if not self.checkChain(): + return "Mining unsuccessfully" + node = block(time.ctime(), data, self.chain[-1].hash) + # mining a new block to the blockchain + self.chain.append(node) + return "New Block mined successfully" + - def printBlockchain(self): - for i in range(len(self.chain)): - print("\n-----Block ", i ,"---------\n timestamp = "\ - , self.chain[i].timestamp,"\n data = ", \ - self.chain[i].data, "\n previousHash = ",\ - self.chain[i].previousHash,"\n hash = ", \ - self.chain[i].hash) + def checkChain(self): + for i in range(len(self.chain)): + if i > 0 and self.chain[i-1].hash != self.chain[i].previousHash: + return False + + return True + + def printBlockchain(self): + if self.checkChain(): + print("Chain is valid") + else: + print("Chain is invalid") + return 0 + + for i in range(len(self.chain)): + print("\n-----Block ", i ,"---------\n timestamp = "\ + , self.chain[i].timestamp,"\n data = ", \ + self.chain[i].data, "\n previousHash = ",\ + self.chain[i].previousHash,"\n hash = ", \ + self.chain[i].hash) @@ -40,8 +57,8 @@ def printBlockchain(self): # sending data to get mined print("\n\n ----> Mining New Block -->") -CEVcoin.mineBlock(data) -print("\n\n ----> New Block mined successfully --> ") + +print("\n\n ----> ",CEVcoin.mineBlock(data)," --> ") CEVcoin.printBlockchain()