Skip to content
Closed
Changes from 8 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
49 changes: 49 additions & 0 deletions blockchain/proof_of_work.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import hashlib
import time

Check failure on line 2 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

blockchain/proof_of_work.py:2:8: F401 `time` imported but unused
from datetime import datetime

class Block:

Check failure on line 5 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
def __init__(self, index: int, previous_hash: str, data: str, timestamp: str):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

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

value = (str(self.index) + str(self.previous_hash) +

Check failure on line 15 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

blockchain/proof_of_work.py:15:61: W291 Trailing whitespace
str(self.data) + str(self.timestamp) + str(self.nonce))
return hashlib.sha256(value.encode('utf-8')).hexdigest()

def mine_block(self, difficulty: int) -> 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 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

target = '0' * difficulty
while self.hash[:difficulty] != target:
self.nonce += 1
self.hash = self.calculate_hash()
print(f"Block mined: {self.hash}")

class Blockchain:
def __init__(self):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> 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

return Block(0, "0", "Genesis Block", datetime.now().isoformat())

Check failure on line 32 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (DTZ005)

blockchain/proof_of_work.py:32: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

new_block.previous_hash = self.get_latest_block().hash
new_block.mine_block(self.difficulty)
self.chain.append(new_block)

# Example usage
blockchain = Blockchain()
blockchain.add_block(Block(1, "", "Block 1 Data", datetime.now().isoformat()))

Check failure on line 44 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (DTZ005)

blockchain/proof_of_work.py:44:51: DTZ005 `datetime.datetime.now()` called without a `tz` argument
blockchain.add_block(Block(2, "", "Block 2 Data", datetime.now().isoformat()))

Check failure on line 45 in blockchain/proof_of_work.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (DTZ005)

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

# Display the blockchain
for block in blockchain.chain:
print(f"Block {block.index} - Hash: {block.hash} - Data: {block.data}")
Loading