From 11239f18821ea309dbfa25e2841a07407edd233c Mon Sep 17 00:00:00 2001 From: Tumas Date: Thu, 14 Nov 2024 10:36:57 +0200 Subject: [PATCH] Prune Eth1 unfinalized blocks if deposits requests are active --- block_producer/src/block_producer.rs | 8 +++++-- block_producer/src/eth1_storage.rs | 33 +++++++++++++++++++++------- fork_choice_control/src/messages.rs | 2 +- fork_choice_control/src/mutator.rs | 8 +++---- types/src/combined.rs | 16 +++++++++++++- types/src/electra/beacon_state.rs | 2 +- validator/src/validator.rs | 4 ++-- 7 files changed, 54 insertions(+), 19 deletions(-) diff --git a/block_producer/src/block_producer.rs b/block_producer/src/block_producer.rs index 158802d..bd9ddd3 100644 --- a/block_producer/src/block_producer.rs +++ b/block_producer/src/block_producer.rs @@ -270,10 +270,14 @@ impl BlockProducer { }) } - pub fn finalize_deposits(&self, finalized_deposit_index: DepositIndex) -> Result<()> { + pub fn finalize_deposits( + &self, + finalized_deposit_index: DepositIndex, + deposit_requests_start_index: Option, + ) -> Result<()> { self.producer_context .eth1_chain - .finalize_deposits(finalized_deposit_index) + .finalize_deposits(finalized_deposit_index, deposit_requests_start_index) } pub async fn get_attester_slashings(&self) -> Vec> { diff --git a/block_producer/src/eth1_storage.rs b/block_producer/src/eth1_storage.rs index 0b8c1a6..d80be31 100644 --- a/block_producer/src/eth1_storage.rs +++ b/block_producer/src/eth1_storage.rs @@ -290,17 +290,34 @@ pub trait Eth1Storage { deposits.try_into().map_err(Into::into) } - fn finalize_deposits(&self, finalized_deposit_index: DepositIndex) -> Result<()> { - features::log!(DebugEth1, "Finalizing deposits: {finalized_deposit_index}"); + fn finalize_deposits( + &self, + finalized_deposit_index: DepositIndex, + deposit_requests_start_index: Option, + ) -> Result<()> { + features::log!( + DebugEth1, + "Finalizing deposits: {finalized_deposit_index}, \ + deposit requests start index: {deposit_requests_start_index:?}" + ); let mut unfinalized_blocks = self.unfinalized_blocks_mut(); - let position = unfinalized_blocks.iter().position(|block| { - block - .deposit_events - .iter() - .any(|deposit| deposit.index == finalized_deposit_index) - }); + // Prune downloaded eth1 blocks as a temporary measure: + // `https://eips.ethereum.org/EIPS/eip-6110#eth1data-poll-deprecation` + let deposit_requests_transition_period_finished = deposit_requests_start_index + .is_some_and(|requests_start_index| requests_start_index <= finalized_deposit_index); + + let position = if deposit_requests_transition_period_finished { + Some(unfinalized_blocks.len()) + } else { + unfinalized_blocks.iter().position(|block| { + block + .deposit_events + .iter() + .any(|deposit| deposit.index == finalized_deposit_index) + }) + }; let Some(block_position) = position else { return Ok(()); diff --git a/fork_choice_control/src/messages.rs b/fork_choice_control/src/messages.rs index a2775ca..a9547a0 100644 --- a/fork_choice_control/src/messages.rs +++ b/fork_choice_control/src/messages.rs @@ -198,7 +198,7 @@ impl PoolMessage { pub enum ValidatorMessage { Tick(W, Tick), - FinalizedEth1Data(DepositIndex), + FinalizedEth1Data(DepositIndex, Option), Head(W, ChainLink

), ValidAttestation(W, Arc>), PrepareExecutionPayload(Slot, ExecutionBlockHash, ExecutionBlockHash), diff --git a/fork_choice_control/src/mutator.rs b/fork_choice_control/src/mutator.rs index f272f8b..f3bca16 100644 --- a/fork_choice_control/src/mutator.rs +++ b/fork_choice_control/src/mutator.rs @@ -1662,11 +1662,11 @@ where metrics.set_beacon_previous_justified_epoch(previous_justified_checkpoint.epoch); } + let finalized_state = self.store.last_finalized().state(&self.store); + ValidatorMessage::FinalizedEth1Data( - self.store - .last_finalized() - .state(&self.store) - .eth1_deposit_index(), + finalized_state.eth1_deposit_index(), + finalized_state.deposit_requests_start_index(), ) .send(&self.validator_tx); diff --git a/types/src/combined.rs b/types/src/combined.rs index fefe566..ba949a5 100644 --- a/types/src/combined.rs +++ b/types/src/combined.rs @@ -84,7 +84,10 @@ use crate::{ SignedAggregateAndProof as Phase0SignedAggregateAndProof, SignedBeaconBlock as Phase0SignedBeaconBlock, SignedBeaconBlockHeader, }, - primitives::{ExecutionBlockHash, ExecutionBlockNumber, Slot, UnixSeconds, ValidatorIndex}, + primitives::{ + DepositIndex, ExecutionBlockHash, ExecutionBlockNumber, Slot, UnixSeconds, + ValidatorIndex, + }, }, preset::{Mainnet, Preset}, traits::{ @@ -316,6 +319,17 @@ impl BeaconState

{ Self::Electra(state) => state.set_cached_root(root), } } + + pub fn deposit_requests_start_index(&self) -> Option { + match self { + Self::Phase0(_) + | Self::Altair(_) + | Self::Bellatrix(_) + | Self::Capella(_) + | Self::Deneb(_) => None, + Self::Electra(state) => Some(state.deposit_requests_start_index), + } + } } #[derive(Clone, PartialEq, Eq, Debug, From, VariantCount, Deserialize, Serialize)] diff --git a/types/src/electra/beacon_state.rs b/types/src/electra/beacon_state.rs index c050c85..dd86fdd 100644 --- a/types/src/electra/beacon_state.rs +++ b/types/src/electra/beacon_state.rs @@ -90,7 +90,7 @@ pub struct BeaconState { // > Deep history valid from Capella onwards pub historical_summaries: HistoricalSummaries

, #[serde(with = "serde_utils::string_or_native")] - pub deposit_requests_start_index: u64, + pub deposit_requests_start_index: DepositIndex, #[serde(with = "serde_utils::string_or_native")] pub deposit_balance_to_consume: Gwei, #[serde(with = "serde_utils::string_or_native")] diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 4a13874..24853e6 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -252,8 +252,8 @@ impl Validator { ValidatorMessage::Tick(wait_group, tick) => { self.handle_tick(wait_group, tick).await?; } - ValidatorMessage::FinalizedEth1Data(finalized_eth1_deposit_index) => { - self.block_producer.finalize_deposits(finalized_eth1_deposit_index)?; + ValidatorMessage::FinalizedEth1Data(finalized_eth1_data, deposit_requests_start_index) => { + self.block_producer.finalize_deposits(finalized_eth1_data, deposit_requests_start_index)?; }, ValidatorMessage::Head(wait_group, head) => { if let Some(validator_to_liveness_tx) = &self.validator_to_liveness_tx {