- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.py
More file actions
Latest commit
239 lines (190 loc) · 6.91 KB
/
Copy pathnode.py
File metadata and controls
239 lines (190 loc) · 6.91 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
fromflaskimportFlask, jsonify, request, send_from_directory
fromflask_corsimportCORS
fromwalletimportWallet
fromblockchainimportBlockchain
app=Flask(__name__)
CORS(app)
wallet=Wallet()
blockchain=Blockchain(wallet.public_key)
defresp(status_code=200, message='Success', data=None):
"""Helper function to return jsonified response"""
response= { 'message': message }
ifdata:
response.update(data)
returnjsonify(response), status_code
defvalidate_request_data(data, required_fields=None):
"""Helper function to validates the request data.
returns None if data is valid, i.e., data is present and
required_fields, if any, are also present.
Otherwise, returns a jsonified response object
with 400 status and an error message.
Arguments:
:data: Dictionary of request parameters to validate
:required_fields: List of fields that are required to be present
"""
ifnotdata:
returnresp(400, 'No data found')
ifrequired_fields:
ifnotall(fieldindataforfieldinrequired_fields):
returnresp(400, 'Required data is misssing')
returnNone
@app.route('/', methods=['GET'])
defget_ui():
returnsend_from_directory('ui', 'node.html')
@app.route('/network', methods=['GET'])
defget_network_ui():
returnsend_from_directory('ui', 'network.html')
@app.route('/transactions', methods=['GET'])
defget_transactions():
transactions=blockchain.get_open_transactions()
dict_transactions= [tx.to_dict() fortxintransactions]
returnjsonify(dict_transactions), 200
@app.route('/transaction', methods=['POST'])
defadd_transaction():
"""TODO: Validate input parameters"""
ifwallet.public_key==None:
returnresp(400, 'No wallet set up')
data=request.get_json()
required_fields= ['recipient', 'amount']
invalid_data_response=validate_request_data(data, required_fields)
ifinvalid_data_response:
returninvalid_data_response
recipient=data['recipient']
amount=data['amount']
signature=wallet.sign_transactions(wallet.public_key, recipient, amount)
success=blockchain.add_transaction(sender=wallet.public_key, recipient=recipient, amount=amount, signature=signature)
ifsuccess:
returnresp(201, 'Transaction added successfully.', {
'transaction': blockchain.get_open_transactions()[-1].to_dict(),
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Failed to create a transaction')
@app.route('/broadcast-transaction', methods=['POST'])
defbroadcast_transaction():
"""Receive broadcast of newly added transactions from other nodes"""
data=request.get_json()
required_fields= ['sender', 'recipient', 'amount', 'signature']
invalid_data_response=validate_request_data(data, required_fields)
ifinvalid_data_response:
returninvalid_data_response
success=blockchain.add_transaction(sender=data['sender'], recipient=data['recipient'], amount=data['amount'], signature=data['signature'], is_receiving=True)
ifsuccess:
returnresp(201, 'Transaction added successfully.', {
'transaction': blockchain.get_open_transactions()[-1].to_dict(),
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Failed to create a transaction.')
@app.route('/broadcast-block', methods=['POST'])
defbroadcast_block():
"""Receive broadcast of newly mined blocks from other nodes"""
data=request.get_json()
required_fields= ['block']
invalid_data_response=validate_request_data(data, required_fields)
ifinvalid_data_response:
returninvalid_data_response
block=data['block']
last_local_block_index=blockchain.get_last_block()['index']
ifblock['index'] ==last_local_block_index+1:
ifblockchain.add_block(block):
returnresp(201, 'Block added successfully.')
else:
returnresp(409, 'Block seems invalid.') # Invalid data (conflict)
elifblock['index'] >last_local_block_index:
# TODO: Our chain is shorter.. We need to fix it!
blockchain.resolve_conflicts=True
returnresp(200, 'Bockchain seems to differ from local blockchain.') # Not an error (Current node needs to fix the issue)
else:
# Our blockchain is more recent (has longer chain)
returnresp(409, 'Bockchain seems to be shorter. Block not added.') # Invalid data (conflict)
@app.route('/resolve-conflicts', methods=['POST'])
defresolve_conflicts():
replaced=blockchain.resolve()
ifreplaced:
returnresp(200, 'Chain was replaced.')
else:
returnresp(200, 'Local chain kept.')
@app.route('/wallet', methods=['POST'])
defcreate_keys():
wallet.create_keys()
ifwallet.save_keys():
globalblockchain
blockchain=Blockchain(wallet.public_key)
returnresp(201, '', {
'public_key': wallet.public_key,
'private_key': wallet.private_key,
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Failed to save the wallet keys.')
@app.route('/wallet', methods=['GET'])
defload_keys():
ifwallet.load_keys():
globalblockchain
blockchain=Blockchain(wallet.public_key)
returnresp(200, '', {
'public_key': wallet.public_key,
'private_key': wallet.private_key,
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Failed to load the wallet keys.')
@app.route('/balance', methods=['GET'])
defget_balance():
balance=blockchain.get_balance()
ifbalance!=None:
returnresp(200, 'Balance fetched successfully.', {
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Failed to load balance.', {
'is_wallet_setup': wallet.public_key!=None
})
@app.route('/mine', methods=['POST'])
defmine():
ifblockchain.resolve_conflicts:
returnresp(409, 'Block not added due to conflicts.')
block=blockchain.mine_block()
ifblock!=None:
returnresp(201, 'Mining successful. Block added.', {
'added_block': block.to_dict(),
'balance': blockchain.get_balance()
})
else:
returnresp(500, 'Mining failed. Block not added.', {
'is_wallet_setup': wallet.public_key!=None
})
@app.route('/chain', methods=['GET'])
defget_chain():
returnjsonify(blockchain.get_chain_dict()), 200
@app.route('/nodes', methods=['GET'])
defget_nodes():
returnresp(200, '', {
'peer_nodes': blockchain.get_peer_nodes()
})
@app.route('/node', methods=['POST'])
defadd_node():
data=request.get_json()
required_fields= ['node']
invalid_data_response=validate_request_data(data, required_fields)
ifinvalid_data_response:
returninvalid_data_response
blockchain.add_peer_node(data['node'])
returnresp(201, 'Node added successfully.', {
'peer_nodes': blockchain.get_peer_nodes()
})
@app.route('/node/<node_url>', methods=['DELETE'])
defremove_node(node_url):
ifnotnode_url:
returnresp(400, 'No node data found')
blockchain.remove_peer_node(node_url)
returnresp(200, 'Node removed successfully.', {
'peer_nodes': blockchain.get_peer_nodes()
})
if__name__=='__main__':
fromargparseimportArgumentParser
parser=ArgumentParser()
parser.add_argument('-p', '--port', type=int, default=4000)
args=parser.parse_args()
app.run(host='127.0.0.1', port=args.port)