Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix rpc limits version 2 #3146

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 56 additions & 16 deletions beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,11 @@ mod tests {
ForkContext::new::<Spec>(current_slot, Hash256::zero(), &chain_spec)
}

fn base_block() -> SignedBeaconBlock<Spec> {
let full_block = BeaconBlock::Base(BeaconBlockBase::<Spec>::full(&Spec::default_spec()));
SignedBeaconBlock::from_block(full_block, Signature::empty())
/// Smallest sized block across all current forks. Useful for testing
/// min length check conditions.
divagant-martian marked this conversation as resolved.
Show resolved Hide resolved
fn empty_base_block() -> SignedBeaconBlock<Spec> {
let empty_block = BeaconBlock::Base(BeaconBlockBase::<Spec>::empty(&Spec::default_spec()));
SignedBeaconBlock::from_block(empty_block, Signature::empty())
}

fn altair_block() -> SignedBeaconBlock<Spec> {
Expand Down Expand Up @@ -830,10 +832,12 @@ mod tests {
encode_then_decode(
Protocol::BlocksByRange,
Version::V1,
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(empty_base_block()))),
ForkName::Base,
),
Ok(Some(RPCResponse::BlocksByRange(Box::new(base_block()))))
Ok(Some(RPCResponse::BlocksByRange(Box::new(
empty_base_block()
))))
);

assert!(
Expand All @@ -854,10 +858,12 @@ mod tests {
encode_then_decode(
Protocol::BlocksByRoot,
Version::V1,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Base,
),
Ok(Some(RPCResponse::BlocksByRoot(Box::new(base_block()))))
Ok(Some(RPCResponse::BlocksByRoot(
Box::new(empty_base_block())
)))
);

assert!(
Expand Down Expand Up @@ -941,10 +947,27 @@ mod tests {
encode_then_decode(
Protocol::BlocksByRange,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(empty_base_block()))),
ForkName::Base,
),
Ok(Some(RPCResponse::BlocksByRange(Box::new(base_block()))))
Ok(Some(RPCResponse::BlocksByRange(Box::new(
empty_base_block()
))))
);

// Decode the smallest possible base block when current fork is altair
// This is useful for checking that we allow for blocks smaller than
// the current_fork's rpc limit
assert_eq!(
encode_then_decode(
Protocol::BlocksByRange,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(empty_base_block()))),
ForkName::Altair,
),
Ok(Some(RPCResponse::BlocksByRange(Box::new(
empty_base_block()
))))
);

