Clarify get_pow_block block-not-found case

This commit is contained in:
Hsiao-Wei Wang 2021-10-18 13:32:29 +08:00
parent 6852c5a1d0
commit cd5cf60deb
No known key found for this signature in database
GPG Key ID: 1111A8A81778319E
3 changed files with 13 additions and 2 deletions

View File

@ -510,7 +510,7 @@ from eth2spec.utils.ssz.ssz_typing import Bytes20, ByteList, ByteVector, uint256
ExecutionState = Any
def get_pow_block(hash: Bytes32) -> PowBlock:
def get_pow_block(hash: Bytes32) -> Optional[PowBlock]:
return PowBlock(block_hash=hash, parent_hash=Bytes32(), total_difficulty=uint256(0), difficulty=uint256(0))

View File

@ -64,7 +64,8 @@ class PowBlock(Container):
### `get_pow_block`
Let `get_pow_block(block_hash: Hash32) -> PowBlock` be the function that given the hash of the PoW block returns its data.
Let `get_pow_block(block_hash: Hash32) -> Optional[PowBlock]` be the function that given the hash of the PoW block returns its data.
It may result in `None` if the requested block is not found if execution engine is still syncing.
*Note*: The `eth_getBlockByHash` JSON-RPC method may be used to pull this information from an execution client.
@ -90,6 +91,12 @@ def is_valid_terminal_pow_block(block: PowBlock, parent: PowBlock) -> bool:
```python
def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
"""
Run ``on_block`` upon receiving a new block.
An block that is asserted as invalid due to unavailable PoW block may be valid at a later time,
consider scheduling it for later processing in such case.
"""
block = signed_block.message
# Parent block must be known
assert block.parent_root in store.block_states
@ -110,8 +117,11 @@ def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
# [New in Merge]
if is_merge_block(pre_state, block.body):
# Note: the unavailable PoW block(s) may be available later
pow_block = get_pow_block(block.body.execution_payload.parent_hash)
assert pow_block is not None
pow_parent = get_pow_block(pow_block.parent_hash)
assert pow_parent is not None
assert is_valid_terminal_pow_block(pow_block, pow_parent)
# Add new block to the store

View File

@ -103,6 +103,7 @@ def get_pow_block_at_terminal_total_difficulty(pow_chain: Sequence[PowBlock]) ->
# `pow_chain` abstractly represents all blocks in the PoW chain
for block in pow_chain:
parent = get_pow_block(block.parent_hash)
assert parent is not None
block_reached_ttd = block.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
parent_reached_ttd = parent.total_difficulty >= TERMINAL_TOTAL_DIFFICULTY
if block_reached_ttd and not parent_reached_ttd: