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

proof_of_work.py #12003

wants to merge 17 commits into from

Conversation

Khushi-51
Copy link

Add an algorithm

This pull request introduces the Proof of Work algorithm in the blockchain directory. The algorithm is written in Python and demonstrates the process of mining a new block by finding a nonce value that satisfies a specific condition.

Checklist:

  1. I have read CONTRIBUTING.md.
  2. This pull request only changes one algorithm file.
  3. All new functions are properly typed and include doctests.
  4. I have tested my code and ensured it passes automated tests.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 12, 2024
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 12, 2024
@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 12, 2024
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

from datetime import datetime

class Block:
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:

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

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

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

def create_genesis_block(self) -> Block:
return Block(0, "0", "Genesis Block", datetime.now().isoformat())

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

def get_latest_block(self) -> 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

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 12, 2024
@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Oct 12, 2024
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

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

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

def get_latest_block(self) -> 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

@algorithms-keeper algorithms-keeper bot added the require type hints https://docs.python.org/3/library/typing.html label Oct 12, 2024
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



class Block:
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:

self.data = data
self.timestamp = timestamp

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

value = f"{self.index}{self.previous_hash}{self.data}{self.timestamp}"
return hashlib.sha256(value.encode("utf-8")).hexdigest()

def mine_block(self) -> 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

# Implementation of mining logic goes here

@staticmethod
def create_genesis_block() -> "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(timezone.utc).isoformat())

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

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

import hashlib

class Block:
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:

self.data = data
self.timestamp = timestamp

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

value = f"{self.index}{self.previous_hash}{self.data}{self.timestamp}"
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

"""
pass # Implement mining logic here

def get_block(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 get_block

"""
return f"Block {self.index}:\nHash: {self.calculate_hash()}\nData: {self.data}\n"

def create_genesis_block() -> 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(timezone.utc).isoformat())

def get_latest_block() -> 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

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

import hashlib

class Block:
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:

self.data = data
self.timestamp = timestamp

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

pass

@staticmethod
def create_genesis_block() -> '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

@Khushi-51 Khushi-51 marked this pull request as draft October 12, 2024 10:35
@Khushi-51 Khushi-51 marked this pull request as ready for review October 12, 2024 10:35
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.



class Block:
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:

self.data = data
self.timestamp = timestamp

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

pass

@staticmethod
def create_genesis_block() -> "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

@Khushi-51 Khushi-51 marked this pull request as draft October 12, 2024 10:35
@cclauss
Copy link
Member

cclauss commented Oct 22, 2024

Closing require_type_hints PRs to prepare for Hacktoberfest

@cclauss cclauss closed this Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants