-
-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add Ethereum-style timestamp validation and PoW hash check #117
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| import json | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -25,6 +26,17 @@ def validate_block_link_and_hash(previous_block, block): | |
| if block.hash != expected_hash: | ||
| raise ValueError(f"invalid hash {block.hash}") | ||
|
|
||
| target = "0" * (block.difficulty or 1) | ||
| if not block.hash.startswith(target): | ||
| raise ValueError(f"invalid Proof of Work: hash {block.hash} does not satisfy difficulty {block.difficulty}") | ||
|
|
||
| if block.timestamp <= previous_block.timestamp: | ||
| raise ValueError(f"invalid timestamp: {block.timestamp} is not strictly greater than previous block timestamp {previous_block.timestamp}") | ||
|
|
||
| max_allowed_time = int(time.time() * 1000) + 15000 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a future PR, these and many other magic constants spread throughout the code should be moved to a separate config file. Could you create an issue for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. already added in #123 |
||
| if block.timestamp > max_allowed_time: | ||
| raise ValueError(f"invalid timestamp: {block.timestamp} is too far in the future (max allowed: {max_allowed_time})") | ||
|
|
||
|
|
||
| class Blockchain: | ||
| """ | ||
|
|
||
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.
I'm concerned about the "or 1".
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.
If we want to impose a minimum difficulty of 1, I feel that this should be done elsewhere. And then here we wouldn't need the "or 1".
Could you create an issue for this?
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.
will do!