mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-06 10:11:44 +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:
@@ -98,7 +98,7 @@ fn test_tcp_status_rpc() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: AppRequestId::Router,
|
||||
app_request_id: AppRequestId::Router,
|
||||
response,
|
||||
} => {
|
||||
// Should receive the RPC response
|
||||
@@ -118,13 +118,17 @@ fn test_tcp_status_rpc() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
debug!("Receiver Received");
|
||||
receiver.send_response(peer_id, id, request.id, rpc_response.clone());
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
inbound_request_id,
|
||||
rpc_response.clone(),
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other events
|
||||
@@ -204,7 +208,7 @@ fn test_tcp_blocks_by_range_chunked_rpc() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: _,
|
||||
app_request_id: _,
|
||||
response,
|
||||
} => {
|
||||
warn!("Sender received a response");
|
||||
@@ -240,10 +244,10 @@ fn test_tcp_blocks_by_range_chunked_rpc() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
for i in 0..messages_to_send {
|
||||
@@ -258,16 +262,14 @@ fn test_tcp_blocks_by_range_chunked_rpc() {
|
||||
};
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
rpc_response.clone(),
|
||||
);
|
||||
}
|
||||
// send the stream termination
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
Response::BlocksByRange(None),
|
||||
);
|
||||
}
|
||||
@@ -338,7 +340,7 @@ fn test_blobs_by_range_chunked_rpc() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: _,
|
||||
app_request_id: _,
|
||||
response,
|
||||
} => {
|
||||
warn!("Sender received a response");
|
||||
@@ -368,10 +370,10 @@ fn test_blobs_by_range_chunked_rpc() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
for _ in 0..messages_to_send {
|
||||
@@ -379,16 +381,14 @@ fn test_blobs_by_range_chunked_rpc() {
|
||||
// second as altair and third as bellatrix.
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
rpc_response.clone(),
|
||||
);
|
||||
}
|
||||
// send the stream termination
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
Response::BlobsByRange(None),
|
||||
);
|
||||
}
|
||||
@@ -459,8 +459,8 @@ fn test_tcp_blocks_by_range_over_limit() {
|
||||
.unwrap();
|
||||
}
|
||||
// The request will fail because the sender will refuse to send anything > MAX_RPC_SIZE
|
||||
NetworkEvent::RPCFailed { id, .. } => {
|
||||
assert!(matches!(id, AppRequestId::Router));
|
||||
NetworkEvent::RPCFailed { app_request_id, .. } => {
|
||||
assert!(matches!(app_request_id, AppRequestId::Router));
|
||||
return;
|
||||
}
|
||||
_ => {} // Ignore other behaviour events
|
||||
@@ -474,26 +474,24 @@ fn test_tcp_blocks_by_range_over_limit() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
for _ in 0..messages_to_send {
|
||||
let rpc_response = rpc_response_bellatrix_large.clone();
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
rpc_response.clone(),
|
||||
);
|
||||
}
|
||||
// send the stream termination
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
Response::BlocksByRange(None),
|
||||
);
|
||||
}
|
||||
@@ -566,7 +564,7 @@ fn test_tcp_blocks_by_range_chunked_rpc_terminates_correctly() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: _,
|
||||
app_request_id: _,
|
||||
response,
|
||||
} =>
|
||||
// Should receive the RPC response
|
||||
@@ -608,15 +606,15 @@ fn test_tcp_blocks_by_range_chunked_rpc_terminates_correctly() {
|
||||
futures::future::Either::Left((
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
},
|
||||
_,
|
||||
)) => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
message_info = Some((peer_id, id, request.id));
|
||||
message_info = Some((peer_id, inbound_request_id));
|
||||
}
|
||||
}
|
||||
futures::future::Either::Right((_, _)) => {} // The timeout hit, send messages if required
|
||||
@@ -626,8 +624,8 @@ fn test_tcp_blocks_by_range_chunked_rpc_terminates_correctly() {
|
||||
// if we need to send messages send them here. This will happen after a delay
|
||||
if message_info.is_some() {
|
||||
messages_sent += 1;
|
||||
let (peer_id, stream_id, request_id) = message_info.as_ref().unwrap();
|
||||
receiver.send_response(*peer_id, *stream_id, *request_id, rpc_response.clone());
|
||||
let (peer_id, inbound_request_id) = message_info.as_ref().unwrap();
|
||||
receiver.send_response(*peer_id, *inbound_request_id, rpc_response.clone());
|
||||
debug!("Sending message {}", messages_sent);
|
||||
if messages_sent == messages_to_send + extra_messages_to_send {
|
||||
// stop sending messages
|
||||
@@ -700,7 +698,7 @@ fn test_tcp_blocks_by_range_single_empty_rpc() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: AppRequestId::Router,
|
||||
app_request_id: AppRequestId::Router,
|
||||
response,
|
||||
} => match response {
|
||||
Response::BlocksByRange(Some(_)) => {
|
||||
@@ -727,26 +725,24 @@ fn test_tcp_blocks_by_range_single_empty_rpc() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
|
||||
for _ in 1..=messages_to_send {
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
rpc_response.clone(),
|
||||
);
|
||||
}
|
||||
// send the stream termination
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
Response::BlocksByRange(None),
|
||||
);
|
||||
}
|
||||
@@ -837,7 +833,7 @@ fn test_tcp_blocks_by_root_chunked_rpc() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: AppRequestId::Router,
|
||||
app_request_id: AppRequestId::Router,
|
||||
response,
|
||||
} => match response {
|
||||
Response::BlocksByRoot(Some(_)) => {
|
||||
@@ -870,10 +866,10 @@ fn test_tcp_blocks_by_root_chunked_rpc() {
|
||||
match receiver.next_event().await {
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
} => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
debug!("Receiver got request");
|
||||
|
||||
@@ -886,14 +882,13 @@ fn test_tcp_blocks_by_root_chunked_rpc() {
|
||||
} else {
|
||||
rpc_response_bellatrix_small.clone()
|
||||
};
|
||||
receiver.send_response(peer_id, id, request.id, rpc_response);
|
||||
receiver.send_response(peer_id, inbound_request_id, rpc_response);
|
||||
debug!("Sending message");
|
||||
}
|
||||
// send the stream termination
|
||||
receiver.send_response(
|
||||
peer_id,
|
||||
id,
|
||||
request.id,
|
||||
inbound_request_id,
|
||||
Response::BlocksByRange(None),
|
||||
);
|
||||
debug!("Send stream term");
|
||||
@@ -977,7 +972,7 @@ fn test_tcp_blocks_by_root_chunked_rpc_terminates_correctly() {
|
||||
}
|
||||
NetworkEvent::ResponseReceived {
|
||||
peer_id: _,
|
||||
id: AppRequestId::Router,
|
||||
app_request_id: AppRequestId::Router,
|
||||
response,
|
||||
} => {
|
||||
debug!("Sender received a response");
|
||||
@@ -1019,15 +1014,15 @@ fn test_tcp_blocks_by_root_chunked_rpc_terminates_correctly() {
|
||||
futures::future::Either::Left((
|
||||
NetworkEvent::RequestReceived {
|
||||
peer_id,
|
||||
id,
|
||||
request,
|
||||
inbound_request_id,
|
||||
request_type,
|
||||
},
|
||||
_,
|
||||
)) => {
|
||||
if request.r#type == rpc_request {
|
||||
if request_type == rpc_request {
|
||||
// send the response
|
||||
warn!("Receiver got request");
|
||||
message_info = Some((peer_id, id, request.id));
|
||||
message_info = Some((peer_id, inbound_request_id));
|
||||
}
|
||||
}
|
||||
futures::future::Either::Right((_, _)) => {} // The timeout hit, send messages if required
|
||||
@@ -1037,8 +1032,8 @@ fn test_tcp_blocks_by_root_chunked_rpc_terminates_correctly() {
|
||||
// if we need to send messages send them here. This will happen after a delay
|
||||
if message_info.is_some() {
|
||||
messages_sent += 1;
|
||||
let (peer_id, stream_id, request_id) = message_info.as_ref().unwrap();
|
||||
receiver.send_response(*peer_id, *stream_id, *request_id, rpc_response.clone());
|
||||
let (peer_id, inbound_request_id) = message_info.as_ref().unwrap();
|
||||
receiver.send_response(*peer_id, *inbound_request_id, rpc_response.clone());
|
||||
debug!("Sending message {}", messages_sent);
|
||||
if messages_sent == messages_to_send + extra_messages_to_send {
|
||||
// stop sending messages
|
||||
|
||||
Reference in New Issue
Block a user