2023-02-07 23:22:28 +00:00
# Deneb -- Fork Choice
2023-01-03 15:28:37 +00:00
## Table of contents
<!-- TOC -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE - RUN doctoc TO UPDATE -->
- [Introduction ](#introduction )
2023-01-06 15:39:04 +00:00
- [Containers ](#containers )
2023-01-03 15:28:37 +00:00
- [Helpers ](#helpers )
2023-06-12 18:29:07 +00:00
- [Extended `PayloadAttributes` ](#extended-payloadattributes )
- [`is_data_available` ](#is_data_available )
2023-01-03 15:28:37 +00:00
- [Updated fork-choice handlers ](#updated-fork-choice-handlers )
- [`on_block` ](#on_block )
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
2023-02-07 23:22:28 +00:00
This is the modification of the fork choice accompanying the Deneb upgrade.
2023-01-03 15:28:37 +00:00
2023-01-06 15:39:04 +00:00
## Containers
2023-01-03 15:28:37 +00:00
## Helpers
2023-06-12 18:29:07 +00:00
### Extended `PayloadAttributes`
2023-01-06 15:19:44 +00:00
2023-06-22 16:01:49 +00:00
`PayloadAttributes` is extended with the parent beacon block root for EIP-4788.
2023-02-15 07:51:57 +00:00
2023-06-12 18:29:07 +00:00
```python
@dataclass
class PayloadAttributes(object):
timestamp: uint64
prev_randao: Bytes32
suggested_fee_recipient: ExecutionAddress
withdrawals: Sequence[Withdrawal]
parent_beacon_block_root: Root # [New in Deneb:EIP4788]
2023-01-06 15:19:44 +00:00
```
2023-06-12 18:29:07 +00:00
### `is_data_available`
2023-01-03 15:28:37 +00:00
2023-06-08 07:05:46 +00:00
*[New in Deneb:EIP4844]*
2023-01-03 15:28:37 +00:00
2023-01-06 05:08:32 +00:00
The implementation of `is_data_available` will become more sophisticated during later scaling upgrades.
2023-06-01 15:40:46 +00:00
Initially, verification requires every verifying actor to retrieve all matching `Blob` s and `KZGProof` s, and validate them with `verify_blob_kzg_proof_batch` .
2023-01-03 15:28:37 +00:00
2023-02-15 07:51:57 +00:00
The block MUST NOT be considered valid until all valid `Blob` s have been downloaded. Blocks that have been previously validated as available SHOULD be considered available even if the associated `Blob` s have subsequently been pruned.
2023-01-03 15:28:37 +00:00
2023-10-20 18:06:47 +00:00
*Note*: Extraneous or invalid Blobs (in addition to KZG expected/referenced valid blobs) received on the p2p network MUST NOT invalidate a block that is otherwise valid and available.
2023-10-20 02:58:20 +00:00
2023-01-03 15:28:37 +00:00
```python
2023-02-15 07:51:57 +00:00
def is_data_available(beacon_block_root: Root, blob_kzg_commitments: Sequence[KZGCommitment]) -> bool:
2023-02-16 07:18:52 +00:00
# `retrieve_blobs_and_proofs` is implementation and context dependent
# It returns all the blobs for the given block root, and raises an exception if not available
2023-06-09 15:10:00 +00:00
# Note: the p2p network does not guarantee sidecar retrieval outside of
# `MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS`
2023-02-15 07:51:57 +00:00
blobs, proofs = retrieve_blobs_and_proofs(beacon_block_root)
2023-01-03 15:28:37 +00:00
2023-06-07 05:59:51 +00:00
return verify_blob_kzg_proof_batch(blobs, blob_kzg_commitments, proofs)
2023-01-03 15:28:37 +00:00
```
## Updated fork-choice handlers
### `on_block`
2023-02-24 13:07:16 +00:00
*Note*: The only modification is the addition of the blob data availability check.
2023-01-03 15:28:37 +00:00
```python
def on_block(store: Store, signed_block: SignedBeaconBlock) -> None:
"""
Run ``on_block`` upon receiving a new block.
"""
block = signed_block.message
# Parent block must be known
assert block.parent_root in store.block_states
# Make a copy of the state to avoid mutability issues
pre_state = copy(store.block_states[block.parent_root])
# Blocks cannot be in the future. If they are, their consideration must be delayed until they are in the past.
assert get_current_slot(store) >= block.slot
# Check that block is later than the finalized epoch slot (optimization to reduce calls to get_ancestor)
finalized_slot = compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)
assert block.slot > finalized_slot
# Check block is a descendant of the finalized block at the checkpoint finalized slot
2023-04-18 03:26:16 +00:00
finalized_checkpoint_block = get_checkpoint_block(
2023-04-05 03:38:20 +00:00
store,
block.parent_root,
store.finalized_checkpoint.epoch,
)
2023-04-18 03:26:16 +00:00
assert store.finalized_checkpoint.root == finalized_checkpoint_block
2023-01-03 15:28:37 +00:00
2023-06-07 09:45:39 +00:00
# [New in Deneb:EIP4844]
2023-01-06 05:08:32 +00:00
# Check if blob data is available
# If not, this block MAY be queued and subsequently considered when blob data becomes available
2023-10-20 18:06:47 +00:00
# *Note* : Extraneous or invalid Blobs (in addition to the expected/referenced valid blobs)
# received on the p2p network MUST NOT invalidate a block that is otherwise valid and available
2023-02-15 07:51:57 +00:00
assert is_data_available(hash_tree_root(block), block.body.blob_kzg_commitments)
2023-01-03 15:28:37 +00:00
# Check the block is valid and compute the post-state
state = pre_state.copy()
2023-03-09 23:17:11 +00:00
block_root = hash_tree_root(block)
2023-01-03 15:28:37 +00:00
state_transition(state, signed_block, True)
# Add new block to the store
2023-03-09 23:17:11 +00:00
store.blocks[block_root] = block
2023-01-03 15:28:37 +00:00
# Add new state for this block to the store
2023-03-09 23:17:11 +00:00
store.block_states[block_root] = state
2023-01-03 15:28:37 +00:00
2023-10-20 06:41:51 +00:00
# Add block timeliness to the store
2023-01-03 15:28:37 +00:00
time_into_slot = (store.time - store.genesis_time) % SECONDS_PER_SLOT
is_before_attesting_interval = time_into_slot < SECONDS_PER_SLOT / / INTERVALS_PER_SLOT
2023-10-20 06:41:51 +00:00
is_timely = get_current_slot(store) == block.slot and is_before_attesting_interval
store.block_timeliness[hash_tree_root(block)] = is_timely
# Add proposer score boost if the block is timely and not conflicting with an existing block
2023-05-11 03:16:52 +00:00
is_first_block = store.proposer_boost_root == Root()
2023-10-20 06:41:51 +00:00
if is_timely and is_first_block:
2023-01-03 15:28:37 +00:00
store.proposer_boost_root = hash_tree_root(block)
2023-03-09 23:17:11 +00:00
# Update checkpoints in store if necessary
update_checkpoints(store, state.current_justified_checkpoint, state.finalized_checkpoint)
# Eagerly compute unrealized justification and finality.
compute_pulled_up_tip(store, block_root)
2023-01-03 15:28:37 +00:00
```