forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
132 lines (98 loc) · 3.54 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
123
124
125
126
127
128
129
130
131
132
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
from uuid import uuid4
from blockchain import Blockchain, MINING_REWARD, MINING_SENDER
# Initialize Flask app
app = Flask(__name__)
CORS(app)
# Define blockchain Variables
blockchain = Blockchain()
node_identifier = str(uuid4()).replace("-", "")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/configure")
def configure():
return render_template("configure.html")
@app.route("/transactions/new", methods=["POST"])
def new_transaction():
values = request.form
required = ["sender_address", "recipient_address", "amount", "signature"]
if not all(k in values for k in required):
return "Missing values", 400
transaction_result = blockchain.submit_transaction(
values["sender_address"],
values["recipient_address"],
values["amount"],
values["signature"],
)
if not transaction_result:
response = {"message": "Invalid Transaction!"}
return jsonify(response), 406
else:
response = {
"message": "Transaction will be added to Block " + str(transaction_result)
}
return jsonify(response), 201
@app.route("/transactions/get", methods=["GET"])
def get_transactions():
transactions = blockchain.transactions
response = {"transactions": transactions}
return jsonify(response), 200
@app.route("/chain", methods=["GET"])
def full_chain():
response = {
"chain": blockchain.chain,
"length": len(blockchain.chain),
}
return jsonify(response), 200
@app.route("/mine", methods=["GET"])
def mine():
# We run the proof of work algorithm to get the next proof...
last_block = blockchain.chain[-1]
nonce = blockchain.proof_of_work()
print(last_block)
# We must receive a reward for finding the proof.
blockchain.submit_transaction(
sender_address=MINING_SENDER,
recipient_address=blockchain.node_id,
value=MINING_REWARD,
signature="",
)
# Forge the new Block by adding it to the chain
previous_hash = blockchain.hash(last_block)
block = blockchain.new_block(nonce, previous_hash)
response = {
"message": "New Block Forged",
"block_number": block["index"],
"transactions": block["transactions"],
"nonce": block["nonce"],
"previous_hash": block["previous_hash"],
}
return jsonify(response), 200
@app.route("/nodes/register", methods=["POST"])
def register_nodes():
values = request.form
nodes = values.get("nodes").replace(" ", "").split(",")
if nodes is None:
return "Error: Please supply a valid list of nodes", 400
for node in nodes:
blockchain.register_node(node)
response = {
"message": "New nodes have been added",
"total_nodes": [node for node in blockchain.nodes],
}
return jsonify(response), 201
@app.route("/nodes/resolve", methods=["GET"])
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
response = {"message": "Our chain was replaced", "new_chain": blockchain.chain}
else:
response = {"message": "Our chain is authoritative", "chain": blockchain.chain}
return jsonify(response), 200
@app.route("/nodes/get", methods=["GET"])
def get_nodes():
nodes = list(blockchain.nodes)
response = {"nodes": nodes}
return jsonify(response), 200