Skip to content

Commit 66cd40e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 91a4325 commit 66cd40e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

blockchain/pow_algorithm.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
# Title: Proof of Work Algorithm for Blockchain
33
44
## 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
88
difficulty is defined by the number of leading zeros required in the block hash.
99
"""
1010

1111
import hashlib
1212
import time
1313

14+
1415
class Block:
1516
def __init__(self, index, previous_hash, transactions, timestamp, difficulty):
1617
self.index = index
@@ -36,16 +37,19 @@ def compute_hash(self):
3637
def mine_block(self):
3738
"""
3839
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
4041
level.
4142
"""
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:
4447
self.nonce += 1
4548
self.hash = self.compute_hash()
4649

4750
print(f"Block mined with nonce {self.nonce}, hash: {self.hash}")
4851

52+
4953
class Blockchain:
5054
def __init__(self, difficulty):
5155
self.chain = []
@@ -65,14 +69,19 @@ def add_block(self, transactions):
6569
Adds a new block to the blockchain after performing Proof of Work.
6670
"""
6771
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+
)
7079
new_block.mine_block()
7180
self.chain.append(new_block)
7281

7382
def is_chain_valid(self):
7483
"""
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
7685
hash matches and that all blocks meet the Proof of Work requirement.
7786
"""
7887
for i in range(1, len(self.chain)):
@@ -89,8 +98,10 @@ def is_chain_valid(self):
8998

9099
return True
91100

101+
92102
# Test cases
93103

104+
94105
def test_blockchain():
95106
"""
96107
Test cases for the Blockchain proof of work algorithm.
@@ -106,11 +117,16 @@ def test_blockchain():
106117
assert blockchain.is_chain_valid(), "Blockchain should be valid"
107118

108119
# 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"
111126

112127
print("All test cases passed.")
113128

129+
114130
if __name__ == "__main__":
115131
test_blockchain()
116132

0 commit comments

Comments
 (0)