-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
41 lines (34 loc) · 979 Bytes
/
models.py
File metadata and controls
41 lines (34 loc) · 979 Bytes
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
# models.py
import hashlib
import json
from dataclasses import dataclass, field
from typing import List
@dataclass
class Transaction:
sender: str
receiver: str
amount: float
@dataclass
class Block:
index: int
timestamp: float
transactions: List[Transaction]
previous_hash: str
nonce: int = 0
hash: str = field(init=False)
def __post_init__(self):
self.hash = self.compute_hash()
def compute_hash(self) -> str:
"""
Compute SHA-256 hash of the block contents
(excluding the current hash field itself).
"""
block_dict = {
"index": self.index,
"timestamp": self.timestamp,
"transactions": [t.__dict__ for t in self.transactions],
"previous_hash": self.previous_hash,
"nonce": self.nonce,
}
block_string = json.dumps(block_dict, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()