mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-21 14:58:31 +00:00
much work
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use beacon_chain::blob_verification::{AsBlock, BlockWrapper, MaybeAvailableBlock};
|
||||
use beacon_chain::blob_verification::{AsBlock, BlockWrapper};
|
||||
use beacon_chain::data_availability_checker::DataAvailabilityChecker;
|
||||
use beacon_chain::{AvailabilityProcessingStatus, BeaconChainTypes, BlockError};
|
||||
use lighthouse_network::rpc::RPCError;
|
||||
@@ -7,11 +7,11 @@ use lru_cache::LRUTimeCache;
|
||||
use slog::{debug, error, trace, warn, Logger};
|
||||
use smallvec::SmallVec;
|
||||
use ssz_types::FixedVector;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use store::Hash256;
|
||||
use types::{BlobSidecar, SignedBeaconBlock, Slot};
|
||||
use types::{BlobSidecar, EthSpec, SignedBeaconBlock, Slot};
|
||||
|
||||
use self::parent_lookup::{LookupDownloadStatus, PARENT_FAIL_TOLERANCE};
|
||||
use self::parent_lookup::{ParentLookup, ParentVerifyError};
|
||||
@@ -259,7 +259,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
let triggered_parent_request = self
|
||||
.parent_lookups
|
||||
.iter()
|
||||
.any(|lookup| lookup.chain_hash() == block_root);
|
||||
.any(|lookup| lookup.chain_hash() == root);
|
||||
|
||||
if triggered_parent_request {
|
||||
// The lookup status here is irrelevant because we wait until the parent chain
|
||||
@@ -269,7 +269,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
// This is the correct block, send it for processing
|
||||
if self
|
||||
.send_block_for_processing(
|
||||
block_root,
|
||||
root,
|
||||
BlockWrapper::Block(block),
|
||||
seen_timestamp,
|
||||
BlockProcessType::SingleBlock { id },
|
||||
@@ -290,7 +290,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
cx.report_peer(peer_id, PeerAction::LowToleranceError, msg);
|
||||
|
||||
debug!(self.log, "Single block lookup failed";
|
||||
"peer_id" => %peer_id, "error" => msg, "block_root" => %request.requested_block_root);
|
||||
"peer_id" => %peer_id, "error" => msg, "block_root" => %request_ref.requested_block_root);
|
||||
// try the request again if possible
|
||||
if let Ok((peer_id, request)) = request_ref.request_block() {
|
||||
if let Ok(id) = cx.single_block_lookup_request(peer_id, request) {
|
||||
@@ -337,7 +337,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
if triggered_parent_request {
|
||||
// The lookup status here is irrelevant because we wait until the parent chain
|
||||
// is complete before processing the block.
|
||||
let _ = request_ref.add_block(root, block)?;
|
||||
let _ = request_ref.add_blobs(block_root, blobs)?;
|
||||
} else {
|
||||
// These are the correct blobs, send them for processing
|
||||
if self
|
||||
@@ -362,7 +362,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
cx.report_peer(peer_id, PeerAction::LowToleranceError, msg);
|
||||
|
||||
debug!(self.log, "Single block lookup failed";
|
||||
"peer_id" => %peer_id, "error" => msg, "block_root" => %request.requested_block_root);
|
||||
"peer_id" => %peer_id, "error" => msg, "block_root" => %request_ref.requested_block_root);
|
||||
// try the request again if possible
|
||||
if let Ok((peer_id, request)) = request_ref.request_blobs() {
|
||||
if let Ok(id) = cx.single_blobs_lookup_request(peer_id, request) {
|
||||
@@ -566,10 +566,6 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
self.search_block(block_root, peer_id, cx);
|
||||
self.parent_lookups.push(parent_lookup)
|
||||
}
|
||||
LookupDownloadStatus::Err(e) => {
|
||||
warn!(self.log, "Peer sent invalid response to parent request.";
|
||||
"peer_id" => %peer_id, "reason" => %e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
@@ -748,7 +744,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
self.single_block_lookups.retain_mut(|(block_id, blob_id, req)|{
|
||||
if &Some(id) == block_id {
|
||||
req.block_request_state.register_failure_downloading();
|
||||
trace!(self.log, "Single block lookup failed"; "block" => %request.requested_block_root);
|
||||
trace!(self.log, "Single block lookup failed"; "block" => %req.requested_block_root);
|
||||
match req.request_block() {
|
||||
Ok(Some((peer_id, block_request))) => {
|
||||
if let Ok(request_id) = cx.single_block_lookup_request(peer_id, block_request) {
|
||||
@@ -769,7 +765,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
}
|
||||
if &Some(id) == blob_id {
|
||||
req.blob_request_state.register_failure_downloading();
|
||||
trace!(self.log, "Single blob lookup failed"; "block" => %request.requested_block_root);
|
||||
trace!(self.log, "Single blob lookup failed"; "block" => %req.requested_block_root);
|
||||
match req.request_blobs() {
|
||||
Ok(Some((peer_id, blob_request))) => {
|
||||
if let Ok(request_id) = cx.single_blobs_lookup_request(peer_id, blob_request) {
|
||||
@@ -851,7 +847,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
trace!(self.log, "Single block processing succeeded"; "block" => %root);
|
||||
true
|
||||
}
|
||||
AvailabilityProcessingStatus::MissingParts(block_root) => {
|
||||
AvailabilityProcessingStatus::MissingParts(_, block_root) => {
|
||||
self.search_block(block_root, peer_id, cx);
|
||||
false
|
||||
}
|
||||
@@ -907,7 +903,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
ResponseType::Block => {
|
||||
req.block_request_state.register_failure_processing();
|
||||
match req.request_block() {
|
||||
Ok(Some((peer_id, requeest))) => {
|
||||
Ok(Some((peer_id, request))) => {
|
||||
if let Ok(request_id) =
|
||||
cx.single_block_lookup_request(peer_id, request)
|
||||
{
|
||||
@@ -965,7 +961,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
.parent_lookups
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(index, _)| lookup.chain_hash() == chain_hash)
|
||||
.find(|(_, lookup)| lookup.chain_hash() == chain_hash)
|
||||
.map(|(index, _)| index);
|
||||
|
||||
let Some(mut parent_lookup) = index.map(|index|self.parent_lookups.remove(index)) else {
|
||||
@@ -993,7 +989,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
AvailabilityProcessingStatus::Imported(hash) => {
|
||||
trace!(self.log, "Parent block processing succeeded"; &parent_lookup)
|
||||
}
|
||||
AvailabilityProcessingStatus::MissingParts(block_root) => {
|
||||
AvailabilityProcessingStatus::MissingParts(_, block_root) => {
|
||||
trace!(self.log, "Parent missing parts, triggering single block lookup "; &parent_lookup)
|
||||
}
|
||||
},
|
||||
@@ -1012,6 +1008,7 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
|
||||
match result {
|
||||
BlockPartProcessingResult::Ok(AvailabilityProcessingStatus::MissingParts(
|
||||
_,
|
||||
block_root,
|
||||
)) => {
|
||||
self.search_block(block_root, peer_id, cx);
|
||||
@@ -1124,14 +1121,42 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
debug!(self.log, "Parent chain processed"; "chain_hash" => %chain_hash, "result" => ?result);
|
||||
match result {
|
||||
BatchProcessResult::Success { .. } => {
|
||||
//TODO(sean) find single blob and block lookups and send for processing
|
||||
if let Some((index, (_, _, req))) = self
|
||||
.single_block_lookups
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(index, (_, _, req))| req.requested_block_root == chain_hash)
|
||||
{
|
||||
self.single_block_lookups
|
||||
.get_mut(index)
|
||||
.and_then(|(_, _, lookup)| lookup.get_downloaded_block())
|
||||
.map(|block_wrapper| {
|
||||
// This is the correct block, send it for processing
|
||||
if self
|
||||
.send_block_for_processing(
|
||||
chain_hash,
|
||||
block_wrapper,
|
||||
Duration::from_secs(0), //TODO(sean) pipe this through
|
||||
BlockProcessType::SingleBlock { id },
|
||||
cx,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
// Remove to avoid inconsistencies
|
||||
self.single_block_lookups.remove(index);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
BatchProcessResult::FaultyFailure {
|
||||
imported_blocks: _,
|
||||
penalty,
|
||||
} => {
|
||||
//TODO(sean) improve peer scoring to block or blob granularity
|
||||
self.failed_chains.insert(chain_hash);
|
||||
for peer_id in request.used_peers {
|
||||
let mut all_peers = request.blob_request_state.used_peers.clone();
|
||||
all_peers.extend(request.blob_request_state.used_peers);
|
||||
for peer_id in all_peers {
|
||||
cx.report_peer(peer_id, penalty, "parent_chain_failure")
|
||||
}
|
||||
}
|
||||
@@ -1181,14 +1206,17 @@ impl<T: BeaconChainTypes> BlockLookups<T> {
|
||||
fn send_blobs_for_processing(
|
||||
&self,
|
||||
block_root: Hash256,
|
||||
blobs: FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>,
|
||||
seen_timestamp: Duration,
|
||||
id: BlockProcessType,
|
||||
blobs: FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
duration: Duration,
|
||||
process_type: BlockProcessType,
|
||||
cx: &mut SyncNetworkContext<T>,
|
||||
) -> Result<(), ()> {
|
||||
match cx.processor_channel_if_enabled() {
|
||||
Some(beacon_processor_send) => {
|
||||
trace!(self.log, "Sending blobs for processing"; "block" => ?block_root, "process" => ?process_type);
|
||||
trace!(self.log, "Sending blobs for processing"; "block" => ?block_root, "process_type" => ?process_type);
|
||||
let event = WorkEvent::rpc_blobs(block_root, blobs, duration, process_type);
|
||||
if let Err(e) = beacon_processor_send.try_send(event) {
|
||||
error!(
|
||||
|
||||
@@ -192,7 +192,10 @@ impl<T: BeaconChainTypes> ParentLookup<T> {
|
||||
pub fn add_blobs(
|
||||
&mut self,
|
||||
block_root: Hash256,
|
||||
blobs: FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>,
|
||||
blobs: FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
) -> Result<LookupDownloadStatus<T::EthSpec>, ParentVerifyError> {
|
||||
self.current_parent_blob_request_id = None;
|
||||
self.current_parent_request
|
||||
@@ -302,7 +305,10 @@ impl<T: BeaconChainTypes> ParentLookup<T> {
|
||||
) -> Result<
|
||||
Option<(
|
||||
Hash256,
|
||||
FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>,
|
||||
FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
)>,
|
||||
ParentVerifyError,
|
||||
> {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::sync::block_lookups::parent_lookup::LookupDownloadStatus;
|
||||
use crate::sync::block_lookups::RootBlockTuple;
|
||||
use beacon_chain::blob_verification::AsBlock;
|
||||
use beacon_chain::blob_verification::BlockWrapper;
|
||||
use beacon_chain::data_availability_checker::{AvailabilityCheckError, DataAvailabilityChecker};
|
||||
use beacon_chain::{get_block_root, BeaconChainTypes};
|
||||
@@ -10,17 +9,19 @@ use rand::seq::IteratorRandom;
|
||||
use ssz_types::{FixedVector, VariableList};
|
||||
use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
use store::{EthSpec, Hash256};
|
||||
use store::Hash256;
|
||||
use strum::IntoStaticStr;
|
||||
use types::blob_sidecar::BlobIdentifier;
|
||||
use types::{BlobSidecar, SignedBeaconBlock};
|
||||
use types::{BlobSidecar, EthSpec, SignedBeaconBlock};
|
||||
|
||||
pub struct SingleBlockLookup<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> {
|
||||
pub requested_block_root: Hash256,
|
||||
pub requested_ids: Vec<BlobIdentifier>,
|
||||
pub downloaded_blobs:
|
||||
FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>,
|
||||
pub downloaded_block: Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
pub downloaded_blobs: FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
pub downloaded_block: Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
|
||||
pub block_request_state: SingleLookupRequestState<MAX_ATTEMPTS>,
|
||||
pub blob_request_state: SingleLookupRequestState<MAX_ATTEMPTS>,
|
||||
pub da_checker: Arc<DataAvailabilityChecker<T::EthSpec, T::SlotClock>>,
|
||||
@@ -88,10 +89,25 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_downloaded_block(&mut self) -> Option<BlockWrapper<T::EthSpec>> {
|
||||
if self.requested_ids.is_empty() {
|
||||
if let Some(block) = self.downloaded_block.take() {
|
||||
return Some(BlockWrapper::BlockAndBlobs(
|
||||
block,
|
||||
self.downloaded_blobs.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn add_blobs(
|
||||
&mut self,
|
||||
block_root: Hash256,
|
||||
blobs: FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>,
|
||||
blobs: FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
) -> Result<LookupDownloadStatus<T::EthSpec>, LookupVerifyError> {
|
||||
for (index, blob_opt) in self.downloaded_blobs.iter_mut().enumerate() {
|
||||
if let Some(Some(downloaded_blob)) = blobs.get(index) {
|
||||
@@ -109,7 +125,7 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
Err(e) => Err(LookupVerifyError::AvailabilityCheck(e)),
|
||||
}
|
||||
} else {
|
||||
Ok(LookupDownloadStatus::SearchBlock(block_hash))
|
||||
Ok(LookupDownloadStatus::SearchBlock(block_root))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,12 +134,8 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
block_root: Hash256,
|
||||
block: Arc<SignedBeaconBlock<T::EthSpec>>,
|
||||
) -> Result<LookupDownloadStatus<T::EthSpec>, LookupVerifyError> {
|
||||
for (index, blob_opt) in self.downloaded_blobs.iter_mut().enumerate() {
|
||||
if let Some(Some(downloaded_blob)) = blobs.get(index) {
|
||||
//TODO(sean) should we log a warn if there is already a downloaded blob?
|
||||
*blob_opt = Some(downloaded_blob);
|
||||
}
|
||||
}
|
||||
//TODO(sean) check for existing block?
|
||||
self.downloaded_block = Some(block);
|
||||
|
||||
match self
|
||||
.da_checker
|
||||
@@ -204,27 +216,32 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
&mut self,
|
||||
blob: Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
) -> Result<
|
||||
Option<FixedVector<Option<Arc<BlobSidecar<T::EthSpec>>>, T::EthSpec::MaxBlobsPerBlock>>,
|
||||
BlobVerifyError,
|
||||
Option<
|
||||
FixedVector<
|
||||
Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
<<T as BeaconChainTypes>::EthSpec as EthSpec>::MaxBlobsPerBlock,
|
||||
>,
|
||||
>,
|
||||
LookupVerifyError,
|
||||
> {
|
||||
match self.block_request_state.state {
|
||||
State::AwaitingDownload => {
|
||||
self.blob_request_state.register_failure_downloading();
|
||||
Err(BlobVerifyError::ExtraBlobsReturned)
|
||||
Err(LookupVerifyError::ExtraBlobsReturned)
|
||||
}
|
||||
State::Downloading { peer_id } => match blob {
|
||||
Some(blob) => {
|
||||
let received_id = blob.id();
|
||||
if !self.requested_ids.contains(&received_id) {
|
||||
self.blob_request_state.register_failure_downloading();
|
||||
Err(BlobVerifyError::UnrequestedBlobId)
|
||||
Err(LookupVerifyError::UnrequestedBlobId)
|
||||
} else {
|
||||
// State should remain downloading until we receive the stream terminator.
|
||||
self.requested_ids.retain(|id| id != received_id);
|
||||
if let Some(blob_opt) = self.downloaded_blobs.get_mut(blob.index) {
|
||||
*blob_opt = Some(blob);
|
||||
} else {
|
||||
return Err(BlobVerifyError::InvalidIndex(blob.index));
|
||||
return Err(LookupVerifyError::InvalidIndex(blob.index));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -233,11 +250,11 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
Ok(Some(self.downloaded_blobs.clone()))
|
||||
}
|
||||
},
|
||||
State::Processing { peer_id: _ } => match block {
|
||||
State::Processing { peer_id: _ } => match blob {
|
||||
Some(_) => {
|
||||
// We sent the blob for processing and received an extra blob.
|
||||
self.blob_request_state.register_failure_downloading();
|
||||
Err(BlobVerifyError::ExtraBlobsReturned)
|
||||
Err(LookupVerifyError::ExtraBlobsReturned)
|
||||
}
|
||||
None => {
|
||||
// This is simply the stream termination and we are already processing the
|
||||
@@ -304,7 +321,7 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
.choose(&mut rand::thread_rng())
|
||||
{
|
||||
let request = BlobsByRootRequest {
|
||||
blob_ids: VariableList::from(self.requested_ids),
|
||||
blob_ids: VariableList::from(self.requested_ids.clone()),
|
||||
};
|
||||
self.blob_request_state.state = State::Downloading { peer_id };
|
||||
self.blob_request_state.used_peers.insert(peer_id);
|
||||
@@ -313,15 +330,6 @@ impl<const MAX_ATTEMPTS: u8, T: BeaconChainTypes> SingleBlockLookup<MAX_ATTEMPTS
|
||||
Err(LookupRequestError::NoPeers)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: only add peers if they have *all* blob ids in the request, this could probably be improved.
|
||||
pub fn add_peer_if_useful(&mut self, blob_id: &[BlobIdentifier], peer_id: &PeerId) -> bool {
|
||||
let is_useful = self.requested_ids.contains(blob_id);
|
||||
if is_useful {
|
||||
self.block_request_state.add_peer(peer_id);
|
||||
}
|
||||
is_useful
|
||||
}
|
||||
}
|
||||
|
||||
impl<const MAX_ATTEMPTS: u8> SingleLookupRequestState<MAX_ATTEMPTS> {
|
||||
|
||||
Reference in New Issue
Block a user