mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-14 18:32:42 +00:00
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:
@@ -108,7 +108,7 @@ pub enum SyncMessage<E: EthSpec> {
|
||||
|
||||
/// A block has been received from the RPC.
|
||||
RpcBlock {
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
beacon_block: Option<Arc<SignedBeaconBlock<E>>>,
|
||||
seen_timestamp: Duration,
|
||||
@@ -116,7 +116,7 @@ pub enum SyncMessage<E: EthSpec> {
|
||||
|
||||
/// A blob has been received from the RPC.
|
||||
RpcBlob {
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
blob_sidecar: Option<Arc<BlobSidecar<E>>>,
|
||||
seen_timestamp: Duration,
|
||||
@@ -124,7 +124,7 @@ pub enum SyncMessage<E: EthSpec> {
|
||||
|
||||
/// A data columns has been received from the RPC
|
||||
RpcDataColumn {
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
data_column: Option<Arc<DataColumnSidecar<E>>>,
|
||||
seen_timestamp: Duration,
|
||||
@@ -153,7 +153,7 @@ pub enum SyncMessage<E: EthSpec> {
|
||||
/// An RPC Error has occurred on a request.
|
||||
RpcError {
|
||||
peer_id: PeerId,
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
error: RPCError,
|
||||
},
|
||||
|
||||
@@ -477,9 +477,9 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
}
|
||||
|
||||
/// Handles RPC errors related to requests that were emitted from the sync manager.
|
||||
fn inject_error(&mut self, peer_id: PeerId, request_id: SyncRequestId, error: RPCError) {
|
||||
fn inject_error(&mut self, peer_id: PeerId, sync_request_id: SyncRequestId, error: RPCError) {
|
||||
trace!("Sync manager received a failed RPC");
|
||||
match request_id {
|
||||
match sync_request_id {
|
||||
SyncRequestId::SingleBlock { id } => {
|
||||
self.on_single_block_response(id, peer_id, RpcEvent::RPCError(error))
|
||||
}
|
||||
@@ -509,8 +509,8 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
fn peer_disconnect(&mut self, peer_id: &PeerId) {
|
||||
// Inject a Disconnected error on all requests associated with the disconnected peer
|
||||
// to retry all batches/lookups
|
||||
for request_id in self.network.peer_disconnected(peer_id) {
|
||||
self.inject_error(*peer_id, request_id, RPCError::Disconnected);
|
||||
for sync_request_id in self.network.peer_disconnected(peer_id) {
|
||||
self.inject_error(*peer_id, sync_request_id, RPCError::Disconnected);
|
||||
}
|
||||
|
||||
// Remove peer from all data structures
|
||||
@@ -751,25 +751,27 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
self.add_peers_force_range_sync(&peers, head_root, head_slot);
|
||||
}
|
||||
SyncMessage::RpcBlock {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
beacon_block,
|
||||
seen_timestamp,
|
||||
} => {
|
||||
self.rpc_block_received(request_id, peer_id, beacon_block, seen_timestamp);
|
||||
self.rpc_block_received(sync_request_id, peer_id, beacon_block, seen_timestamp);
|
||||
}
|
||||
SyncMessage::RpcBlob {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
blob_sidecar,
|
||||
seen_timestamp,
|
||||
} => self.rpc_blob_received(request_id, peer_id, blob_sidecar, seen_timestamp),
|
||||
} => self.rpc_blob_received(sync_request_id, peer_id, blob_sidecar, seen_timestamp),
|
||||
SyncMessage::RpcDataColumn {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
data_column,
|
||||
seen_timestamp,
|
||||
} => self.rpc_data_column_received(request_id, peer_id, data_column, seen_timestamp),
|
||||
} => {
|
||||
self.rpc_data_column_received(sync_request_id, peer_id, data_column, seen_timestamp)
|
||||
}
|
||||
SyncMessage::UnknownParentBlock(peer_id, block, block_root) => {
|
||||
let block_slot = block.slot();
|
||||
let parent_root = block.parent_root();
|
||||
@@ -845,9 +847,9 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
}
|
||||
SyncMessage::RpcError {
|
||||
peer_id,
|
||||
request_id,
|
||||
sync_request_id,
|
||||
error,
|
||||
} => self.inject_error(peer_id, request_id, error),
|
||||
} => self.inject_error(peer_id, sync_request_id, error),
|
||||
SyncMessage::BlockComponentProcessed {
|
||||
process_type,
|
||||
result,
|
||||
@@ -1018,12 +1020,12 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
|
||||
fn rpc_block_received(
|
||||
&mut self,
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
block: Option<Arc<SignedBeaconBlock<T::EthSpec>>>,
|
||||
seen_timestamp: Duration,
|
||||
) {
|
||||
match request_id {
|
||||
match sync_request_id {
|
||||
SyncRequestId::SingleBlock { id } => self.on_single_block_response(
|
||||
id,
|
||||
peer_id,
|
||||
@@ -1060,12 +1062,12 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
|
||||
fn rpc_blob_received(
|
||||
&mut self,
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
blob: Option<Arc<BlobSidecar<T::EthSpec>>>,
|
||||
seen_timestamp: Duration,
|
||||
) {
|
||||
match request_id {
|
||||
match sync_request_id {
|
||||
SyncRequestId::SingleBlob { id } => self.on_single_blob_response(
|
||||
id,
|
||||
peer_id,
|
||||
@@ -1084,12 +1086,12 @@ impl<T: BeaconChainTypes> SyncManager<T> {
|
||||
|
||||
fn rpc_data_column_received(
|
||||
&mut self,
|
||||
request_id: SyncRequestId,
|
||||
sync_request_id: SyncRequestId,
|
||||
peer_id: PeerId,
|
||||
data_column: Option<Arc<DataColumnSidecar<T::EthSpec>>>,
|
||||
seen_timestamp: Duration,
|
||||
) {
|
||||
match request_id {
|
||||
match sync_request_id {
|
||||
SyncRequestId::DataColumnsByRoot(req_id) => {
|
||||
self.on_data_columns_by_root_response(
|
||||
req_id,
|
||||
|
||||
@@ -372,11 +372,11 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
);
|
||||
|
||||
let request = RequestType::Status(status_message.clone());
|
||||
let request_id = AppRequestId::Router;
|
||||
let app_request_id = AppRequestId::Router;
|
||||
let _ = self.send_network_msg(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request,
|
||||
request_id,
|
||||
app_request_id,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -595,7 +595,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
.send(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::BlocksByRoot(request.into_request(&self.fork_context)),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
})
|
||||
.map_err(|_| RpcRequestSendError::NetworkSendError)?;
|
||||
|
||||
@@ -684,7 +684,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
.send(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::BlobsByRoot(request.clone().into_request(&self.fork_context)),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
})
|
||||
.map_err(|_| RpcRequestSendError::NetworkSendError)?;
|
||||
|
||||
@@ -733,7 +733,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
self.send_network_msg(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::DataColumnsByRoot(request.clone().into_request(&self.chain.spec)),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRoot(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRoot(id)),
|
||||
})?;
|
||||
|
||||
debug!(
|
||||
@@ -839,7 +839,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
.send(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::BlocksByRange(request.clone().into()),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::BlocksByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::BlocksByRange(id)),
|
||||
})
|
||||
.map_err(|_| RpcRequestSendError::NetworkSendError)?;
|
||||
|
||||
@@ -880,7 +880,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
.send(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::BlobsByRange(request.clone()),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::BlobsByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::BlobsByRange(id)),
|
||||
})
|
||||
.map_err(|_| RpcRequestSendError::NetworkSendError)?;
|
||||
|
||||
@@ -919,7 +919,7 @@ impl<T: BeaconChainTypes> SyncNetworkContext<T> {
|
||||
self.send_network_msg(NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::DataColumnsByRange(request.clone()),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRange(id)),
|
||||
})
|
||||
.map_err(|_| RpcRequestSendError::NetworkSendError)?;
|
||||
|
||||
|
||||
@@ -460,7 +460,7 @@ impl TestRig {
|
||||
) {
|
||||
self.log("parent_lookup_block_response");
|
||||
self.send_sync_message(SyncMessage::RpcBlock {
|
||||
request_id: SyncRequestId::SingleBlock { id },
|
||||
sync_request_id: SyncRequestId::SingleBlock { id },
|
||||
peer_id,
|
||||
beacon_block,
|
||||
seen_timestamp: D,
|
||||
@@ -475,7 +475,7 @@ impl TestRig {
|
||||
) {
|
||||
self.log("single_lookup_block_response");
|
||||
self.send_sync_message(SyncMessage::RpcBlock {
|
||||
request_id: SyncRequestId::SingleBlock { id },
|
||||
sync_request_id: SyncRequestId::SingleBlock { id },
|
||||
peer_id,
|
||||
beacon_block,
|
||||
seen_timestamp: D,
|
||||
@@ -493,7 +493,7 @@ impl TestRig {
|
||||
blob_sidecar.as_ref().map(|b| b.index)
|
||||
));
|
||||
self.send_sync_message(SyncMessage::RpcBlob {
|
||||
request_id: SyncRequestId::SingleBlob { id },
|
||||
sync_request_id: SyncRequestId::SingleBlob { id },
|
||||
peer_id,
|
||||
blob_sidecar,
|
||||
seen_timestamp: D,
|
||||
@@ -507,7 +507,7 @@ impl TestRig {
|
||||
blob_sidecar: Option<Arc<BlobSidecar<E>>>,
|
||||
) {
|
||||
self.send_sync_message(SyncMessage::RpcBlob {
|
||||
request_id: SyncRequestId::SingleBlob { id },
|
||||
sync_request_id: SyncRequestId::SingleBlob { id },
|
||||
peer_id,
|
||||
blob_sidecar,
|
||||
seen_timestamp: D,
|
||||
@@ -583,7 +583,7 @@ impl TestRig {
|
||||
fn parent_lookup_failed(&mut self, id: SingleLookupReqId, peer_id: PeerId, error: RPCError) {
|
||||
self.send_sync_message(SyncMessage::RpcError {
|
||||
peer_id,
|
||||
request_id: SyncRequestId::SingleBlock { id },
|
||||
sync_request_id: SyncRequestId::SingleBlock { id },
|
||||
error,
|
||||
})
|
||||
}
|
||||
@@ -602,7 +602,7 @@ impl TestRig {
|
||||
fn single_lookup_failed(&mut self, id: SingleLookupReqId, peer_id: PeerId, error: RPCError) {
|
||||
self.send_sync_message(SyncMessage::RpcError {
|
||||
peer_id,
|
||||
request_id: SyncRequestId::SingleBlock { id },
|
||||
sync_request_id: SyncRequestId::SingleBlock { id },
|
||||
error,
|
||||
})
|
||||
}
|
||||
@@ -614,11 +614,11 @@ impl TestRig {
|
||||
}
|
||||
}
|
||||
|
||||
fn return_empty_sampling_request(&mut self, (request_id, _): DCByRootId) {
|
||||
fn return_empty_sampling_request(&mut self, (sync_request_id, _): DCByRootId) {
|
||||
let peer_id = PeerId::random();
|
||||
// Send stream termination
|
||||
self.send_sync_message(SyncMessage::RpcDataColumn {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
data_column: None,
|
||||
seen_timestamp: timestamp_now(),
|
||||
@@ -631,10 +631,10 @@ impl TestRig {
|
||||
peer_id: PeerId,
|
||||
error: RPCError,
|
||||
) {
|
||||
for (request_id, _) in sampling_ids {
|
||||
for (sync_request_id, _) in sampling_ids {
|
||||
self.send_sync_message(SyncMessage::RpcError {
|
||||
peer_id,
|
||||
request_id,
|
||||
sync_request_id,
|
||||
error: error.clone(),
|
||||
})
|
||||
}
|
||||
@@ -760,14 +760,14 @@ impl TestRig {
|
||||
|
||||
fn complete_data_columns_by_root_request(
|
||||
&mut self,
|
||||
(request_id, _): DCByRootId,
|
||||
(sync_request_id, _): DCByRootId,
|
||||
data_columns: &[Arc<DataColumnSidecar<E>>],
|
||||
) {
|
||||
let peer_id = PeerId::random();
|
||||
for data_column in data_columns {
|
||||
// Send chunks
|
||||
self.send_sync_message(SyncMessage::RpcDataColumn {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
data_column: Some(data_column.clone()),
|
||||
seen_timestamp: timestamp_now(),
|
||||
@@ -775,7 +775,7 @@ impl TestRig {
|
||||
}
|
||||
// Send stream termination
|
||||
self.send_sync_message(SyncMessage::RpcDataColumn {
|
||||
request_id,
|
||||
sync_request_id,
|
||||
peer_id,
|
||||
data_column: None,
|
||||
seen_timestamp: timestamp_now(),
|
||||
@@ -785,17 +785,17 @@ impl TestRig {
|
||||
/// Return RPCErrors for all active requests of peer
|
||||
fn rpc_error_all_active_requests(&mut self, disconnected_peer_id: PeerId) {
|
||||
self.drain_network_rx();
|
||||
while let Ok(request_id) = self.pop_received_network_event(|ev| match ev {
|
||||
while let Ok(sync_request_id) = self.pop_received_network_event(|ev| match ev {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request_id: AppRequestId::Sync(id),
|
||||
app_request_id: AppRequestId::Sync(id),
|
||||
..
|
||||
} if *peer_id == disconnected_peer_id => Some(*id),
|
||||
_ => None,
|
||||
}) {
|
||||
self.send_sync_message(SyncMessage::RpcError {
|
||||
peer_id: disconnected_peer_id,
|
||||
request_id,
|
||||
sync_request_id,
|
||||
error: RPCError::Disconnected,
|
||||
});
|
||||
}
|
||||
@@ -879,7 +879,7 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id: _,
|
||||
request: RequestType::BlocksByRoot(request),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
} if request.block_roots().to_vec().contains(&for_block) => Some(*id),
|
||||
_ => None,
|
||||
})
|
||||
@@ -899,7 +899,7 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id: _,
|
||||
request: RequestType::BlobsByRoot(request),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
} if request
|
||||
.blob_ids
|
||||
.to_vec()
|
||||
@@ -924,7 +924,7 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id: _,
|
||||
request: RequestType::BlocksByRoot(request),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlock { id }),
|
||||
} if request.block_roots().to_vec().contains(&for_block) => Some(*id),
|
||||
_ => None,
|
||||
})
|
||||
@@ -946,7 +946,7 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id: _,
|
||||
request: RequestType::BlobsByRoot(request),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::SingleBlob { id }),
|
||||
} if request
|
||||
.blob_ids
|
||||
.to_vec()
|
||||
@@ -974,7 +974,8 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id: _,
|
||||
request: RequestType::DataColumnsByRoot(request),
|
||||
request_id: AppRequestId::Sync(id @ SyncRequestId::DataColumnsByRoot { .. }),
|
||||
app_request_id:
|
||||
AppRequestId::Sync(id @ SyncRequestId::DataColumnsByRoot { .. }),
|
||||
} if request
|
||||
.data_column_ids
|
||||
.to_vec()
|
||||
|
||||
@@ -223,7 +223,7 @@ impl TestRig {
|
||||
RequestType::BlocksByRange(OldBlocksByRangeRequest::V2(
|
||||
OldBlocksByRangeRequestV2 { start_slot, .. },
|
||||
)),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::BlocksByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::BlocksByRange(id)),
|
||||
} if filter_f(*peer_id, *start_slot) => Some((*id, *peer_id)),
|
||||
_ => None,
|
||||
})
|
||||
@@ -240,7 +240,7 @@ impl TestRig {
|
||||
RequestType::DataColumnsByRange(DataColumnsByRangeRequest {
|
||||
start_slot, ..
|
||||
}),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::DataColumnsByRange(id)),
|
||||
} if filter_f(*peer_id, *start_slot) => Some((*id, *peer_id)),
|
||||
_ => None,
|
||||
}) {
|
||||
@@ -256,7 +256,7 @@ impl TestRig {
|
||||
NetworkMessage::SendRequest {
|
||||
peer_id,
|
||||
request: RequestType::BlobsByRange(BlobsByRangeRequest { start_slot, .. }),
|
||||
request_id: AppRequestId::Sync(SyncRequestId::BlobsByRange(id)),
|
||||
app_request_id: AppRequestId::Sync(SyncRequestId::BlobsByRange(id)),
|
||||
} if filter_f(*peer_id, *start_slot) => Some((*id, *peer_id)),
|
||||
_ => None,
|
||||
})
|
||||
@@ -283,7 +283,7 @@ impl TestRig {
|
||||
"Completing BlocksByRange request {blocks_req_id:?} with empty stream"
|
||||
));
|
||||
self.send_sync_message(SyncMessage::RpcBlock {
|
||||
request_id: SyncRequestId::BlocksByRange(blocks_req_id),
|
||||
sync_request_id: SyncRequestId::BlocksByRange(blocks_req_id),
|
||||
peer_id: block_peer,
|
||||
beacon_block: None,
|
||||
seen_timestamp: D,
|
||||
@@ -297,7 +297,7 @@ impl TestRig {
|
||||
"Completing BlobsByRange request {id:?} with empty stream"
|
||||
));
|
||||
self.send_sync_message(SyncMessage::RpcBlob {
|
||||
request_id: SyncRequestId::BlobsByRange(id),
|
||||
sync_request_id: SyncRequestId::BlobsByRange(id),
|
||||
peer_id,
|
||||
blob_sidecar: None,
|
||||
seen_timestamp: D,
|
||||
@@ -310,7 +310,7 @@ impl TestRig {
|
||||
"Completing DataColumnsByRange request {id:?} with empty stream"
|
||||
));
|
||||
self.send_sync_message(SyncMessage::RpcDataColumn {
|
||||
request_id: SyncRequestId::DataColumnsByRange(id),
|
||||
sync_request_id: SyncRequestId::DataColumnsByRange(id),
|
||||
peer_id,
|
||||
data_column: None,
|
||||
seen_timestamp: D,
|
||||
|
||||
Reference in New Issue
Block a user