-
Notifications
You must be signed in to change notification settings - Fork 971
Gloas payload envelope processing [WIP] #8806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
43c24d3
a4b993f
8204241
22f3fd4
9f972d1
f637a68
4c70392
5796864
47782a6
7d0d438
72fe220
64c30b8
1859bc2
fd9d4a7
de2362a
b525fe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| //! This module provides the `EnvelopeTimesCache` which contains information regarding payload | ||
| //! envelope timings. | ||
| //! | ||
| //! This provides `BeaconChain` and associated functions with access to the timestamps of when a | ||
| //! payload envelope was observed, verified, executed, and imported. | ||
| //! This allows for better traceability and allows us to determine the root cause for why an | ||
| //! envelope was imported late. | ||
| //! This allows us to distinguish between the following scenarios: | ||
| //! - The envelope was observed late. | ||
| //! - Consensus verification was slow. | ||
| //! - Execution verification was slow. | ||
| //! - The DB write was slow. | ||
|
|
||
| use eth2::types::{Hash256, Slot}; | ||
| use std::collections::HashMap; | ||
| use std::time::Duration; | ||
|
|
||
| type BlockRoot = Hash256; | ||
|
|
||
| #[derive(Clone, Default)] | ||
| pub struct EnvelopeTimestamps { | ||
| /// When the envelope was first observed (gossip or RPC). | ||
| pub observed: Option<Duration>, | ||
| /// When consensus verification (state transition) completed. | ||
| pub consensus_verified: Option<Duration>, | ||
| /// When execution layer verification started. | ||
| pub started_execution: Option<Duration>, | ||
| /// When execution layer verification completed. | ||
| pub executed: Option<Duration>, | ||
| /// When the envelope was imported into the DB. | ||
| pub imported: Option<Duration>, | ||
| } | ||
|
|
||
| /// Delay data for envelope processing, computed relative to the slot start time. | ||
| #[derive(Debug, Default)] | ||
| pub struct EnvelopeDelays { | ||
| /// Time after start of slot we saw the envelope. | ||
| pub observed: Option<Duration>, | ||
| /// The time it took to complete consensus verification of the envelope. | ||
| pub consensus_verification_time: Option<Duration>, | ||
| /// The time it took to complete execution verification of the envelope. | ||
| pub execution_time: Option<Duration>, | ||
| /// Time after execution until the envelope was imported. | ||
| pub imported: Option<Duration>, | ||
| } | ||
|
|
||
| impl EnvelopeDelays { | ||
| fn new(times: EnvelopeTimestamps, slot_start_time: Duration) -> EnvelopeDelays { | ||
| let observed = times | ||
| .observed | ||
| .and_then(|observed_time| observed_time.checked_sub(slot_start_time)); | ||
| let consensus_verification_time = times | ||
| .consensus_verified | ||
| .and_then(|consensus_verified| consensus_verified.checked_sub(times.observed?)); | ||
| let execution_time = times | ||
| .executed | ||
| .and_then(|executed| executed.checked_sub(times.started_execution?)); | ||
| let imported = times | ||
| .imported | ||
| .and_then(|imported_time| imported_time.checked_sub(times.executed?)); | ||
| EnvelopeDelays { | ||
| observed, | ||
| consensus_verification_time, | ||
| execution_time, | ||
| imported, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct EnvelopeTimesCacheValue { | ||
| pub slot: Slot, | ||
| pub timestamps: EnvelopeTimestamps, | ||
| pub peer_id: Option<String>, | ||
| } | ||
|
|
||
| impl EnvelopeTimesCacheValue { | ||
| fn new(slot: Slot) -> Self { | ||
| EnvelopeTimesCacheValue { | ||
| slot, | ||
| timestamps: Default::default(), | ||
| peer_id: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub struct EnvelopeTimesCache { | ||
| pub cache: HashMap<BlockRoot, EnvelopeTimesCacheValue>, | ||
| } | ||
|
|
||
| impl EnvelopeTimesCache { | ||
| /// Set the observation time for `block_root` to `timestamp` if `timestamp` is less than | ||
| /// any previous timestamp at which this envelope was observed. | ||
| pub fn set_time_observed( | ||
| &mut self, | ||
| block_root: BlockRoot, | ||
| slot: Slot, | ||
| timestamp: Duration, | ||
| peer_id: Option<String>, | ||
| ) { | ||
| let entry = self | ||
| .cache | ||
| .entry(block_root) | ||
| .or_insert_with(|| EnvelopeTimesCacheValue::new(slot)); | ||
| match entry.timestamps.observed { | ||
| Some(existing) if existing <= timestamp => { | ||
| // Existing timestamp is earlier, do nothing. | ||
| } | ||
| _ => { | ||
| entry.timestamps.observed = Some(timestamp); | ||
| entry.peer_id = peer_id; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Set the timestamp for `field` if that timestamp is less than any previously known value. | ||
| fn set_time_if_less( | ||
| &mut self, | ||
| block_root: BlockRoot, | ||
| slot: Slot, | ||
| field: impl Fn(&mut EnvelopeTimestamps) -> &mut Option<Duration>, | ||
| timestamp: Duration, | ||
| ) { | ||
| let entry = self | ||
| .cache | ||
| .entry(block_root) | ||
| .or_insert_with(|| EnvelopeTimesCacheValue::new(slot)); | ||
| let existing_timestamp = field(&mut entry.timestamps); | ||
| if existing_timestamp.is_none_or(|prev| timestamp < prev) { | ||
| *existing_timestamp = Some(timestamp); | ||
| } | ||
| } | ||
|
|
||
| pub fn set_time_consensus_verified( | ||
| &mut self, | ||
| block_root: BlockRoot, | ||
| slot: Slot, | ||
| timestamp: Duration, | ||
| ) { | ||
| self.set_time_if_less( | ||
| block_root, | ||
| slot, | ||
| |timestamps| &mut timestamps.consensus_verified, | ||
| timestamp, | ||
| ) | ||
| } | ||
|
|
||
| pub fn set_time_started_execution( | ||
| &mut self, | ||
| block_root: BlockRoot, | ||
| slot: Slot, | ||
| timestamp: Duration, | ||
| ) { | ||
| self.set_time_if_less( | ||
| block_root, | ||
| slot, | ||
| |timestamps| &mut timestamps.started_execution, | ||
| timestamp, | ||
| ) | ||
| } | ||
|
|
||
| pub fn set_time_executed(&mut self, block_root: BlockRoot, slot: Slot, timestamp: Duration) { | ||
| self.set_time_if_less( | ||
| block_root, | ||
| slot, | ||
| |timestamps| &mut timestamps.executed, | ||
| timestamp, | ||
| ) | ||
| } | ||
|
|
||
| pub fn set_time_imported(&mut self, block_root: BlockRoot, slot: Slot, timestamp: Duration) { | ||
| self.set_time_if_less( | ||
| block_root, | ||
| slot, | ||
| |timestamps| &mut timestamps.imported, | ||
| timestamp, | ||
| ) | ||
| } | ||
|
|
||
| pub fn get_envelope_delays( | ||
| &self, | ||
| block_root: BlockRoot, | ||
| slot_start_time: Duration, | ||
| ) -> EnvelopeDelays { | ||
| if let Some(entry) = self.cache.get(&block_root) { | ||
| EnvelopeDelays::new(entry.timestamps.clone(), slot_start_time) | ||
| } else { | ||
| EnvelopeDelays::default() | ||
| } | ||
| } | ||
|
|
||
| /// Prune the cache to only store the most recent 2 epochs. | ||
| pub fn prune(&mut self, current_slot: Slot) { | ||
| self.cache | ||
| .retain(|_, entry| entry.slot > current_slot.saturating_sub(64_u64)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,12 +112,17 @@ impl<T: BeaconChainTypes> PayloadNotifier<T> { | |
| if let Some(precomputed_status) = self.payload_verification_status { | ||
| Ok(precomputed_status) | ||
| } else { | ||
| notify_new_payload(&self.chain, self.block.message()).await | ||
| notify_new_payload( | ||
| &self.chain, | ||
| self.block.message().tree_hash_root(), | ||
| self.block.message().try_into()?, | ||
| ) | ||
| .await | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Verify that `execution_payload` contained by `block` is considered valid by an execution | ||
| /// Verify that `execution_payload` associated with `beacon_block_root` is considered valid by an execution | ||
| /// engine. | ||
| /// | ||
| /// ## Specification | ||
|
|
@@ -126,17 +131,20 @@ impl<T: BeaconChainTypes> PayloadNotifier<T> { | |
| /// contains a few extra checks by running `partially_verify_execution_payload` first: | ||
| /// | ||
| /// https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/bellatrix/beacon-chain.md#notify_new_payload | ||
| async fn notify_new_payload<T: BeaconChainTypes>( | ||
| pub async fn notify_new_payload<T: BeaconChainTypes>( | ||
| chain: &Arc<BeaconChain<T>>, | ||
| block: BeaconBlockRef<'_, T::EthSpec>, | ||
| beacon_block_root: Hash256, | ||
| new_payload_request: NewPayloadRequest<'_, T::EthSpec>, | ||
| ) -> Result<PayloadVerificationStatus, BlockError> { | ||
| let execution_layer = chain | ||
| .execution_layer | ||
| .as_ref() | ||
| .ok_or(ExecutionPayloadError::NoExecutionConnection)?; | ||
|
|
||
| let execution_block_hash = block.execution_payload()?.block_hash(); | ||
| let new_payload_response = execution_layer.notify_new_payload(block.try_into()?).await; | ||
| let execution_block_hash = new_payload_request.execution_payload_ref().block_hash(); | ||
| let new_payload_response = execution_layer | ||
| .notify_new_payload(new_payload_request.clone()) | ||
| .await; | ||
|
|
||
| match new_payload_response { | ||
| Ok(status) => match status { | ||
|
|
@@ -152,10 +160,11 @@ async fn notify_new_payload<T: BeaconChainTypes>( | |
| ?validation_error, | ||
| ?latest_valid_hash, | ||
| ?execution_block_hash, | ||
| root = ?block.tree_hash_root(), | ||
| graffiti = block.body().graffiti().as_utf8_lossy(), | ||
| proposer_index = block.proposer_index(), | ||
| slot = %block.slot(), | ||
| // TODO(gloas) are these other logs important? | ||
| root = ?beacon_block_root, | ||
| // graffiti = block.body().graffiti().as_utf8_lossy(), | ||
| // proposer_index = block.proposer_index(), | ||
| // slot = %block.slot(), | ||
|
Comment on lines
+163
to
+167
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we wont have this info post gloas, I'd like to delete these fields if thats okay
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. slot might be useful too, otherwise i agree |
||
| method = "new_payload", | ||
| "Invalid execution payload" | ||
| ); | ||
|
|
@@ -178,11 +187,11 @@ async fn notify_new_payload<T: BeaconChainTypes>( | |
| { | ||
| // This block has not yet been applied to fork choice, so the latest block that was | ||
| // imported to fork choice was the parent. | ||
| let latest_root = block.parent_root(); | ||
| let latest_root = new_payload_request.parent_beacon_block_root()?; | ||
|
|
||
| chain | ||
| .process_invalid_execution_payload(&InvalidationOperation::InvalidateMany { | ||
| head_block_root: latest_root, | ||
| head_block_root: *latest_root, | ||
| always_invalidate_head: false, | ||
| latest_valid_ancestor: latest_valid_hash, | ||
| }) | ||
|
|
@@ -197,10 +206,11 @@ async fn notify_new_payload<T: BeaconChainTypes>( | |
| warn!( | ||
| ?validation_error, | ||
| ?execution_block_hash, | ||
| root = ?block.tree_hash_root(), | ||
| graffiti = block.body().graffiti().as_utf8_lossy(), | ||
| proposer_index = block.proposer_index(), | ||
| slot = %block.slot(), | ||
| // TODO(gloas) are these other logs important? | ||
| root = ?beacon_block_root, | ||
| // graffiti = block.body().graffiti().as_utf8_lossy(), | ||
| // proposer_index = block.proposer_index(), | ||
| // slot = %block.slot(), | ||
|
Comment on lines
+209
to
+213
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would like to delete these as well |
||
| method = "new_payload", | ||
| "Invalid execution payload block hash" | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could potentially look up a block status table here to figure out if we have processed the block and it turned out to be invalid