Skip to content

proof_of_work.py #12003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

## Blockchain
* [Diophantine Equation](blockchain/diophantine_equation.py)
* [Proof Of Work](blockchain/proof_of_work.py)

## Boolean Algebra
* [And Gate](boolean_algebra/and_gate.py)
Expand Down
60 changes: 60 additions & 0 deletions blockchain/proof_of_work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
This module implements the Proof of Work algorithm for a blockchain.
It includes classes for Block and Blockchain, demonstrating how to mine a block
by finding a nonce that satisfies a given difficulty level.
"""

from datetime import datetime
import hashlib

class Block:

Check failure on line 10 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

blockchain/proof_of_work.py:7:1: I001 Import block is un-sorted or un-formatted
def __init__(self, index: int, previous_hash: str, data: str, timestamp: str) -> None:

Check failure on line 11 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

blockchain/proof_of_work.py:11:89: E501 Line too long (90 > 88)
self.index = index
self.previous_hash = previous_hash
self.data = data
self.timestamp = timestamp
self.nonce = 0
self.hash = self.calculate_hash()

def calculate_hash(self) -> str:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function calculate_hash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function calculate_hash

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function calculate_hash

"""
Calculates the hash of the block using SHA-256.

>>> block = Block(1, '0', 'data', '2024-10-12T00:00:00')
>>> block.calculate_hash()
'...' # Expected hash value here
"""
value = str(self.index) + self.previous_hash + str(self.data) + str(self.timestamp) + str(self.nonce)

Check failure on line 27 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

blockchain/proof_of_work.py:27:89: E501 Line too long (109 > 88)
return hashlib.sha256(value.encode('utf-8')).hexdigest()

def mine_block(self, difficulty: int) -> None:

Check failure on line 30 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (ARG002)

blockchain/proof_of_work.py:30:26: ARG002 Unused method argument: `difficulty`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function mine_block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function mine_block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function mine_block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function mine_block

"""
Mines a block by finding a nonce that produces a hash
starting with a specified number of zeros.

>>> block = Block(1, '0', 'data', '2024-10-12T00:00:00')
>>> block.mine_block(4) # Method should modify the nonce
"""
# Implementation of mining logic
print(f"Block mined: {self.hash}")

class Blockchain:
def __init__(self) -> None:
self.chain = [self.create_genesis_block()]
self.difficulty = 4

def create_genesis_block(self) -> Block:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function create_genesis_block

"""
Creates the genesis block for the blockchain.

>>> blockchain = Blockchain()
>>> blockchain.create_genesis_block().data
'Genesis Block'
"""
return Block(0, "0", "Genesis Block", datetime.now().isoformat())

Check failure on line 54 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (DTZ005)

blockchain/proof_of_work.py:54:47: DTZ005 `datetime.datetime.now()` called without a `tz` argument

def get_latest_block(self) -> Block:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function get_latest_block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function get_latest_block

return self.chain[-1]

def add_block(self, new_block: Block) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function add_block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file blockchain/proof_of_work.py, please provide doctest for the function add_block

self.chain.append(new_block)
Loading