-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.py
75 lines (61 loc) · 2.28 KB
/
peer.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
#import libraries
import json
from Blockchain import Blockchain
from Block import Block
from flask import Flask, request
#app object
app = Flask(__name__)
#blockchain object
blockchain = Blockchain()
#peers list
peers = []
@app.route("/new_transaction", methods=["POST"])
# new transaction added to the block. When user selects to submit new request
def new_transaction():
file_data = request.get_json() #get json response
required_fields = ["user", "v_file", "file_data", "file_size"]
#if any of the fields is missing dont append and throw the message
for field in required_fields:
if not file_data.get(field):
return "Transaction does not have valid fields!", 404
#else append it to pending transaction
blockchain.add_pending(file_data)
return "Success", 201
#gets the whole chain to user if not already displayed
@app.route("/chain", methods=["GET"])
def get_chain():
# consensus()
chain = []
#create a new chain from our blockchain
for block in blockchain.chain:
chain.append(block.__dict__)
#print chain len
print("Chain Len: {0}".format(len(chain)))
return json.dumps({"length" : len(chain), "chain" : chain})
@app.route("/mine", methods=["GET"])
#Mines pending tx blocks and call mine method in blockchain
def mine_uncofirmed_transactions():
result = blockchain.mine()
if result:
return "Block #{0} mined successfully.".format(result)
else:
return '<h2>No pending transactions to mine.</h2><a href="https://localhost:9000/"><button>Go Home</button></a>'
@app.route("/pending_tx")
# Queries uncofirmed transactions
def get_pending_tx():
return json.dumps(blockchain.pending)
@app.route("/add_block", methods=["POST"])
# Adds a block mined by user to the chain
def validate_and_add_block():
block_data = request.get_json() #get the json response
#create a new block incl its hash
block = Block(block_data["index"],block_data["transactions"],block_data["prev_hash"])
hashl = block_data["hash"]
#append the new block
added = blockchain.add_block(block, hashl)
#if not added succesfully
if not added:
return "The Block was discarded by the node.", 400
return "The block was added to the chain.", 201
#run the app
app.run(port=8800, debug=True)