forked from JulienMalka/SimpleBlockChain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
227 lines (167 loc) · 5.73 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
import json
from Wallet import *
from flask import Flask, jsonify, request
from uuid import uuid4
import requests
import time
import json
app = Flask(__name__)
from BlockChain import *
from Transaction import *
from Network import *
from Node import *
from Nodes import *
n0 = Node("0.0.0.0", 1337, str(uuid4()).replace('-', ''))
n1 = Node("0.0.0.0", 1338, str(uuid4()).replace('-', ''))
nodes = Nodes([n0, n1])
blockchain = BlockChain()
blockchain.chain.append(blockchain.genesis())
# add into db
# share to other nodes
walletAlice = Wallet(blockchain, "Alice")
walletBob = Wallet(blockchain, "Bob")
@app.route('/valid', methods=['GET'])
def valid():
return jsonify(blockchain.is_valid())
@app.route('/minerate', methods=['POST'])
def minerate():
request_values = request.get_json()
amount = request_values["amount"]
blockchain.set_mining_rate(amount)
return "Changed mining rate"
@app.route('/chain', methods=['GET'])
def chain():
response = {
'chain': blockchain.todict(),
'length': len(blockchain.chain),
}
return jsonify(response), 200
@app.route("/tx/<id>", methods=['GET'])
def tx(id):
print(id)
for block in blockchain.todict():
for i in block.pool:
if i == id:
return id
return "error"
@app.route('/wallet/balance', methods=['GET'])
def balance():
return jsonify({'Balance': str(wallet.balance())})
@app.route('/mine', methods=['GET'])
def mine():
return jsonify(blockchain.mine())
@app.route('/work', methods=['GET'])
def getWork():
#txPool = [tx.toJson() for tx in blockchain.transactions_pool]
return jsonify({
"last_block": str(blockchain.last_block().hash),
"pool": [tx.toJson() for tx in blockchain.transactions_pool],
"nonce": 0,
"timestamp": time.time(),
"string_difficulty": blockchain.string_difficulty(),
"difficulty": blockchain.DIFFICULTY,
"minerate": blockchain.minerate,
})
@app.route('/work', methods=['POST'])
def postWork():
request_values = request.get_json(force=True)
required = ['last_block', 'pool', 'nonce', 'timestamp', 'difficulty', 'minerate']
if request_values == None or not all(k in request_values for k in required):
return 'Missing values', 400
result = blockchain.addBlock(request_values['last_block'], request_values['pool'], request_values['nonce'], request_values['timestamp'])
if result:
return jsonify(blockchain.last_block().compute_hash())
else:
return jsonify("Don't be a jerk")
@app.route('/registerNode', methods=['POST'])
def registerNode():
return request.get_json()
"""
request_values = request.get_json()
address = request_values["address"]
blockchain.addNode(address)
return jsonify(address, " was added to node list")
"""
@app.route('/addNode', methods=['POST'])
def addNode():
request_values = request.get_json()
address = request_values["address"]
blockchain.addNode(address)
r = requests.post(address + '/registerNode', data=jsonify({'address': 'http://localhost:1337'}))
return "l"
"""
response = {
'message': 'New node has been added',
'node_count': len(blockchain.nodes),
'nodes': list(blockchain.nodes),
}
return jsonify(response), 201
"""
@app.route('/sync-chain', methods=['GET'])
def consensus():
def get_neighbour_chains():
neighbour_chains = []
for node_address in blockchain.nodes:
resp = requests.get(node_address + '/chain').json()
chain = resp['chain']
neighbour_chains.append(chain)
return neighbour_chains
neighbour_chains = get_neighbour_chains()
if not neighbour_chains:
return jsonify({'message': 'No neighbour chain is available'})
longest_chain = max(neighbour_chains, key=len) # Get the longest chain
if len(blockchain.chain) >= len(longest_chain): # If our chain is longest, then do nothing
response = {
'message': 'Chain is already up to date',
'chain': blockchain.todict()
}
else: # If our chain isn't longest, then we store the longest chain
blockchain.chain = [blockchain.get_block_object_from_block_data(block) for block in longest_chain]
response = {
'message': 'Chain was replaced',
'chain': blockchain.todict()
}
return jsonify(response)
"""
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
request_values = request.get_json()
print(request)
print(request_values)
required = ['sender', 'recipient', 'amount']
if request_values == None or not all(k in request_values for k in required):
return 'Missing values', 400
transaction = Transaction(request_values["sender"], request_values["recipient"], request_values["amount"])
blockchain.add_transaction(transaction)
return "Your transaction has been added to the pool"
@app.route('/wallet/transaction', methods=['POST'])
def new_transaction_wallet():
request_values = request.get_json()
required = ['recipient', 'amount', 'owner']
if request_values == None or not all(k in request_values for k in required):
return 'Missing values', 400
wallet = Wallet(blockchain, request_values['owner'])
transaction_signed = wallet.new_transaction(request_values['recipient'], request_values['amount'])
return jsonify({
'status': "Your transaction has been added to the pool",
'data': transaction_signed
})
"""
@app.route('/wallet/transaction/signed', methods=['POST'])
def new_transaction_signed():
request_values = request.get_json()
required = ['public_key', 'private_key', 'recipient', 'amount']
if request_values == None or not all(k in request_values for k in required):
return 'Missing values', 400
transaction_signed = walletAlice.new_transaction_signed(
request_values['public_key'],
request_values['private_key'],
request_values['recipient'],
request_values['amount']
)
return jsonify({
'status': "Your transaction has been added to the pool",
'data': transaction_signed
})
if __name__ == '__main__':
app.run(host='localhost', port=1337)