-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
173 lines (143 loc) · 3.76 KB
/
api.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
from flask import Flask,request,render_template,redirect,url_for
from core import blockchain
from argparse import ArgumentParser
import requests
import time
import json
currentPort=0
uCombs={}
winners={}
winner=0
rcomb={}
app = Flask(__name__)
bc = blockchain()
@app.route('/chain',methods=['GET'])
def displayChain():
return bc.displayChain()
@app.route('/peers/get',methods=['GET'])
def getPeers():
return {
'peers': bc.user.peers
}
@app.route('/postComb',methods=["POST"])
def combinations():
global uCombs
data = request.get_json(force=True)
uCombs.update(data)
return 'Combinations have been updated.'
@app.route('/getRSPcomb',methods=["POST"])
def getRSPcomb():
global rcomb
data = request.get_json(force=True)
rcomb = data["RSPcomb"]
return 'Common RSP combination received'
@app.route('/winner',methods=["POST"])
def decideWinner():
global winners
global winner
maxwins=0
data = request.get_json(force=True)
dk = list(data.keys())
if dk[0] in winners.keys():
winners[dk[0]]+=1
else:
winners[dk[0]]=1
for key,value in winners.items():
if winners[key]>maxwins:
winner=key
return 'Winner decided'
@app.route('/mine/addBlock',methods=["POST"])
def addblk():
data = request.get_json(force=True)
bc.addBlock(data["data"])
return 'Block added.'
@app.route('/mine',methods=['POST'])
def mine():
global uCombs
global rcomb
global winner
coin = bc.user.calcCoin('i7',16)
comb = bc.user.genComb(coin)
uCombs[str(currentPort)]=comb
Sum=0
N = len(bc.user.peers)
for peer in bc.user.peers:
if peer!=currentPort:
r=requests.post('http://127.0.0.1:{}/postComb'.format(peer),json={str(currentPort):comb})
print(r.text)
time.sleep(5)
for i in uCombs.keys():
if uCombs[i][0]=='S':
Sum+=1
elif uCombs[i][0]=='P':
Sum+=2
delegator = Sum%N
print("delegator:",delegator)
if bc.user.peers[delegator]==currentPort:
print(uCombs)
rcomb = bc.user.genComb(len(max(uCombs,key=len)))
for peer in bc.user.peers:
if peer!=currentPort:
r=requests.post('http://127.0.0.1:{}/getRSPcomb'.format(peer),json={"RSPcomb":rcomb})
print(r.text)
else:
time.sleep(5)
wins={}
for port in uCombs.keys():
wins[port]=bc.RSP(rcomb,comb)
if wins[port] > winner:
winner=port
for peer in bc.user.peers:
if peer!=currentPort:
r=requests.post('http://127.0.0.1:{}/winner'.format(peer),json={str(winner):1})
print(r.text)
time.sleep(5)
blockdata = request.get_json()
bc.addBlock(blockdata["data"])
for peer in bc.user.peers:
if peer!=currentPort:
r=requests.post('http://127.0.0.1:{}/mine/addBlock'.format(peer),json=blockdata)
print(r.text)
uCombs={}
winners={}
rcomb={}
if int(winner)==currentPort:
print('You are the winner. Block Added.')
winner=0
return 'Consensus complete.'
@app.route('/validitycheck',methods=["GET"])
def validityCheck():
if blockchain.isValid(bc.chain):
return 'Chain is Valid.'
else:
return 'Chain is Invalid'
if __name__ == '__main__':
parser = ArgumentParser(description='Blockchain Command Line')
parser.add_argument(
'-ho',
'--host',
default='127.0.0.1',
help='host to run service')
parser.add_argument(
'-p',
'--port',
default=5000,
type=int,
help='port to run service')
parser.add_argument(
'-n',
'--neighbors',
nargs='+',
required=True,
type=int,
help='neighboring ports <Required>')
args=parser.parse_args()
for i in args.neighbors:
if bc.user.addPeer(i):
print("Peer added.")
else:
print("Invalid peer. Will not be added.")
bc.user.addPeer(args.port)
currentPort=args.port
print("currentPort:",currentPort)
app.run(host=args.host,port=args.port,debug=True)