Skip to content

Commit

Permalink
Created proof_of_work.py
Browse files Browse the repository at this point in the history
  • Loading branch information
divyaSree-S0 authored Oct 10, 2024
1 parent e14f6d5 commit 374cbc2
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions blockchain/proof_of_work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import hashlib
import time

def proof_of_work(difficulty: int) -> int:

Check failure on line 4 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

blockchain/proof_of_work.py:1:1: I001 Import block is un-sorted or un-formatted
"""
Simulates a Proof of Work mining process.
The miner must find a nonce such that the hash of the nonce starts

Check failure on line 8 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

blockchain/proof_of_work.py:8:71: W291 Trailing whitespace
with a specific number of leading zeros (difficulty).
Args:
difficulty (int): The number of leading zeros required in the hash.
Returns:
int: The nonce value that solves the puzzle.
Example:
>>> result = proof_of_work(2) # Difficulty of 2 should be fast
>>> isinstance(result, int)
True
"""
prefix = '0' * difficulty
nonce = 0
start = time.time()

Check failure on line 25 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

blockchain/proof_of_work.py:25:1: W293 Blank line contains whitespace
while True:
hash_result = hashlib.sha256(f"{nonce}".encode()).hexdigest()
if hash_result.startswith(prefix):
end = time.time()
print(f"Nonce: {nonce}, Hash: {hash_result}, Time: {end - start:.2f}s")
return nonce
nonce += 1

0 comments on commit 374cbc2

Please sign in to comment.