Skip to content

Commit d9b4e40

Browse files
committed
PR feedback
1 parent 72b40ec commit d9b4e40

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

eth/db/chain.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
GENESIS_PARENT_HASH,
3636
)
3737
from eth.exceptions import (
38-
CanonicalHeadNotFound,
3938
HeaderNotFound,
4039
ReceiptNotFound,
4140
TransactionNotFound,
@@ -91,24 +90,11 @@ def get_block_uncles(self, uncles_hash: Hash32) -> Tuple[BlockHeaderAPI, ...]:
9190
return tuple(rlp.decode(encoded_uncles, sedes=rlp.sedes.CountableList(BlockHeader)))
9291

9392
@classmethod
94-
def _set_as_canonical_chain_head(
95-
cls,
96-
db: DatabaseAPI,
97-
header: BlockHeaderAPI,
98-
genesis_parent_hash: Hash32,
99-
) -> Tuple[Tuple[BlockHeaderAPI, ...], Tuple[BlockHeaderAPI, ...]]:
100-
# performance optimization, short circuit if we're just adding a single header
101-
try:
102-
current_canonical_head = cls._get_canonical_head_hash(db)
103-
except CanonicalHeadNotFound:
104-
current_canonical_head = None
105-
if current_canonical_head and header.parent_hash == current_canonical_head:
106-
cls._add_block_number_to_hash_lookup(db, header)
107-
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)
108-
return (header,), tuple()
109-
110-
new_canonical_headers = tuple(reversed(
111-
cls._find_new_ancestors(db, header, genesis_parent_hash)))
93+
def _decanonicalize_old_headers(
94+
cls,
95+
db: DatabaseAPI,
96+
new_canonical_headers: Tuple[BlockHeaderAPI, ...]
97+
) -> Tuple[BlockHeaderAPI, ...]:
11298
old_canonical_headers = []
11399

114100
# remove transaction lookups for blocks that are no longer canonical
@@ -122,19 +108,16 @@ def _set_as_canonical_chain_head(
122108
old_header = cls._get_block_header_by_hash(db, old_hash)
123109
old_canonical_headers.append(old_header)
124110
try:
125-
for transaction_hash in cls._get_block_transaction_hashes(db, old_header):
111+
transaction_hashes = cls._get_block_transaction_hashes(db, old_header)
112+
for transaction_hash in transaction_hashes:
126113
cls._remove_transaction_from_canonical_chain(db, transaction_hash)
127114
except MissingTrieNode:
128-
# If the transactions were never stored for the (now) non-canonical chain,
129-
# then you don't need to remove them from the canonical chain lookup.
115+
# If the transactions were never stored for the (now) non-canonical
116+
# chain, then you don't need to remove them from the canonical chain
117+
# lookup.
130118
pass
131119

132-
for h in new_canonical_headers:
133-
cls._add_block_number_to_hash_lookup(db, h)
134-
135-
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)
136-
137-
return new_canonical_headers, tuple(old_canonical_headers)
120+
return tuple(old_canonical_headers)
138121

139122
#
140123
# Block API

eth/db/header.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,42 @@ def _set_as_canonical_chain_head(
254254
:return: a tuple of the headers that are newly in the canonical chain, and the headers that
255255
are no longer in the canonical chain
256256
"""
257-
# performance optimization, short circuit if we're just adding a single header
258257
try:
259258
current_canonical_head = cls._get_canonical_head_hash(db)
260259
except CanonicalHeadNotFound:
261260
current_canonical_head = None
261+
262+
new_canonical_headers: Tuple[BlockHeaderAPI, ...]
263+
old_canonical_headers: Tuple[BlockHeaderAPI, ...]
264+
262265
if current_canonical_head and header.parent_hash == current_canonical_head:
263-
cls._add_block_number_to_hash_lookup(db, header)
264-
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)
265-
return (header,), tuple()
266+
# the calls to _find_new_ancestors and _decanonicalize_old_headers are
267+
# relatively expensive, it's better to skip them in this case, where we're
268+
# extending the canonical chain by a header
269+
new_canonical_headers = (header,)
270+
old_canonical_headers = ()
271+
else:
272+
new_canonical_headers = cast(
273+
Tuple[BlockHeaderAPI, ...],
274+
tuple(reversed(cls._find_new_ancestors(db, header, genesis_parent_hash)))
275+
)
276+
old_canonical_headers = cls._decanonicalize_old_headers(
277+
db, new_canonical_headers
278+
)
279+
280+
for h in new_canonical_headers:
281+
cls._add_block_number_to_hash_lookup(db, h)
282+
283+
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)
266284

267-
new_canonical_headers = tuple(reversed(
268-
cls._find_new_ancestors(db, header, genesis_parent_hash)))
285+
return new_canonical_headers, old_canonical_headers
286+
287+
@classmethod
288+
def _decanonicalize_old_headers(
289+
cls,
290+
db: DatabaseAPI,
291+
new_canonical_headers: Tuple[BlockHeaderAPI, ...]
292+
) -> Tuple[BlockHeaderAPI, ...]:
269293
old_canonical_headers = []
270294

271295
for h in new_canonical_headers:
@@ -278,12 +302,7 @@ def _set_as_canonical_chain_head(
278302
old_canonical_header = cls._get_block_header_by_hash(db, old_canonical_hash)
279303
old_canonical_headers.append(old_canonical_header)
280304

281-
for h in new_canonical_headers:
282-
cls._add_block_number_to_hash_lookup(db, h)
283-
284-
db.set(SchemaV1.make_canonical_head_hash_lookup_key(), header.hash)
285-
286-
return new_canonical_headers, tuple(old_canonical_headers)
305+
return tuple(old_canonical_headers)
287306

288307
@classmethod
289308
@to_tuple

0 commit comments

Comments
 (0)