123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- from flask import Flask, jsonify, request
- from uuid import uuid4
- from blockchain import Blockchain
- app = Flask(__name__)
- # Generate a unique address for this node
- node_identifier = str(uuid4()).replace('-', '')
- # Instantiate the blockchain
- blockchain = Blockchain()
- @app.route('/mine', methods=['GET'])
- def mine():
- # We run the proof of work algorithm to get the next proof...
- last_block = blockchain.last_block
- last_proof = last_block['proof']
- proof = blockchain.proof_of_work(last_proof)
- # We must receive a reward for finding the proof.
- # The sender is "0" to signify that this node has mined a new coin.
- blockchain.new_transaction(
- sender="0",
- recipient=node_identifier,
- amount=1,
- )
- # Forge the new Block by adding it to the chain
- previous_hash = blockchain.hash(last_block)
- block = blockchain.new_block(proof, previous_hash)
- response = {
- 'message': "New Block Forged",
- 'index': block['index'],
- 'transactions': block['transactions'],
- 'proof': block['proof'],
- 'previous_hash': block['previous_hash'],
- }
- return jsonify(response), 200
- @app.route('/transactions/new', methods=['POST'])
- def new_transaction():
- values = request.get_json()
- # Check that the required fields are in the POST'ed data
- required = ['sender', 'recipient', 'amount']
- if not all(k in values for k in required):
- return 'Missing values', 400
- # Create a new Transaction
- index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
- response = {'message': f'Transaction will be added to Block {index}'}
- return jsonify(response), 201
- @app.route('/chain', methods=['GET'])
- def full_chain():
- response = {
- 'chain': blockchain.chain,
- 'length': len(blockchain.chain),
- }
- return jsonify(response), 200
- @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
- @app.route('/chain/remove', methods=['POST'])
- def remove_block():
- values = request.get_json()
- if not all(k in values for k in ['index']):
- return 'Missing values', 400
- index = int(values['index'])
- blockchain.remove_block(index)
- response = {'message': f'Block {index} has been removed.',
- 'chain': blockchain.chain}
- return jsonify(response), 200
- if __name__ == '__main__':
- from argparse import ArgumentParser
- parser = ArgumentParser()
- parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
- args = parser.parse_args()
- port = args.port
- app.run(host='0.0.0.0', port=port)
|