|
| 1 | +use std::{collections::HashSet, sync::Arc}; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use dashmap::DashMap; |
| 5 | +use derive_more::derive::Constructor; |
| 6 | +use eth2_libp2p::PeerId; |
| 7 | +use execution_engine::BlobAndProofV1; |
| 8 | +use fork_choice_control::Wait; |
| 9 | +use futures::{ |
| 10 | + channel::mpsc::{UnboundedReceiver, UnboundedSender}, |
| 11 | + StreamExt as _, |
| 12 | +}; |
| 13 | +use helper_functions::misc; |
| 14 | +use log::{debug, warn}; |
| 15 | +use types::{ |
| 16 | + combined::SignedBeaconBlock, |
| 17 | + deneb::{containers::BlobIdentifier, primitives::BlobIndex}, |
| 18 | + phase0::primitives::Slot, |
| 19 | + preset::Preset, |
| 20 | + traits::SignedBeaconBlock as _, |
| 21 | +}; |
| 22 | + |
| 23 | +use crate::{ |
| 24 | + messages::{BlobFetcherToP2p, Eth1ApiToBlobFetcher}, |
| 25 | + ApiController, Eth1Api, |
| 26 | +}; |
| 27 | + |
| 28 | +#[derive(Constructor)] |
| 29 | +pub struct ExecutionBlobFetcher<P: Preset, W: Wait> { |
| 30 | + api: Arc<Eth1Api>, |
| 31 | + controller: ApiController<P, W>, |
| 32 | + received_blob_sidecars: Arc<DashMap<BlobIdentifier, Slot>>, |
| 33 | + p2p_tx: UnboundedSender<BlobFetcherToP2p>, |
| 34 | + rx: UnboundedReceiver<Eth1ApiToBlobFetcher<P>>, |
| 35 | +} |
| 36 | + |
| 37 | +impl<P: Preset, W: Wait> ExecutionBlobFetcher<P, W> { |
| 38 | + pub async fn run(mut self) -> Result<()> { |
| 39 | + while let Some(message) = self.rx.next().await { |
| 40 | + match message { |
| 41 | + Eth1ApiToBlobFetcher::GetBlobs { |
| 42 | + block, |
| 43 | + blob_identifiers, |
| 44 | + peer_id, |
| 45 | + } => { |
| 46 | + self.get_blobs(block, blob_identifiers, peer_id).await; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + async fn get_blobs( |
| 55 | + &self, |
| 56 | + block: Arc<SignedBeaconBlock<P>>, |
| 57 | + blob_identifiers: Vec<BlobIdentifier>, |
| 58 | + peer_id: Option<PeerId>, |
| 59 | + ) { |
| 60 | + let slot = block.message().slot(); |
| 61 | + |
| 62 | + if let Some(body) = block.message().body().post_deneb() { |
| 63 | + let missing_blob_indices = blob_identifiers |
| 64 | + .iter() |
| 65 | + .filter(|identifier| !self.received_blob_sidecars.contains_key(identifier)) |
| 66 | + .map(|identifier| identifier.index) |
| 67 | + .collect::<HashSet<BlobIndex>>(); |
| 68 | + |
| 69 | + let kzg_commitments = body |
| 70 | + .blob_kzg_commitments() |
| 71 | + .iter() |
| 72 | + .zip(0..) |
| 73 | + .filter(|(_, index)| missing_blob_indices.contains(index)) |
| 74 | + .collect::<Vec<_>>(); |
| 75 | + |
| 76 | + if kzg_commitments.is_empty() { |
| 77 | + debug!( |
| 78 | + "cannot fetch blobs from EL: all requested blob sidecars have been received" |
| 79 | + ); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + let versioned_hashes = kzg_commitments |
| 84 | + .iter() |
| 85 | + .copied() |
| 86 | + .map(|(commitment, _)| misc::kzg_commitment_to_versioned_hash(*commitment)) |
| 87 | + .collect(); |
| 88 | + |
| 89 | + let mut blob_sidecars = vec![]; |
| 90 | + let block_root = block.message().hash_tree_root(); |
| 91 | + |
| 92 | + match self.api.get_blobs::<P>(versioned_hashes).await { |
| 93 | + Ok(blobs_and_proofs) => { |
| 94 | + let block_header = block.to_header(); |
| 95 | + |
| 96 | + for (blob_and_proof, kzg_commitment, index) in blobs_and_proofs |
| 97 | + .into_iter() |
| 98 | + .zip(kzg_commitments.into_iter()) |
| 99 | + .filter_map(|(blob_and_proof, (kzg_commitment, index))| { |
| 100 | + blob_and_proof |
| 101 | + .map(|blob_and_proof| (blob_and_proof, kzg_commitment, index)) |
| 102 | + }) |
| 103 | + { |
| 104 | + let BlobAndProofV1 { blob, proof } = blob_and_proof; |
| 105 | + let blob_identifier = BlobIdentifier { block_root, index }; |
| 106 | + |
| 107 | + if self.received_blob_sidecars.contains_key(&blob_identifier) { |
| 108 | + debug!( |
| 109 | + "received blob from EL is already known: {blob_identifier:?}, \ |
| 110 | + slot: {slot}" |
| 111 | + ); |
| 112 | + } else { |
| 113 | + match misc::construct_blob_sidecar( |
| 114 | + &block, |
| 115 | + block_header, |
| 116 | + index, |
| 117 | + blob, |
| 118 | + *kzg_commitment, |
| 119 | + proof, |
| 120 | + ) { |
| 121 | + Ok(blob_sidecar) => { |
| 122 | + debug!( |
| 123 | + "received blob sidecar from EL: {blob_identifier:?}, \ |
| 124 | + slot: {slot}" |
| 125 | + ); |
| 126 | + |
| 127 | + // Record all blob_sidecars as received first and push to controller |
| 128 | + // on a second pass to avoid spawning extra `engine_getBlobs` calls. |
| 129 | + self.received_blob_sidecars.insert(blob_identifier, slot); |
| 130 | + blob_sidecars.push(Arc::new(blob_sidecar)); |
| 131 | + } |
| 132 | + Err(error) => warn!( |
| 133 | + "failed to construct blob sidecar with blob and proof \ |
| 134 | + received from execution layer: {error:?}" |
| 135 | + ), |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + Err(error) => warn!("engine_getBlobsV1 call failed: {error}"), |
| 141 | + } |
| 142 | + |
| 143 | + for blob_sidecar in blob_sidecars { |
| 144 | + self.controller.on_el_blob_sidecar(blob_sidecar); |
| 145 | + } |
| 146 | + |
| 147 | + // Request remaining missing blob sidecars from P2P |
| 148 | + let missing_blob_identifiers = blob_identifiers |
| 149 | + .into_iter() |
| 150 | + .filter(|identifier| !self.received_blob_sidecars.contains_key(identifier)) |
| 151 | + .collect::<Vec<_>>(); |
| 152 | + |
| 153 | + debug!("missing blob sidecars after EL: {missing_blob_identifiers:?}"); |
| 154 | + |
| 155 | + if !missing_blob_identifiers.is_empty() { |
| 156 | + BlobFetcherToP2p::BlobsNeeded(missing_blob_identifiers, slot, peer_id) |
| 157 | + .send(&self.p2p_tx); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments