-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypcoin_node_5001.py
More file actions
179 lines (150 loc) · 5.74 KB
/
crypcoin_node_5001.py
File metadata and controls
179 lines (150 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import datetime
from flask import Flask, jsonify, request
import hashlib
import json
import requests
from uuid import uuid4
from urllib.parse import urlparse
# 1. create blockchain class
class Blockchain():
def __init__(self):
self.chain = []
self.transactions = []
self.create_block(prev_hash='0', proof=1)
self.nodes = set()
def create_block(self, prev_hash, proof):
block = {'timestamp': str(datetime.datetime.now()),
'prev_hash': prev_hash,
'proof': proof,
'index': len(self.chain)+1,
'transactions': self.transactions}
self.transactions = []
self.chain.append(block)
return block
def get_prev_block(self):
return self.chain[-1]
def get_proof(self, prev_proof):
#prev_proof = self.get_prev_block['proof']
found = False
proof = 1
while(not found):
# any non symmetric function with fair enough complexity
hash_found = hashlib.sha256(
str(proof**2-prev_proof**2).encode()).hexdigest()
if(hash_found[:4] == '0000'):
found = True
else:
proof = proof+1
return proof
def hash(self, block):
# convert to json and sort to keys to get uniform hashing
encoded_block = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(encoded_block).hexdigest()
def sanity_check(self):
# 2 checks needed
# 1. prev hash matches with current hash stored
prev = self.chain[0]
index = 1
while(index < len(self.chain)):
prev_hash = self.chain[index]['prev_hash']
if(prev_hash != self.hash(prev)):
return False
# 2. proof of work is correct
proof = self.chain[index]['proof']
prev_proof = prev['proof']
hash_found = hashlib.sha256(
str(proof**2-prev_proof**2).encode()).hexdigest()
if(hash_found[:4] != '0000'):
found = False
prev = self.chain[index]
index += 1
return True
# transactions to make it cryptocurrency
def add_transaction(self, amount, sender, receiver):
self.transactions.append(
{'sender': sender, 'receiver': receiver, 'amount': amount})
prev_block = self.get_prev_block()
return prev_block['index']+1
def add_node(self, address):
parsed_url = urlparse(address)
self.nodes.add(parsed_url.netloc)
# consesus to make all nodes have same chain
def replace_nodes(self):
network = self.nodes
longest_chain = None
max_len = len(self.chain)
for node in network:
response = requests.get(f'http://{node}/list_chain')
if response.status_code == 200:
length = response.json()['length']
chain = response.json()['chain']
if length > max_len and self.sanity_check(chain):
max_len = length
longest_chain = chain
if longest_chain:
self.chain = longest_chain
return True
return False
# 2. Actual working and mining
blockchain = Blockchain()
# Webapp
app = Flask(__name__)
# Create node addresses
node_addresses = str(uuid4()).replace('-', '')
@app.route('/mine', methods=['GET'])
def mine():
prev_block = blockchain.get_prev_block()
proof = blockchain.get_proof(prev_block['proof'])
block = blockchain.create_block(blockchain.hash(prev_block), proof)
blockchain.add_transaction(
sender=node_addresses, receiver='Alex', amount=10)
response = {'message': ' You did it! New block created',
'index': block['index'], 'timestamp': block['timestamp'], 'proof': block['proof'], 'prev_hash': block['prev_hash'], 'transactions': block['transactions']}
return jsonify(response), 200
@app.route('/list_chain', methods=['GET'])
def list_chain():
response = {'chain': blockchain.chain, 'length': len(blockchain.chain)}
return jsonify(response), 200
@app.route('/sanity', methods=['GET'])
def sanity():
valid = blockchain.sanity_check()
if(valid):
response = {'message': 'Blockchain is valid'}
else:
response = {'message': 'Sorry. Something went wrong!'}
return jsonify(response), 200
@app.route('/add_transaction', methods=['POST'])
def add_transaction():
json = request.get_json()
transaction_keys = {'sender', 'receiver', 'amount'}
if not all(key in json for key in transaction_keys):
return 'Missing keys', 400
index = blockchain.add_transaction(
json['sender'], json['receiver'], json['amount'])
response = {'message': f'Transaction will be added to {index}'}
return response, 201
# 3. decentralizing blockchain
# Create new nodes
@app.route('/connect_node', methods=['POST'])
def connect_node():
json = request.get_json()
nodes = json.get('nodes')
if nodes is None:
return 'No node found', 400
for node in nodes:
blockchain.add_node(node)
response = {'message': 'All nodes added successfully. Chain contains following nodes',
'total_nodes': list(blockchain.nodes)}
return jsonify(response), 201
# Replace all chains with most recent one
@app.route('/update_chain', methods=['GET'])
def update_chain():
is_chain_replaced = blockchain.update_chain()
if(is_chain_replaced):
response = {
'message': 'Nodes had different chians. Hence, Chain is replaced', 'new_chain': blockchain.chain}
else:
response = {'message': 'Chain is already updated',
'actual_chain': blockchain.chain}
return jsonify(response), 200
app.run(host='0.0.0.0', port=5001)