-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (39 loc) · 1.37 KB
/
main.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
from modules.blockchain import Blockchain
from flask import Flask, jsonify
def main():
# Create Blockchain
blockchain = Blockchain()
app = Flask(__name__)
# routing
@app.route('/blockchain',methods=["GET"])
def blockchains():
response = {
"chain":blockchain.chain,
"length": len(blockchain.chain)
}
return jsonify(response), 200
@app.route('/mining', methods=["GET"])
def mining_block():
previous_block = blockchain.get_previous_block()
nonce = blockchain.proof_of_work(previous_block["nonce"])
previous_hash = blockchain.hash(previous_block)
block = blockchain.create_block(previous_hash, nonce)
response = {
"message": "Mining Suceed",
"index": block["index"],
"timestamp": block["timestamp"],
"nonce": block["nonce"],
"previous_hash": block["previous_hash"]
}
return jsonify(response), 200
@app.route('/is_chains_valid', methods=["GET"])
def is_chains_valid():
is_valid = blockchain.is_chain_valid(blockchain.chain)
if is_valid:
response = {"message": "Blockchain is valid"}
else:
response = {"message": "Problem, Blockchain invalid"}
return jsonify(response), 200
app.run()
if __name__ == "__main__":
main()