|  | 
|  | 1 | +import datetime | 
|  | 2 | +import hashlib | 
|  | 3 | +import json | 
|  | 4 | +from flask import Flask,jsonify | 
|  | 5 | + | 
|  | 6 | +# Building the Blockchain | 
|  | 7 | + | 
|  | 8 | +class Blockchain: | 
|  | 9 | +     | 
|  | 10 | +    def __init__(self): | 
|  | 11 | +        self.chain = [] | 
|  | 12 | +        self.create_block(proof = 1, previous_hash='0') | 
|  | 13 | +     | 
|  | 14 | +    def create_block(self,proof,previous_hash): | 
|  | 15 | +        block = {'index': len(self.chain)+1, | 
|  | 16 | +                 'timestamp': str(datetime.datetime.now()), | 
|  | 17 | +                 'proof': proof, | 
|  | 18 | +                 'previous_hash': previous_hash | 
|  | 19 | +                 } | 
|  | 20 | +        self.chain.append(block) | 
|  | 21 | +        return block | 
|  | 22 | +     | 
|  | 23 | +    def get_previous_block(self): | 
|  | 24 | +        return self.chain[-1] | 
|  | 25 | +     | 
|  | 26 | +    def proof_of_work(self,previous_proof): | 
|  | 27 | +        new_proof = 1 | 
|  | 28 | +        check_proof = False | 
|  | 29 | +        while check_proof is False: | 
|  | 30 | +            hash_operation = hashlib.sha256(str(new_proof**2 - previous_proof**2).encode()).hexdigest() | 
|  | 31 | +            if hash_operation[:4]  == '0000': | 
|  | 32 | +                check_proof = True | 
|  | 33 | +            else: | 
|  | 34 | +                new_proof += 1 | 
|  | 35 | +        return new_proof | 
|  | 36 | +     | 
|  | 37 | +    def hash(self,block): | 
|  | 38 | +        encoded_block = json.dumps(block,sort_keys=True).encode() | 
|  | 39 | +        return hashlib.sha256(encoded_block).hexdigest() | 
|  | 40 | +     | 
|  | 41 | +    def is_chain_valid(self,chain): | 
|  | 42 | +        previous_block = chain[0] | 
|  | 43 | +        block_index = 1 | 
|  | 44 | +        while block_index < len(chain): | 
|  | 45 | +            block = chain[block_index] | 
|  | 46 | +            if block['previous_hash'] != self.hash(previous_block): | 
|  | 47 | +                return False | 
|  | 48 | +            previous_proof = previous_block['proof'] | 
|  | 49 | +            proof = block['proof'] | 
|  | 50 | +            hash_operation = hashlib.sha256(str(proof**2 - previous_proof**2).encode()).hexdigest() | 
|  | 51 | +            if hash_operation[:4]  != '0000': | 
|  | 52 | +                return False | 
|  | 53 | +            previous_block = block | 
|  | 54 | +            block_index += 1 | 
|  | 55 | +        return True | 
|  | 56 | +     | 
|  | 57 | +# Mining the Blockchain | 
|  | 58 | + | 
|  | 59 | +app = Flask(__name__) | 
|  | 60 | +app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False | 
|  | 61 | + | 
|  | 62 | +blockchain = Blockchain() | 
|  | 63 | + | 
|  | 64 | +@app.route('/mine_block', methods=['GET']) | 
|  | 65 | +def mine_block(): | 
|  | 66 | +    previous_block = blockchain.get_previous_block() | 
|  | 67 | +    previous_proof = previous_block['proof'] | 
|  | 68 | +    proof = blockchain.proof_of_work(previous_proof) | 
|  | 69 | +    previous_hash = blockchain.hash(previous_block) | 
|  | 70 | +    block = blockchain.create_block(proof, previous_hash) | 
|  | 71 | +    response = {'message': 'Congratulations, you just mined a block', | 
|  | 72 | +                'index': block['index'], | 
|  | 73 | +                 'timestamp': block['timestamp'], | 
|  | 74 | +                'proof': block['proof'], | 
|  | 75 | +                'previous_hash': block['previous_hash']} | 
|  | 76 | +    return jsonify(response), 200 | 
|  | 77 | + | 
|  | 78 | +@app.route('/get_chain', methods=['GET']) | 
|  | 79 | +def get_chain(): | 
|  | 80 | +    response = {'chain':blockchain.chain, | 
|  | 81 | +               'length':len(blockchain.chain)} | 
|  | 82 | +    return jsonify(response), 200 | 
|  | 83 | + | 
|  | 84 | +@app.route('/is_valid', methods=['GET']) | 
|  | 85 | +def is_valid(): | 
|  | 86 | +    is_valid = blockchain.is_chain_valid(blockchain.chain) | 
|  | 87 | +    if is_valid: | 
|  | 88 | +        response = {'message':'The Blockchain is valid'} | 
|  | 89 | +    else: | 
|  | 90 | +        response = {'message':'The Blockchain is invalid'} | 
|  | 91 | +    return jsonify(response),200 | 
|  | 92 | + | 
|  | 93 | +app.run(host='0.0.0.0',port=5000)    | 
|  | 94 | +             | 
|  | 95 | +     | 
|  | 96 | +         | 
|  | 97 | +             | 
|  | 98 | +         | 
|  | 99 | + | 
0 commit comments