assert_eq!(
Expand Down Expand Up @@ -996,10 +1019,27 @@ mod tests {
encode_then_decode(
Protocol::BlocksByRoot,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Base,
),
Ok(Some(RPCResponse::BlocksByRoot(Box::new(base_block())))),
Ok(Some(RPCResponse::BlocksByRoot(
Box::new(empty_base_block())
))),
);

// Decode the smallest possible base block when current fork is altair
// This is useful for checking that we allow for blocks smaller than
// the current_fork's rpc limit
assert_eq!(
encode_then_decode(
Protocol::BlocksByRoot,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Altair,
),
Ok(Some(RPCResponse::BlocksByRoot(
Box::new(empty_base_block())
)))
);

assert_eq!(
Expand Down Expand Up @@ -1073,7 +1113,7 @@ mod tests {
let mut encoded_bytes = encode(
Protocol::BlocksByRange,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(empty_base_block()))),
ForkName::Base,
)
.unwrap();
Expand All @@ -1094,7 +1134,7 @@ mod tests {
let mut encoded_bytes = encode(
Protocol::BlocksByRoot,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Base,
)
.unwrap();
Expand All @@ -1116,7 +1156,7 @@ mod tests {
let mut encoded_bytes = encode(
Protocol::BlocksByRange,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRange(Box::new(empty_base_block()))),
ForkName::Altair,
)
.unwrap();
Expand Down Expand Up @@ -1186,7 +1226,7 @@ mod tests {
let mut encoded_bytes = encode(
Protocol::BlocksByRoot,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Altair,
)
.unwrap();
Expand All @@ -1210,7 +1250,7 @@ mod tests {
let mut encoded_bytes = encode(
Protocol::BlocksByRoot,
Version::V2,
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(base_block()))),
RPCCodedResponse::Success(RPCResponse::BlocksByRoot(Box::new(empty_base_block()))),
ForkName::Altair,
)
.unwrap();
Expand Down
48 changes: 22 additions & 26 deletions beacon_node/lighthouse_network/src/rpc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ pub fn max_rpc_size(fork_context: &ForkContext) -> usize {
}
}

/// Returns the rpc limits for beacon_block_by_range and beacon_block_by_root responses.
///
/// Note: This function should take care to return the min/max limits accounting for all
/// previous valid forks when adding a new fork variant.
pub fn rpc_block_limits_by_fork(current_fork: ForkName) -> RpcLimits {
match &current_fork {
ForkName::Base => {
RpcLimits::new(*SIGNED_BEACON_BLOCK_BASE_MIN, *SIGNED_BEACON_BLOCK_BASE_MAX)
}
ForkName::Altair => RpcLimits::new(
*SIGNED_BEACON_BLOCK_BASE_MIN, // Base block is smaller than altair blocks
*SIGNED_BEACON_BLOCK_ALTAIR_MAX, // Altair block is larger than base blocks
),
ForkName::Merge => RpcLimits::new(
*SIGNED_BEACON_BLOCK_BASE_MIN, // Base block is smaller than altair and merge blocks
*SIGNED_BEACON_BLOCK_MERGE_MAX, // Merge block is larger than base and altair blocks
),
}
}

/// Protocol names to be used.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Protocol {
Expand Down Expand Up @@ -281,32 +301,8 @@ impl ProtocolId {
<StatusMessage as Encode>::ssz_fixed_len(),
),
Protocol::Goodbye => RpcLimits::new(0, 0), // Goodbye request has no response
Protocol::BlocksByRange => match fork_context.current_fork() {
ForkName::Base => {
RpcLimits::new(*SIGNED_BEACON_BLOCK_BASE_MIN, *SIGNED_BEACON_BLOCK_BASE_MAX)
}
ForkName::Altair => RpcLimits::new(
*SIGNED_BEACON_BLOCK_ALTAIR_MIN,
*SIGNED_BEACON_BLOCK_ALTAIR_MAX,
),
ForkName::Merge => RpcLimits::new(
*SIGNED_BEACON_BLOCK_MERGE_MIN,
*SIGNED_BEACON_BLOCK_MERGE_MAX,
),
},
Protocol::BlocksByRoot => match fork_context.current_fork() {
ForkName::Base => {
RpcLimits::new(*SIGNED_BEACON_BLOCK_BASE_MIN, *SIGNED_BEACON_BLOCK_BASE_MAX)
}
ForkName::Altair => RpcLimits::new(
*SIGNED_BEACON_BLOCK_ALTAIR_MIN,
*SIGNED_BEACON_BLOCK_ALTAIR_MAX,
),
ForkName::Merge => RpcLimits::new(
*SIGNED_BEACON_BLOCK_MERGE_MIN,
*SIGNED_BEACON_BLOCK_MERGE_MAX,
),
},
Protocol::BlocksByRange => rpc_block_limits_by_fork(fork_context.current_fork()),
Protocol::BlocksByRoot => rpc_block_limits_by_fork(fork_context.current_fork()),

Protocol::Ping => RpcLimits::new(
<Ping as Encode>::ssz_fixed_len(),
Expand Down
5 changes: 5 additions & 0 deletions testing/simulator/src/sync_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ fn syncing_sim(
let end_after_checks = true;
let eth1_block_time = Duration::from_millis(15_000 / speed_up_factor);

// Set fork epochs to test syncing across fork boundaries
spec.altair_fork_epoch = Some(Epoch::new(1));
spec.bellatrix_fork_epoch = Some(Epoch::new(2));
spec.seconds_per_slot /= speed_up_factor;
spec.seconds_per_slot = max(1, spec.seconds_per_slot);
spec.eth1_follow_distance = 16;
Expand All @@ -86,6 +89,8 @@ fn syncing_sim(
beacon_config.dummy_eth1_backend = true;
beacon_config.sync_eth1_chain = true;

beacon_config.http_api.allow_sync_stalled = true;

beacon_config.network.enr_address = Some(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));

// Generate the directories and keystores required for the validator clients.
Expand Down