RPC RequestId Cleanup (#7238)

I've been working at updating another library to latest Lighthouse and got very confused with RPC request Ids.

There were types that had fields called `request_id` and `id`. And interchangeably could have types `PeerRequestId`, `rpc::RequestId`, `AppRequestId`, `api_types::RequestId` or even `Request.id`.

I couldn't keep track of which Id was linked to what and what each type meant.

So this PR mainly does a few things:
- Changes the field naming to match the actual type. So any field that has an  `AppRequestId` will be named `app_request_id` rather than `id` or `request_id` for example.
- I simplified the types. I removed the two different `RequestId` types (one in Lighthouse_network the other in the rpc) and grouped them into one. It has one downside tho. I had to add a few unreachable lines of code in the beacon processor, which the extra type would prevent, but I feel like it might be worth it. Happy to add an extra type to avoid those few lines.
- I also removed the concept of `PeerRequestId` which sometimes went alongside a `request_id`. There were times were had a `PeerRequest` and a `Request` being returned, both of which contain a `RequestId` so we had redundant information. I've simplified the logic by removing `PeerRequestId` and made a `ResponseId`. I think if you look at the code changes, it simplifies things a bit and removes the redundant extra info.

I think with this PR things are a little bit easier to reasonable about what is going on with all these RPC Ids.

NOTE: I did this with the help of AI, so probably should be checked
This commit is contained in:
Age Manning
2025-04-03 21:10:15 +11:00
committed by GitHub
parent 80626e58d2
commit d6cd049a45
16 changed files with 438 additions and 739 deletions

View File

@@ -14,12 +14,10 @@ use beacon_processor::{
work_reprocessing_queue::ReprocessQueueMessage, BeaconProcessorSend, DuplicateCache,
};
use futures::prelude::*;
use lighthouse_network::discovery::ConnectionId;
use lighthouse_network::rpc::*;
use lighthouse_network::{
rpc,
service::api_types::{AppRequestId, SyncRequestId},
MessageId, NetworkGlobals, PeerId, PeerRequestId, PubsubMessage, Response,
MessageId, NetworkGlobals, PeerId, PubsubMessage, Response,
};
use logging::crit;
use logging::TimeLatch;
@@ -54,19 +52,19 @@ pub enum RouterMessage<E: EthSpec> {
/// An RPC request has been received.
RPCRequestReceived {
peer_id: PeerId,
id: PeerRequestId,
request: rpc::Request<E>,
inbound_request_id: InboundRequestId,
request_type: RequestType<E>,
},
/// An RPC response has been received.
RPCResponseReceived {
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
response: Response<E>,
},
/// An RPC request failed
RPCFailed {
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
error: RPCError,
},
/// A gossip message has been received. The fields are: message id, the peer that sent us this
@@ -159,24 +157,24 @@ impl<T: BeaconChainTypes> Router<T> {
}
RouterMessage::RPCRequestReceived {
peer_id,
id,
request,
inbound_request_id,
request_type,
} => {
self.handle_rpc_request(peer_id, id, request);
self.handle_rpc_request(peer_id, inbound_request_id, request_type);
}
RouterMessage::RPCResponseReceived {
peer_id,
request_id,
app_request_id,
response,
} => {
self.handle_rpc_response(peer_id, request_id, response);
self.handle_rpc_response(peer_id, app_request_id, response);
}
RouterMessage::RPCFailed {
peer_id,
request_id,
app_request_id,
error,
} => {
self.on_rpc_error(peer_id, request_id, error);
self.on_rpc_error(peer_id, app_request_id, error);
}
RouterMessage::PubsubMessage(id, peer_id, gossip, should_process) => {
self.handle_gossip(id, peer_id, gossip, should_process);
@@ -190,23 +188,18 @@ impl<T: BeaconChainTypes> Router<T> {
fn handle_rpc_request<E: EthSpec>(
&mut self,
peer_id: PeerId,
request_id: PeerRequestId,
rpc_request: rpc::Request<E>,
inbound_request_id: InboundRequestId, // Use ResponseId here
request_type: RequestType<E>,
) {
if !self.network_globals.peers.read().is_connected(&peer_id) {
debug!( %peer_id, request = ?rpc_request, "Dropping request of disconnected peer");
debug!(%peer_id, request = ?request_type, "Dropping request of disconnected peer");
return;
}
match rpc_request.r#type {
RequestType::Status(status_message) => self.on_status_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
status_message,
),
match request_type {
RequestType::Status(status_message) => {
self.on_status_request(peer_id, inbound_request_id, status_message)
}
RequestType::BlocksByRange(request) => {
// return just one block in case the step parameter is used. https://github.com/ethereum/consensus-specs/pull/2856
let mut count = *request.count();
if *request.step() > 1 {
count = 1;
@@ -223,9 +216,7 @@ impl<T: BeaconChainTypes> Router<T> {
self.handle_beacon_processor_send_result(
self.network_beacon_processor.send_blocks_by_range_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
inbound_request_id,
blocks_request,
),
)
@@ -233,86 +224,50 @@ impl<T: BeaconChainTypes> Router<T> {
RequestType::BlocksByRoot(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor.send_blocks_by_roots_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
inbound_request_id,
request,
),
),
RequestType::BlobsByRange(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor.send_blobs_by_range_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
inbound_request_id,
request,
),
),
RequestType::BlobsByRoot(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor.send_blobs_by_roots_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
inbound_request_id,
request,
),
),
RequestType::DataColumnsByRoot(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_data_columns_by_roots_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
request,
),
.send_data_columns_by_roots_request(peer_id, inbound_request_id, request),
),
RequestType::DataColumnsByRange(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_data_columns_by_range_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
request,
),
.send_data_columns_by_range_request(peer_id, inbound_request_id, request),
),
RequestType::LightClientBootstrap(request) => self.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_light_client_bootstrap_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
request,
),
.send_light_client_bootstrap_request(peer_id, inbound_request_id, request),
),
RequestType::LightClientOptimisticUpdate => self.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_light_client_optimistic_update_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
),
.send_light_client_optimistic_update_request(peer_id, inbound_request_id),
),
RequestType::LightClientFinalityUpdate => self.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_light_client_finality_update_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
),
.send_light_client_finality_update_request(peer_id, inbound_request_id),
),
RequestType::LightClientUpdatesByRange(request) => self
.handle_beacon_processor_send_result(
self.network_beacon_processor
.send_light_client_updates_by_range_request(
peer_id,
request_id.0,
request_id.1,
rpc_request.id,
inbound_request_id,
request,
),
),
@@ -324,7 +279,7 @@ impl<T: BeaconChainTypes> Router<T> {
fn handle_rpc_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
response: Response<T::EthSpec>,
) {
match response {
@@ -336,22 +291,22 @@ impl<T: BeaconChainTypes> Router<T> {
)
}
Response::BlocksByRange(beacon_block) => {
self.on_blocks_by_range_response(peer_id, request_id, beacon_block);
self.on_blocks_by_range_response(peer_id, app_request_id, beacon_block);
}
Response::BlocksByRoot(beacon_block) => {
self.on_blocks_by_root_response(peer_id, request_id, beacon_block);
self.on_blocks_by_root_response(peer_id, app_request_id, beacon_block);
}
Response::BlobsByRange(blob) => {
self.on_blobs_by_range_response(peer_id, request_id, blob);
self.on_blobs_by_range_response(peer_id, app_request_id, blob);
}
Response::BlobsByRoot(blob) => {
self.on_blobs_by_root_response(peer_id, request_id, blob);
self.on_blobs_by_root_response(peer_id, app_request_id, blob);
}
Response::DataColumnsByRoot(data_column) => {
self.on_data_columns_by_root_response(peer_id, request_id, data_column);
self.on_data_columns_by_root_response(peer_id, app_request_id, data_column);
}
Response::DataColumnsByRange(data_column) => {
self.on_data_columns_by_range_response(peer_id, request_id, data_column);
self.on_data_columns_by_range_response(peer_id, app_request_id, data_column);
}
// Light client responses should not be received
Response::LightClientBootstrap(_)
@@ -563,12 +518,12 @@ impl<T: BeaconChainTypes> Router<T> {
/// An error occurred during an RPC request. The state is maintained by the sync manager, so
/// this function notifies the sync manager of the error.
pub fn on_rpc_error(&mut self, peer_id: PeerId, request_id: AppRequestId, error: RPCError) {
pub fn on_rpc_error(&mut self, peer_id: PeerId, app_request_id: AppRequestId, error: RPCError) {
// Check if the failed RPC belongs to sync
if let AppRequestId::Sync(request_id) = request_id {
if let AppRequestId::Sync(sync_request_id) = app_request_id {
self.send_to_sync(SyncMessage::RpcError {
peer_id,
request_id,
sync_request_id,
error,
});
}
@@ -580,9 +535,7 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_status_request(
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
substream_id: SubstreamId,
request_id: RequestId,
inbound_request_id: InboundRequestId, // Use ResponseId here
status: StatusMessage,
) {
debug!(%peer_id, ?status, "Received Status Request");
@@ -590,9 +543,8 @@ impl<T: BeaconChainTypes> Router<T> {
// Say status back.
self.network.send_response(
peer_id,
inbound_request_id,
Response::Status(status_message(&self.chain)),
(connection_id, substream_id),
request_id,
);
self.handle_beacon_processor_send_result(
@@ -606,11 +558,11 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_blocks_by_range_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
beacon_block: Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
) {
let request_id = match request_id {
AppRequestId::Sync(sync_id) => match sync_id {
let sync_request_id = match app_request_id {
AppRequestId::Sync(sync_request_id) => match sync_request_id {
id @ SyncRequestId::BlocksByRange { .. } => id,
other => {
crit!(request = ?other, "BlocksByRange response on incorrect request");
@@ -621,6 +573,7 @@ impl<T: BeaconChainTypes> Router<T> {
crit!(%peer_id, "All BBRange requests belong to sync");
return;
}
AppRequestId::Internal => unreachable!("Handled internally"),
};
trace!(
@@ -631,7 +584,7 @@ impl<T: BeaconChainTypes> Router<T> {
self.send_to_sync(SyncMessage::RpcBlock {
peer_id,
request_id,
sync_request_id,
beacon_block,
seen_timestamp: timestamp_now(),
});
@@ -640,7 +593,7 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_blobs_by_range_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
blob_sidecar: Option<Arc<BlobSidecar<T::EthSpec>>>,
) {
trace!(
@@ -648,10 +601,10 @@ impl<T: BeaconChainTypes> Router<T> {
"Received BlobsByRange Response"
);
if let AppRequestId::Sync(id) = request_id {
if let AppRequestId::Sync(sync_request_id) = app_request_id {
self.send_to_sync(SyncMessage::RpcBlob {
peer_id,
request_id: id,
sync_request_id,
blob_sidecar,
seen_timestamp: timestamp_now(),
});
@@ -664,10 +617,10 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_blocks_by_root_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
beacon_block: Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
) {
let request_id = match request_id {
let sync_request_id = match app_request_id {
AppRequestId::Sync(sync_id) => match sync_id {
id @ SyncRequestId::SingleBlock { .. } => id,
other => {
@@ -679,6 +632,7 @@ impl<T: BeaconChainTypes> Router<T> {
crit!(%peer_id, "All BBRoot requests belong to sync");
return;
}
AppRequestId::Internal => unreachable!("Handled internally"),
};
trace!(
@@ -687,7 +641,7 @@ impl<T: BeaconChainTypes> Router<T> {
);
self.send_to_sync(SyncMessage::RpcBlock {
peer_id,
request_id,
sync_request_id,
beacon_block,
seen_timestamp: timestamp_now(),
});
@@ -697,10 +651,10 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_blobs_by_root_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
blob_sidecar: Option<Arc<BlobSidecar<T::EthSpec>>>,
) {
let request_id = match request_id {
let sync_request_id = match app_request_id {
AppRequestId::Sync(sync_id) => match sync_id {
id @ SyncRequestId::SingleBlob { .. } => id,
other => {
@@ -712,6 +666,7 @@ impl<T: BeaconChainTypes> Router<T> {
crit!(%peer_id, "All BlobsByRoot requests belong to sync");
return;
}
AppRequestId::Internal => unreachable!("Handled internally"),
};
trace!(
@@ -719,7 +674,7 @@ impl<T: BeaconChainTypes> Router<T> {
"Received BlobsByRoot Response"
);
self.send_to_sync(SyncMessage::RpcBlob {
request_id,
sync_request_id,
peer_id,
blob_sidecar,
seen_timestamp: timestamp_now(),
@@ -730,10 +685,10 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_data_columns_by_root_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
data_column: Option<Arc<DataColumnSidecar<T::EthSpec>>>,
) {
let request_id = match request_id {
let sync_request_id = match app_request_id {
AppRequestId::Sync(sync_id) => match sync_id {
id @ SyncRequestId::DataColumnsByRoot { .. } => id,
other => {
@@ -745,6 +700,7 @@ impl<T: BeaconChainTypes> Router<T> {
crit!(%peer_id, "All DataColumnsByRoot requests belong to sync");
return;
}
AppRequestId::Internal => unreachable!("Handled internally"),
};
trace!(
@@ -752,7 +708,7 @@ impl<T: BeaconChainTypes> Router<T> {
"Received DataColumnsByRoot Response"
);
self.send_to_sync(SyncMessage::RpcDataColumn {
request_id,
sync_request_id,
peer_id,
data_column,
seen_timestamp: timestamp_now(),
@@ -762,7 +718,7 @@ impl<T: BeaconChainTypes> Router<T> {
pub fn on_data_columns_by_range_response(
&mut self,
peer_id: PeerId,
request_id: AppRequestId,
app_request_id: AppRequestId,
data_column: Option<Arc<DataColumnSidecar<T::EthSpec>>>,
) {
trace!(
@@ -770,10 +726,10 @@ impl<T: BeaconChainTypes> Router<T> {
"Received DataColumnsByRange Response"
);
if let AppRequestId::Sync(id) = request_id {
if let AppRequestId::Sync(sync_request_id) = app_request_id {
self.send_to_sync(SyncMessage::RpcDataColumn {
peer_id,
request_id: id,
sync_request_id,
data_column,
seen_timestamp: timestamp_now(),
});
@@ -824,7 +780,7 @@ impl<E: EthSpec> HandlerNetworkContext<E> {
pub fn send_processor_request(&mut self, peer_id: PeerId, request: RequestType<E>) {
self.inform_network(NetworkMessage::SendRequest {
peer_id,
request_id: AppRequestId::Router,
app_request_id: AppRequestId::Router,
request,
})
}
@@ -833,14 +789,12 @@ impl<E: EthSpec> HandlerNetworkContext<E> {
pub fn send_response(
&mut self,
peer_id: PeerId,
inbound_request_id: InboundRequestId,
response: Response<E>,
id: PeerRequestId,
request_id: RequestId,
) {
self.inform_network(NetworkMessage::SendResponse {
request_id,
peer_id,
id,
inbound_request_id,
response,
})
}