-
-
Notifications
You must be signed in to change notification settings - Fork 18
fix chain fundamentals #129
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
Open
SIDDHANTCOOKIE
wants to merge
1
commit into
feat/cross-contract-calls
Choose a base branch
from
fix/architecture-audit-findings
base: feat/cross-contract-calls
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,9 +27,8 @@ 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 int(block.hash, 16) > block.target: | ||
| raise ValueError(f"invalid Proof of Work: hash {block.hash} does not satisfy target {block.target}") | ||
|
|
||
| if block.timestamp <= previous_block.timestamp: | ||
| raise ValueError(f"invalid timestamp: {block.timestamp} is not strictly greater than previous block timestamp {previous_block.timestamp}") | ||
|
|
@@ -89,6 +88,8 @@ def _create_genesis_block(self, genesis_path): | |
|
|
||
| timestamp = config.get("timestamp") | ||
| difficulty = config.get("difficulty") | ||
| if isinstance(difficulty, int) and difficulty <= 256: | ||
| difficulty = (1 << (256 - 4 * difficulty)) - 1 | ||
|
|
||
| self.target_block_time = config.get("target_block_time", 10000) | ||
| self.alpha = config.get("alpha", 0.1) | ||
|
|
@@ -133,20 +134,21 @@ def last_block(self): | |
| def get_total_work(self, chain_list=None): | ||
| """ | ||
| Calculates the cumulative PoW of a chain. | ||
| Work is proportional to 2^difficulty. | ||
| Work is proportional to expected hashes (2^256 // target). | ||
| """ | ||
| if chain_list is None: | ||
| with self._lock: | ||
| chain_list = self.chain | ||
| return sum(2 ** (block.difficulty or 1) for block in chain_list) | ||
| return sum((1 << 256) // (block.target + 1) for block in chain_list) | ||
|
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.
|
||
|
|
||
| def _next_difficulty(self, difficulty, avg_block_time): | ||
| """Advance the EMA difficulty control after a block, returning the new value.""" | ||
| if avg_block_time > self.target_block_time: | ||
| return max(1, difficulty - 1) | ||
| if avg_block_time < self.target_block_time: | ||
| return difficulty + 1 | ||
| return difficulty | ||
| """Advance the EMA difficulty control after a block, returning the new target.""" | ||
| ratio = avg_block_time / self.target_block_time | ||
| # Clamp ratio to prevent extreme swings | ||
| ratio = max(0.25, min(4.0, ratio)) | ||
| new_target = int(difficulty * ratio) | ||
| max_target = (1 << 256) - 1 | ||
| return max(1, min(max_target, new_target)) | ||
|
|
||
| def _apply_block(self, prev_block, block, state, difficulty, avg_block_time): | ||
| """ | ||
|
|
@@ -164,8 +166,8 @@ def _apply_block(self, prev_block, block, state, difficulty, avg_block_time): | |
| status = ValidationStatus.INVALID if "hash" in str(exc) else ValidationStatus.FAILED | ||
| return status, difficulty, avg_block_time | ||
|
|
||
| if block.difficulty != difficulty: | ||
| logger.warning("Block %s rejected: Invalid difficulty. Expected %s, got %s", block.index, difficulty, block.difficulty) | ||
| if block.target != difficulty: | ||
| logger.warning("Block %s rejected: Invalid target. Expected %s, got %s", block.index, difficulty, block.target) | ||
| return ValidationStatus.INVALID, difficulty, avg_block_time | ||
|
|
||
| receipts = [] | ||
|
|
@@ -264,7 +266,7 @@ def resolve_conflicts(self, new_chain_list) -> tuple[bool, list]: | |
| temp_state.chain_id = self.chain_id | ||
| temp_state.restore(self._genesis_state_snapshot) | ||
|
|
||
| temp_difficulty = proposed_chain[0].difficulty | ||
| temp_difficulty = proposed_chain[0].target | ||
| temp_avg_block_time = self.target_block_time | ||
|
|
||
| for i in range(1, len(proposed_chain)): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There seem to be many magic constants in this PR.