-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeAPI.py
44 lines (34 loc) · 1.35 KB
/
NodeAPI.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
from flask_classful import FlaskView, route
from flask import Flask, jsonify, request
from BlockchainUtils import BlockchainUtils
node = None
class NodeAPI(FlaskView):
def __init__(self):
self.app = Flask(__name__)
def start(self, port):
NodeAPI.register(self.app, route_base='/')
self.app.run(host='localhost', port=port)
def injectNode(self, injectedNode):
global node
node = injectedNode
@route('/info', methods=['GET'])
def info(self):
return 'This is a communiction interface to a nodes blockchain', 200
@route('/blockchain', methods=['GET'])
def blockchain(self):
return node.blockchain.toJson(), 200
@route('/transactionPool', methods=['GET'])
def transactionPool(self):
transactions = {}
for ctr, transaction in enumerate(node.transactionPool.transactions):
transactions[ctr] = transaction.toJson()
return jsonify(transactions), 200
@route('/transaction', methods=['POST'])
def transaction(self):
values = request.get_json()
if not 'transaction' in values:
return 'Missing transaction value', 400
transaction = BlockchainUtils.decode(values['transaction'])
node.handleTransaction(transaction)
response = {'message': 'Received transaction'}
return jsonify(response), 201