Spaces:
Runtime error
Runtime error
Commit
·
6b57175
1
Parent(s):
56521d6
Upload 3 files
Browse files- Blockchain.jpg +0 -0
- blockchain.py +143 -0
- requirements.txt +1 -0
Blockchain.jpg
ADDED
![]() |
blockchain.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Blockchain.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1wfXS6MTMX2J77EP5X1qFiKQzdo_eh511
|
8 |
+
"""
|
9 |
+
|
10 |
+
import datetime
|
11 |
+
import hashlib
|
12 |
+
import json
|
13 |
+
|
14 |
+
class Blockchain:
|
15 |
+
|
16 |
+
def __init__(self):
|
17 |
+
self.chain = []
|
18 |
+
self.create_block(proof=1, previous_hash='0')
|
19 |
+
|
20 |
+
def create_block(self, proof, previous_hash):
|
21 |
+
block = {'index': len(self.chain) + 1,
|
22 |
+
'timestamp': str(datetime.datetime.now()),
|
23 |
+
'proof': proof,
|
24 |
+
'previous_hash': previous_hash}
|
25 |
+
self.chain.append(block)
|
26 |
+
return block
|
27 |
+
|
28 |
+
def print_previous_block(self):
|
29 |
+
return self.chain[-1]
|
30 |
+
|
31 |
+
def proof_of_work(self, previous_proof):
|
32 |
+
new_proof = 1
|
33 |
+
check_proof = False
|
34 |
+
|
35 |
+
while check_proof is False:
|
36 |
+
hash_operation = hashlib.sha256(
|
37 |
+
str(new_proof**2 - previous_proof**2).encode()).hexdigest()
|
38 |
+
if hash_operation[:5] == '00000':
|
39 |
+
check_proof = True
|
40 |
+
else:
|
41 |
+
new_proof += 1
|
42 |
+
|
43 |
+
return new_proof
|
44 |
+
|
45 |
+
def hash(self, block):
|
46 |
+
encoded_block = json.dumps(block, sort_keys=True).encode()
|
47 |
+
return hashlib.sha256(encoded_block).hexdigest()
|
48 |
+
|
49 |
+
def chain_valid(self, chain):
|
50 |
+
previous_block = chain[0]
|
51 |
+
block_index = 1
|
52 |
+
|
53 |
+
while block_index < len(chain):
|
54 |
+
block = chain[block_index]
|
55 |
+
if block['previous_hash'] != self.hash(previous_block):
|
56 |
+
return False
|
57 |
+
|
58 |
+
previous_proof = previous_block['proof']
|
59 |
+
proof = block['proof']
|
60 |
+
hash_operation = hashlib.sha256(
|
61 |
+
str(proof**2 - previous_proof**2).encode()).hexdigest()
|
62 |
+
|
63 |
+
if hash_operation[:5] != '00000':
|
64 |
+
return False
|
65 |
+
previous_block = block
|
66 |
+
block_index += 1
|
67 |
+
|
68 |
+
return True
|
69 |
+
|
70 |
+
def create_chain():
|
71 |
+
global blockchain
|
72 |
+
blockchain = Blockchain()
|
73 |
+
return "Chain Instantiated"
|
74 |
+
|
75 |
+
def del_chain():
|
76 |
+
global blockchain
|
77 |
+
del blockchain
|
78 |
+
return "Chain Deleted"
|
79 |
+
|
80 |
+
def mine_block():
|
81 |
+
previous_block = blockchain.print_previous_block()
|
82 |
+
previous_proof = previous_block['proof']
|
83 |
+
proof = blockchain.proof_of_work(previous_proof)
|
84 |
+
previous_hash = blockchain.hash(previous_block)
|
85 |
+
block = blockchain.create_block(proof, previous_hash)
|
86 |
+
|
87 |
+
response = {'message': 'A block is MINED',
|
88 |
+
'index': block['index'],
|
89 |
+
'timestamp': block['timestamp'],
|
90 |
+
'proof': block['proof'],
|
91 |
+
'previous_hash': block['previous_hash']}
|
92 |
+
|
93 |
+
return response
|
94 |
+
|
95 |
+
mine_block()
|
96 |
+
|
97 |
+
def hack_block(index,value):
|
98 |
+
index=int(index.split(" ")[1])-1
|
99 |
+
blockchain.chain[index]['proof']=value
|
100 |
+
return f"block modified at index:{index+1}"
|
101 |
+
|
102 |
+
def display_chain():
|
103 |
+
try:
|
104 |
+
response = {'chain': blockchain.chain,'length': len(blockchain.chain)}
|
105 |
+
|
106 |
+
except:
|
107 |
+
response = "No chain found"
|
108 |
+
return response
|
109 |
+
|
110 |
+
def valid():
|
111 |
+
valid = blockchain.chain_valid(blockchain.chain)
|
112 |
+
|
113 |
+
if valid:
|
114 |
+
response = {'message': 'The Blockchain is valid.'}
|
115 |
+
else:
|
116 |
+
response = {'message': 'The Blockchain is not valid.'}
|
117 |
+
return response
|
118 |
+
|
119 |
+
import gradio as gr
|
120 |
+
|
121 |
+
with gr.Blocks(title='Blockchain Simulator') as demo:
|
122 |
+
gr.Image(value="./Blockchain.jpg",show_label=False,shape=(1500,300))
|
123 |
+
with gr.Row():
|
124 |
+
with gr.Column(scale=3):
|
125 |
+
display = gr.Textbox(label="Blockchain Output",lines=5)
|
126 |
+
dblock = gr.Button("Display/Refresh Blockchain")
|
127 |
+
with gr.Column(scale=2):
|
128 |
+
cblock = gr.Button("Instantiate Blockchain")
|
129 |
+
mblock = gr.Button("Mine a Block")
|
130 |
+
vblock = gr.Button("Validate Blockchain")
|
131 |
+
delblock = gr.Button("Delete Blockchain")
|
132 |
+
with gr.Column(scale=2):
|
133 |
+
index = gr.Radio(choices=["index 1", "index 2", "index 3"],label="Choose an Index to hack")
|
134 |
+
value = gr.Textbox(label="New Value", placeholder = "Enter a Value")
|
135 |
+
hblock = gr.Button("Hack a Block")
|
136 |
+
dblock.click(fn=display_chain, inputs=[], outputs=[display],show_progress=True)
|
137 |
+
cblock.click(fn=create_chain, inputs=[], outputs=[display],show_progress=True)
|
138 |
+
mblock.click(fn=mine_block, inputs=[], outputs=[display],show_progress=True)
|
139 |
+
vblock.click(fn=valid, inputs=[], outputs=[display],show_progress=True)
|
140 |
+
delblock.click(fn=del_chain, inputs=[], outputs=[display],show_progress=True)
|
141 |
+
hblock.click(fn=hack_block, inputs=[index,value], outputs=[display],show_progress=True)
|
142 |
+
|
143 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio
|