-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.py
33 lines (23 loc) · 920 Bytes
/
blockchain.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
from block import Block
class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
return Block(0, "Bloco de Gênese", "0")
def get_last_block(self):
return self.chain[-1]
def add_block(self, new_block):
new_block.previous_hash = self.get_last_block().hash
new_block.hash = new_block.generate_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.generate_hash():
return False
if current_block.index != previous_block.index + 1:
return False
if current_block.previous_hash != previous_block.hash:
return False
return True