|
| 1 | +# Electra Light Client -- Fork Logic |
| 2 | + |
| 3 | +## Table of contents |
| 4 | + |
| 5 | +<!-- TOC --> |
| 6 | +<!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 7 | +<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 8 | + |
| 9 | +- [Introduction](#introduction) |
| 10 | +- [Helper functions](#helper-functions) |
| 11 | + - [`normalize_merkle_branch`](#normalize_merkle_branch) |
| 12 | +- [Upgrading light client data](#upgrading-light-client-data) |
| 13 | +- [Upgrading the store](#upgrading-the-store) |
| 14 | + |
| 15 | +<!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 16 | +<!-- /TOC --> |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +This document describes how to upgrade existing light client objects based on the [Deneb specification](../../deneb/light-client/sync-protocol.md) to Electra. This is necessary when processing pre-Electra data with a post-Electra `LightClientStore`. Note that the data being exchanged over the network protocols uses the original format. |
| 21 | + |
| 22 | +## Helper functions |
| 23 | + |
| 24 | +### `normalize_merkle_branch` |
| 25 | + |
| 26 | +```python |
| 27 | +def normalize_merkle_branch(branch: Sequence[Bytes32], |
| 28 | + gindex: GeneralizedIndex) -> Sequence[Bytes32]: |
| 29 | + depth = floorlog2(gindex) |
| 30 | + num_extra = depth - len(branch) |
| 31 | + return [Bytes32()] * num_extra + [*branch] |
| 32 | +``` |
| 33 | + |
| 34 | +## Upgrading light client data |
| 35 | + |
| 36 | +A Electra `LightClientStore` can still process earlier light client data. In order to do so, that pre-Electra data needs to be locally upgraded to Electra before processing. |
| 37 | + |
| 38 | +```python |
| 39 | +def upgrade_lc_header_to_electra(pre: deneb.LightClientHeader) -> LightClientHeader: |
| 40 | + return LightClientHeader( |
| 41 | + beacon=pre.beacon, |
| 42 | + execution=ExecutionPayloadHeader( |
| 43 | + parent_hash=pre.execution.parent_hash, |
| 44 | + fee_recipient=pre.execution.fee_recipient, |
| 45 | + state_root=pre.execution.state_root, |
| 46 | + receipts_root=pre.execution.receipts_root, |
| 47 | + logs_bloom=pre.execution.logs_bloom, |
| 48 | + prev_randao=pre.execution.prev_randao, |
| 49 | + block_number=pre.execution.block_number, |
| 50 | + gas_limit=pre.execution.gas_limit, |
| 51 | + gas_used=pre.execution.gas_used, |
| 52 | + timestamp=pre.execution.timestamp, |
| 53 | + extra_data=pre.execution.extra_data, |
| 54 | + base_fee_per_gas=pre.execution.base_fee_per_gas, |
| 55 | + block_hash=pre.execution.block_hash, |
| 56 | + transactions_root=pre.execution.transactions_root, |
| 57 | + withdrawals_root=pre.execution.withdrawals_root, |
| 58 | + blob_gas_used=pre.execution.blob_gas_used, |
| 59 | + excess_blob_gas=pre.execution.blob_gas_used, |
| 60 | + deposit_requests_root=Root(), # [New in Electra:EIP6110] |
| 61 | + withdrawal_requests_root=Root(), # [New in Electra:EIP7002:EIP7251] |
| 62 | + consolidation_requests_root=Root(), # [New in Electra:EIP7251] |
| 63 | + ), |
| 64 | + execution_branch=pre.execution_branch, |
| 65 | + ) |
| 66 | +``` |
| 67 | + |
| 68 | +```python |
| 69 | +def upgrade_lc_bootstrap_to_electra(pre: deneb.LightClientBootstrap) -> LightClientBootstrap: |
| 70 | + return LightClientBootstrap( |
| 71 | + header=upgrade_lc_header_to_electra(pre.header), |
| 72 | + current_sync_committee=pre.current_sync_committee, |
| 73 | + current_sync_committee_branch=normalize_merkle_branch( |
| 74 | + pre.current_sync_committee_branch, CURRENT_SYNC_COMMITTEE_GINDEX_ELECTRA), |
| 75 | + ) |
| 76 | +``` |
| 77 | + |
| 78 | +```python |
| 79 | +def upgrade_lc_update_to_electra(pre: deneb.LightClientUpdate) -> LightClientUpdate: |
| 80 | + return LightClientUpdate( |
| 81 | + attested_header=upgrade_lc_header_to_electra(pre.attested_header), |
| 82 | + next_sync_committee=pre.next_sync_committee, |
| 83 | + next_sync_committee_branch=normalize_merkle_branch( |
| 84 | + pre.next_sync_committee_branch, NEXT_SYNC_COMMITTEE_GINDEX_ELECTRA), |
| 85 | + finalized_header=upgrade_lc_header_to_electra(pre.finalized_header), |
| 86 | + finality_branch=normalize_merkle_branch( |
| 87 | + pre.finality_branch, FINALIZED_ROOT_GINDEX_ELECTRA), |
| 88 | + sync_aggregate=pre.sync_aggregate, |
| 89 | + signature_slot=pre.signature_slot, |
| 90 | + ) |
| 91 | +``` |
| 92 | + |
| 93 | +```python |
| 94 | +def upgrade_lc_finality_update_to_electra(pre: deneb.LightClientFinalityUpdate) -> LightClientFinalityUpdate: |
| 95 | + return LightClientFinalityUpdate( |
| 96 | + attested_header=upgrade_lc_header_to_electra(pre.attested_header), |
| 97 | + finalized_header=upgrade_lc_header_to_electra(pre.finalized_header), |
| 98 | + finality_branch=normalize_merkle_branch( |
| 99 | + pre.finality_branch, FINALIZED_ROOT_GINDEX_ELECTRA), |
| 100 | + sync_aggregate=pre.sync_aggregate, |
| 101 | + signature_slot=pre.signature_slot, |
| 102 | + ) |
| 103 | +``` |
| 104 | + |
| 105 | +```python |
| 106 | +def upgrade_lc_optimistic_update_to_electra(pre: deneb.LightClientOptimisticUpdate) -> LightClientOptimisticUpdate: |
| 107 | + return LightClientOptimisticUpdate( |
| 108 | + attested_header=upgrade_lc_header_to_electra(pre.attested_header), |
| 109 | + sync_aggregate=pre.sync_aggregate, |
| 110 | + signature_slot=pre.signature_slot, |
| 111 | + ) |
| 112 | +``` |
| 113 | + |
| 114 | +## Upgrading the store |
| 115 | + |
| 116 | +Existing `LightClientStore` objects based on Deneb MUST be upgraded to Electra before Electra based light client data can be processed. The `LightClientStore` upgrade MAY be performed before `ELECTRA_FORK_EPOCH`. |
| 117 | + |
| 118 | +```python |
| 119 | +def upgrade_lc_store_to_electra(pre: deneb.LightClientStore) -> LightClientStore: |
| 120 | + if pre.best_valid_update is None: |
| 121 | + best_valid_update = None |
| 122 | + else: |
| 123 | + best_valid_update = upgrade_lc_update_to_electra(pre.best_valid_update) |
| 124 | + return LightClientStore( |
| 125 | + finalized_header=upgrade_lc_header_to_electra(pre.finalized_header), |
| 126 | + current_sync_committee=pre.current_sync_committee, |
| 127 | + next_sync_committee=pre.next_sync_committee, |
| 128 | + best_valid_update=best_valid_update, |
| 129 | + optimistic_header=upgrade_lc_header_to_electra(pre.optimistic_header), |
| 130 | + previous_max_active_participants=pre.previous_max_active_participants, |
| 131 | + current_max_active_participants=pre.current_max_active_participants, |
| 132 | + ) |
| 133 | +``` |
0 commit comments