-
Notifications
You must be signed in to change notification settings - Fork 649
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
Improve header chain extension performance #1891
Merged
lithp
merged 1 commit into
ethereum:master
from
lithp:lithp/improve-chain-extension-performance
Dec 10, 2019
Merged
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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import functools | ||
from typing import Iterable, Tuple | ||
from typing import cast, Iterable, Tuple | ||
|
||
import rlp | ||
|
||
|
@@ -83,11 +83,15 @@ def get_canonical_head(self) -> BlockHeaderAPI: | |
|
||
@classmethod | ||
def _get_canonical_head(cls, db: DatabaseAPI) -> BlockHeaderAPI: | ||
canonical_head_hash = cls._get_canonical_head_hash(db) | ||
return cls._get_block_header_by_hash(db, canonical_head_hash) | ||
|
||
@classmethod | ||
def _get_canonical_head_hash(cls, db: DatabaseAPI) -> Hash32: | ||
try: | ||
canonical_head_hash = db[SchemaV1.make_canonical_head_hash_lookup_key()] | ||
return Hash32(db[SchemaV1.make_canonical_head_hash_lookup_key()]) | ||
except KeyError: | ||
raise CanonicalHeadNotFound("No canonical head set for this chain") | ||
return cls._get_block_header_by_hash(db, Hash32(canonical_head_hash)) | ||
|
||
# | ||
# Header API | ||
|
@@ -173,7 +177,7 @@ def _persist_checkpoint_header( | |
) | ||
previous_score = score - header.difficulty | ||
cls._set_hash_scores_to_db(db, header, previous_score) | ||
cls._set_as_canonical_chain_head(db, header.hash, header.parent_hash) | ||
cls._set_as_canonical_chain_head(db, header, header.parent_hash) | ||
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. Yay that this change has so straightforward, and didn't require a new header lookup anywhere! |
||
|
||
@classmethod | ||
def _persist_header_chain( | ||
|
@@ -226,21 +230,21 @@ def _persist_header_chain( | |
score = cls._set_hash_scores_to_db(db, curr_chain_head, score) | ||
|
||
try: | ||
previous_canonical_head = cls._get_canonical_head(db).hash | ||
previous_canonical_head = cls._get_canonical_head_hash(db) | ||
head_score = cls._get_score(db, previous_canonical_head) | ||
except CanonicalHeadNotFound: | ||
return cls._set_as_canonical_chain_head(db, curr_chain_head.hash, genesis_parent_hash) | ||
return cls._set_as_canonical_chain_head(db, curr_chain_head, genesis_parent_hash) | ||
|
||
if score > head_score: | ||
return cls._set_as_canonical_chain_head(db, curr_chain_head.hash, genesis_parent_hash) | ||
return cls._set_as_canonical_chain_head(db, curr_chain_head, genesis_parent_hash) | ||
|
||
return tuple(), tuple() | ||
|
||
@classmethod | ||
def _set_as_canonical_chain_head( | ||
cls, | ||
db: DatabaseAPI, | ||
block_hash: Hash32, | ||
header: BlockHeaderAPI, | ||
genesis_parent_hash: Hash32, | ||
) -> Tuple[Tuple[BlockHeaderAPI, ...], Tuple[BlockHeaderAPI, ...]]: | ||
""" | ||
|
@@ -251,14 +255,41 @@ def _set_as_canonical_chain_head( | |
are no longer in the canonical chain | ||
""" | ||
try: | ||
header = cls._get_block_header_by_hash(db, block_hash) | ||
except HeaderNotFound: | ||
raise ValueError( | ||
f"Cannot use unknown block hash as canonical head: {block_hash}" | ||
current_canonical_head = cls._get_canonical_head_hash(db) | ||
except CanonicalHeadNotFound: | ||
current_canonical_head = None | ||
|
||
new_canonical_headers: Tuple[BlockHeaderAPI, ...] | ||
old_canonical_headers: Tuple[BlockHeaderAPI, ...] | ||
|
||
if current_canonical_head and header.parent_hash == current_canonical_head: | ||
# the calls to _find_new_ancestors and _decanonicalize_old_headers are | ||
# relatively expensive, it's better to skip them in this case, where we're | ||
# extending the canonical chain by a header | ||
new_canonical_headers = (header,) | ||
old_canonical_headers = () | ||
else: | ||
new_canonical_headers = cast( | ||
Tuple[BlockHeaderAPI, ...], | ||
tuple(reversed(cls._find_new_ancestors(db, header, genesis_parent_hash))) | ||
) | ||
old_canonical_headers = cls._decanonicalize_old_headers( | ||
db, new_canonical_headers | ||
) | ||
|
||
new_canonical_headers = tuple(reversed( | ||
cls._find_new_ancestors(db, header, genesis_parent_hash))) | ||
for h in new_canonical_headers: | ||
cls._add_block_number_to_hash_lookup(db, h) | ||
|
||
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash) | ||
|
||
return new_canonical_headers, old_canonical_headers | ||
|
||
@classmethod | ||
def _decanonicalize_old_headers( | ||
cls, | ||
db: DatabaseAPI, | ||
new_canonical_headers: Tuple[BlockHeaderAPI, ...] | ||
) -> Tuple[BlockHeaderAPI, ...]: | ||
old_canonical_headers = [] | ||
|
||
for h in new_canonical_headers: | ||
|
@@ -271,12 +302,7 @@ def _set_as_canonical_chain_head( | |
old_canonical_header = cls._get_block_header_by_hash(db, old_canonical_hash) | ||
old_canonical_headers.append(old_canonical_header) | ||
|
||
for h in new_canonical_headers: | ||
cls._add_block_number_to_hash_lookup(db, h) | ||
|
||
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash) | ||
|
||
return new_canonical_headers, tuple(old_canonical_headers) | ||
return tuple(old_canonical_headers) | ||
|
||
@classmethod | ||
@to_tuple | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Improve performance when importing a header which is a child of the current canonical | ||
chain tip. |
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.
This split was just to do fewer things on one line, right? (I'm fine with it, just wondering if there was something subtle I was missing)
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.
Nope, no subtle reason, the change was to get us under the 90-character line width limit