-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.py
More file actions
254 lines (181 loc) · 5.96 KB
/
Copy pathrest.py
File metadata and controls
254 lines (181 loc) · 5.96 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import requests
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
import time,json
from threading import Thread,Lock
from block import Block
from node import Node
import wallet
import transaction
import wallet
import config
app = Flask(__name__)
CORS(app)
node = None
def mining_daemon(node):
print('starting mining_loop...')
node.mining_loop()
#.......................................................................................
# get all transactions in the blockchain
@app.route('/receive_transaction', methods=['POST'])
def get_transactions():
data = request.get_json(force=True)
tx= transaction.Transaction(None,None,None,None,None,data)
node.receive_transaction(tx)
node.add_transaction_to_pool(tx)
return jsonify(status='ok')
@app.route('/create_transaction',methods=['POST'])
def create_transaction():
if node.first_tx_time is None:
node.first_tx_time = time.time()
data = request.get_json(force=True)
#get pubkey from id
receiver_address = None
for key in node.ring:
if node.ring[key]['id'] == data['id']:
receiver_address = key
break
amount = data['amount']
t = node.create_transaction(receiver_address,amount)
if t is None:
return jsonify(status="not enough funds")
node.broadcast_transaction(t)
return jsonify(status='ok')
@app.route('/receive_block',methods=['POST'])
def receive_block():
data = request.get_json(force=True)
#create python object from block
new_block = Block(None,None,data)
#set flag true and point reference to object
node.block_received = True
node.new_block = new_block
return jsonify(status='ok')
@app.route('/chain_length')
def chain_length():
length = len(node.chain)
return jsonify(length = length )
@app.route('/chain')
def chain():
j_chain = [b.block_to_dict() for b in node.chain]
return jsonify(chain=j_chain)
#-------------bootstrap-------------
@app.route('/add_to_ring',methods=['POST'])
def add_to_ring():
data = request.get_json(force=True)
node.add_node(data)
return jsonify(status='ok')
#----------clients-------------
@app.route('/register')
def register():
data = {'pubkey':node.wallet.get_pubaddress(),
'ip':node.ip,
'port':node.port}
data = json.dumps(data)
r = requests.post('http://{0}:{1}/add_to_ring'.format(node.bootstrap_ip,node.bootstrap_port),data=data)
return jsonify(status='ok')
@app.route('/info',methods=['POST'])
def get_info():
data = request.get_json(force=True)
node.ring = data
for key in node.ring:
if key not in node.utxos:
node.utxos[key] = []
return jsonify(status='ok')
@app.route('/receive_genesis',methods=['POST'])
def receive_genesis():
data = request.get_json(force=True)
block = Block(None,None,data)
flag = node.receive_genesis(block)
t = Thread(target=mining_daemon,args=(node,),daemon=True)
t.start()
return jsonify(status=flag)
#--------timing endpoints---------
@app.route('/get_total_time')
def get_total_time():
total_time = node.last_mine_time - node.first_tx_time
return jsonify(time=total_time)
#--------testing endpoints------
@app.route('/get_ring')
def get_ring():
return jsonify(data=node.ring)
@app.route('/get_utxos')
def get_utxos():
temp = {}
for key in node.utxos:
temp[key] = [to.to_dict() for to in node.utxos[key]]
return jsonify(temp)
@app.route('/get_temp_utxos')
def get_temp_utxos():
temp = {}
for key in node.temp_utxos:
temp[key] = [to.to_dict() for to in node.utxos[key]]
return jsonify(temp)
@app.route('/get_pool')
def get_pool():
temp = []
for tx in node.transaction_pool:
temp.append(tx.jsonify_transaction())
return jsonify(temp)
@app.route('/get_from_pool')
def get_from_pool():
node.get_transaction_from_pool()
return jsonify(status="ok")
@app.route('/mine_block')
def mine_block():
flag = node.mine_block()
if flag == -1:
return jsonify(status='block received')
node.chain.append(node.curr_block)
node.broadcast_block()
node.commit_utxos(node.temp_utxos)
node.create_new_block(node.chain[-1])
return jsonify(status='ok')
@app.route('/get_curr_block')
def get_curr_block():
return jsonify(node.curr_block.block_to_dict())
@app.route('/get_new_block')
def get_new_block():
if node.new_block is not None:
return jsonify(node.new_block.block_to_dict())
else:
return jsonify(data='no new block')
@app.route('/process_new_block')
def process_new_block():
node.receive_block()
node.create_new_block(node.chain[-1])
return jsonify(status='ok')
@app.route('/get_flag')
def get_flag():
return jsonify(node.block_received)
@app.route('/resolve')
def resolve():
flag = node.resolve_conflicts()
return jsonify(status=flag)
@app.route('/modify_last_block')
def modify_last_block():
node.chain[-1].hash = '1234'
return jsonify(status='last block changed')
#-----cli------------
@app.route('/last_block')
def last_block():
return jsonify(data=node.chain[-1].block_to_dict())
@app.route('/get_balance')
def get_balance():
mypubkey = node.wallet.get_pubaddress()
data = node.wallet.balance(node.utxos[mypubkey])
return jsonify(data=data)
#---------------------
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', type=int, help='port to listen on')
parser.add_argument('-i', '--ip', type=str, help='ip address')
parser.add_argument('-bp', '--bootstrap_port', type=int, help='port of bootstrap node')
parser.add_argument('-bi', '--bootstrap_ip', type=str, help='ip address of bootstrap node')
args = parser.parse_args()
port = args.port
ip = args.ip
bport = args.bootstrap_port
bip = args.bootstrap_ip
node = Node(ip,port,bip,bport)
app.run(host=ip, port=port,debug=True)