-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
297 lines (250 loc) · 7.2 KB
/
app.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#app.py
from flask import Flask, request, render_template, redirect
from models.Bee import Bee
from models.Block import Block
from models.Blockchain import Blockchain
from models.Transaction import Transaction
import json
import requests
import sys
import time
app = Flask(__name__)
transaction_format = ["amount", "recipient", "sender", "timestamp"]
peer_format = ["address"]
transactions = []
peers = set()
blockchain = Blockchain()
address = "http://127.0.0.1:" + str(sys.argv[3])
bee = Bee(address, 0)
bee.calculate_balance(blockchain.chain, 1)
@app.route("/", methods=["GET"])
def index():
update_transactions()
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions), 200
@app.route("/add_block", methods=["POST"])
def add_block():
data = json.loads(request.json)
block_data = json.loads(data["block"])
transactions = []
for transaction in block_data["transactions"]:
transactions.append(
Transaction(
amount=transaction["amount"],
recipient=transaction["recipient"],
sender=transaction["sender"],
timestamp=transaction["timestamp"]
)
)
block = Block(
index=block_data["index"],
transactions=transactions,
timestamp=block_data["timestamp"],
previous_hash=block_data["previous_hash"],
proof_type=block_data["proof_type"],
validator=block_data["validator"],
stake=block_data["stake"],
nonce=block_data["nonce"],
signature=block_data["signature"])
valid = blockchain.verify_block(block, blockchain.last_block())
if not valid:
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Invalid block"), 400
blockchain.add_block(block)
propogate_new_block(block)
update_balance()
update_transactions()
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Block successfully added"), 201
@app.route("/add_transaction", methods=["POST"])
def add_transaction():
data = request.form.to_dict()
if not data:
data = json.loads(json.loads(request.data))
if not data.get("timestamp"):
data["timestamp"] = time.time()
for field in transaction_format:
if not data.get(field):
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Invalid transaction data"), 406
transaction = Transaction(
amount=data["amount"],
recipient=data["recipient"],
sender=data["sender"],
timestamp=data["timestamp"]
)
valid = blockchain.verify_transaction(transaction)
if not valid:
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Transaction is not valid"), 403
added = blockchain.add_transaction(transaction)
if not added:
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Transaction has already been recorded"), 200
propogate_new_transaction(transaction)
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Transaction recorded"), 201
@app.route("/consensus", methods=["GET"])
def consensus():
global blockchain
best_chain = None
current_stake = blockchain.stake
current_length = len(blockchain.chain)
for peer in peers:
response = requests.get("{}/get_chain".format(peer))
chain = response.json()["chain"]
peer_blockchain = Blockchain()
peer_blockchain.parse_json(chain)
if not peer_blockchain.verify_chain():
continue
length = len(peer_blockchain.chain)
stake = peer_blockchain.stake
if length > current_length:
current_length = length
current_stake = stake
best_chain = peer_blockchain.chain
elif length == current_length and stake > current_stake:
current_length = lentgh
current_stake = stake
best_chain = peer_blockchain.chain
if best_chain:
blockchain.chain = best_chain
update_transactions()
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Blockchain updated"), 201
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Blockchain already up to date"), 409
@app.route("/get_chain", methods=["GET"])
def get_chain():
chain = []
for block in blockchain.chain:
chain.append(block.to_dict())
return json.dumps({"chain": chain}), 200
@app.route("/mine_block", methods=["POST"])
def mine_block():
data = request.form.to_dict()
new_block = blockchain.mine_block(bee.address, data["proof_type"], int(data["stake"]))
if not new_block:
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Could not mine transactions"), 412
blockchain.add_block(new_block)
propogate_new_block(new_block)
update_balance()
update_transactions()
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Block #{} successfully mined".format(new_block.index)), 201
@app.route("/register_new_peer", methods=["POST"])
def register_new_peer():
peer_data = request.form.to_dict()
for field in peer_format:
if not peer_data.get(field):
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Invalid node data"), 412
peer = peer_data["address"]
peers.add(peer)
return render_template(
"index.html",
address=address,
balance=bee.honeycomb,
peers=peers,
stakes=bee.get_stakes(),
transactions=transactions,
message="Peer succesfully added"), 201
def propogate_new_block(block):
for peer in peers:
url = "{}/add_block".format(peer)
data = json.dumps({"block": json.dumps(block.to_dict(), sort_keys=True)})
requests.post(url, json=data)
def propogate_new_transaction(transaction):
for peer in peers:
url = "{}/add_transaction".format(peer)
data = json.dumps(transaction.__dict__)
requests.post(url, json=data)
def update_balance():
bee.calculate_balance(blockchain.chain, blockchain.last_block().index + 1)
def update_transactions():
global transactions
chain, status_code = get_chain()
chain_data = json.loads(chain)
transactions = []
for block in chain_data["chain"]:
for transaction in block["transactions"]:
transaction["block_index"] = block["index"]
transaction["previous_hash"] = block["previous_hash"]
transaction["validator"] = block["validator"]
transaction["stake"] = block["stake"]
transaction["timestamp"] = time.ctime(transaction["timestamp"])
transactions.append(transaction)
if __name__ == "__main__":
app.run(host="192.168.1.54", debug=True)