|
| 1 | +from hashlib import sha256 |
| 2 | +import time |
| 3 | + |
| 4 | +class block: |
| 5 | + def __init__ (self, timestamp, data, previousHash = ' '): |
| 6 | + self.timestamp = timestamp |
| 7 | + self.data = data |
| 8 | + self.previousHash = previousHash |
| 9 | + self.hash = self.calculateHash() |
| 10 | + |
| 11 | + def calculateHash(self): |
| 12 | + return sha256((str(self.timestamp) + str(self.data) + str(self.previousHash)).encode()).hexdigest() |
| 13 | + |
| 14 | + |
| 15 | +class blockchain: |
| 16 | + def __init__(self): |
| 17 | + self.chain = [self.createGenesis()] |
| 18 | + |
| 19 | + def createGenesis(self): |
| 20 | + return block(time.ctime(), "genesisBlock", "00000") |
| 21 | + |
| 22 | + def mineBlock(self, data): |
| 23 | + node = block(time.ctime(), data, self.chain[-1].hash) |
| 24 | + # mining a new block to the blockchain |
| 25 | + self.chain.append(node) |
| 26 | + |
| 27 | + def printBlockchain(self): |
| 28 | + for i in range(len(self.chain)): |
| 29 | + print("\n-----Block ", i ,"---------\n timestamp = "\ |
| 30 | + , self.chain[i].timestamp,"\n data = ", \ |
| 31 | + self.chain[i].data, "\n previousHash = ",\ |
| 32 | + self.chain[i].previousHash,"\n hash = ", \ |
| 33 | + self.chain[i].hash) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +CEVcoin = blockchain() |
| 38 | + |
| 39 | +data = input() |
| 40 | + |
| 41 | +# sending data to get mined |
| 42 | +print("\n\n ----> Mining New Block -->") |
| 43 | +CEVcoin.mineBlock(data) |
| 44 | + |
| 45 | +print("\n\n ----> New Block mined successfully --> ") |
| 46 | + |
| 47 | +CEVcoin.printBlockchain() |
0 commit comments