2
2
# Title: Proof of Work Algorithm for Blockchain
3
3
4
4
## Algorithm Statement:
5
- The algorithm implements the Proof of Work (PoW) consensus mechanism used in
6
- blockchain to validate blocks. PoW ensures participants (miners) perform a
7
- computational task to create a valid block and add it to the blockchain. The
5
+ The algorithm implements the Proof of Work (PoW) consensus mechanism used in
6
+ blockchain to validate blocks. PoW ensures participants (miners) perform a
7
+ computational task to create a valid block and add it to the blockchain. The
8
8
difficulty is defined by the number of leading zeros required in the block hash.
9
9
"""
10
10
11
11
import hashlib
12
12
import time
13
13
14
+
14
15
class Block :
15
16
def __init__ (
16
- self ,
17
- index : int ,
18
- previous_hash : str ,
19
- transactions : str ,
20
- timestamp : float ,
21
- difficulty : int
17
+ self ,
18
+ index : int ,
19
+ previous_hash : str ,
20
+ transactions : str ,
21
+ timestamp : float ,
22
+ difficulty : int ,
22
23
) -> None :
23
24
"""
24
25
Initializes a Block object with the specified parameters.
@@ -43,7 +44,7 @@ def compute_hash(self) -> str:
43
44
Generates the hash of the block content.
44
45
Combines index, previous hash, transactions, timestamp, and nonce into a string,
45
46
which is then hashed using SHA-256.
46
-
47
+
47
48
Returns:
48
49
- str: The hash of the block.
49
50
"""
@@ -57,25 +58,28 @@ def mine_block(self) -> None:
57
58
"""
58
59
Performs Proof of Work by adjusting the nonce until a valid hash is found.
59
60
A valid hash has the required number of leading zeros based on the difficulty level.
60
-
61
+
61
62
Returns:
62
63
- None
63
64
"""
64
- target = '0' * self .difficulty # Target hash should start with 'difficulty' zeros
65
- while self .hash [:self .difficulty ] != target :
65
+ target = (
66
+ "0" * self .difficulty
67
+ ) # Target hash should start with 'difficulty' zeros
68
+ while self .hash [: self .difficulty ] != target :
66
69
self .nonce += 1
67
70
self .hash = self .compute_hash ()
68
71
69
72
print (f"Block mined with nonce { self .nonce } , hash: { self .hash } " )
70
73
74
+
71
75
class Blockchain :
72
76
def __init__ (self , difficulty : int ) -> None :
73
77
"""
74
78
Initializes the blockchain with a given difficulty level.
75
79
76
80
Parameters:
77
81
- difficulty (int): The difficulty level for mining blocks in this blockchain.
78
-
82
+
79
83
Returns:
80
84
- None
81
85
"""
@@ -86,7 +90,7 @@ def __init__(self, difficulty: int) -> None:
86
90
def create_genesis_block (self ) -> None :
87
91
"""
88
92
Creates the first block in the blockchain (the Genesis block).
89
-
93
+
90
94
Returns:
91
95
- None
92
96
"""
@@ -100,21 +104,24 @@ def add_block(self, transactions: str) -> None:
100
104
101
105
Parameters:
102
106
- transactions (str): The list of transactions to be added in the new block.
103
-
107
+
104
108
Returns:
105
109
- None
106
110
"""
107
111
previous_block = self .chain [- 1 ]
108
112
new_block = Block (
109
- len (self .chain ), previous_block .hash , transactions , time .time (),
110
- self .difficulty
113
+ len (self .chain ),
114
+ previous_block .hash ,
115
+ transactions ,
116
+ time .time (),
117
+ self .difficulty ,
111
118
)
112
119
new_block .mine_block ()
113
120
self .chain .append (new_block )
114
121
115
122
def is_chain_valid (self ) -> bool :
116
123
"""
117
- Verifies the integrity of the blockchain by ensuring each block's previous
124
+ Verifies the integrity of the blockchain by ensuring each block's previous
118
125
hash matches and that all blocks meet the Proof of Work requirement.
119
126
120
127
Returns:
@@ -134,12 +141,14 @@ def is_chain_valid(self) -> bool:
134
141
135
142
return True
136
143
144
+
137
145
# Test cases
138
146
147
+
139
148
def test_blockchain () -> None :
140
149
"""
141
150
Test cases for the Blockchain proof of work algorithm.
142
-
151
+
143
152
Returns:
144
153
- None
145
154
"""
@@ -154,10 +163,15 @@ def test_blockchain() -> None:
154
163
assert blockchain .is_chain_valid (), "Blockchain should be valid"
155
164
156
165
# Tamper with the blockchain and check validation
157
- blockchain .chain [1 ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
158
- assert not blockchain .is_chain_valid (), "Blockchain should be invalid due to tampering"
166
+ blockchain .chain [
167
+ 1
168
+ ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
169
+ assert (
170
+ not blockchain .is_chain_valid ()
171
+ ), "Blockchain should be invalid due to tampering"
159
172
160
173
print ("All test cases passed." )
161
174
175
+
162
176
if __name__ == "__main__" :
163
177
test_blockchain ()
0 commit comments