Advertisement
Kitood

Untitled

Oct 23rd, 2022
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import datetime
  2. import hashlib
  3. import json
  4. from flask import Flask, jsonify
  5.  
  6. # Building a 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.         self.chain.append(block)
  20.         return block
  21.  
  22.     def get_previous_block(self):
  23.         return self.chain[-1]
  24.  
  25.     def proof_of_work(self, previous_proof):
  26.         new_proof = 1
  27.         check_proof = False
  28.         while check_proof is False:
  29.             hash_operation = hashlib.sha256(str(new_proof**2 - previous_proof**2).encode()).hexdigest()
  30.             if hash_operation[:4] == '0000':
  31.                 check_proof = True
  32.             else:
  33.                 new_proof += 1
  34.         return new_proof
  35.    
  36.     def hash(self, block):
  37.         encoded_block = json.dumps(block, sort_keys = True).encode()
  38.         return hashlib.sha256(encoded_block).hexdigest()
  39.    
  40.     def is_chain_valid(self, chain):
  41.         previous_block = chain[0]
  42.         block_index = 1
  43.         while block_index < len(chain):
  44.             block = chain[block_index]
  45.             if block['previous_hash'] != self.hash(previous_block):
  46.                 return False
  47.             previous_proof = previous_block['proof']
  48.             proof = block['proof']
  49.             hash_operation = hashlib.sha256(str(proof**2 - previous_proof**2).encode()).hexdigest()
  50.             if hash_operation[:4] != '0000':
  51.                 return False
  52.             previous_block = block
  53.             block_index += 1
  54.         return True
  55.  
  56. # Mining our Blockchain
  57.  
  58. # Creating a Web App
  59. app = Flask(__name__)
  60.  
  61. # Creating a Blockchain
  62. blockchain = Blockchain()
  63.  
  64. # Mining a new block
  65. @app.route('/mine_block', methods = ['GET'])
  66. def mine_block():
  67.     previous_block = blockchain.get_previous_block()
  68.     previous_proof = previous_block['proof']
  69.     proof = blockchain.proof_of_work(previous_proof)
  70.     previous_hash = blockchain.hash(previous_block)
  71.     block = blockchain.create_block(proof, previous_hash)
  72.     response = {'message': 'Block Mined Successfully!',
  73.                 'index': block['index'],
  74.                 'timestamp': block['timestamp'],
  75.                 'proof': block['proof'],
  76.                 'previous_hash': block['previous_hash']}
  77.     return jsonify(response), 200
  78. # 200 is the HTTP status code here and 200 is the code for SUCCESS here
  79.  
  80. # Getting the full Blockchain
  81. @app.route('/get_chain', methods = ['GET'])
  82. def get_chain():
  83.     response = {'chain': blockchain.chain,
  84.                 'length': len(blockchain.chain)}
  85.     print('here')
  86.     return jsonify(response), 200
  87.  
  88. # Running the app
  89. app.run(host = '0.0.0.0', port = 5000)
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement