Skip to content

Commit b4ffabd

Browse files
committed
fix: split req-resp behaviour
libp2p::request_response expects protocols to be equivalent between them, and negotiates with the peer which one to use. Since we specified the status first in the list of protocols, it seems to default to that one when we send a request.
1 parent 27263e5 commit b4ffabd

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

crates/net/p2p/src/lib.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,26 @@ pub async fn start_p2p(
7373
let gossipsub = libp2p::gossipsub::Behaviour::new(MessageAuthenticity::Anonymous, config)
7474
.expect("failed to initiate behaviour");
7575

76-
let req_resp = request_response::Behaviour::new(
77-
vec![
78-
(
79-
StreamProtocol::new(STATUS_PROTOCOL_V1),
80-
request_response::ProtocolSupport::Full,
81-
),
82-
(
83-
StreamProtocol::new(BLOCKS_BY_ROOT_PROTOCOL_V1),
84-
request_response::ProtocolSupport::Full,
85-
),
86-
],
76+
let status_req_resp = request_response::Behaviour::new(
77+
vec![(
78+
StreamProtocol::new(STATUS_PROTOCOL_V1),
79+
request_response::ProtocolSupport::Full,
80+
)],
81+
Default::default(),
82+
);
83+
84+
let blocks_by_root_req_resp = request_response::Behaviour::new(
85+
vec![(
86+
StreamProtocol::new(BLOCKS_BY_ROOT_PROTOCOL_V1),
87+
request_response::ProtocolSupport::Full,
88+
)],
8789
Default::default(),
8890
);
8991

9092
let behavior = Behaviour {
9193
gossipsub,
92-
req_resp,
94+
status_req_resp,
95+
blocks_by_root_req_resp,
9396
};
9497

9598
// TODO: set peer scoring params
@@ -166,7 +169,8 @@ pub async fn start_p2p(
166169
#[derive(NetworkBehaviour)]
167170
pub(crate) struct Behaviour {
168171
gossipsub: libp2p::gossipsub::Behaviour,
169-
req_resp: request_response::Behaviour<Codec>,
172+
status_req_resp: request_response::Behaviour<Codec>,
173+
blocks_by_root_req_resp: request_response::Behaviour<req_resp::Codec>,
170174
}
171175

172176
pub(crate) struct P2PServer {
@@ -204,7 +208,10 @@ async fn event_loop(mut server: P2PServer) {
204208

205209
async fn handle_swarm_event(server: &mut P2PServer, event: SwarmEvent<BehaviourEvent>) {
206210
match event {
207-
SwarmEvent::Behaviour(BehaviourEvent::ReqResp(req_resp_event)) => {
211+
SwarmEvent::Behaviour(BehaviourEvent::StatusReqResp(req_resp_event)) => {
212+
req_resp::handle_req_resp_message(server, req_resp_event).await;
213+
}
214+
SwarmEvent::Behaviour(BehaviourEvent::BlocksByRootReqResp(req_resp_event)) => {
208215
req_resp::handle_req_resp_message(server, req_resp_event).await;
209216
}
210217
SwarmEvent::Behaviour(BehaviourEvent::Gossipsub(
@@ -228,7 +235,7 @@ async fn handle_swarm_event(server: &mut P2PServer, event: SwarmEvent<BehaviourE
228235
server
229236
.swarm
230237
.behaviour_mut()
231-
.req_resp
238+
.status_req_resp
232239
.send_request(&peer_id, Request::Status(our_status));
233240
} else {
234241
info!(%peer_id, %direction, "Added peer connection");

crates/net/p2p/src/req_resp/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async fn handle_status_request(
6868
server
6969
.swarm
7070
.behaviour_mut()
71-
.req_resp
71+
.status_req_resp
7272
.send_response(
7373
channel,
7474
Response::new(ResponseResult::Success, ResponsePayload::Status(our_status)),
@@ -153,6 +153,6 @@ pub async fn fetch_block_from_peer(
153153
server
154154
.swarm
155155
.behaviour_mut()
156-
.req_resp
156+
.blocks_by_root_req_resp
157157
.send_request(&peer, Request::BlocksByRoot(request));
158158
}

0 commit comments

Comments
 (0)