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

@@ -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()

View File

@@ -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,