Skip to content

Commit 6f5fdbb

Browse files
committed
chore: fix formatting and add type hints
1 parent f7236cc commit 6f5fdbb

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

blockchain/simple_proof_of_work.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import hashlib
22

3-
def proof_of_work(block_number: int, transactions: str, previous_hash: str,
4-
difficulty: int) -> tuple[int, str]:
3+
4+
def proof_of_work(
5+
block_number: int, transactions: str, previous_hash: str, difficulty: int
6+
) -> tuple[int, str]:
57
"""
6-
Finds a nonce such that the hash of the block
7-
starts with a specfic number of zeros
8+
Finds a nonce such that the hash of the block
9+
starts with a specific number of zeros
810
"""
9-
prefix = '0' * difficulty
11+
prefix = "0" * difficulty
1012
nonce = 0
1113

1214
while True:
1315
# Create a single string representing all block data
1416
text = str(block_number) + transactions + previous_hash + str(nonce)
15-
17+
1618
# Calculate the SHA-256
1719
current_hash = hashlib.sha256(text.encode()).hexdigest()
1820

@@ -22,14 +24,15 @@ def proof_of_work(block_number: int, transactions: str, previous_hash: str,
2224

2325
nonce += 1
2426

27+
2528
if __name__ == "__main__":
2629
# Example usage:
2730
example_tx = "Alice sends 1 BTC to Bob"
2831
prev_h = "00000abcdef1234567890"
29-
diff = 5 # Increase to see get much slower
32+
diff = 5 # Increase to see get much slower
3033

3134
print(f"Mining block... (Difficulty: {diff})")
32-
nonce,hash_found = proof_of_work(1, example_tx, prev_h, diff)
35+
nonce, hash_found = proof_of_work(1, example_tx, prev_h, diff)
3336

3437
print(f"Success! Nonce: {nonce}")
3538
print(f"Hash: {hash_found}")

0 commit comments

Comments
 (0)