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__ (self , index , previous_hash , transactions , timestamp , difficulty ):
16
17
self .index = index
@@ -36,16 +37,19 @@ def compute_hash(self):
36
37
def mine_block (self ):
37
38
"""
38
39
Performs Proof of Work by adjusting the nonce until a valid hash is found.
39
- A valid hash has the required number of leading zeros based on the difficulty
40
+ A valid hash has the required number of leading zeros based on the difficulty
40
41
level.
41
42
"""
42
- target = '0' * self .difficulty # Target hash should start with 'difficulty' zeros
43
- while self .hash [:self .difficulty ] != target :
43
+ target = (
44
+ "0" * self .difficulty
45
+ ) # Target hash should start with 'difficulty' zeros
46
+ while self .hash [: self .difficulty ] != target :
44
47
self .nonce += 1
45
48
self .hash = self .compute_hash ()
46
49
47
50
print (f"Block mined with nonce { self .nonce } , hash: { self .hash } " )
48
51
52
+
49
53
class Blockchain :
50
54
def __init__ (self , difficulty ):
51
55
self .chain = []
@@ -65,14 +69,19 @@ def add_block(self, transactions):
65
69
Adds a new block to the blockchain after performing Proof of Work.
66
70
"""
67
71
previous_block = self .chain [- 1 ]
68
- new_block = Block (len (self .chain ), previous_block .hash , transactions , time .time (),
69
- self .difficulty )
72
+ new_block = Block (
73
+ len (self .chain ),
74
+ previous_block .hash ,
75
+ transactions ,
76
+ time .time (),
77
+ self .difficulty ,
78
+ )
70
79
new_block .mine_block ()
71
80
self .chain .append (new_block )
72
81
73
82
def is_chain_valid (self ):
74
83
"""
75
- Verifies the integrity of the blockchain by ensuring each block's previous
84
+ Verifies the integrity of the blockchain by ensuring each block's previous
76
85
hash matches and that all blocks meet the Proof of Work requirement.
77
86
"""
78
87
for i in range (1 , len (self .chain )):
@@ -89,8 +98,10 @@ def is_chain_valid(self):
89
98
90
99
return True
91
100
101
+
92
102
# Test cases
93
103
104
+
94
105
def test_blockchain ():
95
106
"""
96
107
Test cases for the Blockchain proof of work algorithm.
@@ -106,11 +117,16 @@ def test_blockchain():
106
117
assert blockchain .is_chain_valid (), "Blockchain should be valid"
107
118
108
119
# Tamper with the blockchain and check validation
109
- blockchain .chain [1 ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
110
- assert not blockchain .is_chain_valid (), "Blockchain should be invalid due to tampering"
120
+ blockchain .chain [
121
+ 1
122
+ ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering
123
+ assert (
124
+ not blockchain .is_chain_valid ()
125
+ ), "Blockchain should be invalid due to tampering"
111
126
112
127
print ("All test cases passed." )
113
128
129
+
114
130
if __name__ == "__main__" :
115
131
test_blockchain ()
116
132
0 commit comments