Last active
May 22, 2020 14:10
-
-
Save dvf/3cdf736856a0af22a60bb516bb75514f to your computer and use it in GitHub Desktop.
Step 12: New Endpoints for Consensus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/nodes/register', methods=['POST']) | |
def register_nodes(): | |
values = request.get_json() | |
nodes = values.get('nodes') | |
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': list(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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment