Repository navigation
Expand file tree
/
Copy pathBlockchain.py
More file actions
66 lines (55 loc) · 2.12 KB
/
Copy pathBlockchain.py
File metadata and controls
66 lines (55 loc) · 2.12 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from Block import Block
from BlockchainUtils import BlockChainUtils
from AccountModel import AccountModel
class Blockchain():
def __init__(self):
self.blocks = [Block.genesis()]
self.accountModel = AccountModel()
def addBlock(self, block):
self.executeTransactions(block.transactions)
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):
lastBlockchainBlockHash = BlockChainUtils.hash(
self.blocks[-1].payload()).hexdigest()
if lastBlockchainBlockHash == block.lastHash:
return True
else:
return False
def getCoveredTransactionSet(self, transactions):
CoveredTransactions = []
for transaction in transactions:
if self.transactionCovered(transaction):
CoveredTransactions.append(transaction)
else:
print("Transaction is not covered by sender")
return CoveredTransactions
def transactionCovered(self, transaction):
if transaction.type == 'EXCHANGE':
return True
senderBalance = self.accountModel.getBalance(
transaction.senderpublickey)
if senderBalance >= transaction.amount:
return True
else:
return False
def executeTransactions(self, transactions):
for transaction in transactions:
self.executeTransaction(transaction)
def executeTransaction(self, transaction):
sender = transaction.senderpublickey
receiver = transaction.recieverpublickey
amount = transaction.amount
self.accountModel.updateBalance(sender, -amount)
self.accountModel.updateBalance(receiver, amount)