Skip to content

Commit

Permalink
get values off chain spec
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed Oct 16, 2023
1 parent b9c5926 commit d350ea3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 49 deletions.
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/src/rpc/codec/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ mod tests {
));

// Request limits
let limit = protocol_id.rpc_request_limits(&fork_context);
let limit = protocol_id.rpc_request_limits(&fork_context.spec);
let mut max = encode_len(limit.max + 1);
let mut codec = SSZSnappyOutboundCodec::<Spec>::new(
protocol_id.clone(),
Expand Down
14 changes: 7 additions & 7 deletions beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::io::{Read, Write};
use std::marker::PhantomData;
use std::sync::Arc;
use tokio_util::codec::{Decoder, Encoder};
use types::{light_client_bootstrap::LightClientBootstrap, BlobSidecar};
use types::{light_client_bootstrap::LightClientBootstrap, BlobSidecar, ChainSpec};
use types::{
EthSpec, ForkContext, ForkName, Hash256, RuntimeVariableList, SignedBeaconBlock,
SignedBeaconBlockAltair, SignedBeaconBlockBase, SignedBeaconBlockCapella,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {

// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
// packet size for ssz container corresponding to `self.protocol`.
let ssz_limits = self.protocol.rpc_request_limits(&self.fork_context);
let ssz_limits = self.protocol.rpc_request_limits(&self.fork_context.spec);
if ssz_limits.is_out_of_bounds(length, self.max_packet_size) {
return Err(RPCError::InvalidData(format!(
"RPC request length for protocol {:?} is out of bounds, length {}",
Expand All @@ -166,7 +166,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
handle_rpc_request(
self.protocol.versioned_protocol,
&decoded_buffer,
&self.fork_context,
&self.fork_context.spec,
)
}
Err(e) => handle_error(e, reader.get_ref().get_ref().position(), max_compressed_len),
Expand Down Expand Up @@ -459,7 +459,7 @@ fn handle_length(
fn handle_rpc_request<T: EthSpec>(
versioned_protocol: SupportedProtocol,
decoded_buffer: &[u8],
fork_context: &ForkContext,
spec: &ChainSpec,
) -> Result<Option<InboundRequest<T>>, RPCError> {
match versioned_protocol {
SupportedProtocol::StatusV1 => Ok(Some(InboundRequest::Status(
Expand All @@ -478,15 +478,15 @@ fn handle_rpc_request<T: EthSpec>(
BlocksByRootRequest::V2(BlocksByRootRequestV2 {
block_roots: RuntimeVariableList::from_ssz_bytes(
decoded_buffer,
fork_context.max_request_blocks(),
spec.max_request_blocks as usize,
)?,
}),
))),
SupportedProtocol::BlocksByRootV1 => Ok(Some(InboundRequest::BlocksByRoot(
BlocksByRootRequest::V1(BlocksByRootRequestV1 {
block_roots: RuntimeVariableList::from_ssz_bytes(
decoded_buffer,
fork_context.max_request_blocks(),
spec.max_request_blocks as usize,
)?,
}),
))),
Expand All @@ -497,7 +497,7 @@ fn handle_rpc_request<T: EthSpec>(
Ok(Some(InboundRequest::BlobsByRoot(BlobsByRootRequest {
blob_ids: RuntimeVariableList::from_ssz_bytes(
decoded_buffer,
fork_context.max_request_blob_sidecars(),
spec.max_request_blob_sidecars as usize,
)?,
})))
}
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/lighthouse_network/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,13 @@ pub struct BlocksByRootRequest {
impl BlocksByRootRequest {
pub fn new(block_roots: Vec<Hash256>, spec: &ChainSpec) -> Self {
let block_roots =
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks_deneb as usize);
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks as usize);
Self::V2(BlocksByRootRequestV2 { block_roots })
}

pub fn new_v1(block_roots: Vec<Hash256>, spec: &ChainSpec) -> Self {
let block_roots =
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks_deneb as usize);
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks as usize);
Self::V1(BlocksByRootRequestV1 { block_roots })
}
}
Expand Down
12 changes: 6 additions & 6 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tokio_util::{
};
use types::{
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockCapella, BeaconBlockMerge,
BlobSidecar, EmptyBlock, EthSpec, ForkContext, ForkName, MainnetEthSpec, Signature,
BlobSidecar, ChainSpec, EmptyBlock, EthSpec, ForkContext, ForkName, MainnetEthSpec, Signature,
SignedBeaconBlock,
};

Expand Down Expand Up @@ -348,7 +348,7 @@ impl AsRef<str> for ProtocolId {

impl ProtocolId {
/// Returns min and max size for messages of given protocol id requests.
pub fn rpc_request_limits(&self, fork_context: &ForkContext) -> RpcLimits {
pub fn rpc_request_limits(&self, spec: &ChainSpec) -> RpcLimits {
match self.versioned_protocol.protocol() {
Protocol::Status => RpcLimits::new(
<StatusMessage as Encode>::ssz_fixed_len(),
Expand All @@ -364,16 +364,16 @@ impl ProtocolId {
<OldBlocksByRangeRequestV2 as Encode>::ssz_fixed_len(),
),
Protocol::BlocksByRoot => RpcLimits::new(
fork_context.min_blocks_by_root_request(),
fork_context.max_blocks_by_root_request(),
spec.min_blocks_by_root_request,
spec.max_blocks_by_root_request,
),
Protocol::BlobsByRange => RpcLimits::new(
<BlobsByRangeRequest as Encode>::ssz_fixed_len(),
<BlobsByRangeRequest as Encode>::ssz_fixed_len(),
),
Protocol::BlobsByRoot => RpcLimits::new(
fork_context.min_blobs_by_root_request(),
fork_context.max_blobs_by_root_request(),
spec.min_blobs_by_root_request,
spec.max_blobs_by_root_request,
),
Protocol::Ping => RpcLimits::new(
<Ping as Encode>::ssz_fixed_len(),
Expand Down
33 changes: 0 additions & 33 deletions consensus/types/src/fork_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,37 +117,4 @@ impl ForkContext {
pub fn all_fork_digests(&self) -> Vec<[u8; 4]> {
self.digest_to_fork.keys().cloned().collect()
}

/// Returns the `min_blocks_by_root_request` corresponding to the current fork.
pub fn min_blocks_by_root_request(&self) -> usize {
let fork_name = self.current_fork();
self.spec.min_blocks_by_root_request(fork_name)
}

/// Returns the `max_blocks_by_root_request` corresponding to the current fork.
pub fn max_blocks_by_root_request(&self) -> usize {
let fork_name = self.current_fork();
self.spec.max_blocks_by_root_request(fork_name)
}

/// Returns the `max_request_blocks` corresponding to the current fork.
pub fn max_request_blocks(&self) -> usize {
let fork_name = self.current_fork();
self.spec.max_request_blocks(fork_name)
}

/// Returns the `min_blobs_by_root_request` set in `ChainSpec`.
pub fn min_blobs_by_root_request(&self) -> usize {
self.spec.min_blobs_by_root_request
}

/// Returns the `max_blobs_by_root_request` set in `ChainSpec`.
pub fn max_blobs_by_root_request(&self) -> usize {
self.spec.max_blobs_by_root_request
}

/// Returns the `max_request_blob_sidecars` set in `ChainSpec`.
pub fn max_request_blob_sidecars(&self) -> usize {
self.spec.max_request_blob_sidecars as usize
}
}

0 comments on commit d350ea3

Please sign in to comment.