forked from gauravvjn/blockchain-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
Latest commit
112 lines (80 loc) · 2.87 KB
/
Copy pathapp.py
File metadata and controls
112 lines (80 loc) · 2.87 KB
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
fromuuidimportuuid4
importrequests
fromflaskimportFlask, jsonify, url_for, request
fromblockchainimportBlockChain
app=Flask(__name__)
blockchain=BlockChain()
node_address=uuid4().hex# Unique address for current node
@app.route('/create-transaction', methods=['POST'])
defcreate_transaction():
"""
Input Payload:
{
"sender": "address_1"
"recipient": "address_2",
"amount": 3
}
"""
transaction_data=request.get_json()
index=blockchain.create_new_transaction(**transaction_data)
response= {
'message': 'Transaction has been submitted successfully',
'block_index': index
}
returnjsonify(response), 201
@app.route('/mine', methods=['GET'])
defmine():
block=blockchain.mine_block(node_address)
response= {
'message': 'Successfully Mined the new Block',
'block_data': block
}
returnjsonify(response)
@app.route('/chain', methods=['GET'])
defget_full_chain():
response= {
'chain': blockchain.get_serialized_chain
}
returnjsonify(response)
@app.route('/register-node', methods=['POST'])
defregister_node():
node_data=request.get_json()
blockchain.create_node(node_data.get('address'))
response= {
'message': 'New node has been added',
'node_count': len(blockchain.nodes),
'nodes': list(blockchain.nodes),
}
returnjsonify(response), 201
@app.route('/sync-chain', methods=['GET'])
defconsensus():
defget_neighbour_chains():
neighbour_chains= []
fornode_addressinblockchain.nodes:
resp=requests.get(node_address+url_for('get_full_chain')).json()
chain=resp['chain']
neighbour_chains.append(chain)
returnneighbour_chains
neighbour_chains=get_neighbour_chains()
ifnotneighbour_chains:
returnjsonify({'message': 'No neighbour chain is available'})
longest_chain=max(neighbour_chains, key=len) # Get the longest chain
iflen(blockchain.chain) >=len(longest_chain): # If our chain is longest, then do nothing
response= {
'message': 'Chain is already up to date',
'chain': blockchain.get_serialized_chain
}
else: # If our chain isn't longest, then we store the longest chain
blockchain.chain= [blockchain.get_block_object_from_block_data(block) forblockinlongest_chain]
response= {
'message': 'Chain was replaced',
'chain': blockchain.get_serialized_chain
}
returnjsonify(response)
if__name__=='__main__':
fromargparseimportArgumentParser
parser=ArgumentParser()
parser.add_argument('-H', '--host', default='127.0.0.1')
parser.add_argument('-p', '--port', default=5000, type=int)
args=parser.parse_args()
app.run(host=args.host, port=args.port, debug=True)