-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchainOO.py
194 lines (167 loc) · 4.61 KB
/
blockchainOO.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import hashlib
from time import time
import json
class Block:
content= {}
isFull= False
def __init__(self, index,previousHash, blockSize=3):
self.size=blockSize
Block.content= {
'index':index,
'hash': '',
'transactions':[],
'nonce':0,
'previousHash': previousHash,
'timestamp': time()
}
def addTransaction(self):
"""
adds a transaction to the block
"""
transaction= {
'sender': input('Expéditeur: '),
'receiver': input('Destinataire: '),
'amount': int(input('Montant: '))
}
Block.content.transactions.append(transaction)
def addTransaction(self,sender, receiver, amount):
"""
Overloaded Transaction method
"""
transaction= {
'sender': sender,
'receiver': receiver,
'amount': amount
}
Block.content['transactions'].append(transaction)
def removeTransaction(self, idx):
"""
Removes the transaction at idx returns the removed transaction
"""
t= Block.content['transactions']
del Block.content['transactions']
return t
def editTransaction(self, idx, sender, receiver, amount):
"""
edits a transaction
"""
Block.content['transactions'][idx]['sender']= sender
Block.content['transactions'][idx]['receiver']= receiver
Block.content['transactions'][idx]['amount']= amount
def isFull(self):
return len( Block.content['transactions']) == self.size
BLOCK_SIZE=3
BLOCKCHAIN=[]
MENU_STRING="1- Continuer\n2- Afficher la blockhain\n0- Quittter"
pendingBlock={}
makeDecision=1
currentIndex=0
def init():
global currentIndex
global pendingBlock
block_0= {
'index':currentIndex,
'hash': '',
'transactions':[],
'nonce':0,
'previousHash': 'NULL',
'timestamp': time()
}
block_0['hash']= hashlib.sha256(str(block_0).encode()).hexdigest()
blockchain= [block_0]
pendingBlock= block_0
currentIndex+=1
print("Genesis block initialised!")
newPendingBlock()
init()
def newTransaction():
"""
Adds a new transaction to the pending block
"""
global pendingBlock
global makeDecision
transaction= {
'sender': input('Expéditeur: '),
'receiver': input('Destinataire: '),
'amount': int(input('Montant: '))
}
pendingBlock['transactions'].append(transaction)
print("Transaction ajouté!")
if( len(pendingBlock['transactions']) == BLOCK_SIZE ):
print("Block complet\n")
mining()
makeDecision= int(input(MENU_STRING))
return
def newPendingBlock():
"""
New block
"""
global currentIndex
global pendingBlock
block= {
'index':currentIndex,
'hash': '',
'transactions':[],
'nonce':0,
'previousHash': '',
'timestamp': time()
}
block['previousHash']= pendingBlock['hash']
pendingBlock= block
currentIndex+=1
print("\n\nNouveau block initialisé!")
return
def mining():
"""
Mining Funciton
"""
print("Minage.... :P")
global BLOCKCHAIN
validHash= computeHash(pendingBlock)
pendingBlock['hash']= validHash
BLOCKCHAIN.append(pendingBlock)
print("Minage Terminé\nBlock ajouté à la chaine:")
printty(pendingBlock)
newPendingBlock()
return
def computeHash(block):
"""
Hash Function
"""
b= str(block)
while 1:
data= str(block)+ str(block['nonce'])
block['nonce']+=1
h= hashlib.sha256(data.encode()).hexdigest()
if(h[0]=='0' and h[1]=='0' and h[2]=='0'):
print("Hash valide trouvé! avec un nonce de "+ str(block['nonce']))
break
return h
def displayBlockchain():
"""
Displayin func
"""
printty(BLOCKCHAIN)
return
def editBlock():
idx= int(input("Entrez l'index du bloc à modifer: "))
block= BLOCKCHAIN[idx]
printty(block)
idx= int(input("Entrez l'index de la transaction à modifer: "))
printty(block['transactions'][idx])
block['transactions'][idx]= {
'sender': input('Expéditeur: '),
'receiver': input('Destinataire: '),
'amount': int(input('Montant: '))
}
def printty(obj):
print (json.dumps(obj, indent=4))
while 1:
if(makeDecision==1):
newTransaction()
elif (makeDecision==2):
displayBlockchain()
makeDecision= int(input(MENU_STRING))
else:
print("Why so soon ??\nbye!")
break