switch to a single list of historical batch summaries

This commit is contained in:
Jacek Sieka 2021-05-24 22:47:01 +02:00
parent 96dc7f6061
commit 3ea0b90d4c
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
2 changed files with 31 additions and 23 deletions

View File

@ -52,7 +52,7 @@
- [Rewards and penalties](#rewards-and-penalties) - [Rewards and penalties](#rewards-and-penalties)
- [Slashings](#slashings) - [Slashings](#slashings)
- [Participation flags updates](#participation-flags-updates) - [Participation flags updates](#participation-flags-updates)
- [Historical roots updates](#historical-roots-updates) - [Historical batches updates](#historical-batches-updates)
- [Sync committee updates](#sync-committee-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) - [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 latest_block_header: BeaconBlockHeader
block_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT] block_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT] state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
historical_block_roots: List[Root, HISTORICAL_ROOTS_LIMIT] historical_batches: List[HistoricalBatchSummary, HISTORICAL_ROOTS_LIMIT]
historical_state_roots: List[Root, HISTORICAL_ROOTS_LIMIT]
# Eth1 # Eth1
eth1_data: Eth1Data eth1_data: Eth1Data
eth1_data_votes: List[Eth1Data, EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH] eth1_data_votes: List[Eth1Data, EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH]
@ -205,6 +204,15 @@ class BeaconState(Container):
### New containers ### 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` #### `SyncAggregate`
```python ```python
@ -600,7 +608,7 @@ def process_epoch(state: BeaconState) -> None:
process_effective_balance_updates(state) process_effective_balance_updates(state)
process_slashings_reset(state) process_slashings_reset(state)
process_randao_mixes_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_participation_flag_updates(state) # [New in Altair]
process_sync_committee_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))] 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 ```python
def process_historical_roots_update(state: BeaconState) -> None: def process_historical_batches_update(state: BeaconState) -> None:
# Set historical block root accumulator # Set historical block root accumulator
next_epoch = Epoch(get_current_epoch(state) + 1) next_epoch = Epoch(get_current_epoch(state) + 1)
if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0: if next_epoch % (SLOTS_PER_HISTORICAL_ROOT // SLOTS_PER_EPOCH) == 0:
state.historical_block_roots.append(hash_tree_root(state.block_roots)) historical_batch = HistoricalBatchSummary(
state.historical_state_roots.append(hash_tree_root(state.state_roots)) 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 #### Sync committee updates

View File

@ -58,20 +58,21 @@ def translate_participation(state: BeaconState, pending_attestations: Sequence[p
for flag_index in participation_flag_indices: for flag_index in participation_flag_indices:
epoch_participation[index] = add_flag(epoch_participation[index], flag_index) epoch_participation[index] = add_flag(epoch_participation[index], flag_index)
def backfill_historical_roots(state: BeaconState, pre: BeaconState) -> None: def load_historical_batches(state: BeaconState, pre: BeaconState) -> List[HistoricalBatchSummary, HISTORICAL_ROOTS_LIMIT]:
# Clients need to recreate `historical_block_roots` and # Clients need to recalculate the historical batches from genesis and return
# `historical_state_roots` from their history. A large part can be # them here with the roots of the blocks and states separated. It's assumed
# precalculated and verified against historical_roots in state. # most of these are pre-calculated and the rest are stored on-the-fly by
# Before the fork, clients should start collecting the part that has not # altair-compatible clients leading up to the fork. All in all, this should
# been pre-calculated. # be around 10kb of hashes.
#
# The calclulated split historical roots can be verified against the existing
# historical_roots field up to the fork.
return []
def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState: def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState:
epoch = phase0.get_current_epoch(pre) 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( post = BeaconState(
# Versioning # Versioning
genesis_time=pre.genesis_time, genesis_time=pre.genesis_time,
@ -86,8 +87,7 @@ def upgrade_to_altair(pre: phase0.BeaconState) -> BeaconState:
latest_block_header=pre.latest_block_header, latest_block_header=pre.latest_block_header,
block_roots=pre.block_roots, block_roots=pre.block_roots,
state_roots=pre.state_roots, state_roots=pre.state_roots,
historical_block_roots=[], historical_batches=historical_batches,
historical_state_roots=[],
# Eth1 # Eth1
eth1_data=pre.eth1_data, eth1_data=pre.eth1_data,
eth1_data_votes=pre.eth1_data_votes, 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 # Fill in previous epoch participation from the pre state's pending attestations
translate_participation(post, pre.previous_epoch_attestations) translate_participation(post, pre.previous_epoch_attestations)
backfill_historical_roots(post, pre)
# Fill in sync committees # Fill in sync committees
# Note: A duplicate committee is assigned for the current and next committee at the fork boundary # 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) post.current_sync_committee = get_next_sync_committee(post)