-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.py
More file actions
45 lines (37 loc) · 1.28 KB
/
block.py
File metadata and controls
45 lines (37 loc) · 1.28 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
import typing
from hashlib import sha256
import json
def calculate_hash(data: typing.Any) -> str:
if isinstance(data, list):
unified_data = json.dumps(data, sort_keys=True)
elif isinstance(data, object):
unified_data = json.dumps(
{key: value for key, value in data.__dict__.items() if key not in ["data", "hash"]}, sort_keys=True
)
else:
unified_data = data
return sha256(unified_data.encode("utf-8")).hexdigest()
class Block:
def __init__(
self, height: int, data: typing.List[typing.Any], timestamp: float, previous_hash: str, difficulty, nonce: int = 0
):
self.data = data
self.height = height
self.timestamp = timestamp
self.previous_hash = previous_hash
self.difficulty = difficulty
self.data_hash = calculate_hash(self.data)
self.nonce = nonce
self.hash = None
def calculate_hash(self) -> str:
return calculate_hash(self)
def as_dict(self) -> dict:
return {
"height": self.height,
"timestamp": self.timestamp,
"previous_hash": self.previous_hash,
"difficulty": self.difficulty,
"nonce": self.nonce,
"data": self.data,
"hash": self.hash,
}