forked from rajatdiptabiswas/python-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.py
More file actions
42 lines (33 loc) · 1.02 KB
/
Copy pathblockchain.py
File metadata and controls
42 lines (33 loc) · 1.02 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
from block import Block
from datetime import datetime
class Blockchain(object):
def __init__(self):
super().__init__()
self.chain = [Block(0, datetime.now().timestamp(), 'Genesis Block', '0')]
def get_previous_hash(self):
return self.chain[len(self.chain) - 1].hash
def is_valid(self, block):
current_block = self.chain[len(self.chain) - 1]
if current_block.index + 1 != block.index:
return False
elif block.previous_hash != current_block.hash:
return False
elif block.hash != block.calculate_hash():
return False
return True
def add_block(self, data):
timestamp = datetime.now().timestamp()
index = len(self.chain)
previous_hash = self.get_previous_hash()
new_block = Block(index, timestamp, data, previous_hash)
if self.is_valid(new_block):
self.chain.append(new_block)
else:
print('ERROR: Block is invalid!')
def print_blockchain(self):
print()
for block in self.chain:
print('{} {}'.format(block.index, block.real_time()))
print(block.data)
print(block.hash)
print()