-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode.py
113 lines (91 loc) · 3.3 KB
/
node.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
from flask import Flask, jsonify, request
from blockchain import Blockchain
import dataclasses
import requests
import argparse
app = Flask(__name__)
@app.route("/chain", methods=["GET", "POST"])
def chain():
if request.method == "GET":
response = {
"chain": [dataclasses.asdict(block) for block in local_blockchain.chain],
"length": len(local_blockchain.chain),
}
return jsonify(response), 200
else:
new_chain = request.get_json()
replaced = local_blockchain.receive_chain(new_chain)
if replaced:
response = {
"message": "The chain was replaced",
"chain": local_blockchain.chain,
}
else:
response = {
"message": "No changes to the chain",
"chain": local_blockchain.chain,
}
return jsonify(response), 200
@app.route("/mine", methods=["GET"])
def mine():
local_blockchain.mine()
response = {
"status": "Success",
"index": local_blockchain.current_block().index,
"transactions": [
dataclasses.asdict(t) for t in local_blockchain.current_block().transactions
],
"proof": local_blockchain.current_block().proof,
}
return jsonify(response), 200
@app.route("/transactions/new", methods=["POST"])
def new_transaction():
values = request.get_json()
required = ["sender", "recipient", "amount"]
if not values or not all(k in values for k in required):
return "Missing values", 400
local_blockchain.add_transaction(
values["sender"], values["recipient"], values["amount"]
)
response = {
"message": f"Transaction will be added to block {local_blockchain.next_index()}"
}
return jsonify(response), 201
@app.route("/network", methods=["GET", "POST"])
def network():
if request.method == "GET":
response = {"nodes": list(local_blockchain.players)}
return jsonify(response), 200
else:
value = request.get_json()
if not value or not ("address" in value):
return "Missing values", 400
local_blockchain.add_player(value["address"])
response = {"message": f"Added player address {value['address']}"}
return jsonify(response), 200
@app.route("/broadcast", methods=["GET"])
def broadcast():
successful_broadcasts = []
for a in local_blockchain.players:
try:
r = requests.post(
a + "/chain",
json=[dataclasses.asdict(block) for block in local_blockchain.chain],
)
successful_broadcasts.append(a)
except Exception as e:
print("Failed to send to ", a)
print(e)
response = {"message": "Chain broadcasted", "recipients": successful_broadcasts}
return jsonify(response), 200
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run a node in a blockchain network.")
parser.add_argument("-i", "--identifier", default="")
parser.add_argument("-p", "--port", default="5000")
args = parser.parse_args()
identifier = args.identifier
port_num = args.port
difficulty_number = 2
mining_reward = 10
local_blockchain = Blockchain(identifier, difficulty_number, mining_reward)
app.run(port=port_num)