-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.py
More file actions
62 lines (51 loc) · 1.69 KB
/
blockchain.py
File metadata and controls
62 lines (51 loc) · 1.69 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
# blockchain.py
import time
from typing import List
from models import Block, Transaction
class Blockchain:
def __init__(self):
self.chain: List[Block] = []
self.create_genesis_block()
def create_genesis_block(self):
"""
Create the first block with no real transactions.
"""
genesis_block = Block(
index=0,
timestamp=time.time(),
transactions=[],
previous_hash="0"
)
self.chain.append(genesis_block)
@property
def last_block(self) -> Block:
return self.chain[-1]
def add_block(self, transactions: List[Transaction]) -> Block:
"""
Add a new block with the given transactions.
No real consensus or PoW; just link and hash.
"""
new_block = Block(
index=len(self.chain),
timestamp=time.time(),
transactions=transactions,
previous_hash=self.last_block.hash
)
self.chain.append(new_block)
return new_block
def is_chain_valid(self) -> bool:
"""
Verify that all blocks are correctly linked and not tampered.
"""
for i in range(1, len(self.chain)):
current = self.chain[i]
previous = self.chain[i - 1]
# Recompute hash and compare
if current.hash != current.compute_hash():
print(f"Invalid hash at block {current.index}")
return False
# Check chaining
if current.previous_hash != previous.hash:
print(f"Broken chain link between blocks {previous.index} and {current.index}")
return False
return True