mirror of
https://github.com/sigp/lighthouse.git
synced 2026-06-18 22:49:34 +00:00
Update Rust Edition to 2024 (#7766)
* #7749 Thanks @dknopik and @michaelsproul for your help!
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::rpc::RequestType;
|
||||
use crate::rpc::methods::*;
|
||||
use crate::rpc::protocol::{
|
||||
Encoding, ProtocolId, RPCError, SupportedProtocol, ERROR_TYPE_MAX, ERROR_TYPE_MIN,
|
||||
ERROR_TYPE_MAX, ERROR_TYPE_MIN, Encoding, ProtocolId, RPCError, SupportedProtocol,
|
||||
};
|
||||
use crate::rpc::RequestType;
|
||||
use libp2p::bytes::BufMut;
|
||||
use libp2p::bytes::BytesMut;
|
||||
use snap::read::FrameDecoder;
|
||||
@@ -467,12 +467,12 @@ fn context_bytes<E: EthSpec>(
|
||||
resp: &RpcResponse<E>,
|
||||
) -> Option<[u8; CONTEXT_BYTES_LEN]> {
|
||||
// Add the context bytes if required
|
||||
if protocol.has_context_bytes() {
|
||||
if let RpcResponse::Success(rpc_variant) = resp {
|
||||
return rpc_variant
|
||||
.slot()
|
||||
.map(|slot| fork_context.context_bytes(slot.epoch(E::slots_per_epoch())));
|
||||
}
|
||||
if protocol.has_context_bytes()
|
||||
&& let RpcResponse::Success(rpc_variant) = resp
|
||||
{
|
||||
return rpc_variant
|
||||
.slot()
|
||||
.map(|slot| fork_context.context_bytes(slot.epoch(E::slots_per_epoch())));
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -902,10 +902,10 @@ mod tests {
|
||||
use crate::rpc::protocol::*;
|
||||
use crate::types::{EnrAttestationBitfield, EnrSyncCommitteeBitfield};
|
||||
use types::{
|
||||
blob_sidecar::BlobIdentifier, data_column_sidecar::Cell, BeaconBlock, BeaconBlockAltair,
|
||||
BeaconBlockBase, BeaconBlockBellatrix, BeaconBlockHeader, DataColumnsByRootIdentifier,
|
||||
EmptyBlock, Epoch, FixedBytesExtended, FullPayload, KzgCommitment, KzgProof, Signature,
|
||||
SignedBeaconBlockHeader, Slot,
|
||||
BeaconBlock, BeaconBlockAltair, BeaconBlockBase, BeaconBlockBellatrix, BeaconBlockHeader,
|
||||
DataColumnsByRootIdentifier, EmptyBlock, Epoch, FixedBytesExtended, FullPayload,
|
||||
KzgCommitment, KzgProof, Signature, SignedBeaconBlockHeader, Slot,
|
||||
blob_sidecar::BlobIdentifier, data_column_sidecar::Cell,
|
||||
};
|
||||
|
||||
type Spec = types::MainnetEthSpec;
|
||||
@@ -1903,13 +1903,15 @@ mod tests {
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
assert!(decode_response(
|
||||
SupportedProtocol::MetaDataV2,
|
||||
&mut encoded_bytes,
|
||||
ForkName::Altair,
|
||||
&chain_spec,
|
||||
)
|
||||
.is_err());
|
||||
assert!(
|
||||
decode_response(
|
||||
SupportedProtocol::MetaDataV2,
|
||||
&mut encoded_bytes,
|
||||
ForkName::Altair,
|
||||
&chain_spec,
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
|
||||
// Sending context bytes which do not correspond to any fork should return an error
|
||||
let mut encoded_bytes = encode_response(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{rate_limiter::Quota, Protocol};
|
||||
use super::{Protocol, rate_limiter::Quota};
|
||||
use std::num::NonZeroU64;
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
|
||||
@@ -8,25 +8,25 @@ use super::{RPCReceived, RPCSend, ReqId};
|
||||
use crate::rpc::outbound::OutboundFramed;
|
||||
use crate::rpc::protocol::InboundFramed;
|
||||
use fnv::FnvHashMap;
|
||||
use futures::prelude::*;
|
||||
use futures::SinkExt;
|
||||
use futures::prelude::*;
|
||||
use libp2p::PeerId;
|
||||
use libp2p::swarm::handler::{
|
||||
ConnectionEvent, ConnectionHandler, ConnectionHandlerEvent, DialUpgradeError,
|
||||
FullyNegotiatedInbound, FullyNegotiatedOutbound, StreamUpgradeError, SubstreamProtocol,
|
||||
};
|
||||
use libp2p::swarm::{ConnectionId, Stream};
|
||||
use libp2p::PeerId;
|
||||
use logging::crit;
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
collections::{hash_map::Entry, VecDeque},
|
||||
collections::{VecDeque, hash_map::Entry},
|
||||
pin::Pin,
|
||||
sync::Arc,
|
||||
task::{Context, Poll},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::time::{sleep, Sleep};
|
||||
use tokio_util::time::{delay_queue, DelayQueue};
|
||||
use tokio::time::{Sleep, sleep};
|
||||
use tokio_util::time::{DelayQueue, delay_queue};
|
||||
use tracing::{debug, trace};
|
||||
use types::{EthSpec, ForkContext, Slot};
|
||||
|
||||
@@ -848,23 +848,22 @@ where
|
||||
}
|
||||
|
||||
// Check if we have completed sending a goodbye, disconnect.
|
||||
if let HandlerState::ShuttingDown(_) = self.state {
|
||||
if self.dial_queue.is_empty()
|
||||
&& self.outbound_substreams.is_empty()
|
||||
&& self.inbound_substreams.is_empty()
|
||||
&& self.events_out.is_empty()
|
||||
&& self.dial_negotiated == 0
|
||||
{
|
||||
debug!(
|
||||
peer_id = %self.peer_id,
|
||||
connection_id = %self.connection_id,
|
||||
"Goodbye sent, Handler deactivated"
|
||||
);
|
||||
self.state = HandlerState::Deactivated;
|
||||
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
|
||||
HandlerEvent::Close(RPCError::Disconnected),
|
||||
));
|
||||
}
|
||||
if let HandlerState::ShuttingDown(_) = self.state
|
||||
&& self.dial_queue.is_empty()
|
||||
&& self.outbound_substreams.is_empty()
|
||||
&& self.inbound_substreams.is_empty()
|
||||
&& self.events_out.is_empty()
|
||||
&& self.dial_negotiated == 0
|
||||
{
|
||||
debug!(
|
||||
peer_id = %self.peer_id,
|
||||
connection_id = %self.connection_id,
|
||||
"Goodbye sent, Handler deactivated"
|
||||
);
|
||||
self.state = HandlerState::Deactivated;
|
||||
return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
|
||||
HandlerEvent::Close(RPCError::Disconnected),
|
||||
));
|
||||
}
|
||||
|
||||
Poll::Pending
|
||||
|
||||
@@ -5,7 +5,7 @@ use regex::bytes::Regex;
|
||||
use serde::Serialize;
|
||||
use ssz::Encode;
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use ssz_types::{typenum::U256, VariableList};
|
||||
use ssz_types::{VariableList, typenum::U256};
|
||||
use std::fmt::Display;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
@@ -15,10 +15,10 @@ use superstruct::superstruct;
|
||||
use types::blob_sidecar::BlobIdentifier;
|
||||
use types::light_client_update::MAX_REQUEST_LIGHT_CLIENT_UPDATES;
|
||||
use types::{
|
||||
blob_sidecar::BlobSidecar, ChainSpec, ColumnIndex, DataColumnSidecar,
|
||||
DataColumnsByRootIdentifier, Epoch, EthSpec, ForkContext, Hash256, LightClientBootstrap,
|
||||
LightClientFinalityUpdate, LightClientOptimisticUpdate, LightClientUpdate, RuntimeVariableList,
|
||||
SignedBeaconBlock, Slot,
|
||||
ChainSpec, ColumnIndex, DataColumnSidecar, DataColumnsByRootIdentifier, Epoch, EthSpec,
|
||||
ForkContext, Hash256, LightClientBootstrap, LightClientFinalityUpdate,
|
||||
LightClientOptimisticUpdate, LightClientUpdate, RuntimeVariableList, SignedBeaconBlock, Slot,
|
||||
blob_sidecar::BlobSidecar,
|
||||
};
|
||||
|
||||
/// Maximum length of error message.
|
||||
@@ -784,7 +784,16 @@ impl std::fmt::Display for RpcErrorResponse {
|
||||
|
||||
impl std::fmt::Display for StatusMessage {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Status Message: Fork Digest: {:?}, Finalized Root: {}, Finalized Epoch: {}, Head Root: {}, Head Slot: {}, Earliest available slot: {:?}", self.fork_digest(), self.finalized_root(), self.finalized_epoch(), self.head_root(), self.head_slot(), self.earliest_available_slot())
|
||||
write!(
|
||||
f,
|
||||
"Status Message: Fork Digest: {:?}, Finalized Root: {}, Finalized Epoch: {}, Head Root: {}, Head Slot: {}, Earliest available slot: {:?}",
|
||||
self.fork_digest(),
|
||||
self.finalized_root(),
|
||||
self.finalized_epoch(),
|
||||
self.head_root(),
|
||||
self.head_slot(),
|
||||
self.earliest_available_slot()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
//! syncing.
|
||||
|
||||
use handler::RPCHandler;
|
||||
use libp2p::PeerId;
|
||||
use libp2p::core::transport::PortUse;
|
||||
use libp2p::swarm::{
|
||||
handler::ConnectionHandler, CloseConnection, ConnectionId, NetworkBehaviour, NotifyHandler,
|
||||
ToSwarm,
|
||||
CloseConnection, ConnectionId, NetworkBehaviour, NotifyHandler, ToSwarm,
|
||||
handler::ConnectionHandler,
|
||||
};
|
||||
use libp2p::swarm::{ConnectionClosed, FromSwarm, SubstreamProtocol, THandlerInEvent};
|
||||
use libp2p::PeerId;
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
@@ -247,17 +247,17 @@ impl<Id: ReqId, E: EthSpec> RPC<Id, E> {
|
||||
request_id: InboundRequestId,
|
||||
response: RpcResponse<E>,
|
||||
) {
|
||||
if let Some(response_limiter) = self.response_limiter.as_mut() {
|
||||
if !response_limiter.allows(
|
||||
if let Some(response_limiter) = self.response_limiter.as_mut()
|
||||
&& !response_limiter.allows(
|
||||
peer_id,
|
||||
protocol,
|
||||
request_id.connection_id,
|
||||
request_id.substream_id,
|
||||
response.clone(),
|
||||
) {
|
||||
// Response is logged and queued internally in the response limiter.
|
||||
return;
|
||||
}
|
||||
)
|
||||
{
|
||||
// Response is logged and queued internally in the response limiter.
|
||||
return;
|
||||
}
|
||||
|
||||
self.events.push(ToSwarm::NotifyHandler {
|
||||
@@ -564,15 +564,15 @@ where
|
||||
}
|
||||
|
||||
fn poll(&mut self, cx: &mut Context) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
if let Some(response_limiter) = self.response_limiter.as_mut() {
|
||||
if let Poll::Ready(responses) = response_limiter.poll_ready(cx) {
|
||||
for response in responses {
|
||||
self.events.push(ToSwarm::NotifyHandler {
|
||||
peer_id: response.peer_id,
|
||||
handler: NotifyHandler::One(response.connection_id),
|
||||
event: RPCSend::Response(response.substream_id, response.response),
|
||||
});
|
||||
}
|
||||
if let Some(response_limiter) = self.response_limiter.as_mut()
|
||||
&& let Poll::Ready(responses) = response_limiter.poll_ready(cx)
|
||||
{
|
||||
for response in responses {
|
||||
self.events.push(ToSwarm::NotifyHandler {
|
||||
peer_id: response.peer_id,
|
||||
handler: NotifyHandler::One(response.connection_id),
|
||||
event: RPCSend::Response(response.substream_id, response.response),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::protocol::ProtocolId;
|
||||
use super::RPCError;
|
||||
use super::RequestType;
|
||||
use super::protocol::ProtocolId;
|
||||
use crate::rpc::codec::SSZSnappyOutboundCodec;
|
||||
use crate::rpc::protocol::Encoding;
|
||||
use futures::future::BoxFuture;
|
||||
|
||||
@@ -1030,7 +1030,7 @@ impl RPCError {
|
||||
/// Used for metrics.
|
||||
pub fn as_static_str(&self) -> &'static str {
|
||||
match self {
|
||||
RPCError::ErrorResponse(ref code, ..) => code.into(),
|
||||
RPCError::ErrorResponse(code, ..) => code.into(),
|
||||
e => e.into(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -506,25 +506,37 @@ mod tests {
|
||||
// | | | | |
|
||||
// 0 1 2
|
||||
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.0), &key, 4)
|
||||
.is_ok());
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.0), &key, 4)
|
||||
.is_ok()
|
||||
);
|
||||
limiter.prune(Duration::from_secs_f32(0.1));
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.1), &key, 1)
|
||||
.is_err());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.5), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(1.0), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(1.4), &key, 1)
|
||||
.is_err());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(2.0), &key, 2)
|
||||
.is_ok());
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.1), &key, 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.5), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(1.0), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(1.4), &key, 1)
|
||||
.is_err()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(2.0), &key, 2)
|
||||
.is_ok()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -539,21 +551,31 @@ mod tests {
|
||||
// first half second, when one token will be available again. Check also that before
|
||||
// regaining a token, another request is rejected
|
||||
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.0), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.1), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.2), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.3), &key, 1)
|
||||
.is_ok());
|
||||
assert!(limiter
|
||||
.allows(Duration::from_secs_f32(0.4), &key, 1)
|
||||
.is_err());
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.0), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.1), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.2), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.3), &key, 1)
|
||||
.is_ok()
|
||||
);
|
||||
assert!(
|
||||
limiter
|
||||
.allows(Duration::from_secs_f32(0.4), &key, 1)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::PeerId;
|
||||
use crate::rpc::config::InboundRateLimiterConfig;
|
||||
use crate::rpc::rate_limiter::{RPCRateLimiter, RateLimitedErr};
|
||||
use crate::rpc::self_limiter::timestamp_now;
|
||||
use crate::rpc::{Protocol, RpcResponse, SubstreamId};
|
||||
use crate::PeerId;
|
||||
use futures::FutureExt;
|
||||
use libp2p::swarm::ConnectionId;
|
||||
use logging::crit;
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
use super::{
|
||||
BehaviourAction, MAX_CONCURRENT_REQUESTS, Protocol, RPCSend, ReqId, RequestType,
|
||||
config::OutboundRateLimiterConfig,
|
||||
rate_limiter::{RPCRateLimiter as RateLimiter, RateLimitedErr},
|
||||
BehaviourAction, Protocol, RPCSend, ReqId, RequestType, MAX_CONCURRENT_REQUESTS,
|
||||
};
|
||||
use crate::rpc::rate_limiter::RateLimiterItem;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{
|
||||
collections::{hash_map::Entry, HashMap, VecDeque},
|
||||
collections::{HashMap, VecDeque, hash_map::Entry},
|
||||
sync::Arc,
|
||||
task::{Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use futures::FutureExt;
|
||||
use libp2p::{swarm::NotifyHandler, PeerId};
|
||||
use libp2p::{PeerId, swarm::NotifyHandler};
|
||||
use logging::crit;
|
||||
use smallvec::SmallVec;
|
||||
use tokio_util::time::DelayQueue;
|
||||
@@ -130,24 +130,23 @@ impl<Id: ReqId, E: EthSpec> SelfRateLimiter<Id, E> {
|
||||
request_id: Id,
|
||||
req: RequestType<E>,
|
||||
) -> Result<RPCSend<Id, E>, (QueuedRequest<Id, E>, Duration)> {
|
||||
if let Some(active_request) = active_requests.get(&peer_id) {
|
||||
if let Some(count) = active_request.get(&req.protocol()) {
|
||||
if *count >= MAX_CONCURRENT_REQUESTS {
|
||||
debug!(
|
||||
%peer_id,
|
||||
protocol = %req.protocol(),
|
||||
"Self rate limiting due to the number of concurrent requests"
|
||||
);
|
||||
return Err((
|
||||
QueuedRequest {
|
||||
req,
|
||||
request_id,
|
||||
queued_at: timestamp_now(),
|
||||
},
|
||||
Duration::from_millis(WAIT_TIME_DUE_TO_CONCURRENT_REQUESTS),
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(active_request) = active_requests.get(&peer_id)
|
||||
&& let Some(count) = active_request.get(&req.protocol())
|
||||
&& *count >= MAX_CONCURRENT_REQUESTS
|
||||
{
|
||||
debug!(
|
||||
%peer_id,
|
||||
protocol = %req.protocol(),
|
||||
"Self rate limiting due to the number of concurrent requests"
|
||||
);
|
||||
return Err((
|
||||
QueuedRequest {
|
||||
req,
|
||||
request_id,
|
||||
queued_at: timestamp_now(),
|
||||
},
|
||||
Duration::from_millis(WAIT_TIME_DUE_TO_CONCURRENT_REQUESTS),
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(limiter) = rate_limiter.as_mut() {
|
||||
@@ -258,13 +257,13 @@ impl<Id: ReqId, E: EthSpec> SelfRateLimiter<Id, E> {
|
||||
|
||||
/// Informs the limiter that a response has been received.
|
||||
pub fn request_completed(&mut self, peer_id: &PeerId, protocol: Protocol) {
|
||||
if let Some(active_requests) = self.active_requests.get_mut(peer_id) {
|
||||
if let Entry::Occupied(mut entry) = active_requests.entry(protocol) {
|
||||
if *entry.get() > 1 {
|
||||
*entry.get_mut() -= 1;
|
||||
} else {
|
||||
entry.remove();
|
||||
}
|
||||
if let Some(active_requests) = self.active_requests.get_mut(peer_id)
|
||||
&& let Entry::Occupied(mut entry) = active_requests.entry(protocol)
|
||||
{
|
||||
if *entry.get() > 1 {
|
||||
*entry.get_mut() -= 1;
|
||||
} else {
|
||||
entry.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -511,13 +510,17 @@ mod tests {
|
||||
}
|
||||
|
||||
assert!(limiter.active_requests.contains_key(&peer1));
|
||||
assert!(limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer1, Protocol::Ping)));
|
||||
assert!(
|
||||
limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer1, Protocol::Ping))
|
||||
);
|
||||
assert!(limiter.active_requests.contains_key(&peer2));
|
||||
assert!(limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer2, Protocol::Ping)));
|
||||
assert!(
|
||||
limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer2, Protocol::Ping))
|
||||
);
|
||||
|
||||
// Check that the limiter returns the IDs of pending requests and that the IDs are ordered correctly.
|
||||
let mut failed_requests = limiter.peer_disconnected(peer1);
|
||||
@@ -533,13 +536,17 @@ mod tests {
|
||||
|
||||
// Check that peer1’s active and delayed requests have been removed.
|
||||
assert!(!limiter.active_requests.contains_key(&peer1));
|
||||
assert!(!limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer1, Protocol::Ping)));
|
||||
assert!(
|
||||
!limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer1, Protocol::Ping))
|
||||
);
|
||||
|
||||
assert!(limiter.active_requests.contains_key(&peer2));
|
||||
assert!(limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer2, Protocol::Ping)));
|
||||
assert!(
|
||||
limiter
|
||||
.delayed_requests
|
||||
.contains_key(&(peer2, Protocol::Ping))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user