-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.py
73 lines (68 loc) · 1.7 KB
/
blockchain.py
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
from flask import *
import datetime
import hashlib
class Block:
blockno=0
data=None
next=None
hash=None
nonce=0
amount=0
previoushash=0x0
timestamp=datetime.datetime.now()
def __init__(self,data,amount):
self.data=data
self.amount=amount
def hash(self):
h=hashlib.sha256()
h.update(
str(self.blockno).encode('utf-8')+
str(self.data).encode('utf-8')+
str(self.amount).encode('utf-8')+
str(self.previoushash).encode('utf-8')+
str(self.nonce).encode('utf-8')+
str(self.timestamp).encode('utf-8')
)
return h.hexdigest()
def __str__(self):
return "DATA: " +str(self.data) +" block hash:" + str(self.hash()) +" block no :" + str(self.blockno)
app = Flask(__name__)
class blockchain:
block=Block("Genesis Block",0)
maxnonce=2**32
diff=10
target=2**(256-diff)
head=block
def add(self,block):
block.previoushash=self.block.hash()
block.blockno=self.block.blockno+1
self.block.next=block
self.block=self.block.next
def mine(self,block):
for n in range(self.maxnonce):
if int(block.hash(),16) <= self.target :
self.add(block)
break
else :
block.nonce+=1
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
task_content = request.form['content']
x=int(request.form['content123'])
krish.mine(Block(str(task_content),(x)))
return redirect('/')
else:
kri=[]
t=krish.head
j=Block('just',9)
j=krish.head
while t!=None:
print(t)
j=t
t=t.next
kri.append(j)
return render_template('index.html',tasks=kri)
if __name__ == "__main__":
krish=blockchain()
app.run(debug=True)