-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain_solution.py
91 lines (75 loc) · 3.11 KB
/
blockchain_solution.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
import hashlib
import datetime
import json
def resetTransactions():
with open('./blockchainscan/src/transactions.json', 'w') as outfile:
outfile.write('[]')
outfile.close()
def resetBlocks():
with open('./blockchainscan/src/blocks.json', 'w') as outfile:
outfile.write('[]')
outfile.close()
class Transaction:
def __init__(self, sender, receiver, amount):
self.sender = sender
self.receiver = receiver
self.amount = amount
self.date = datetime.datetime.now().timestamp()
self.hash = hashlib.sha256(f"{sender}{receiver}{amount}{self.date}".encode()).hexdigest()
def save(self, blockHash):
self.blockHash = blockHash
with open('./blockchainscan/src/transactions.json', 'r') as openfile:
json_object = json.load(openfile)
json_object.append(self.__dict__)
with open("./blockchainscan/src/transactions.json", "w") as outfile:
json.dump(json_object, outfile)
class Block:
def __init__(self, previous_block_hash, transaction_list):
self.previous_block_hash = previous_block_hash
self.transaction_list = transaction_list
self.block_data = f"{' - '.join(map(lambda x: x.hash, transaction_list))} - {previous_block_hash}"
self.block_hash = hashlib.sha256(self.block_data.encode()).hexdigest()
for tx in transaction_list:
tx.save(self.block_hash)
self.date = datetime.datetime.now().timestamp()
with open('./blockchainscan/src/blocks.json', 'r') as openfile:
json_object = json.load(openfile)
dict = {
"hash": self.block_hash,
"previous_block": self.previous_block_hash,
"transactions": list(map(lambda x: x.__dict__, self.transaction_list)),
"date": self.date
}
json_object.append(dict)
with open("./blockchainscan/src/blocks.json", "w") as outfile:
json.dump(json_object, outfile)
class Blockchain:
def __init__(self):
self.chain = []
self.generate_genesis_block()
def generate_genesis_block(self):
self.chain.append(Block("0", []))
print(self.chain[0].block_data)
def create_block_from_transactions(self, transaction_list):
previous_block_hash = self.last_block.block_hash
self.chain.append(Block(previous_block_hash, transaction_list))
def display_chain(self):
for i in range(len(self.chain)):
print(f"Data {i + 1}: {self.chain[i].block_data}")
print(f"Hash {i + 1}: {self.chain[i].block_hash}\n")
@property
def last_block(self):
return self.chain[-1]
resetBlocks()
resetTransactions()
t1 = Transaction('George', 'Joe', 3.1)
t2 = Transaction('Joe', 'Adam', 2.5)
t3 = Transaction('Adam', 'Bob', 1.2)
t4 = Transaction('Bob', 'Charlie', 0.5)
t5 = Transaction('Charlie', 'David', 0.2)
t6 = Transaction('David', 'Eric', 0.1)
myblockchain = Blockchain()
myblockchain.create_block_from_transactions([t1, t2])
myblockchain.create_block_from_transactions([t3, t4])
myblockchain.create_block_from_transactions([t5, t6])
myblockchain.display_chain()