mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-17 21:08:32 +00:00
SSE and enw endpoint
This commit is contained in:
@@ -1101,6 +1101,14 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
|
||||
})));
|
||||
}
|
||||
|
||||
// Beacon API execution_payload_bid events
|
||||
if let Some(event_handler) = chain.event_handler.as_ref()
|
||||
&& event_handler.has_execution_payload_bid_subscribers()
|
||||
&& let Ok(bid) = block.message().body().signed_execution_payload_bid()
|
||||
{
|
||||
event_handler.register(EventKind::ExecutionPayloadBid(Box::new(bid.clone())));
|
||||
}
|
||||
|
||||
// Having checked the proposer index and the block root we can cache them.
|
||||
let consensus_context = ConsensusContext::new(block.slot())
|
||||
.set_current_block_root(block_root)
|
||||
|
||||
@@ -26,6 +26,8 @@ pub struct ServerSentEventHandler<E: EthSpec> {
|
||||
attester_slashing_tx: Sender<EventKind<E>>,
|
||||
bls_to_execution_change_tx: Sender<EventKind<E>>,
|
||||
block_gossip_tx: Sender<EventKind<E>>,
|
||||
execution_payload_bid_tx: Sender<EventKind<E>>,
|
||||
execution_payload_available_tx: Sender<EventKind<E>>,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> ServerSentEventHandler<E> {
|
||||
@@ -53,6 +55,8 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
|
||||
let (attester_slashing_tx, _) = broadcast::channel(capacity);
|
||||
let (bls_to_execution_change_tx, _) = broadcast::channel(capacity);
|
||||
let (block_gossip_tx, _) = broadcast::channel(capacity);
|
||||
let (execution_payload_bid_tx, _) = broadcast::channel(capacity);
|
||||
let (execution_payload_available_tx, _) = broadcast::channel(capacity);
|
||||
|
||||
Self {
|
||||
attestation_tx,
|
||||
@@ -74,6 +78,8 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
|
||||
attester_slashing_tx,
|
||||
bls_to_execution_change_tx,
|
||||
block_gossip_tx,
|
||||
execution_payload_bid_tx,
|
||||
execution_payload_available_tx,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,6 +168,14 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
|
||||
.block_gossip_tx
|
||||
.send(kind)
|
||||
.map(|count| log_count("block gossip", count)),
|
||||
EventKind::ExecutionPayloadBid(_) => self
|
||||
.execution_payload_bid_tx
|
||||
.send(kind)
|
||||
.map(|count| log_count("execution payload bid", count)),
|
||||
EventKind::ExecutionPayloadAvailable(_) => self
|
||||
.execution_payload_available_tx
|
||||
.send(kind)
|
||||
.map(|count| log_count("execution payload available", count)),
|
||||
};
|
||||
if let Err(SendError(event)) = result {
|
||||
trace!(?event, "No receivers registered to listen for event");
|
||||
@@ -311,4 +325,20 @@ impl<E: EthSpec> ServerSentEventHandler<E> {
|
||||
pub fn has_block_gossip_subscribers(&self) -> bool {
|
||||
self.block_gossip_tx.receiver_count() > 0
|
||||
}
|
||||
|
||||
pub fn subscribe_execution_payload_bid(&self) -> Receiver<EventKind<E>> {
|
||||
self.execution_payload_bid_tx.subscribe()
|
||||
}
|
||||
|
||||
pub fn subscribe_execution_payload_available(&self) -> Receiver<EventKind<E>> {
|
||||
self.execution_payload_available_tx.subscribe()
|
||||
}
|
||||
|
||||
pub fn has_execution_payload_bid_subscribers(&self) -> bool {
|
||||
self.execution_payload_bid_tx.receiver_count() > 0
|
||||
}
|
||||
|
||||
pub fn has_execution_payload_available_subscribers(&self) -> bool {
|
||||
self.execution_payload_available_tx.receiver_count() > 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,9 +387,18 @@ pub fn get_execution_payload<T: BeaconChainTypes>(
|
||||
let timestamp =
|
||||
compute_timestamp_at_slot(state, state.slot(), spec).map_err(BeaconStateError::from)?;
|
||||
let random = *state.get_randao_mix(current_epoch)?;
|
||||
let latest_execution_payload_header = state.latest_execution_payload_header()?;
|
||||
let latest_execution_payload_header_block_hash = latest_execution_payload_header.block_hash();
|
||||
let latest_execution_payload_header_gas_limit = latest_execution_payload_header.gas_limit();
|
||||
// In GLOAS (ePBS), the execution payload header is replaced by
|
||||
// `latest_block_hash` and `latest_execution_payload_bid`.
|
||||
let (latest_execution_payload_header_block_hash, latest_execution_payload_header_gas_limit) =
|
||||
if state.fork_name_unchecked() == ForkName::Gloas {
|
||||
(
|
||||
*state.latest_block_hash()?,
|
||||
state.latest_execution_payload_bid()?.gas_limit,
|
||||
)
|
||||
} else {
|
||||
let header = state.latest_execution_payload_header()?;
|
||||
(header.block_hash(), header.gas_limit())
|
||||
};
|
||||
let withdrawals = if state.fork_name_unchecked().capella_enabled() {
|
||||
Some(Withdrawals::<T::EthSpec>::from(get_expected_withdrawals(state, spec)?).into())
|
||||
} else {
|
||||
|
||||
@@ -19,6 +19,7 @@ use crate::{
|
||||
metrics,
|
||||
validator_monitor::{get_slot_delay_ms, timestamp_now},
|
||||
};
|
||||
use eth2::types::{EventKind, SseExecutionPayloadAvailable};
|
||||
|
||||
impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
/// Returns `Ok(block_root)` if the given `unverified_envelope` was successfully verified and
|
||||
@@ -357,6 +358,16 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
|
||||
);
|
||||
}
|
||||
|
||||
// TODO(gloas) emit SSE event for envelope import (similar to SseBlock for blocks).
|
||||
// Beacon API execution_payload_available events
|
||||
if let Some(event_handler) = self.event_handler.as_ref()
|
||||
&& event_handler.has_execution_payload_available_subscribers()
|
||||
{
|
||||
event_handler.register(EventKind::ExecutionPayloadAvailable(
|
||||
SseExecutionPayloadAvailable {
|
||||
slot: envelope_slot,
|
||||
block_root,
|
||||
},
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
use crate::block_id::BlockId;
|
||||
use crate::task_spawner::{Priority, TaskSpawner};
|
||||
use crate::utils::{ChainFilter, EthV1Filter, NetworkTxFilter, ResponseFilter, TaskSpawnerFilter};
|
||||
use crate::version::{
|
||||
ResponseIncludesVersion, add_consensus_version_header, add_ssz_content_type_header,
|
||||
execution_optimistic_finalized_beacon_response,
|
||||
};
|
||||
use beacon_chain::{BeaconChain, BeaconChainTypes};
|
||||
use bytes::Bytes;
|
||||
use eth2::types as api_types;
|
||||
use eth2::{CONTENT_TYPE_HEADER, SSZ_CONTENT_TYPE_HEADER};
|
||||
use lighthouse_network::PubsubMessage;
|
||||
use network::NetworkMessage;
|
||||
use ssz::Decode;
|
||||
use ssz::{Decode, Encode};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::UnboundedSender;
|
||||
use tracing::{info, warn};
|
||||
@@ -127,3 +133,67 @@ pub async fn publish_execution_payload_envelope<T: BeaconChainTypes>(
|
||||
|
||||
Ok(warp::reply().into_response())
|
||||
}
|
||||
|
||||
// GET beacon/execution_payload_envelope/{block_id}
|
||||
pub(crate) fn get_beacon_execution_payload_envelope<T: BeaconChainTypes>(
|
||||
eth_v1: EthV1Filter,
|
||||
block_id_or_err: impl Filter<Extract = (BlockId,), Error = Rejection> + Clone + Send + Sync + 'static,
|
||||
task_spawner_filter: TaskSpawnerFilter<T>,
|
||||
chain_filter: ChainFilter<T>,
|
||||
) -> ResponseFilter {
|
||||
eth_v1
|
||||
.and(warp::path("beacon"))
|
||||
.and(warp::path("execution_payload_envelope"))
|
||||
.and(block_id_or_err)
|
||||
.and(warp::path::end())
|
||||
.and(task_spawner_filter)
|
||||
.and(chain_filter)
|
||||
.and(warp::header::optional::<api_types::Accept>("accept"))
|
||||
.then(
|
||||
|block_id: BlockId,
|
||||
task_spawner: TaskSpawner<T::EthSpec>,
|
||||
chain: Arc<BeaconChain<T>>,
|
||||
accept_header: Option<api_types::Accept>| {
|
||||
task_spawner.blocking_response_task(Priority::P1, move || {
|
||||
let (root, execution_optimistic, finalized) = block_id.root(&chain)?;
|
||||
|
||||
let envelope = chain
|
||||
.get_payload_envelope(&root)
|
||||
.map_err(warp_utils::reject::unhandled_error)?
|
||||
.ok_or_else(|| {
|
||||
warp_utils::reject::custom_not_found(format!(
|
||||
"execution payload envelope for block root {root}"
|
||||
))
|
||||
})?;
|
||||
|
||||
let fork_name = chain
|
||||
.spec
|
||||
.fork_name_at_slot::<T::EthSpec>(envelope.message.slot);
|
||||
|
||||
match accept_header {
|
||||
Some(api_types::Accept::Ssz) => warp::http::Response::builder()
|
||||
.status(200)
|
||||
.body(warp::hyper::Body::from(envelope.as_ssz_bytes()))
|
||||
.map(add_ssz_content_type_header)
|
||||
.map_err(|e| {
|
||||
warp_utils::reject::custom_server_error(format!(
|
||||
"failed to create response: {}",
|
||||
e
|
||||
))
|
||||
}),
|
||||
_ => {
|
||||
let res = execution_optimistic_finalized_beacon_response(
|
||||
ResponseIncludesVersion::Yes(fork_name),
|
||||
execution_optimistic,
|
||||
finalized,
|
||||
&envelope,
|
||||
)?;
|
||||
Ok(warp::reply::json(&res).into_response())
|
||||
}
|
||||
}
|
||||
.map(|resp| add_consensus_version_header(resp, fork_name))
|
||||
})
|
||||
},
|
||||
)
|
||||
.boxed()
|
||||
}
|
||||
|
||||
@@ -37,7 +37,8 @@ mod validators;
|
||||
mod version;
|
||||
|
||||
use crate::beacon::execution_payload_envelope::{
|
||||
post_beacon_execution_payload_envelope, post_beacon_execution_payload_envelope_ssz,
|
||||
get_beacon_execution_payload_envelope, post_beacon_execution_payload_envelope,
|
||||
post_beacon_execution_payload_envelope_ssz,
|
||||
};
|
||||
use crate::beacon::pool::*;
|
||||
use crate::light_client::{get_light_client_bootstrap, get_light_client_updates};
|
||||
@@ -1506,6 +1507,14 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
network_tx_filter.clone(),
|
||||
);
|
||||
|
||||
// GET beacon/execution_payload_envelope/{block_id}
|
||||
let get_beacon_execution_payload_envelope = get_beacon_execution_payload_envelope(
|
||||
eth_v1.clone(),
|
||||
block_id_or_err,
|
||||
task_spawner_filter.clone(),
|
||||
chain_filter.clone(),
|
||||
);
|
||||
|
||||
let beacon_rewards_path = eth_v1
|
||||
.clone()
|
||||
.and(warp::path("beacon"))
|
||||
@@ -3217,6 +3226,12 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
api_types::EventTopic::BlockGossip => {
|
||||
event_handler.subscribe_block_gossip()
|
||||
}
|
||||
api_types::EventTopic::ExecutionPayloadBid => {
|
||||
event_handler.subscribe_execution_payload_bid()
|
||||
}
|
||||
api_types::EventTopic::ExecutionPayloadAvailable => {
|
||||
event_handler.subscribe_execution_payload_available()
|
||||
}
|
||||
};
|
||||
|
||||
receivers.push(
|
||||
@@ -3341,6 +3356,7 @@ pub fn serve<T: BeaconChainTypes>(
|
||||
.uor(get_beacon_block_root)
|
||||
.uor(get_blob_sidecars)
|
||||
.uor(get_blobs)
|
||||
.uor(get_beacon_execution_payload_envelope)
|
||||
.uor(get_beacon_pool_attestations)
|
||||
.uor(get_beacon_pool_attester_slashings)
|
||||
.uor(get_beacon_pool_proposer_slashings)
|
||||
|
||||
Reference in New Issue
Block a user