Skip to content

Commit b47e3f2

Browse files
Runtime rpc request sizes (#4841)
* add runtime variable list type * add configs to ChainSpec * git rid of max request blocks type * fix tests and lints * remove todos * git rid of old const usage * fix decode impl * add new config to `Config` api struct * add docs fix compilt * move methods for per-fork-spec to chainspec * get values off chain spec * fix compile * remove min by root size * add tests for runtime var list --------- Co-authored-by: Jimmy Chen <[email protected]>
1 parent 5c8c8da commit b47e3f2

File tree

25 files changed

+507
-179
lines changed

25 files changed

+507
-179
lines changed

beacon_node/beacon_chain/src/data_availability_checker.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use std::sync::Arc;
2222
use task_executor::TaskExecutor;
2323
use types::beacon_block_body::{KzgCommitmentOpts, KzgCommitments};
2424
use types::blob_sidecar::{BlobIdentifier, BlobSidecar, FixedBlobSidecarList};
25-
use types::consts::deneb::MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS;
2625
use types::{BlobSidecarList, ChainSpec, Epoch, EthSpec, Hash256, SignedBeaconBlock, Slot};
2726

2827
mod availability_view;
@@ -424,7 +423,8 @@ impl<T: BeaconChainTypes> DataAvailabilityChecker<T> {
424423
.map(|current_epoch| {
425424
std::cmp::max(
426425
fork_epoch,
427-
current_epoch.saturating_sub(MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS),
426+
current_epoch
427+
.saturating_sub(self.spec.min_epochs_for_blob_sidecars_requests),
428428
)
429429
})
430430
})
@@ -517,7 +517,8 @@ async fn availability_cache_maintenance_service<T: BeaconChainTypes>(
517517
let cutoff_epoch = std::cmp::max(
518518
finalized_epoch + 1,
519519
std::cmp::max(
520-
current_epoch.saturating_sub(MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS),
520+
current_epoch
521+
.saturating_sub(chain.spec.min_epochs_for_blob_sidecars_requests),
521522
deneb_fork_epoch,
522523
),
523524
);

beacon_node/beacon_processor/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ use std::time::Duration;
6060
use task_executor::TaskExecutor;
6161
use tokio::sync::mpsc;
6262
use tokio::sync::mpsc::error::TrySendError;
63-
use types::consts::deneb::MAX_BLOBS_PER_BLOCK;
6463
use types::{Attestation, Hash256, SignedAggregateAndProof, SubnetId};
6564
use types::{EthSpec, Slot};
6665
use work_reprocessing_queue::IgnoredRpcBlock;
@@ -168,8 +167,7 @@ const MAX_BLOCKS_BY_RANGE_QUEUE_LEN: usize = 1_024;
168167

169168
/// The maximum number of queued `BlobsByRangeRequest` objects received from the network RPC that
170169
/// will be stored before we start dropping them.
171-
const MAX_BLOBS_BY_RANGE_QUEUE_LEN: usize =
172-
MAX_BLOCKS_BY_RANGE_QUEUE_LEN * MAX_BLOBS_PER_BLOCK as usize;
170+
const MAX_BLOBS_BY_RANGE_QUEUE_LEN: usize = 1024;
173171

174172
/// The maximum number of queued `BlocksByRootRequest` objects received from the network RPC that
175173
/// will be stored before we start dropping them.

beacon_node/lighthouse_network/src/rpc/codec/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ mod tests {
316316
));
317317

318318
// Request limits
319-
let limit = protocol_id.rpc_request_limits();
319+
let limit = protocol_id.rpc_request_limits(&fork_context.spec);
320320
let mut max = encode_len(limit.max + 1);
321321
let mut codec = SSZSnappyOutboundCodec::<Spec>::new(
322322
protocol_id.clone(),

beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ use std::io::{Read, Write};
1515
use std::marker::PhantomData;
1616
use std::sync::Arc;
1717
use tokio_util::codec::{Decoder, Encoder};
18+
use types::ChainSpec;
1819
use types::{
19-
BlobSidecar, EthSpec, ForkContext, ForkName, Hash256, LightClientBootstrap, SignedBeaconBlock,
20-
SignedBeaconBlockAltair, SignedBeaconBlockBase, SignedBeaconBlockCapella,
21-
SignedBeaconBlockDeneb, SignedBeaconBlockMerge,
20+
BlobSidecar, EthSpec, ForkContext, ForkName, Hash256, LightClientBootstrap,
21+
RuntimeVariableList, SignedBeaconBlock, SignedBeaconBlockAltair, SignedBeaconBlockBase,
22+
SignedBeaconBlockCapella, SignedBeaconBlockDeneb, SignedBeaconBlockMerge,
2223
};
2324
use unsigned_varint::codec::Uvi;
2425

@@ -140,7 +141,7 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
140141

141142
// Should not attempt to decode rpc chunks with `length > max_packet_size` or not within bounds of
142143
// packet size for ssz container corresponding to `self.protocol`.
143-
let ssz_limits = self.protocol.rpc_request_limits();
144+
let ssz_limits = self.protocol.rpc_request_limits(&self.fork_context.spec);
144145
if ssz_limits.is_out_of_bounds(length, self.max_packet_size) {
145146
return Err(RPCError::InvalidData(format!(
146147
"RPC request length for protocol {:?} is out of bounds, length {}",
@@ -161,7 +162,11 @@ impl<TSpec: EthSpec> Decoder for SSZSnappyInboundCodec<TSpec> {
161162
let n = reader.get_ref().get_ref().position();
162163
self.len = None;
163164
let _read_bytes = src.split_to(n as usize);
164-
handle_rpc_request(self.protocol.versioned_protocol, &decoded_buffer)
165+
handle_rpc_request(
166+
self.protocol.versioned_protocol,
167+
&decoded_buffer,
168+
&self.fork_context.spec,
169+
)
165170
}
166171
Err(e) => handle_error(e, reader.get_ref().get_ref().position(), max_compressed_len),
167172
}
@@ -451,6 +456,7 @@ fn handle_length(
451456
fn handle_rpc_request<T: EthSpec>(
452457
versioned_protocol: SupportedProtocol,
453458
decoded_buffer: &[u8],
459+
spec: &ChainSpec,
454460
) -> Result<Option<InboundRequest<T>>, RPCError> {
455461
match versioned_protocol {
456462
SupportedProtocol::StatusV1 => Ok(Some(InboundRequest::Status(
@@ -467,20 +473,29 @@ fn handle_rpc_request<T: EthSpec>(
467473
))),
468474
SupportedProtocol::BlocksByRootV2 => Ok(Some(InboundRequest::BlocksByRoot(
469475
BlocksByRootRequest::V2(BlocksByRootRequestV2 {
470-
block_roots: VariableList::from_ssz_bytes(decoded_buffer)?,
476+
block_roots: RuntimeVariableList::from_ssz_bytes(
477+
decoded_buffer,
478+
spec.max_request_blocks as usize,
479+
)?,
471480
}),
472481
))),
473482
SupportedProtocol::BlocksByRootV1 => Ok(Some(InboundRequest::BlocksByRoot(
474483
BlocksByRootRequest::V1(BlocksByRootRequestV1 {
475-
block_roots: VariableList::from_ssz_bytes(decoded_buffer)?,
484+
block_roots: RuntimeVariableList::from_ssz_bytes(
485+
decoded_buffer,
486+
spec.max_request_blocks as usize,
487+
)?,
476488
}),
477489
))),
478490
SupportedProtocol::BlobsByRangeV1 => Ok(Some(InboundRequest::BlobsByRange(
479491
BlobsByRangeRequest::from_ssz_bytes(decoded_buffer)?,
480492
))),
481493
SupportedProtocol::BlobsByRootV1 => {
482494
Ok(Some(InboundRequest::BlobsByRoot(BlobsByRootRequest {
483-
blob_ids: VariableList::from_ssz_bytes(decoded_buffer)?,
495+
blob_ids: RuntimeVariableList::from_ssz_bytes(
496+
decoded_buffer,
497+
spec.max_request_blob_sidecars as usize,
498+
)?,
484499
})))
485500
}
486501
SupportedProtocol::PingV1 => Ok(Some(InboundRequest::Ping(Ping {
@@ -773,21 +788,22 @@ mod tests {
773788
}
774789
}
775790

776-
fn bbroot_request_v1() -> BlocksByRootRequest {
777-
BlocksByRootRequest::new_v1(vec![Hash256::zero()].into())
791+
fn bbroot_request_v1(spec: &ChainSpec) -> BlocksByRootRequest {
792+
BlocksByRootRequest::new_v1(vec![Hash256::zero()], spec)
778793
}
779794

780-
fn bbroot_request_v2() -> BlocksByRootRequest {
781-
BlocksByRootRequest::new(vec![Hash256::zero()].into())
795+
fn bbroot_request_v2(spec: &ChainSpec) -> BlocksByRootRequest {
796+
BlocksByRootRequest::new(vec![Hash256::zero()], spec)
782797
}
783798

784-
fn blbroot_request() -> BlobsByRootRequest {
785-
BlobsByRootRequest {
786-
blob_ids: VariableList::from(vec![BlobIdentifier {
799+
fn blbroot_request(spec: &ChainSpec) -> BlobsByRootRequest {
800+
BlobsByRootRequest::new(
801+
vec![BlobIdentifier {
787802
block_root: Hash256::zero(),
788803
index: 0,
789-
}]),
790-
}
804+
}],
805+
spec,
806+
)
791807
}
792808

793809
fn ping_message() -> Ping {
@@ -1391,22 +1407,22 @@ mod tests {
13911407

13921408
#[test]
13931409
fn test_encode_then_decode_request() {
1410+
let chain_spec = Spec::default_spec();
1411+
13941412
let requests: &[OutboundRequest<Spec>] = &[
13951413
OutboundRequest::Ping(ping_message()),
13961414
OutboundRequest::Status(status_message()),
13971415
OutboundRequest::Goodbye(GoodbyeReason::Fault),
13981416
OutboundRequest::BlocksByRange(bbrange_request_v1()),
13991417
OutboundRequest::BlocksByRange(bbrange_request_v2()),
1400-
OutboundRequest::BlocksByRoot(bbroot_request_v1()),
1401-
OutboundRequest::BlocksByRoot(bbroot_request_v2()),
1418+
OutboundRequest::BlocksByRoot(bbroot_request_v1(&chain_spec)),
1419+
OutboundRequest::BlocksByRoot(bbroot_request_v2(&chain_spec)),
14021420
OutboundRequest::MetaData(MetadataRequest::new_v1()),
14031421
OutboundRequest::BlobsByRange(blbrange_request()),
1404-
OutboundRequest::BlobsByRoot(blbroot_request()),
1422+
OutboundRequest::BlobsByRoot(blbroot_request(&chain_spec)),
14051423
OutboundRequest::MetaData(MetadataRequest::new_v2()),
14061424
];
14071425

1408-
let chain_spec = Spec::default_spec();
1409-
14101426
for req in requests.iter() {
14111427
for fork_name in ForkName::list_all() {
14121428
encode_then_decode_request(req.clone(), fork_name, &chain_spec);

beacon_node/lighthouse_network/src/rpc/config.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
time::Duration,
55
};
66

7-
use super::{methods, rate_limiter::Quota, Protocol};
7+
use super::{rate_limiter::Quota, Protocol};
88

99
use serde::{Deserialize, Serialize};
1010

@@ -99,11 +99,9 @@ impl RateLimiterConfig {
9999
pub const DEFAULT_META_DATA_QUOTA: Quota = Quota::n_every(2, 5);
100100
pub const DEFAULT_STATUS_QUOTA: Quota = Quota::n_every(5, 15);
101101
pub const DEFAULT_GOODBYE_QUOTA: Quota = Quota::one_every(10);
102-
pub const DEFAULT_BLOCKS_BY_RANGE_QUOTA: Quota =
103-
Quota::n_every(methods::MAX_REQUEST_BLOCKS, 10);
102+
pub const DEFAULT_BLOCKS_BY_RANGE_QUOTA: Quota = Quota::n_every(1024, 10);
104103
pub const DEFAULT_BLOCKS_BY_ROOT_QUOTA: Quota = Quota::n_every(128, 10);
105-
pub const DEFAULT_BLOBS_BY_RANGE_QUOTA: Quota =
106-
Quota::n_every(methods::MAX_REQUEST_BLOB_SIDECARS, 10);
104+
pub const DEFAULT_BLOBS_BY_RANGE_QUOTA: Quota = Quota::n_every(768, 10);
107105
pub const DEFAULT_BLOBS_BY_ROOT_QUOTA: Quota = Quota::n_every(128, 10);
108106
pub const DEFAULT_LIGHT_CLIENT_BOOTSTRAP_QUOTA: Quota = Quota::one_every(10);
109107
}

beacon_node/lighthouse_network/src/rpc/methods.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,22 @@ use regex::bytes::Regex;
55
use serde::Serialize;
66
use ssz::Encode;
77
use ssz_derive::{Decode, Encode};
8-
use ssz_types::{
9-
typenum::{U1024, U128, U256, U768},
10-
VariableList,
11-
};
8+
use ssz_types::{typenum::U256, VariableList};
129
use std::marker::PhantomData;
1310
use std::ops::Deref;
1411
use std::sync::Arc;
1512
use strum::IntoStaticStr;
1613
use superstruct::superstruct;
1714
use types::blob_sidecar::BlobIdentifier;
18-
use types::consts::deneb::MAX_BLOBS_PER_BLOCK;
1915
use types::{
20-
blob_sidecar::BlobSidecar, Epoch, EthSpec, Hash256, LightClientBootstrap, SignedBeaconBlock,
21-
Slot,
16+
blob_sidecar::BlobSidecar, ChainSpec, Epoch, EthSpec, Hash256, LightClientBootstrap,
17+
RuntimeVariableList, SignedBeaconBlock, Slot,
2218
};
2319

24-
/// Maximum number of blocks in a single request.
25-
pub type MaxRequestBlocks = U1024;
26-
pub const MAX_REQUEST_BLOCKS: u64 = 1024;
27-
2820
/// Maximum length of error message.
2921
pub type MaxErrorLen = U256;
3022
pub const MAX_ERROR_LEN: u64 = 256;
3123

32-
pub type MaxRequestBlocksDeneb = U128;
33-
pub const MAX_REQUEST_BLOCKS_DENEB: u64 = 128;
34-
35-
pub type MaxRequestBlobSidecars = U768;
36-
pub const MAX_REQUEST_BLOB_SIDECARS: u64 = MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK;
37-
3824
/// Wrapper over SSZ List to represent error message in rpc responses.
3925
#[derive(Debug, Clone)]
4026
pub struct ErrorType(pub VariableList<u8, MaxErrorLen>);
@@ -344,22 +330,23 @@ impl OldBlocksByRangeRequest {
344330
}
345331

346332
/// Request a number of beacon block bodies from a peer.
347-
#[superstruct(
348-
variants(V1, V2),
349-
variant_attributes(derive(Encode, Decode, Clone, Debug, PartialEq))
350-
)]
333+
#[superstruct(variants(V1, V2), variant_attributes(derive(Clone, Debug, PartialEq)))]
351334
#[derive(Clone, Debug, PartialEq)]
352335
pub struct BlocksByRootRequest {
353336
/// The list of beacon block bodies being requested.
354-
pub block_roots: VariableList<Hash256, MaxRequestBlocks>,
337+
pub block_roots: RuntimeVariableList<Hash256>,
355338
}
356339

357340
impl BlocksByRootRequest {
358-
pub fn new(block_roots: VariableList<Hash256, MaxRequestBlocks>) -> Self {
341+
pub fn new(block_roots: Vec<Hash256>, spec: &ChainSpec) -> Self {
342+
let block_roots =
343+
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks as usize);
359344
Self::V2(BlocksByRootRequestV2 { block_roots })
360345
}
361346

362-
pub fn new_v1(block_roots: VariableList<Hash256, MaxRequestBlocks>) -> Self {
347+
pub fn new_v1(block_roots: Vec<Hash256>, spec: &ChainSpec) -> Self {
348+
let block_roots =
349+
RuntimeVariableList::from_vec(block_roots, spec.max_request_blocks as usize);
363350
Self::V1(BlocksByRootRequestV1 { block_roots })
364351
}
365352
}
@@ -368,7 +355,15 @@ impl BlocksByRootRequest {
368355
#[derive(Clone, Debug, PartialEq)]
369356
pub struct BlobsByRootRequest {
370357
/// The list of beacon block roots being requested.
371-
pub blob_ids: VariableList<BlobIdentifier, MaxRequestBlobSidecars>,
358+
pub blob_ids: RuntimeVariableList<BlobIdentifier>,
359+
}
360+
361+
impl BlobsByRootRequest {
362+
pub fn new(blob_ids: Vec<BlobIdentifier>, spec: &ChainSpec) -> Self {
363+
let blob_ids =
364+
RuntimeVariableList::from_vec(blob_ids, spec.max_request_blob_sidecars as usize);
365+
Self { blob_ids }
366+
}
372367
}
373368

374369
/* RPC Handling and Grouping */

beacon_node/lighthouse_network/src/rpc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) use protocol::InboundRequest;
2626
pub use handler::SubstreamId;
2727
pub use methods::{
2828
BlocksByRangeRequest, BlocksByRootRequest, GoodbyeReason, LightClientBootstrapRequest,
29-
MaxRequestBlocks, RPCResponseErrorCode, ResponseTermination, StatusMessage, MAX_REQUEST_BLOCKS,
29+
RPCResponseErrorCode, ResponseTermination, StatusMessage,
3030
};
3131
pub(crate) use outbound::OutboundRequest;
3232
pub use protocol::{max_rpc_size, Protocol, RPCError};

beacon_node/lighthouse_network/src/rpc/protocol.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::methods::*;
22
use crate::rpc::{
33
codec::{base::BaseInboundCodec, ssz_snappy::SSZSnappyInboundCodec, InboundCodec},
44
methods::{MaxErrorLen, ResponseTermination, MAX_ERROR_LEN},
5-
MaxRequestBlocks, MAX_REQUEST_BLOCKS,
65
};
76
use futures::future::BoxFuture;
87
use futures::prelude::{AsyncRead, AsyncWrite};
@@ -22,7 +21,7 @@ use tokio_util::{
2221
};
2322
use types::{
2423
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockCapella, BeaconBlockMerge,
25-
BlobSidecar, EmptyBlock, EthSpec, ForkContext, ForkName, Hash256, MainnetEthSpec, Signature,
24+
BlobSidecar, ChainSpec, EmptyBlock, EthSpec, ForkContext, ForkName, MainnetEthSpec, Signature,
2625
SignedBeaconBlock,
2726
};
2827

@@ -89,32 +88,6 @@ lazy_static! {
8988
+ (<types::KzgCommitment as Encode>::ssz_fixed_len() * <MainnetEthSpec>::max_blobs_per_block())
9089
+ ssz::BYTES_PER_LENGTH_OFFSET; // Length offset for the blob commitments field.
9190

92-
pub static ref BLOCKS_BY_ROOT_REQUEST_MIN: usize =
93-
VariableList::<Hash256, MaxRequestBlocks>::from(Vec::<Hash256>::new())
94-
.as_ssz_bytes()
95-
.len();
96-
pub static ref BLOCKS_BY_ROOT_REQUEST_MAX: usize =
97-
VariableList::<Hash256, MaxRequestBlocks>::from(vec![
98-
Hash256::zero();
99-
MAX_REQUEST_BLOCKS
100-
as usize
101-
])
102-
.as_ssz_bytes()
103-
.len();
104-
105-
pub static ref BLOBS_BY_ROOT_REQUEST_MIN: usize =
106-
VariableList::<Hash256, MaxRequestBlobSidecars>::from(Vec::<Hash256>::new())
107-
.as_ssz_bytes()
108-
.len();
109-
pub static ref BLOBS_BY_ROOT_REQUEST_MAX: usize =
110-
VariableList::<Hash256, MaxRequestBlobSidecars>::from(vec![
111-
Hash256::zero();
112-
MAX_REQUEST_BLOB_SIDECARS
113-
as usize
114-
])
115-
.as_ssz_bytes()
116-
.len();
117-
11891
pub static ref ERROR_TYPE_MIN: usize =
11992
VariableList::<u8, MaxErrorLen>::from(Vec::<u8>::new())
12093
.as_ssz_bytes()
@@ -375,7 +348,7 @@ impl AsRef<str> for ProtocolId {
375348

376349
impl ProtocolId {
377350
/// Returns min and max size for messages of given protocol id requests.
378-
pub fn rpc_request_limits(&self) -> RpcLimits {
351+
pub fn rpc_request_limits(&self, spec: &ChainSpec) -> RpcLimits {
379352
match self.versioned_protocol.protocol() {
380353
Protocol::Status => RpcLimits::new(
381354
<StatusMessage as Encode>::ssz_fixed_len(),
@@ -390,16 +363,12 @@ impl ProtocolId {
390363
<OldBlocksByRangeRequestV2 as Encode>::ssz_fixed_len(),
391364
<OldBlocksByRangeRequestV2 as Encode>::ssz_fixed_len(),
392365
),
393-
Protocol::BlocksByRoot => {
394-
RpcLimits::new(*BLOCKS_BY_ROOT_REQUEST_MIN, *BLOCKS_BY_ROOT_REQUEST_MAX)
395-
}
366+
Protocol::BlocksByRoot => RpcLimits::new(0, spec.max_blocks_by_root_request),
396367
Protocol::BlobsByRange => RpcLimits::new(
397368
<BlobsByRangeRequest as Encode>::ssz_fixed_len(),
398369
<BlobsByRangeRequest as Encode>::ssz_fixed_len(),
399370
),
400-
Protocol::BlobsByRoot => {
401-
RpcLimits::new(*BLOBS_BY_ROOT_REQUEST_MIN, *BLOBS_BY_ROOT_REQUEST_MAX)
402-
}
371+
Protocol::BlobsByRoot => RpcLimits::new(0, spec.max_blobs_by_root_request),
403372
Protocol::Ping => RpcLimits::new(
404373
<Ping as Encode>::ssz_fixed_len(),
405374
<Ping as Encode>::ssz_fixed_len(),

0 commit comments

Comments
 (0)