block and blob handling progress

This commit is contained in:
realbigsean
2022-11-19 16:53:34 -05:00
parent 45897ad4e1
commit dc87156641
11 changed files with 237 additions and 43 deletions

View File

@@ -915,6 +915,28 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
Ok(self.get_block(block_root).await?.map(Arc::new))
}
pub async fn get_block_and_blobs_checking_early_attester_cache(
&self,
block_root: &Hash256,
) -> Result<
(
Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
Option<Arc<BlobsSidecar<T::EthSpec>>>,
),
Error,
> {
if let (Some(block), Some(blobs)) = (
self.early_attester_cache.get_block(*block_root),
self.early_attester_cache.get_blobs(*block_root),
) {
return Ok((Some(block), Some(blobs)));
}
Ok((
self.get_block(block_root).await?.map(Arc::new),
self.get_blobs(block_root).await?.map(Arc::new),
))
}
/// Returns the block at the given root, if any.
///
/// ## Errors
@@ -923,7 +945,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
pub async fn get_block(
&self,
block_root: &Hash256,
) -> Result<Option<Arc<SignedBeaconBlock<T::EthSpec>>>, Error> {
) -> Result<Option<SignedBeaconBlock<T::EthSpec>>, Error> {
// Load block from database, returning immediately if we have the full block w payload
// stored.
let blinded_block = match self.store.try_get_full_block(block_root)? {
@@ -981,6 +1003,18 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.map(Some)
}
/// Returns the blobs at the given root, if any.
///
/// ## Errors
///
/// May return a database error.
pub async fn get_blobs(
&self,
block_root: &Hash256,
) -> Result<Option<BlobsSidecar<T::EthSpec>>, Error> {
Ok(self.store.get_blobs(block_root)?)
}
pub fn get_blinded_block(
&self,
block_root: &Hash256,
@@ -2338,7 +2372,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let last_index = filtered_chain_segment
.iter()
.position(|(_root, block)| {
block.block().slot().epoch(T::EthSpec::slots_per_epoch()) > start_epoch
block.slot().epoch(T::EthSpec::slots_per_epoch()) > start_epoch
})
.unwrap_or(filtered_chain_segment.len());
@@ -2405,14 +2439,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
pub async fn verify_block_for_gossip(
self: &Arc<Self>,
block: Arc<SignedBeaconBlock<T::EthSpec>>,
blobs: Option<Arc<BlobsSidecar<T::EthSpec>>>,
) -> Result<GossipVerifiedBlock<T>, BlockError<T::EthSpec>> {
let chain = self.clone();
self.task_executor
.clone()
.spawn_blocking_handle(
move || {
let slot = block.block().slot();
let graffiti_string = block.block().message().body().graffiti().as_utf8_lossy();
let slot = block.slot();
let graffiti_string = block.message().body().graffiti().as_utf8_lossy();
match GossipVerifiedBlock::new(block, &chain) {
Ok(verified) => {
@@ -2490,7 +2525,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
self.log,
"Beacon block imported";
"block_root" => ?block_root,
"block_slot" => slot,
"block_slot" => %block.slot(),
);
// Increment the Prometheus counter for block processing successes.
@@ -2540,6 +2575,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
) -> Result<Hash256, BlockError<T::EthSpec>> {
let ExecutionPendingBlock {
block,
blobs,
block_root,
state,
parent_block,
@@ -2592,6 +2628,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
move || {
chain.import_block(
block,
blobs,
block_root,
state,
confirmed_state_roots,
@@ -2616,7 +2653,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
#[allow(clippy::too_many_arguments)]
fn import_block(
&self,
block: Arc<SignedBeaconBlock<T::EthSpec>>,
signed_block: Arc<SignedBeaconBlock<T::EthSpec>>,
blobs: Option<Arc<BlobsSidecar<T::EthSpec>>>,
block_root: Hash256,
mut state: BeaconState<T::EthSpec>,
confirmed_state_roots: Vec<Hash256>,
@@ -2625,7 +2663,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
parent_block: SignedBlindedBeaconBlock<T::EthSpec>,
parent_eth1_finalization_data: Eth1FinalizationData,
) -> Result<Hash256, BlockError<T::EthSpec>> {
let signed_block = block.block();
let current_slot = self.slot()?;
let current_epoch = current_slot.epoch(T::EthSpec::slots_per_epoch());
@@ -2867,6 +2904,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if let Err(e) = self.early_attester_cache.add_head_block(
block_root,
signed_block.clone(),
blobs.clone(),
proto_block,
&state,
&self.spec,
@@ -2959,7 +2997,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
ops.push(StoreOp::PutBlock(block_root, signed_block.clone()));
ops.push(StoreOp::PutState(block.state_root(), &state));
if let Some(blobs) = block.blobs() {
if let Some(blobs) = blobs {
ops.push(StoreOp::PutBlobs(block_root, blobs));
};
let txn_lock = self.store.hot_db.begin_rw_transaction();

View File

@@ -625,6 +625,7 @@ type PayloadVerificationHandle<E> =
/// `BeaconChain` immediately after it is instantiated.
pub struct ExecutionPendingBlock<T: BeaconChainTypes> {
pub block: Arc<SignedBeaconBlock<T::EthSpec>>,
pub blobs: Option<Box<BlobsSidecar<T::EthSpec>>>,
pub block_root: Hash256,
pub state: BeaconState<T::EthSpec>,
pub parent_block: SignedBeaconBlock<T::EthSpec, BlindedPayload<T::EthSpec>>,
@@ -670,6 +671,7 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
/// Returns an error if the block is invalid, or if the block was unable to be verified.
pub fn new(
block: Arc<SignedBeaconBlock<T::EthSpec>>,
blobs: Option<Arc<BlobsSidecar<T::EthSpec>>>,
chain: &BeaconChain<T>,
) -> Result<Self, BlockError<T::EthSpec>> {
// If the block is valid for gossip we don't supply it to the slasher here because
@@ -677,7 +679,7 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
// it to the slasher if an error occurs, because that's the end of this block's journey,
// and it could be a repeat proposal (a likely cause for slashing!).
let header = block.signed_block_header();
Self::new_without_slasher_checks(block, chain).map_err(|e| {
Self::new_without_slasher_checks(block, blobs, chain).map_err(|e| {
process_block_slash_info(chain, BlockSlashInfo::from_early_error(header, e))
})
}
@@ -685,9 +687,9 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
/// As for new, but doesn't pass the block to the slasher.
fn new_without_slasher_checks(
block: Arc<SignedBeaconBlock<T::EthSpec>>,
blobs: Option<Arc<BlobsSidecar<T::EthSpec>>>,
chain: &BeaconChain<T>,
) -> Result<Self, BlockError<T::EthSpec>> {
let block = block.block();
// Ensure the block is the correct structure for the fork at `block.slot()`.
block
.fork_name(&chain.spec)
@@ -881,18 +883,20 @@ impl<T: BeaconChainTypes> GossipVerifiedBlock<T> {
// Validate the block's execution_payload (if any).
validate_execution_payload_for_gossip(&parent_block, block.message(), chain)?;
//FIXME(sean)
// if let Some(blobs_sidecar) = block.blobs() {
// validate_blob_for_gossip(blobs_sidecar, chain)?;
// }
if let Some(blobs_sidecar) = blobs.as_ref() {
validate_blob_for_gossip(blobs_sidecar, chain)?;
//FIXME(sean) validate blobs sidecar
}
// 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)
.set_proposer_index(block.message().proposer_index());
.set_proposer_index(block.message().proposer_index())
//FIXME(sean) set blobs sidecar validation results
.set_blobs_sidecar(blobs);
Ok(Self {
block: block,
block,
block_root,
parent,
consensus_context,
@@ -1054,6 +1058,7 @@ impl<T: BeaconChainTypes> IntoExecutionPendingBlock<T> for SignatureVerifiedBloc
ExecutionPendingBlock::from_signature_verified_components(
block,
self.consensus_context.blobs(),
block_root,
parent,
self.consensus_context,
@@ -1084,10 +1089,7 @@ impl<T: BeaconChainTypes> IntoExecutionPendingBlock<T> for Arc<SignedBeaconBlock
}
fn block(&self) -> &SignedBeaconBlock<T::EthSpec> {
match self {
Self::Block(block) => block,
Self::BlockAndBlobs(block) => &block.beacon_block,
}
self
}
}
@@ -1101,13 +1103,12 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
/// Returns an error if the block is invalid, or if the block was unable to be verified.
pub fn from_signature_verified_components(
block: Arc<SignedBeaconBlock<T::EthSpec>>,
blobs: Option<Box<BlobsSidecar<T::EthSpec>>>,
block_root: Hash256,
parent: PreProcessingSnapshot<T::EthSpec>,
mut consensus_context: ConsensusContext<T::EthSpec>,
chain: &Arc<BeaconChain<T>>,
) -> Result<Self, BlockError<T::EthSpec>> {
let block = block.block();
if let Some(parent) = chain
.canonical_head
.fork_choice_read_lock()
@@ -1437,8 +1438,11 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
});
}
//FIXME(sean) validate blobs sidecar
Ok(Self {
block,
blobs,
block_root,
state,
parent_block: parent.beacon_block,
@@ -1752,7 +1756,7 @@ fn load_parent<T: BeaconChainTypes>(
pre_state: parent_state,
beacon_state_root: Some(parent_state_root),
},
beacon_block,
block,
))
};

View File

@@ -20,6 +20,7 @@ pub struct CacheItem<E: EthSpec> {
* Values used to make the block available.
*/
block: Arc<SignedBeaconBlock<E>>,
blobs: Option<Arc<BlobsSidecar<E>>>,
proto_block: ProtoBlock,
}
@@ -50,6 +51,7 @@ impl<E: EthSpec> EarlyAttesterCache<E> {
&self,
beacon_block_root: Hash256,
block: Arc<SignedBeaconBlock<E>>,
blobs: Option<Arc<BlobsSidecar<E>>>,
proto_block: ProtoBlock,
state: &BeaconState<E>,
spec: &ChainSpec,
@@ -74,6 +76,7 @@ impl<E: EthSpec> EarlyAttesterCache<E> {
source,
target,
block,
blobs,
proto_block,
};
@@ -155,6 +158,16 @@ impl<E: EthSpec> EarlyAttesterCache<E> {
.map(|item| item.block.clone())
}
/// Returns the blobs, if `block_root` matches the cached item.
pub fn get_blobs(&self, block_root: Hash256) -> Option<Arc<BlobsSidecar<E>>> {
self.item
.read()
.as_ref()
.filter(|item| item.beacon_block_root == block_root)
.map(|item| item.blobs.clone())
.flatten()
}
/// Returns the proto-array block, if `block_root` matches the cached item.
pub fn get_proto_block(&self, block_root: Hash256) -> Option<ProtoBlock> {
self.item