|
| 1 | +from flask import Flask |
| 2 | +from flask import request |
| 3 | +import json |
| 4 | +import requests |
| 5 | +import hashlib as hasher |
| 6 | +import datetime as date |
| 7 | +node = Flask(__name__) |
| 8 | + |
| 9 | +class CreateBlock: |
| 10 | + def __init__(self, index, timestamp, data, previous_hash): |
| 11 | + self.index = index |
| 12 | + self.timestamp = timestamp |
| 13 | + self.data = data |
| 14 | + self.previous_hash = previous_hash |
| 15 | + self.hash = self.create_hash() |
| 16 | + |
| 17 | + def create_hash(self): |
| 18 | + sha = hasher.sha256() |
| 19 | + sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)) |
| 20 | + return sha.hexdigest() |
| 21 | + |
| 22 | + |
| 23 | +def create_first_block(): |
| 24 | + return CreateBlock(0, date.datetime.now(), { |
| 25 | + "proof-of-work": 8831*547, |
| 26 | + "transactions": None |
| 27 | + }, "0") |
| 28 | + |
| 29 | + |
| 30 | +miner_address = "frufhi55kefj-akshay-address-ndji45jjg6" |
| 31 | +blockchain = [] |
| 32 | +blockchain.append(create_first_block()) |
| 33 | +this_nodes_transactions = [] |
| 34 | + |
| 35 | +peer_nodes = [] |
| 36 | +mining = True |
| 37 | + |
| 38 | +@node.route('/request_transaction', methods=['POST']) |
| 39 | +def transaction(): |
| 40 | + new_request_data = request.get_json() |
| 41 | + this_nodes_transactions.append(new_request_data) |
| 42 | + print "Transaction" |
| 43 | + print this_nodes_transactions |
| 44 | + print new_request_data |
| 45 | + print "FROM: {}".format(new_request_data['from'].encode('ascii','replace')) |
| 46 | + print "TO: {}".format(new_request_data['to'].encode('ascii','replace')) |
| 47 | + print "AMOUNT: {}\n".format(new_request_data['amount']) |
| 48 | + return "Transaction successful\n" |
| 49 | + |
| 50 | +@node.route('/blocks', methods=['GET']) |
| 51 | +def get_blocks(): |
| 52 | + chain_send = blockchain |
| 53 | + blocklist = "" |
| 54 | + for i in range(len(chain_send)): |
| 55 | + block = chain_send[i] |
| 56 | + block_index = str(block.index) |
| 57 | + block_timestamp = str(block.timestamp) |
| 58 | + block_data = str(block.data) |
| 59 | + block_hash = block.hash |
| 60 | + assembled = json.dumps({ |
| 61 | + "index": block_index, |
| 62 | + "timestamp": block_timestamp, |
| 63 | + "data": block_data, |
| 64 | + "hash": block_hash |
| 65 | + }) |
| 66 | + if blocklist == "": |
| 67 | + blocklist = assembled |
| 68 | + else: |
| 69 | + blocklist += assembled |
| 70 | + return blocklist |
| 71 | + |
| 72 | +def find_new_chains(): |
| 73 | + chains = [] |
| 74 | + for node_url in peer_nodes: |
| 75 | + block = requests.get(node_url + "/blocks").content |
| 76 | + block = json.loads(block) |
| 77 | + chains.append(block) |
| 78 | + return chains |
| 79 | + |
| 80 | +def consensus(): |
| 81 | + chains = find_new_chains() |
| 82 | + longest_chain = blockchain |
| 83 | + for chain in chains: |
| 84 | + if len(longest_chain) < len(chain): |
| 85 | + longest_chain = chain |
| 86 | + blockchain = longest_chain |
| 87 | + |
| 88 | +def proof_of_work(last_proof): |
| 89 | + flag = last_proof + 1 |
| 90 | + proof_of_work_number=8831*547 #choose a combination 2 big prime number so that computational time is more |
| 91 | + while not (flag % proof_of_work_number == 0 and flag % last_proof == 0): |
| 92 | + flag += 1 |
| 93 | + return flag |
| 94 | + |
| 95 | +@node.route('/mine', methods = ['GET']) |
| 96 | +def mine(): |
| 97 | + last_block = blockchain[len(blockchain) - 1] |
| 98 | + last_proof = last_block.data['proof-of-work'] |
| 99 | + proof = proof_of_work(last_proof) |
| 100 | + this_nodes_transactions.append( |
| 101 | + { "from": "System", "to": miner_address, "amount": 1 } |
| 102 | + ) |
| 103 | + |
| 104 | + new_block_data = { |
| 105 | + "proof-of-work": proof, |
| 106 | + "transactions": list(this_nodes_transactions) |
| 107 | + } |
| 108 | + new_block_index = last_block.index + 1 |
| 109 | + new_block_timestamp = this_timestamp = date.datetime.now() |
| 110 | + last_block_hash = last_block.hash |
| 111 | + this_nodes_transactions[:] = [] |
| 112 | + mined_block = CreateBlock( |
| 113 | + new_block_index, |
| 114 | + new_block_timestamp, |
| 115 | + new_block_data, |
| 116 | + last_block_hash |
| 117 | + ) |
| 118 | + blockchain.append(mined_block) |
| 119 | + return json.dumps({ |
| 120 | + "index": new_block_index, |
| 121 | + "timestamp": str(new_block_timestamp), |
| 122 | + "data": new_block_data, |
| 123 | + "hash": last_block_hash |
| 124 | + }) + "\n" |
| 125 | + |
| 126 | +node.run() |
| 127 | + |
0 commit comments