-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.py
More file actions
35 lines (27 loc) · 865 Bytes
/
Blockchain.py
File metadata and controls
35 lines (27 loc) · 865 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
from BlockchainUtils import BlockchainUtils
from Block import Block
class Blockchain:
def __init__(self):
self.blocks = [Block.genesis()]
def addBlock(self, block):
self.blocks.append(block)
def toJson(self):
data = {}
jsonBlocks = []
for block in self.blocks:
jsonBlocks.append(block.toJson())
data['blocks'] = jsonBlocks
return data
def blockCountValid(self, block):
if self.blocks[-1].blockCount == block.blockCount - 1:
return True
else:
return False
def lastBlockHashValid(self, block):
latestBlockchainBlockHash = BlockchainUtils.hash(
self.blocks[-1].payload()
).hexdigest()
if latestBlockchainBlockHash == block.lastHash:
return True
else:
return False