-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.py
More file actions
71 lines (55 loc) · 2.22 KB
/
blockchain.py
File metadata and controls
71 lines (55 loc) · 2.22 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import hashlib
import datetime as date
#creating the block class
#defining the structure of each block in the blockchain
class Block:
def __init__(self,index,timestamp,data,previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
hash_string = str(self.index) + str(self.timestamp) +str(self.data)+str(self.previous_hash)
return hashlib.blake2b(hash_string.encode()).hexdigest()
#creating the blockchain class
#list of blocks in theblockchain
class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
return Block(0,date.datetime.now(),"Genesis Block","0")
def get_latest_block(self):
return self.chain[-1]
def add_block(self,new_block):
new_block.previous_hash = self.get_latest_block().hash
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)
def is_valid(self):
for i in range(1,len(self.chain)):
current_block = self.chain[i]
previous_block = self.chain[i-1]
if current_block.hash != current_block.hash:
return False
if current_block.previous_hash != previous_block.hash:
return False
return True
#testing the blockchain
#create the block chainblockchain = Blockchain()
#add blocks to the blockchain
# Create the blockchain
blockchain = Blockchain()
s = "Nanda"
for i in range(1,len(s)+1):
# Add blocks to the blockchain
blockchain.add_block(Block(i, date.datetime.now(), "Transaction Data 1", ""))
#blockchain.add_block(Block(2, date.datetime.now(), "Transaction Data 2", ""))
#blockchain.add_block(Block(3, date.datetime.now(), "Transaction Data 3", ""))
# Print the contents of the blockchain
for block in blockchain.chain:
print("Block #" + str(block.index))
print("Timestamp: " + str(block.timestamp))
print("Data: " + block.data)
print("Hash: " + block.hash)
print("Previous Hash: " + block.previous_hash)
print("\n")