From 3ea0b90d4c1084ae8505e1bd21b9b5dd2e31fd2a Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 24 May 2021 22:47:01 +0200 Subject: [PATCH] switch to a single list of historical batch summaries --- specs/altair/beacon-chain.md | 28 +++++++++++++++++++--------- specs/altair/fork.md | 26 ++++++++++++-------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index 559b344ee..793e5e8b6 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -52,7 +52,7 @@ - [Rewards and penalties](#rewards-and-penalties) - [Slashings](#slashings) - [Participation flags updates](#participation-flags-updates) - - [Historical roots updates](#historical-roots-updates) + - [Historical batches updates](#historical-batches-updates) - [Sync committee updates](#sync-committee-updates) - [Initialize state for pure Altair testnets and test vectors](#initialize-state-for-pure-altair-testnets-and-test-vectors) @@ -175,8 +175,7 @@ class BeaconState(Container): latest_block_header: BeaconBlockHeader block_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT] state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT] - historical_block_roots: List[Root, HISTORICAL_ROOTS_LIMIT] - historical_state_roots: List[Root, HISTORICAL_ROOTS_LIMIT] + historical_batches: List[HistoricalBatchSummary, HISTORICAL_ROOTS_LIMIT] # Eth1 eth1_data: Eth1Data eth1_data_votes: List[Eth1Data, EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH] @@ -205,6 +204,15 @@ class BeaconState(Container): ### New containers + +```python +# The tree roots that make up a phase0 HistoricalBatch making its layout +# hash_tree_root-compatible. +class HistoricalBatchSummary(Container): + block_batch_root: Root + state_batch_root: Root +``` + #### `SyncAggregate` ```python @@ -600,7 +608,7 @@ def process_epoch(state: BeaconState) -> None: process_effective_balance_updates(state) process_slashings_reset(state) process_randao_mixes_reset(state) - process_historical_roots_update(state) + process_historical_batches_update(state) process_participation_flag_updates(state) # [New in Altair] process_sync_committee_updates(state) # [New in Altair] ``` @@ -689,17 +697,19 @@ def process_participation_flag_updates(state: BeaconState) -> None: state.current_epoch_participation = [ParticipationFlags(0b0000_0000) for _ in range(len(state.validators))] ``` -#### Historical roots updates +#### Historical batches updates -*Note*: The function `process_historical_roots_update` changes definition compared to phase0. +*Note*: The function `process_historical_batches_update` replaces `process_historical_roots_update` in phase0. ```python -def process_historical_roots_update(state: BeaconState) -> None: +def process_historical_batches_update(state: BeaconState) -> None: # Set historical block root accumulator next_epoch = Epoch(get_current_epoch(state) + 1) if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: - state.historical_block_roots.append(hash_tree_root(state.block_roots)) - state.historical_state_roots.append(hash_tree_root(state.state_roots)) + historical_batch = HistoricalBatchSummary( + block_batch_root=hash_tree_root(state.block_roots), + state_batch_root=hash_tree_root(state.state_roots)) + state.historical_batches.append(historical_batch) ``` #### Sync committee updates diff --git a/specs/altair/fork.md b/specs/altair/fork.md index e8165bfb9..a680c3392 100644 --- a/specs/altair/fork.md +++ b/specs/altair/fork.md @@ -58,20 +58,21 @@ def translate_participation(state: BeaconState, pending_attestations: Sequence[p for flag_index in participation_flag_indices: epoch_participation[index] = add_flag(epoch_participation[index], flag_index) -def backfill_historical_roots(state: BeaconState, pre: BeaconState) -> None: - # Clients need to recreate `historical_block_roots` and - # `historical_state_roots` from their history. A large part can be - # precalculated and verified against historical_roots in state. - # Before the fork, clients should start collecting the part that has not - # been pre-calculated. - # - # The calclulated split historical roots can be verified against the existing - # historical_roots field up to the fork. - +def load_historical_batches(state: BeaconState, pre: BeaconState) -> List[HistoricalBatchSummary, HISTORICAL_ROOTS_LIMIT]: + # Clients need to recalculate the historical batches from genesis and return + # them here with the roots of the blocks and states separated. It's assumed + # most of these are pre-calculated and the rest are stored on-the-fly by + # altair-compatible clients leading up to the fork. All in all, this should + # be around 10kb of hashes. + return [] def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState: epoch = phase0.get_current_epoch(pre) + historical_batches = load_historical_batches() + + # Tree hash should match since HistoricalBatchSummary is an intermediate step in calculating the phase0 historical root entry + assert hash_tree_root(historical_batches) == hash_tree_root(pre.historical_roots) post = BeaconState( # Versioning genesis_time=pre.genesis_time, @@ -86,8 +87,7 @@ def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState: latest_block_header=pre.latest_block_header, block_roots=pre.block_roots, state_roots=pre.state_roots, - historical_block_roots=[], - historical_state_roots=[], + historical_batches=historical_batches, # Eth1 eth1_data=pre.eth1_data, eth1_data_votes=pre.eth1_data_votes, @@ -113,8 +113,6 @@ def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState: # Fill in previous epoch participation from the pre state's pending attestations translate_participation(post, pre.previous_epoch_attestations) - backfill_historical_roots(post, pre) - # Fill in sync committees # Note: A duplicate committee is assigned for the current and next committee at the fork boundary post.current_sync_committee = get_next_sync_committee(post)