-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
116 lines (86 loc) · 3.68 KB
/
node.py
File metadata and controls
116 lines (86 loc) · 3.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from blockchain import Blockchain
from wallet import Wallet
from utils.verification import Verification
class Node:
def __init__(self):
# self.id = str(uuid4())
self.wallet = Wallet()
self.wallet.create_keys()
self.blockchain = Blockchain(self.wallet.public_key)
def get_transaction_data(self):
# sender = input("Input new transaction sender: ")
recipient = input("Input new transaction recipient: ")
amount = float(input("Input new transaction amount: "))
return (recipient, amount)
def get_user_choice(self):
return input("Input your choice: ")
def display_blockchain(self):
for block in self.blockchain.get_chain():
print('Outputting Block')
print(block)
else:
print('-' * 20)
def listen_for_input(self):
wait_for_input = True
while wait_for_input:
print("Choose action: ")
print("1. Add new element into the blockchain")
print("2. Mine new block")
print("3. Print blockchain")
# print("4. Print participants")
print("4. Check transactions validity")
print("5. Create wallet")
print("6. Load wallet")
print("7. Save wallet keys")
# print("h: Manipulate blockchain")
print("q. Quit")
action = self.get_user_choice()
if action == '1':
transaction_data = self.get_transaction_data()
recipient, amount = transaction_data
signature = self.wallet.sign_transaction(self.wallet.public_key, recipient, amount)
if self.blockchain.add_transaction(self.wallet.public_key, recipient, amount, signature):
print("Added transaction!")
else:
print("Transcation failed!")
print(self.blockchain.get_open_transactions())
elif action == '2':
if not self.blockchain.mine_block():
print("Mining failed. Got no wallet.")
elif action == '3':
self.display_blockchain()
# elif action == '4':
# print("Participants: ", participants)
elif action == '4':
if Verification.verify_transactions(self.blockchain.get_open_transactions(), self.blockchain.get_balance):
print("All transactions are valid")
else:
print("There are invalid transactions")
elif action == '5':
self.wallet.create_keys()
self.blockchain = Blockchain(self.wallet.public_key)
elif action == '6':
self.wallet.load_keys()
self.blockchain = Blockchain(self.wallet.public_key)
elif action == '7':
self.wallet.save_keys()
# elif action == 'h':
#
# if len(blockchain) >= 1:
# blockchain[0] = {
# 'previous_hash': "",
# 'index': 0,
# 'transactions': [{'sender': 'Tim', 'recipient': 'Steve', 'amount': 10}]
# }
elif action == 'q':
break
else:
print("Invalid input! Please input correct")
if not Verification.verify_blockchain(self.blockchain.get_chain()):
self.display_blockchain()
print("Blockchain is invalid")
break
print("Balance of {}: {:.2f}".format(self.wallet.public_key, self.blockchain.get_balance()))
print("Done!")
node = Node()
node.listen_for_input()