Skip to content

Commit

Permalink
Merge branch 'das' into custody-sync-peers
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Jun 27, 2024
2 parents 4079d2e + 7206909 commit de05355
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
11 changes: 5 additions & 6 deletions beacon_node/lighthouse_network/src/discovery/enr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl Eth2Enr for Enr {
/// if the custody value is non-existent in the ENR, then we assume the minimum custody value
/// defined in the spec.
fn custody_subnet_count<E: EthSpec>(&self, spec: &ChainSpec) -> u64 {
self.get(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY)
.and_then(|custody_bytes| custody_bytes.try_into().map(u64::from_be_bytes).ok())
self.get_decodable::<u64>(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY)
.and_then(|r| r.ok())
// If value supplied in ENR is invalid, fallback to `custody_requirement`
.filter(|csc| csc <= &spec.data_column_sidecar_subnet_count)
.unwrap_or(spec.custody_requirement)
Expand Down Expand Up @@ -245,8 +245,7 @@ pub fn build_enr<E: EthSpec>(
spec.custody_requirement
};

let csc_bytes = custody_subnet_count.to_be_bytes();
builder.add_value(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY, &csc_bytes.as_slice());
builder.add_value(PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY, &custody_subnet_count);

builder
.build(enr_key)
Expand Down Expand Up @@ -353,11 +352,11 @@ mod test {
let config = NetworkConfig::default();
let spec = E::default_spec();
let (mut enr, enr_key) = build_enr_with_config(config, &spec);
let invalid_subnet_count = 999u64;
let invalid_subnet_count = 99u64;

enr.insert(
PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY,
&invalid_subnet_count.to_be_bytes().as_slice(),
&invalid_subnet_count,
&enr_key,
)
.unwrap();
Expand Down
5 changes: 1 addition & 4 deletions beacon_node/lighthouse_network/src/peer_manager/peerdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,7 @@ impl<E: EthSpec> PeerDB<E> {
if supernode {
enr.insert(
PEERDAS_CUSTODY_SUBNET_COUNT_ENR_KEY,
&spec
.data_column_sidecar_subnet_count
.to_be_bytes()
.as_slice(),
&spec.data_column_sidecar_subnet_count,
&enr_key,
)
.expect("u64 can be encoded");
Expand Down
58 changes: 57 additions & 1 deletion beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ mod tests {
use crate::types::{EnrAttestationBitfield, EnrSyncCommitteeBitfield};
use types::{
blob_sidecar::BlobIdentifier, BeaconBlock, BeaconBlockAltair, BeaconBlockBase,
BeaconBlockBellatrix, EmptyBlock, Epoch, FullPayload, Signature, Slot,
BeaconBlockBellatrix, DataColumnIdentifier, EmptyBlock, Epoch, FullPayload, Signature,
Slot,
};

type Spec = types::MainnetEthSpec;
Expand Down Expand Up @@ -848,6 +849,10 @@ mod tests {
Arc::new(BlobSidecar::empty())
}

fn empty_data_column_sidecar() -> Arc<DataColumnSidecar<Spec>> {
Arc::new(DataColumnSidecar::empty())
}

/// Bellatrix block with length < max_rpc_size.
fn bellatrix_block_small(
fork_context: &ForkContext,
Expand Down Expand Up @@ -909,6 +914,27 @@ mod tests {
}
}

fn dcbrange_request() -> DataColumnsByRangeRequest {
DataColumnsByRangeRequest {
start_slot: 0,
count: 10,
columns: vec![1, 2, 3],
}
}

fn dcbroot_request(spec: &ChainSpec) -> DataColumnsByRootRequest {
DataColumnsByRootRequest {
data_column_ids: RuntimeVariableList::new(
vec![DataColumnIdentifier {
block_root: Hash256::zero(),
index: 0,
}],
spec.max_request_data_column_sidecars as usize,
)
.unwrap(),
}
}

fn bbroot_request_v1(spec: &ChainSpec) -> BlocksByRootRequest {
BlocksByRootRequest::new_v1(vec![Hash256::zero()], spec)
}
Expand Down Expand Up @@ -1198,6 +1224,34 @@ mod tests {
),
Ok(Some(RPCResponse::BlobsByRoot(empty_blob_sidecar()))),
);

assert_eq!(
encode_then_decode_response(
SupportedProtocol::DataColumnsByRangeV1,
RPCCodedResponse::Success(RPCResponse::DataColumnsByRange(
empty_data_column_sidecar()
)),
ForkName::Deneb,
&chain_spec
),
Ok(Some(RPCResponse::DataColumnsByRange(
empty_data_column_sidecar()
))),
);

assert_eq!(
encode_then_decode_response(
SupportedProtocol::DataColumnsByRootV1,
RPCCodedResponse::Success(RPCResponse::DataColumnsByRoot(
empty_data_column_sidecar()
)),
ForkName::Deneb,
&chain_spec
),
Ok(Some(RPCResponse::DataColumnsByRoot(
empty_data_column_sidecar()
))),
);
}

// Test RPCResponse encoding/decoding for V1 messages
Expand Down Expand Up @@ -1551,6 +1605,8 @@ mod tests {
OutboundRequest::MetaData(MetadataRequest::new_v1()),
OutboundRequest::BlobsByRange(blbrange_request()),
OutboundRequest::BlobsByRoot(blbroot_request(&chain_spec)),
OutboundRequest::DataColumnsByRange(dcbrange_request()),
OutboundRequest::DataColumnsByRoot(dcbroot_request(&chain_spec)),
OutboundRequest::MetaData(MetadataRequest::new_v2()),
];

Expand Down
20 changes: 20 additions & 0 deletions beacon_node/lighthouse_network/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ impl DataColumnsByRangeRequest {
.saturating_mul(E::max_blobs_per_block() as u64)
.saturating_mul(self.columns.len() as u64)
}

pub fn ssz_min_len() -> usize {
DataColumnsByRangeRequest {
start_slot: 0,
count: 0,
columns: vec![0],
}
.as_ssz_bytes()
.len()
}

pub fn ssz_max_len(spec: &ChainSpec) -> usize {
DataColumnsByRangeRequest {
start_slot: 0,
count: 0,
columns: vec![0; spec.number_of_columns],
}
.as_ssz_bytes()
.len()
}
}

/// Request a number of beacon block roots from a peer.
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ impl ProtocolId {
Protocol::BlobsByRoot => RpcLimits::new(0, spec.max_blobs_by_root_request),
Protocol::DataColumnsByRoot => RpcLimits::new(0, spec.max_data_columns_by_root_request),
Protocol::DataColumnsByRange => RpcLimits::new(
<DataColumnsByRangeRequest as Encode>::ssz_fixed_len(),
<DataColumnsByRangeRequest as Encode>::ssz_fixed_len(),
DataColumnsByRangeRequest::ssz_min_len(),
DataColumnsByRangeRequest::ssz_max_len(spec),
),
Protocol::Ping => RpcLimits::new(
<Ping as Encode>::ssz_fixed_len(),
Expand Down

0 comments on commit de05355

Please sign in to comment.