-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathblock.py
More file actions
51 lines (45 loc) · 1.46 KB
/
Copy pathblock.py
File metadata and controls
51 lines (45 loc) · 1.46 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
import hashlib
import json
class Block:
def __init__(self, index, timestamp, data, prev_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.prev_hash = prev_hash
self.hash = self.hash_block()
def hash_block(self):
"""
Create a SHA-256 hash of the block contents
"""
sha = hashlib.sha256()
# Convert data to JSON string for consistent hashing
data_string = json.dumps(self.data, sort_keys=True) if self.data else ""
block_string = f"{self.index}{self.timestamp}{data_string}{self.prev_hash}"
sha.update(block_string.encode('utf-8'))
return sha.hexdigest()
def __str__(self):
"""
String representation of the block
"""
return f"Block #{self.index} [Hash: {self.hash[:10]}...]"
def __repr__(self):
"""
Detailed representation of the block
"""
return f"Block(index={self.index}, timestamp={self.timestamp}, hash={self.hash[:10]}...)"
def to_dict(self):
"""
Convert block to dictionary for JSON serialization
"""
return {
'index': self.index,
'timestamp': str(self.timestamp),
'data': self.data,
'prev_hash': self.prev_hash,
'hash': self.hash
}
def is_valid(self):
"""
Validate the block's hash
"""
return self.hash == self.hash_block()