-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
45 lines (38 loc) · 1.08 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
"""
Flask App
"""
from flask import Flask, jsonify
from uuid import uuid4
from blockchain import Blockchain
app = Flask(__name__)
node_identifier = str(uuid4()).replace('-', '')
blockchain = Blockchain()
@app.route('/mine', methods=['GET'])
def mine():
last_block = blockchain.last_block
nonce = blockchain.proof_of_work(last_block)
blockchain.add_data(
sender="0",
recipient=node_identifier,
amount=1,
)
previous_hash = last_block.hash
block = blockchain.create_block(previous_hash, nonce)
response = {
'message': "New Block Forged",
'index': block.index,
'data': block.data,
'previous_hash': block.previous_hash,
'nonce': block.nonce,
'hash': block.hash,
}
return jsonify(response), 200
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': [block.__dict__ for block in blockchain.chain],
'length': len(blockchain.chain),
}
return jsonify(response), 200
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5001)