-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (69 loc) · 3.03 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''
Main.py - driver file to be ran as main user interaction. This will handle the
interaction in the form of a menu option and do various tasks based on the selection.
This file also handles the state and maintenance of the blockchain.
'''
from BlockchainClass import Blockchain
from BlockClass import Block
import hashlib
def create_transaction():
''' Create the transaction object and handle inputs into the object.
returns: dictionary of transaction object
'''
transaction_object = {'to': None, 'from': None, 'amt': None}
transaction_object['to'] = input('To? ').strip()
transaction_object['from'] = input('From? ').strip()
transaction_object['amt'] = input('Amount? ').strip()
return transaction_object
def main():
print('*******************************')
print(' *Entering the Blockchain ')
print('*******************************')
blockchain = Blockchain()
# State information is outside user interface loop
current_block = Block(index=blockchain.current_length + 1, previous_hash=blockchain.genesis_block.hash)
prev_block = blockchain.get_last_block()
# User interface loop operates until they're finished
while True:
print('\n\nMENU:---------------------------------')
print("\t '.' - exit the program")
print("\t 'T/t' - Create a new transaction")
print("\t 'mine' - mine the current block")
print("\t 'chain' - display the entire chain")
print("\t 'block' - display the current block")
print("\t 'V/v' - check blockchain is valid")
print('--------------------------------------')
option = input('Please select a menu option: ').strip().lower()
# Handle each case
if option == '.':
print('*Exiting the Blockchain*')
break
elif option == 't':
transaction_object = create_transaction()
try:
current_block.add_transaction(transaction_object)
except Exception as err:
print(err)
elif option == 'mine':
# Mine current block and add to ledger
try:
result = current_block.mine()
print(result)
blockchain.append_block(current_block)
# adjust pointers and state of chain
prev_block._next_block = current_block
prev_block = current_block
current_block = Block(index=blockchain.current_length + 1, previous_hash=prev_block.hash)
except Exception as err:
print(err)
elif option == 'chain':
blockchain.display()
elif option == 'block':
current_block.display()
elif option == 'v':
result = blockchain.check_validity()
print(result)
else:
print('Invalid Argument: ', option, '\n please try again')
if __name__ == "__main__":
main()