-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.py
122 lines (82 loc) · 2.62 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
from argparse import ArgumentParser
from flask import Flask, jsonify, request
from gcoin.node import Node
from gcoin.miner import Miner
from gcoin.blockchain import BlockChain
from gcoin.transaction import Transaction
app = Flask('g-coin')
app.node = Node()
app.miner = Miner(app.node.id)
app.blockchain = BlockChain()
@app.route('/transaction', methods=['POST'])
def add_transaction():
# data.sender(str)
# data.recipient(str)
# data.amount(int)
data = request.get_json()
transaction = Transaction.init_from_json(data)
try:
next_index = app.blockchain.add_transaction(transaction)
except Exception as e:
return jsonify({'message': str(e)}), 403
response = {'message': f'Transaction will be added to {next_index}th block.'}
return jsonify(response), 201
@app.route('/transaction', methods=['GET'])
def get_pending_transactions():
transactions = [t.dump() for t in app.blockchain.transactions]
return jsonify(transactions), 201
@app.route('/mine', methods=['POST'])
def mine():
"""Mining
Have to make a standalone process
But it's just a prototype
"""
block = app.miner(app.blockchain)
response = {
'message': "New block is mined!",
'block': block.dump()
}
return jsonify(response), 200
@app.route('/chain', methods=['GET'])
def get_full_chain():
response = {
'chain': app.blockchain.dump(),
'length': len(app.blockchain)
}
return jsonify(response), 200
@app.route('/node', methods=['GET'])
def get_all_nodes():
response = {
'nodes': list(app.node.neighbor),
'total': len(app.node)
}
return jsonify(response), 201
@app.route('/node', methods=['POST'])
def add_node():
# data.address(str)
data = request.get_json()
app.node.add(data['address'])
response = {
'message': 'New app.node is added.',
'total': len(app.node)
}
return jsonify(response), 201
@app.route('/chain/valid', methods=['GET'])
def valid_chain():
valid = app.blockchain.valid()
response = {'result': valid}
return jsonify(response), 200
@app.route('/consensus', methods=['POST'])
def consensus():
new_blockchain = app.node.consensus_with_neighbor(app.blockchain)
if new_blockchain:
app.blockchain = new_blockchain
response = {'message': 'Our chain was replaced.'}
else:
response = {'message': 'I\'m King of the World.'}
return jsonify(response), 200
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--port', default=5000, type=int)
args = parser.parse_args()
app.run(port=args.port)