Skip to content

Commit 35d6ab8

Browse files
committed
Simplifications and fixups
1 parent 9a7d32f commit 35d6ab8

File tree

1 file changed

+31
-78
lines changed

1 file changed

+31
-78
lines changed

beacon_node/beacon_chain/src/beacon_block_streamer.rs

Lines changed: 31 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ pub enum CheckEarlyAttesterCache {
2828
pub enum Error {
2929
PayloadReconstruction(String),
3030
BlocksByRangeFailure(Box<execution_layer::Error>),
31-
BlocksByHashFailure(Box<execution_layer::Error>),
3231
RequestNotFound,
3332
BlockResultNotFound,
3433
}
3534

36-
// This is the same as a DatabaseBlock
37-
// but the Arc allows us to avoid an
38-
// unnecessary clone
35+
const BLOCKS_PER_RANGE_REQUEST: u64 = 32;
36+
37+
// This is the same as a DatabaseBlock but the Arc allows us to avoid an unnecessary clone.
3938
enum LoadedBeaconBlock<E: EthSpec> {
4039
Full(Arc<SignedBeaconBlock<E>>),
4140
Blinded(Box<SignedBlindedBeaconBlock<E>>),
@@ -305,7 +304,7 @@ impl<E: EthSpec> BodiesByRange<E> {
305304
}
306305

307306
pub fn push_block_parts(&mut self, block_parts: BlockParts<E>) -> Result<(), BlockParts<E>> {
308-
if self.count == 32 {
307+
if self.count == BLOCKS_PER_RANGE_REQUEST {
309308
return Err(block_parts);
310309
}
311310

@@ -320,7 +319,9 @@ impl<E: EthSpec> BodiesByRange<E> {
320319
Ok(())
321320
} else {
322321
// need to figure out if this block fits in the request
323-
if block_number < self.start || self.start + 31 < block_number {
322+
if block_number < self.start
323+
|| self.start + BLOCKS_PER_RANGE_REQUEST <= block_number
324+
{
324325
return Err(block_parts);
325326
}
326327

@@ -519,7 +520,6 @@ impl<E: EthSpec> EngineRequest<E> {
519520

520521
pub struct BeaconBlockStreamer<T: BeaconChainTypes> {
521522
execution_layer: ExecutionLayer<T::EthSpec>,
522-
finalized_slot: Slot,
523523
check_early_attester_cache: CheckEarlyAttesterCache,
524524
beacon_chain: Arc<BeaconChain<T>>,
525525
}
@@ -535,16 +535,8 @@ impl<T: BeaconChainTypes> BeaconBlockStreamer<T> {
535535
.ok_or(BeaconChainError::ExecutionLayerMissing)?
536536
.clone();
537537

538-
let finalized_slot = beacon_chain
539-
.canonical_head
540-
.fork_choice_read_lock()
541-
.get_finalized_block()
542-
.map_err(BeaconChainError::ForkChoiceError)?
543-
.slot;
544-
545538
Ok(Self {
546539
execution_layer,
547-
finalized_slot,
548540
check_early_attester_cache,
549541
beacon_chain: beacon_chain.clone(),
550542
})
@@ -592,10 +584,9 @@ impl<T: BeaconChainTypes> BeaconBlockStreamer<T> {
592584

593585
/// Pre-process the loaded blocks into execution engine requests.
594586
///
595-
/// The purpose of this function is to separate the blocks into 3 categories:
587+
/// The purpose of this function is to separate the blocks into 2 categories:
596588
/// 1) no_request - when we already have the full block or there's an error
597-
/// 2) blocks_by_range - used for finalized blinded blocks
598-
/// 3) blocks_by_root - used for unfinalized blinded blocks
589+
/// 2) blocks_by_range - used for blinded blocks
599590
///
600591
/// The function returns a vector of block roots in the same order as requested
601592
/// along with the engine request that each root corresponds to.
@@ -610,14 +601,16 @@ impl<T: BeaconChainTypes> BeaconBlockStreamer<T> {
610601
// request as it should *better* optimize the number of blocks that
611602
// can fit in the same request
612603
let mut by_range_blocks: Vec<BlockParts<T::EthSpec>> = vec![];
613-
let mut by_hash = EngineRequest::new_by_hash();
614604
let mut no_request = EngineRequest::new_no_request();
615605

616606
for (root, load_result) in payloads {
617607
// preserve the order of the requested blocks
618608
ordered_block_roots.push(root);
619609

620-
match load_result {
610+
let block_result = match load_result {
611+
Err(e) => Err(e),
612+
Ok(None) => Ok(None),
613+
Ok(Some(LoadedBeaconBlock::Full(full_block))) => Ok(Some(full_block)),
621614
Ok(Some(LoadedBeaconBlock::Blinded(blinded_block))) => {
622615
match blinded_block
623616
.message()
@@ -626,65 +619,27 @@ impl<T: BeaconChainTypes> BeaconBlockStreamer<T> {
626619
{
627620
Ok(header) => {
628621
if header.block_hash() == ExecutionBlockHash::zero() {
629-
no_request
630-
.push_block_result(
631-
root,
632-
reconstruct_default_header_block(
633-
blinded_block,
634-
header,
635-
&self.beacon_chain.spec,
636-
),
637-
&self.beacon_chain.log,
638-
)
639-
.await;
640-
requests.insert(root, no_request.clone());
622+
reconstruct_default_header_block(
623+
blinded_block,
624+
header,
625+
&self.beacon_chain.spec,
626+
)
641627
} else {
628+
// Add the block to the set requiring a by-range request.
642629
let block_parts = BlockParts::new(blinded_block, header);
643-
if block_parts.slot() <= self.finalized_slot {
644-
// this is a by_range request
645-
by_range_blocks.push(block_parts);
646-
} else {
647-
// this is a by_hash request
648-
by_hash
649-
.push_block_parts(block_parts, &self.beacon_chain.log)
650-
.await;
651-
requests.insert(root, by_hash.clone());
652-
}
630+
by_range_blocks.push(block_parts);
631+
continue;
653632
}
654633
}
655-
Err(e) => {
656-
debug!(
657-
self.beacon_chain.log,
658-
"block {} is no_request, error getting execution_payload: {:?}",
659-
root,
660-
e
661-
);
662-
no_request
663-
.push_block_result(
664-
root,
665-
Err(BeaconChainError::BlockVariantLacksExecutionPayload(root)),
666-
&self.beacon_chain.log,
667-
)
668-
.await;
669-
requests.insert(root, no_request.clone());
670-
}
634+
Err(e) => Err(BeaconChainError::BeaconStateError(e)),
671635
}
672636
}
673-
// no request when there's an error, or the block doesn't exist, or we already have the full block
674-
no_request_load_result => {
675-
let block_result = match no_request_load_result {
676-
Err(e) => Err(e),
677-
Ok(None) => Ok(None),
678-
Ok(Some(LoadedBeaconBlock::Full(full_block))) => Ok(Some(full_block)),
679-
// unreachable due to the match statement above
680-
Ok(Some(LoadedBeaconBlock::Blinded(_))) => unreachable!(),
681-
};
682-
no_request
683-
.push_block_result(root, block_result, &self.beacon_chain.log)
684-
.await;
685-
requests.insert(root, no_request.clone());
686-
}
687-
}
637+
};
638+
639+
no_request
640+
.push_block_result(root, block_result, &self.beacon_chain.log)
641+
.await;
642+
requests.insert(root, no_request.clone());
688643
}
689644

690645
// Now deal with the by_range requests. Sort them in order of increasing slot
@@ -812,12 +767,10 @@ impl<T: BeaconChainTypes> BeaconBlockStreamer<T> {
812767
.map_err(BeaconChainError::EngineGetCapabilititesFailed)
813768
{
814769
Ok(engine_capabilities) => {
815-
// use the fallback method
816-
if engine_capabilities.get_payload_bodies_by_hash_v1
817-
&& engine_capabilities.get_payload_bodies_by_range_v1
818-
{
770+
if engine_capabilities.get_payload_bodies_by_range_v1 {
819771
self.stream_blocks(block_roots, sender).await;
820772
} else {
773+
// use the fallback method
821774
self.stream_blocks_fallback(block_roots, sender).await;
822775
}
823776
}
@@ -1016,7 +969,7 @@ mod tests {
1016969
}
1017970

1018971
#[tokio::test]
1019-
async fn check_fallback_altiar_to_capella() {
972+
async fn check_fallback_altair_to_capella() {
1020973
let slots_per_epoch = MinimalEthSpec::slots_per_epoch() as usize;
1021974
let num_epochs = 8;
1022975
let bellatrix_fork_epoch = 2usize;

0 commit comments

Comments
 (0)