-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.py
40 lines (31 loc) · 1.21 KB
/
blockchain.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
38
39
40
from block import Block
from utils import create_logger
logger = create_logger(__name__)
class Blockchain:
def __init__(self, *args, **kwargs):
if kwargs.get('json', False):
# Got a JSON Object
bs = kwargs.get('blocks')
if not bs:
bs = kwargs['blockchain']['blocks']
# logger.info('BS TYPE' + str(type(kwargs['blockchain'])))
# logger.info('BLOCKCHAING CONTRUCTOR JSON' + str(kwargs))
self.blocks = [Block(**b) for b in bs]
else:
# logger.info('BLOCKCHAING CONTRUCTOR NOTJSON' + str(kwargs))
self.blocks = kwargs.get('blocks', [Block(previousHash=(b"0").decode())])
def serialize(self):
return {'blocks': [i.serialize() for i in self.blocks]}
def get_transactions(self):
transactions = []
for block in self.blocks:
transactions = transactions + block.listOfTransactions
return(transactions)
def add_block(self, block):
if(block.previousHash == self.blocks[-1].hash):
self.blocks.append(block)
return(True)
else:
return(False)
def get_last_block(self):
return(self.blocks[-1])