-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.py
executable file
·194 lines (174 loc) · 6.42 KB
/
rest.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
import ast
import datetime
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
import json
import requests
import datetime
from blockchain import Blockchain
from block import Block
from node import Node
from transaction import Transaction
import util
import wallet
my_ip_port = util.get_ip()+':'+'5000'
### JUST A BASIC EXAMPLE OF A REST API WITH FLASK
app = Flask(__name__)
CORS(app)
#.......................................................................................
# Get the total balance of a user
@app.route('/balance', methods=['GET'])
def get_balance():
balance = node.get_balance(my_ip_port)
if balance >= 0:
response = {'balance' : balance}
return jsonify(response), 200
else:
response = {'balance' : 'An error occured'}
return jsonify(response), 400
# Get the total balance of a user
@app.route('/view_last_block', methods=['GET'])
def get_last_block():
balance = node.get_balance(my_ip_port)
last_block = node.chain.get_last_block()
if last_block:
response = {'block' : last_block.to_json()}
return jsonify(response), 200
else:
response = {'block': ''}
return jsonify(response), 204
# Print the node's blockchain (all validated blocks)
@app.route('/blockchain', methods=['GET'])
def get_blockchain():
blockchain = str(node.chain)
response = {'blockchain' : blockchain}
return jsonify(response), 200
# Register a node the the ring of the bootstrap node
@app.route('/register', methods=['POST'])
def register():
data = request.get_json()
if data:
ip_port = data.get('ip_port')
public_key_decoded = data.get('public_key')
public_key = public_key_decoded.encode('utf-8')
print("Trying to register a new node to the network...")
id = node.register_node_to_ring(ip_port, public_key)
# ip_port = request.args.get('ip_port')
# public_key = request.args.get('public_key')
response = {'id': str(id)}
return jsonify(response), 200
else:
response = {'id' : "error"}
return jsonify(response), 400
# Receive all ring data from the bootstrap node
@app.route('/ring', methods=['PUT'])
def update_ring():
data = request.get_json()
print("Just got ring data from bootstrap, adding it to the ring...")
if data:
for n in data.get('ring'):
id = n.get('id')
ip_port = n.get('ip_port')
public_key = n.get('public_key').encode()
node.add_to_ring(id, ip_port, public_key)
# ip_port = request.args.get('ip_port')
# public_key = request.args.get('public_key')
print("Ring update finished, the full ring is:")
node.print_ring()
response = {'status' : "OK"}
return jsonify(response), 200
else:
response = {'status' : "Error"}
return jsonify(response), 400
# Receive a new block from a node
@app.route('/receive_block', methods=['POST'])
def receive_block():
data = request.get_json()
if data:
print('Just received data for a new block, checking if it is valid...')
block_data = ast.literal_eval(data.get('block'))
previousHash = block_data['previousHash']
nonce = block_data['nonce']
timestamp = block_data['timestamp']
new_block = Block(previousHash, nonce, timestamp)
transactions = block_data['transactions']
for t in transactions:
t_data = ast.literal_eval(t)
sender_address = t_data['sender_address']
recipient_address = t_data['recipient_address']
amount = t_data['amount']
transaction_inputs = t_data['transaction_inputs']
transaction_outputs = t_data['transaction_outputs']
new_transaction = Transaction(sender_address, recipient_address, amount,
transaction_inputs, transaction_outputs)
new_block.add_transaction_to_block(new_transaction)
new_block.hash_block()
is_genesis = data.get('genesis')
if is_genesis:
print('Adding genesis block to the blockchain...')
all_utxos = data.get('all_utxos')
node.add_block_to_blockchain(new_block, all_utxos, genesis=True)
print('Updating all utxos...')
response = {'status' : 'Block added to blockchain'}
return jsonify(response, 200)
elif node.validate_block(new_block):
print('Block is valid! Adding to the blockchain...')
all_utxos = data.get('all_utxos')
node.add_block_to_blockchain(new_block, all_utxos)
print('Updating all utxos...')
response = {'status' : 'Block added to blockchain'}
return jsonify(response, 200)
else:
print('Block is not valid!')
response = {'status' : 'Error: block cannot be validated!'}
return jsonify(response, 400)
# Create a transaction and potentially broadcast it
@app.route('/create_transaction', methods=['POST'])
def create_transaction():
data = request.get_json()
if data:
recipient = data.get('recipient')
amount = int(data.get('amount'))
if node.create_transaction(recipient, amount):
response = {'status': 'Successful transaction'}
return jsonify(response), 200
else:
response = {'status': 'Error'}
return jsonify(response), 400
# Receive a transaction from a node and add it to the blockchain
@app.route('/receive_transaction', methods=['POST'])
def receive_transaction():
data = request.get_json()
if data:
print(str(datetime.datetime.now())+ "Just received data for a new transaction, checking to see if it is valid...")
t_data = ast.literal_eval(data.get('transaction'))
sender_address = t_data['sender_address']
recipient_address = t_data['recipient_address']
amount = t_data['amount']
transaction_inputs = t_data['transaction_inputs']
transaction_outputs = t_data['transaction_outputs']
new_transaction = Transaction(sender_address, recipient_address, amount,
transaction_inputs, transaction_outputs)
print(new_transaction)
if node.validate_transaction(new_transaction):
node.add_transaction_to_blockchain(new_transaction)
response = 'Transaction is valid, added it to the blockchain'
return response, 200
else:
resposne = 'Error when validating the transaction'
return response, 400
# Run it once for every node
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
parser.add_argument('-b', '--bootstrap', action='store_true', help='Whether the node is the bootstrap node or not')
parser.add_argument('-n', '--nodes', type=int, help='Number of nodes expected on the network')
args = parser.parse_args()
port = args.port
if (args.bootstrap == True):
node = Node(True, args.nodes)
else:
node = Node(False)
node.register_request()
app.run(host='0.0.0.0', port=port)