22
22
import hashlib
23
23
import time
24
24
25
+
25
26
class Block :
26
27
def __init__ (self , index , previous_hash , transactions , timestamp , difficulty ):
27
28
self .index = index
@@ -35,7 +36,7 @@ def __init__(self, index, previous_hash, transactions, timestamp, difficulty):
35
36
def compute_hash (self ):
36
37
"""
37
38
Generates the hash of the block content.
38
- Combines index, previous hash, transactions, timestamp, and nonce into a string,
39
+ Combines index, previous hash, transactions, timestamp, and nonce into a string,
39
40
which is then hashed using SHA-256.
40
41
"""
41
42
block_string = f"{ self .index } { self .previous_hash } { self .transactions } { self .timestamp } { self .nonce } "
@@ -46,13 +47,16 @@ def mine_block(self):
46
47
Performs Proof of Work by adjusting the nonce until a valid hash is found.
47
48
A valid hash has the required number of leading zeros based on the difficulty level.
48
49
"""
49
- target = '0' * self .difficulty # Target hash should start with 'difficulty' number of zeros
50
- while self .hash [:self .difficulty ] != target :
50
+ target = (
51
+ "0" * self .difficulty
52
+ ) # Target hash should start with 'difficulty' number of zeros
53
+ while self .hash [: self .difficulty ] != target :
51
54
self .nonce += 1
52
55
self .hash = self .compute_hash ()
53
56
54
57
print (f"Block mined with nonce { self .nonce } , hash: { self .hash } " )
55
58
59
+
56
60
class Blockchain :
57
61
def __init__ (self , difficulty ):
58
62
self .chain = []
@@ -72,13 +76,19 @@ def add_block(self, transactions):
72
76
Adds a new block to the blockchain after performing Proof of Work.
73
77
"""
74
78
previous_block = self .chain [- 1 ]
75
- new_block = Block (len (self .chain ), previous_block .hash , transactions , time .time (), self .difficulty )
79
+ new_block = Block (
80
+ len (self .chain ),
81
+ previous_block .hash ,
82
+ transactions ,
83
+ time .time (),
84
+ self .difficulty ,
85
+ )
76
86
new_block .mine_block ()
77
87
self .chain .append (new_block )
78
88
79
89
def is_chain_valid (self ):
80
90
"""
81
- Verifies the integrity of the blockchain by ensuring each block's previous hash matches
91
+ Verifies the integrity of the blockchain by ensuring each block's previous hash matches
82
92
and that all blocks meet the Proof of Work requirement.
83
93
"""
84
94
for i in range (1 , len (self .chain )):
@@ -95,8 +105,10 @@ def is_chain_valid(self):
95
105
96
106
return True
97
107
108
+
98
109
# Test cases
99
110
111
+
100
112
def test_blockchain ():
101
113
"""
102
114
Test cases for the Blockchain proof of work algorithm.
@@ -112,11 +124,16 @@ def test_blockchain():
112
124
assert blockchain .is_chain_valid () == True , "Blockchain should be valid"
113
125
114
126
# Tamper with the blockchain and check validation
115
- blockchain .chain [1 ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
116
- assert blockchain .is_chain_valid () == False , "Blockchain should be invalid due to tampering"
127
+ blockchain .chain [
128
+ 1
129
+ ].transactions = "Transaction 1: Alice pays Bob 50 BTC" # Tampering the transaction
130
+ assert (
131
+ blockchain .is_chain_valid () == False
132
+ ), "Blockchain should be invalid due to tampering"
117
133
118
134
print ("All test cases passed." )
119
135
136
+
120
137
if __name__ == "__main__" :
121
138
test_blockchain ()
122
139
0 commit comments