-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
proof_of_work.py #12003
Conversation
There was a problem hiding this 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): |
There was a problem hiding this comment.
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:
blockchain/proof_of_work.py
Outdated
self.nonce = 0 | ||
self.hash = self.calculate_hash() | ||
|
||
def calculate_hash(self) -> str: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
print(f"Block mined: {self.hash}") | ||
|
||
class Blockchain: | ||
def __init__(self): |
There was a problem hiding this comment.
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:
blockchain/proof_of_work.py
Outdated
self.chain = [self.create_genesis_block()] | ||
self.difficulty = 4 | ||
|
||
def create_genesis_block(self) -> Block: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
def create_genesis_block(self) -> Block: | ||
return Block(0, "0", "Genesis Block", datetime.now().isoformat()) | ||
|
||
def get_latest_block(self) -> Block: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
def get_latest_block(self) -> Block: | ||
return self.chain[-1] | ||
|
||
def add_block(self, new_block: Block) -> None: |
There was a problem hiding this comment.
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
for more information, see https://pre-commit.ci
There was a problem hiding this 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.
blockchain/proof_of_work.py
Outdated
""" | ||
return Block(0, "0", "Genesis Block", datetime.now().isoformat()) | ||
|
||
def get_latest_block(self) -> Block: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
def get_latest_block(self) -> Block: | ||
return self.chain[-1] | ||
|
||
def add_block(self, new_block: Block) -> None: |
There was a problem hiding this comment.
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
for more information, see https://pre-commit.ci
There was a problem hiding this 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): |
There was a problem hiding this comment.
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:
blockchain/proof_of_work.py
Outdated
self.data = data | ||
self.timestamp = timestamp | ||
|
||
def calculate_hash(self) -> str: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
value = f"{self.index}{self.previous_hash}{self.data}{self.timestamp}" | ||
return hashlib.sha256(value.encode("utf-8")).hexdigest() | ||
|
||
def mine_block(self) -> None: |
There was a problem hiding this comment.
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": |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
""" | ||
return Block(0, "0", "Genesis Block", datetime.now(timezone.utc).isoformat()) | ||
|
||
def get_latest_block(self) -> "Block": |
There was a problem hiding this comment.
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
There was a problem hiding this 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): |
There was a problem hiding this comment.
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:
blockchain/proof_of_work.py
Outdated
self.data = data | ||
self.timestamp = timestamp | ||
|
||
def calculate_hash(self) -> str: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
""" | ||
pass # Implement mining logic here | ||
|
||
def get_block(self) -> str: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
""" | ||
return f"Block {self.index}:\nHash: {self.calculate_hash()}\nData: {self.data}\n" | ||
|
||
def create_genesis_block() -> Block: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
""" | ||
return Block(0, "0", "Genesis Block", datetime.now(timezone.utc).isoformat()) | ||
|
||
def get_latest_block() -> Block: |
There was a problem hiding this comment.
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
for more information, see https://pre-commit.ci
There was a problem hiding this 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): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
blockchain/proof_of_work.py
Outdated
pass | ||
|
||
@staticmethod | ||
def create_genesis_block() -> 'Block': |
There was a problem hiding this comment.
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
for more information, see https://pre-commit.ci
There was a problem hiding this 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): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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": |
There was a problem hiding this comment.
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
Closing require_type_hints PRs to prepare for Hacktoberfest |
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: