-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockchain_algorithm.py
47 lines (33 loc) · 1.19 KB
/
blockchain_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from hashlib import sha256
import time
class block:
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()
class blockchain:
def __init__(self):
self.chain = [self.createGenesis()]
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 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)
CEVcoin = blockchain()
data = input()
# sending data to get mined
print("\n\n ----> Mining New Block -->")
CEVcoin.mineBlock(data)
print("\n\n ----> New Block mined successfully --> ")
CEVcoin.printBlockchain()