-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.py
88 lines (58 loc) · 1.69 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import hashlib
class Block:
prev_hash = None
nonce = -1
data = None
current_hash = None
def __init__(self, prev_hash, data):
self.prev_hash = prev_hash
self.data = data
def __repr__(self):
return "Block {0} (prev. {1})".format(
self.current_hash,
self.prev_hash
)
@property
def block_string(self):
return "{0}-{1}-{2}".format(
self.prev_hash,
self.data,
self.nonce
)
def hash(self):
return hashlib.sha256(bytes(self.block_string, "utf-8")).hexdigest()
def mine(self, difficulty):
found = False
while not found:
self.nonce += 1
self.current_hash = self.hash()
if self.current_hash[0:difficulty] == "0" * difficulty:
found = True
class Blockchain:
blocks = []
difficulty = 4
def add(self, block):
block.mine(self.difficulty)
self.blocks.append(block)
@property
def last_block(self):
return self.blocks[-1] if len(self.blocks) else None
def build_block(self, data):
if self.last_block:
block = Block(self.last_block.current_hash, data)
self.add(block)
else:
raise Exception("Missing Genesis block")
f = open("test.txt", "w")
chain = Blockchain()
init = "initialisation"
init_Block = Block(None, init)
chain.add(init_Block)
n = 0
while n < 10:
n = n + 1
test = "test"
a_Block = Block(init_Block, test)
chain.add(a_Block)
for b in chain.blocks:
f.write(str(b) + "\n")