mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-15 02:42:38 +00:00
Add get payload envelope route
This commit is contained in:
@@ -589,14 +589,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
state_root: payload_data.state_root,
|
||||
};
|
||||
|
||||
// Cache the envelope for later retrieval for signing and publishing.
|
||||
// Cache the envelope for later retrieval by the validator for signing and publishing.
|
||||
let envelope_slot = payload_data.slot;
|
||||
self.pending_payload_envelopes
|
||||
.write()
|
||||
.insert(beacon_block_root, execution_payload_envelope);
|
||||
.insert(envelope_slot, execution_payload_envelope);
|
||||
|
||||
debug!(
|
||||
%beacon_block_root,
|
||||
slot = %block.slot(),
|
||||
slot = %envelope_slot,
|
||||
"Cached pending execution payload envelope"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
//! Provides the `PendingPayloadEnvelopes` cache for storing execution payload envelopes
|
||||
//! that have been produced during local block production but not yet imported to fork choice.
|
||||
//! that have been produced during local block production.
|
||||
//!
|
||||
//! For local building, the envelope is created during block production.
|
||||
//! This cache holds the envelopes temporarily until the proposer can sign and publish the payload.
|
||||
//! This cache holds the envelopes temporarily until the validator fetches, signs,
|
||||
//! and publishes the payload.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use types::{EthSpec, ExecutionPayloadEnvelope, Hash256, Slot};
|
||||
use types::{EthSpec, ExecutionPayloadEnvelope, Slot};
|
||||
|
||||
/// Cache for pending execution payload envelopes awaiting publishing.
|
||||
///
|
||||
/// Envelopes are keyed by beacon block root and pruned based on slot age.
|
||||
/// Envelopes are keyed by slot and pruned based on slot age.
|
||||
/// This cache is only used for local building.
|
||||
pub struct PendingPayloadEnvelopes<E: EthSpec> {
|
||||
/// Maximum number of slots to keep envelopes before pruning.
|
||||
max_slot_age: u64,
|
||||
/// The envelopes, keyed by beacon block root.
|
||||
envelopes: HashMap<Hash256, ExecutionPayloadEnvelope<E>>,
|
||||
/// The envelopes, keyed by slot.
|
||||
envelopes: HashMap<Slot, ExecutionPayloadEnvelope<E>>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Default for PendingPayloadEnvelopes<E> {
|
||||
@@ -36,32 +38,31 @@ impl<E: EthSpec> PendingPayloadEnvelopes<E> {
|
||||
}
|
||||
|
||||
/// Insert a pending envelope into the cache.
|
||||
pub fn insert(&mut self, block_root: Hash256, envelope: ExecutionPayloadEnvelope<E>) {
|
||||
self.envelopes.insert(block_root, envelope);
|
||||
pub fn insert(&mut self, slot: Slot, envelope: ExecutionPayloadEnvelope<E>) {
|
||||
self.envelopes.insert(slot, envelope);
|
||||
}
|
||||
|
||||
/// Get a pending envelope by block root.
|
||||
pub fn get(&self, block_root: &Hash256) -> Option<&ExecutionPayloadEnvelope<E>> {
|
||||
self.envelopes.get(block_root)
|
||||
/// Get a pending envelope by slot.
|
||||
pub fn get(&self, slot: Slot) -> Option<&ExecutionPayloadEnvelope<E>> {
|
||||
self.envelopes.get(&slot)
|
||||
}
|
||||
|
||||
/// Remove and return a pending envelope by block root.
|
||||
pub fn remove(&mut self, block_root: &Hash256) -> Option<ExecutionPayloadEnvelope<E>> {
|
||||
self.envelopes.remove(block_root)
|
||||
/// Remove and return a pending envelope by slot.
|
||||
pub fn remove(&mut self, slot: Slot) -> Option<ExecutionPayloadEnvelope<E>> {
|
||||
self.envelopes.remove(&slot)
|
||||
}
|
||||
|
||||
/// Check if an envelope exists for the given block root.
|
||||
pub fn contains(&self, block_root: &Hash256) -> bool {
|
||||
self.envelopes.contains_key(block_root)
|
||||
/// Check if an envelope exists for the given slot.
|
||||
pub fn contains(&self, slot: Slot) -> bool {
|
||||
self.envelopes.contains_key(&slot)
|
||||
}
|
||||
|
||||
/// Prune envelopes older than `current_slot - max_slot_age`.
|
||||
///
|
||||
/// This removes stale envelopes from blocks that were never imported.
|
||||
/// This removes stale envelopes from blocks that were never published.
|
||||
pub fn prune(&mut self, current_slot: Slot) {
|
||||
let min_slot = current_slot.saturating_sub(self.max_slot_age);
|
||||
self.envelopes
|
||||
.retain(|_, envelope| envelope.slot >= min_slot);
|
||||
self.envelopes.retain(|slot, _| *slot >= min_slot);
|
||||
}
|
||||
|
||||
/// Returns the number of pending envelopes in the cache.
|
||||
@@ -78,16 +79,16 @@ impl<E: EthSpec> PendingPayloadEnvelopes<E> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use types::{ExecutionPayloadGloas, ExecutionRequests, MainnetEthSpec};
|
||||
use types::{ExecutionPayloadGloas, ExecutionRequests, Hash256, MainnetEthSpec};
|
||||
|
||||
type E = MainnetEthSpec;
|
||||
|
||||
fn make_envelope(slot: Slot, block_root: Hash256) -> ExecutionPayloadEnvelope<E> {
|
||||
fn make_envelope(slot: Slot) -> ExecutionPayloadEnvelope<E> {
|
||||
ExecutionPayloadEnvelope {
|
||||
payload: ExecutionPayloadGloas::default(),
|
||||
execution_requests: ExecutionRequests::default(),
|
||||
builder_index: 0,
|
||||
beacon_block_root: block_root,
|
||||
beacon_block_root: Hash256::ZERO,
|
||||
slot,
|
||||
state_root: Hash256::ZERO,
|
||||
}
|
||||
@@ -96,31 +97,31 @@ mod tests {
|
||||
#[test]
|
||||
fn insert_and_get() {
|
||||
let mut cache = PendingPayloadEnvelopes::<E>::default();
|
||||
let block_root = Hash256::repeat_byte(1);
|
||||
let envelope = make_envelope(Slot::new(1), block_root);
|
||||
let slot = Slot::new(1);
|
||||
let envelope = make_envelope(slot);
|
||||
|
||||
assert!(!cache.contains(&block_root));
|
||||
assert!(!cache.contains(slot));
|
||||
assert_eq!(cache.len(), 0);
|
||||
|
||||
cache.insert(block_root, envelope.clone());
|
||||
cache.insert(slot, envelope.clone());
|
||||
|
||||
assert!(cache.contains(&block_root));
|
||||
assert!(cache.contains(slot));
|
||||
assert_eq!(cache.len(), 1);
|
||||
assert_eq!(cache.get(&block_root), Some(&envelope));
|
||||
assert_eq!(cache.get(slot), Some(&envelope));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove() {
|
||||
let mut cache = PendingPayloadEnvelopes::<E>::default();
|
||||
let block_root = Hash256::repeat_byte(1);
|
||||
let envelope = make_envelope(Slot::new(1), block_root);
|
||||
let slot = Slot::new(1);
|
||||
let envelope = make_envelope(slot);
|
||||
|
||||
cache.insert(block_root, envelope.clone());
|
||||
assert!(cache.contains(&block_root));
|
||||
cache.insert(slot, envelope.clone());
|
||||
assert!(cache.contains(slot));
|
||||
|
||||
let removed = cache.remove(&block_root);
|
||||
let removed = cache.remove(slot);
|
||||
assert_eq!(removed, Some(envelope));
|
||||
assert!(!cache.contains(&block_root));
|
||||
assert!(!cache.contains(slot));
|
||||
assert_eq!(cache.len(), 0);
|
||||
}
|
||||
|
||||
@@ -129,14 +130,12 @@ mod tests {
|
||||
let mut cache = PendingPayloadEnvelopes::<E>::new(2);
|
||||
|
||||
// Insert envelope at slot 5
|
||||
let block_root_1 = Hash256::repeat_byte(1);
|
||||
let envelope_1 = make_envelope(Slot::new(5), block_root_1);
|
||||
cache.insert(block_root_1, envelope_1);
|
||||
let slot_1 = Slot::new(5);
|
||||
cache.insert(slot_1, make_envelope(slot_1));
|
||||
|
||||
// Insert envelope at slot 10
|
||||
let block_root_2 = Hash256::repeat_byte(2);
|
||||
let envelope_2 = make_envelope(Slot::new(10), block_root_2);
|
||||
cache.insert(block_root_2, envelope_2);
|
||||
let slot_2 = Slot::new(10);
|
||||
cache.insert(slot_2, make_envelope(slot_2));
|
||||
|
||||
assert_eq!(cache.len(), 2);
|
||||
|
||||
@@ -144,7 +143,7 @@ mod tests {
|
||||
cache.prune(Slot::new(10));
|
||||
|
||||
assert_eq!(cache.len(), 1);
|
||||
assert!(!cache.contains(&block_root_1)); // slot 5 < 8, pruned
|
||||
assert!(cache.contains(&block_root_2)); // slot 10 >= 8, kept
|
||||
assert!(!cache.contains(slot_1)); // slot 5 < 8, pruned
|
||||
assert!(cache.contains(slot_2)); // slot 10 >= 8, kept
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user