Skip to content

Instantly share code, notes, and snippets.

@dvf
Last active May 22, 2020 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dvf/3cdf736856a0af22a60bb516bb75514f to your computer and use it in GitHub Desktop.
Save dvf/3cdf736856a0af22a60bb516bb75514f to your computer and use it in GitHub Desktop.
Step 12: New Endpoints for Consensus
@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