-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
41 lines (33 loc) · 1.33 KB
/
main.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
41
from transaction import Transaction
from bank import Bank
from hash import encrypt
from chain import Chain
import json, sys
if __name__ == '__main__':
bank = Bank()
transactions = [Transaction().generate(10) for i in range(30)]
accounts = bank.getAccounts()
# Create new chain and init with firt block base on bank accounts
chain = Chain()
chain.makeBlock([accounts])
maxTransactionsPerBlock = 5
blockTransactions = []
# Let's generate block for all valid transactions !!
for transaction in transactions:
if bank.isValidTransaction(transaction.get()):
blockTransactions.append(transaction.get())
bank.updateAccounts(transaction.get())
else:
print("ignored transaction !")
print(json.dumps(transaction.get()))
print(json.dumps(bank.getAccounts()))
sys.stdout.flush()
continue
# Make a new block and reset list
if len(blockTransactions) == maxTransactionsPerBlock:
chain.makeBlock(blockTransactions)
blockTransactions = []
if len(blockTransactions) > 0:
print("Block is not full, getting " + str(len(blockTransactions)) + " transactions")
print(json.dumps(chain.get(), indent=4, sort_keys=True))
print(json.dumps(bank.getAccounts(), indent=4, sort_keys=True))