Skip to content

Commit

Permalink
Prune Eth1 unfinalized blocks if deposits requests are active
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumas committed Nov 14, 2024
1 parent 1bf5024 commit c664f18
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
8 changes: 6 additions & 2 deletions block_producer/src/block_producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,14 @@ impl<P: Preset, W: Wait> BlockProducer<P, W> {
})
}

pub fn finalize_deposits(&self, finalized_deposit_index: DepositIndex) -> Result<()> {
pub fn finalize_deposits(
&self,
finalized_deposit_index: DepositIndex,
deposit_requests_start_index: Option<DepositIndex>,
) -> 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<AttesterSlashing<P>> {
Expand Down
33 changes: 25 additions & 8 deletions block_producer/src/eth1_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DepositIndex>,
) -> 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(());
Expand Down
2 changes: 1 addition & 1 deletion fork_choice_control/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl PoolMessage {

pub enum ValidatorMessage<P: Preset, W> {
Tick(W, Tick),
FinalizedEth1Data(DepositIndex),
FinalizedEth1Data(DepositIndex, Option<DepositIndex>),
Head(W, ChainLink<P>),
ValidAttestation(W, Arc<Attestation<P>>),
PrepareExecutionPayload(Slot, ExecutionBlockHash, ExecutionBlockHash),
Expand Down
8 changes: 4 additions & 4 deletions fork_choice_control/src/mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 15 additions & 1 deletion types/src/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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::{
Expand Down Expand Up @@ -320,6 +323,17 @@ impl<P: Preset> BeaconState<P> {
Self::Electra(state) => state.set_cached_root(root),
}
}

pub fn deposit_requests_start_index(&self) -> Option<DepositIndex> {
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)]
Expand Down
2 changes: 1 addition & 1 deletion types/src/electra/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub struct BeaconState<P: Preset> {
// > Deep history valid from Capella onwards
pub historical_summaries: HistoricalSummaries<P>,
#[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")]
Expand Down
4 changes: 2 additions & 2 deletions validator/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ impl<P: Preset, W: Wait + Sync> Validator<P, W> {
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 {
Expand Down

0 comments on commit c664f18

Please sign in to comment.