Enforce terminal PoW block to be on the cusp

This commit is contained in:
Mikhail Kalinin 2021-07-16 19:16:32 +06:00
parent b541b8e763
commit a4a050e97a
3 changed files with 9 additions and 5 deletions

View File

@ -509,7 +509,7 @@ ExecutionState = Any
def get_pow_block(hash: Bytes32) -> PowBlock: def get_pow_block(hash: Bytes32) -> PowBlock:
return PowBlock(block_hash=hash, is_valid=True, is_processed=True, return PowBlock(block_hash=hash, parent_hash=Bytes32(), is_valid=True, is_processed=True,
total_difficulty=uint256(0), difficulty=uint256(0)) total_difficulty=uint256(0), difficulty=uint256(0))

View File

@ -82,6 +82,7 @@ class TransitionStore(object):
@dataclass @dataclass
class PowBlock(object): class PowBlock(object):
block_hash: Hash32 block_hash: Hash32
parent_hash: Hash32
is_processed: boolean is_processed: boolean
is_valid: boolean is_valid: boolean
total_difficulty: uint256 total_difficulty: uint256
@ -99,9 +100,10 @@ Let `get_pow_block(block_hash: Hash32) -> PowBlock` be the function that given t
Used by fork-choice handler, `on_block`. Used by fork-choice handler, `on_block`.
```python ```python
def is_valid_terminal_pow_block(transition_store: TransitionStore, block: PowBlock) -> bool: def is_valid_terminal_pow_block(transition_store: TransitionStore, block: PowBlock, parent: PowBlock) -> bool:
is_total_difficulty_reached = block.total_difficulty >= transition_store.transition_total_difficulty is_total_difficulty_reached = block.total_difficulty >= transition_store.transition_total_difficulty
return block.is_valid and is_total_difficulty_reached is_parent_total_difficulty_valid = parent.total_difficulty < transition_store.transition_total_difficulty
return block.is_valid and is_total_difficulty_reached and is_parent_total_difficulty_valid
``` ```
## Updated fork-choice handlers ## Updated fork-choice handlers
@ -130,8 +132,9 @@ def on_block(store: Store, signed_block: SignedBeaconBlock, transition_store: Tr
if (transition_store is not None) and is_merge_block(pre_state, block): if (transition_store is not None) and is_merge_block(pre_state, block):
# Delay consideration of block until PoW block is processed by the PoW node # Delay consideration of block until PoW block is processed by the PoW node
pow_block = get_pow_block(block.body.execution_payload.parent_hash) pow_block = get_pow_block(block.body.execution_payload.parent_hash)
pow_parent = get_pow_block(pow_block.parent_hash)
assert pow_block.is_processed assert pow_block.is_processed
assert is_valid_terminal_pow_block(transition_store, pow_block) assert is_valid_terminal_pow_block(transition_store, pow_block, pow_parent)
# Check the block is valid and compute the post-state # Check the block is valid and compute the post-state
state = pre_state.copy() state = pre_state.copy()

View File

@ -90,7 +90,8 @@ def get_execution_payload(state: BeaconState,
execution_engine: ExecutionEngine) -> ExecutionPayload: execution_engine: ExecutionEngine) -> ExecutionPayload:
if not is_merge_complete(state): if not is_merge_complete(state):
pow_block = get_pow_chain_head() pow_block = get_pow_chain_head()
if not is_valid_terminal_pow_block(transition_store, pow_block): pow_parent = get_pow_block(pow_block.parent_hash)
if not is_valid_terminal_pow_block(transition_store, pow_block, pow_parent):
# Pre-merge, empty payload # Pre-merge, empty payload
return ExecutionPayload() return ExecutionPayload()
else: else: