-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblockchain.py
55 lines (52 loc) · 1.51 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
import time
class Blockchain:
def __init__(self):
self.chain = []
def view(self):
if(self.chain):
block = self.chain[-1]
print("Transactions contained in the last validated block:")
for t in block.listOfTransactions:
t.printMe()
else:
print("Chain Empty")
def add_block_to_chain(self, block):
block.timeAdded = time.time()
for t in block.listOfTransactions:
t.timeAdded = time.time()
self.chain.append(block)
def printMe(self):
print()
print()
print("Blockchain:")
for b in self.chain:
b.printMe()
print()
print()
for b in self.chain:
print(b.index, end=" ")
print()
print()
def isInChain(self, hex):
for b in self.chain:
for t in b.listOfTransactions:
if(t.transaction_id_hex == hex):
return True
return False
def getTransactions(self):
all = []
for b in self.chain:
for t in b.listOfTransactions:
all.append(t.transaction_id_hex)
return all
def isCorrect(self):
inchain = []
index = -1
for b in self.chain:
if(not b.index == index + 1):
return False
for t in b.listOfTransactions:
if(t.transaction_id_hex in inchain):
return False
index += 1
return True