-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
57 lines (35 loc) · 1.22 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
from flask import Flask, jsonify
from Blockchain import Blockchain
app = Flask(__name__)
blockchain = Blockchain()
@app.route("/mine_block", methods=["GET"])
def mine_block():
previous_block = blockchain.get_previous_block()
previous_proof = previous_block['proof']
proof = blockchain.proof_of_work(previous_proof)
previous_hash = blockchain.hash(previous_block)
block = blockchain.create_blockchain(proof, previous_hash)
response = {
'message': 'Block mined!',
'index': block['index'],
'timestamp': block['timestamp'],
'proof': block['proof'],
'previous_hash': block['previous_hash']
}
return jsonify(response), 200
@app.route("/get_chain", methods=["GET"])
def get_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain)
}
return jsonify(response), 200
@app.route("/is_blockchain_valid",methods=["GET"])
def is_blockchain_valid():
valid = blockchain.is_chain_valid(blockchain.chain)
if valid:
response = {'message': 'Yes, Blockchain is valid'}
else:
response = {'message': 'No , Blockchain is invalid'}
return jsonify(response), 200
app.run(host="0.0.0.0", port=5000)