Skip to content

Commit 42de037

Browse files
committed
Implement PeerDAS RPC handlers
1 parent d045a6a commit 42de037

File tree

6 files changed

+306
-11
lines changed

6 files changed

+306
-11
lines changed

beacon_node/beacon_chain/src/beacon_chain.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,25 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
11551155
.map_or_else(|| self.get_blobs(block_root), Ok)
11561156
}
11571157

1158+
pub fn get_data_column_checking_all_caches(
1159+
&self,
1160+
block_root: Hash256,
1161+
index: ColumnIndex,
1162+
) -> Result<Option<Arc<DataColumnSidecar<T::EthSpec>>>, Error> {
1163+
if let Some(column) = self
1164+
.data_availability_checker
1165+
.get_data_column(&DataColumnIdentifier { block_root, index })?
1166+
{
1167+
return Ok(Some(column));
1168+
}
1169+
1170+
if let Some(columns) = self.early_attester_cache.get_data_columns(block_root) {
1171+
return Ok(columns.iter().find(|c| c.index == index).cloned());
1172+
}
1173+
1174+
self.get_data_column(&block_root, &index)
1175+
}
1176+
11581177
/// Returns the block at the given root, if any.
11591178
///
11601179
/// ## Errors
@@ -1230,6 +1249,18 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
12301249
}
12311250
}
12321251

1252+
/// Returns the data columns at the given root, if any.
1253+
///
1254+
/// ## Errors
1255+
/// May return a database error.
1256+
pub fn get_data_column(
1257+
&self,
1258+
block_root: &Hash256,
1259+
column_index: &ColumnIndex,
1260+
) -> Result<Option<Arc<DataColumnSidecar<T::EthSpec>>>, Error> {
1261+
Ok(self.store.get_data_column(block_root, column_index)?)
1262+
}
1263+
12331264
pub fn get_blinded_block(
12341265
&self,
12351266
block_root: &Hash256,

beacon_node/beacon_chain/src/data_availability_checker.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use std::time::Duration;
1515
use task_executor::TaskExecutor;
1616
use types::blob_sidecar::{BlobIdentifier, BlobSidecar, FixedBlobSidecarList};
1717
use types::{
18-
BlobSidecarList, ChainSpec, DataColumnSidecarList, Epoch, EthSpec, Hash256, SignedBeaconBlock,
19-
Slot,
18+
BlobSidecarList, ChainSpec, DataColumnIdentifier, DataColumnSidecar, DataColumnSidecarList,
19+
Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot,
2020
};
2121

2222
mod error;
@@ -151,6 +151,14 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
151151
self.availability_cache.peek_blob(blob_id)
152152
}
153153

154+
/// Get a data column from the availability cache.
155+
pub fn get_data_column(
156+
&self,
157+
data_column_id: &DataColumnIdentifier,
158+
) -> Result<Option<Arc<DataColumnSidecar<T::EthSpec>>>, AvailabilityCheckError> {
159+
self.availability_cache.peek_data_column(data_column_id)
160+
}
161+
154162
/// Put a list of blobs received via RPC into the availability cache. This performs KZG
155163
/// verification on the blobs in the list.
156164
pub fn put_rpc_blobs(

beacon_node/beacon_chain/src/data_availability_checker/overflow_lru_cache.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ use ssz_types::{FixedVector, VariableList};
1414
use std::num::NonZeroUsize;
1515
use std::sync::Arc;
1616
use types::blob_sidecar::BlobIdentifier;
17-
use types::{BlobSidecar, ChainSpec, Epoch, EthSpec, Hash256, SignedBeaconBlock};
17+
use types::{
18+
BlobSidecar, ChainSpec, DataColumnIdentifier, DataColumnSidecar, Epoch, EthSpec, Hash256,
19+
SignedBeaconBlock,
20+
};
1821

1922
/// This represents the components of a partially available block
2023
///
@@ -369,6 +372,22 @@ impl<T: BeaconChainTypes> DataAvailabilityCheckerInner<T> {
369372
}
370373
}
371374

375+
/// Fetch a data column from the cache without affecting the LRU ordering
376+
pub fn peek_data_column(
377+
&self,
378+
data_column_id: &DataColumnIdentifier,
379+
) -> Result<Option<Arc<DataColumnSidecar<T::EthSpec>>>, AvailabilityCheckError> {
380+
if let Some(pending_components) = self.critical.read().peek(&data_column_id.block_root) {
381+
Ok(pending_components
382+
.verified_data_columns
383+
.iter()
384+
.find(|data_column| data_column.as_data_column().index == data_column_id.index)
385+
.map(|data_column| data_column.clone_arc()))
386+
} else {
387+
Ok(None)
388+
}
389+
}
390+
372391
pub fn peek_pending_components<R, F: FnOnce(Option<&PendingComponents<T::EthSpec>>) -> R>(
373392
&self,
374393
block_root: &Hash256,

beacon_node/beacon_chain/src/data_column_verification.rs

+7
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ impl<E: EthSpec> KzgVerifiedCustodyDataColumn<E> {
248248
pub fn into_inner(self) -> Arc<DataColumnSidecar<E>> {
249249
self.data
250250
}
251+
252+
pub fn as_data_column(&self) -> &DataColumnSidecar<E> {
253+
&self.data
254+
}
255+
pub fn clone_arc(&self) -> Arc<DataColumnSidecar<E>> {
256+
self.data.clone()
257+
}
251258
}
252259

253260
/// Complete kzg verification for a `DataColumnSidecar`.

beacon_node/lighthouse_network/src/rpc/methods.rs

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::Serialize;
66
use ssz::Encode;
77
use ssz_derive::{Decode, Encode};
88
use ssz_types::{typenum::U256, VariableList};
9+
use std::collections::BTreeMap;
910
use std::fmt::Display;
1011
use std::marker::PhantomData;
1112
use std::ops::Deref;
@@ -426,6 +427,17 @@ impl DataColumnsByRootRequest {
426427
pub fn new_single(block_root: Hash256, index: ColumnIndex, spec: &ChainSpec) -> Self {
427428
Self::new(vec![DataColumnIdentifier { block_root, index }], spec)
428429
}
430+
431+
pub fn group_by_ordered_block_root(&self) -> Vec<(Hash256, Vec<ColumnIndex>)> {
432+
let mut column_indexes_by_block = BTreeMap::<Hash256, Vec<ColumnIndex>>::new();
433+
for request_id in self.data_column_ids.as_slice() {
434+
column_indexes_by_block
435+
.entry(request_id.block_root)
436+
.or_default()
437+
.push(request_id.index);
438+
}
439+
column_indexes_by_block.into_iter().collect()
440+
}
429441
}
430442

431443
/* RPC Handling and Grouping */

0 commit comments

Comments
 (0)