-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.py
More file actions
51 lines (42 loc) · 1.33 KB
/
Block.py
File metadata and controls
51 lines (42 loc) · 1.33 KB
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
import hashlib
class Block:
next = None
nonce = 0
hash = ""
def __init__(self, number, prev_hash, data):
# assign parameters to class variables
self.data = data
self.number = number
self.prev_hash = prev_hash
def mine(self, rule):
"""
Mines the nonce for the current block
:param rule: The rule to search for when mining
:type rule: str
"""
while True:
self.calculate_hash()
if self.hash.startswith(rule):
break
else:
self.nonce += 1
def calculate_hash(self):
"""
Calculates the hash for the Block
:return: Calculated Hash for the block
"""
# create string to be hashed
str_to_hash = str(self.number) + "".join(self.data) + self.prev_hash + str(self.nonce)
# calculate hash
self.hash = hashlib.sha256(str_to_hash.encode()).hexdigest()
return self.hash
def print(self):
"""
Prints the block in a pretty format displaying all necessary information.
"""
print("Number: ", self.number)
print("Nonce: ", self.nonce)
print("Hash: ", self.hash)
print("Prev Hash: ", self.prev_hash)
print("Data: ", self.data)
print("-------------------")