forked from aniquetahir/BlockChainSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
40 lines (31 loc) · 987 Bytes
/
block.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
34
35
36
37
from transaction import Transaction
from typing import List, Dict
from uuid import uuid4
class Block:
def __init__(self, prev: str, transactions: List[Transaction]):
self.prev = prev
self.transactions = transactions
self.reward = []
self.id = str(uuid4())
def to_dict(self):
return {
'hash': self.id,
'transactions': [x.to_dict() for x in self.transactions],
'prev': self.prev
}
def transaction_exists(self, tr: Transaction) -> bool:
for block_transaction in self.transactions:
if block_transaction.id == tr.id:
return True
return False
def __len__(self):
return len(self.transactions)
def add(self, chain, rewards: List[Dict]):
"""
:param chain:
:param rewards:
:return:
"""
self.reward = rewards
chain.add(self)
# TODO basic sanity check for block rewards