-
Notifications
You must be signed in to change notification settings - Fork 2
/
experiment.py
108 lines (84 loc) · 3.38 KB
/
experiment.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
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
from hashlib import *
import time
zeros = 2
class transaction:
transcount=0
def __init__(self,fromadd,toadd,amount):
self.fromadd=fromadd
self.toadd=toadd
self.amount=amount
self.time = time.strftime('%d/%m/%Y - %H:%M:%S')
self.transobj = {'Sender_Address':self.fromadd,'Recipient_Address':self.toadd,'Amount':self.amount,'Time':self.time}
Blockchain.pendingtrans.append(self.transobj)
transaction.transcount+=1
self.abc()
def abc(self):
if transaction.transcount%4==0:
return Block(time.strftime('%d/%m/%Y - %H:%M:%S'),Blockchain.pendingtrans[:transaction.transcount-1:-1])
class Block:
blockindex = 1
def __init__(self,ts,data,prevhash='0'):
self.index=Block.blockindex
Block.blockindex+=1
self.ts = ts
self.data=data
self.nonce=0
self.prevhash=Blockchain.chain[len(Blockchain.chain)-1].hash
self.hash=self.pow()
Blockchain.chain.append(self)
def calcHash(self):
return sha256((str(str(self.data)+str(self.nonce)+str(self.ts)+self.prevhash)).encode('utf-8')).hexdigest()
def blockData(self):
print ("Block number: ", self.index)
print ("Block timestamp: ", self.ts)
print ("Block data: ", self.data)
print ("Nonce: ",self.nonce)
print ("Block previous hash: ", self.prevhash)
print ("Block hash: ", self.hash,'\n')
def pow(self,zero=zeros):
self.nonce=0
while(self.calcHash()[:zero]!='0'*zero):
self.nonce+=1
return self.calcHash()
class Blockchain:
pendingtrans=[]
chain = []
def __init__(self):
pass
def genesis(self):
return Block(time.strftime('%d/%m/%Y - %H:%M:%S'),'data in genesis')
def addGenesis(self): #addBlock vale method me if length of chain is 0 --> add genesis block se ho sakta tha but...
Blockchain.chain.append(self.genesis()) #baar baar block add karne pe ek if condition bar bar execute hoti faltu me
def load_trans_in_block(self,index):
pass
def addBlock(self,newBlock):
newBlock.prevhash=self.chain[len(self.chain)-1].hash
newBlock.hash=newBlock.pow()
self.chain.append(newBlock)
def valid(self):
for i in range(1,len(Blockchain.chain)):
if Blockchain.chain[i].hash!=Blockchain.chain[i].calcHash():
return False
if Blockchain.chain[i-1].hash!=Blockchain.chain[i].prevhash:
return False
return True
def displayChain(self):
for i in range(len(Blockchain.chain)):
Blockchain.chain[i].blockData()
chain = Blockchain()
chain.addGenesis()
b2 = Block(time.strftime('%d/%m/%Y - %H:%M:%S'),'some data in 2nd block')
b3 = Block(time.strftime('%d/%m/%Y - %H:%M:%S'),'data in 3rd block')
chain.addBlock(b2)
chain.addBlock(b3)
t1 = transaction('234523','23543etr',34)
t2 = transaction('234523eafd','23543etadffdvsafsr',878)
t3 = transaction('2dsfa34523','23dsg543etr',324)
t4 = transaction('2svb34523eafd','23543ecbtafdvsafsr',885)
t5 = transaction('234bd523','235cbv43etr',364)
t6 = transaction('234dg523eafd','23543bcvetafdvsafsr',288)
t7 = transaction('2345gdf23','23543bcetr',344)
t8 = transaction('2345gd23eafd','23543ecbtafd5vsafsr',588)
t9 = transaction('2345gdnm23','23543ecvbtr',345)
t10 = transaction('234523vneafd','2354nc3etafdvsafsr',858)
chain.displayChain()