Skip to content

Commit cdcf4d8

Browse files
committed
fix networking bug
1 parent 4095fd1 commit cdcf4d8

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

lib/net/peer.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ pub enum ConnectionError {
7979
SendInfo,
8080
#[error("state error")]
8181
State(#[from] state::Error),
82-
#[error("write error")]
83-
Write(#[from] quinn::WriteError),
82+
#[error("write error ({stream_id})")]
83+
Write {
84+
stream_id: quinn::StreamId,
85+
source: quinn::WriteError,
86+
},
8487
}
8588

8689
impl From<mpsc::TrySendError<Info>> for ConnectionError {
@@ -293,7 +296,7 @@ impl Connection {
293296
Request::GetHeaders {
294297
height: Some(height),
295298
..
296-
} => *height as usize * Self::READ_HEADER_LIMIT,
299+
} => (*height as usize + 1) * Self::READ_HEADER_LIMIT,
297300
// Should have no response, so limit zero
298301
Request::Heartbeat(_) => 0,
299302
Request::PushTransaction { .. } => Self::READ_TX_ACK_LIMIT,
@@ -320,9 +323,15 @@ impl Connection {
320323
&self,
321324
) -> Result<(Request, SendStream), ConnectionError> {
322325
let (tx, mut rx) = self.0.accept_bi().await?;
326+
tracing::trace!(recv_id = %rx.id(), "Receiving request");
323327
let request_bytes =
324328
rx.read_to_end(Connection::READ_REQUEST_LIMIT).await?;
325329
let request: Request = bincode::deserialize(&request_bytes)?;
330+
tracing::trace!(
331+
recv_id = %rx.id(),
332+
?request,
333+
"Received request"
334+
);
326335
Ok((request, tx))
327336
}
328337

@@ -332,12 +341,28 @@ impl Connection {
332341
) -> Result<Option<Response>, ConnectionError> {
333342
let read_response_limit = Self::read_response_limit(message);
334343
let (mut send, mut recv) = self.0.open_bi().await?;
344+
tracing::trace!(
345+
request = ?message,
346+
send_id = %send.id(),
347+
"Sending request"
348+
);
335349
let message = bincode::serialize(message)?;
336-
send.write_all(&message).await?;
350+
send.write_all(&message).await.map_err(|err| {
351+
ConnectionError::Write {
352+
stream_id: send.id(),
353+
source: err,
354+
}
355+
})?;
337356
send.finish()?;
338357
if read_response_limit > 0 {
358+
tracing::trace!(recv_id = %recv.id(), "Receiving response");
339359
let response_bytes = recv.read_to_end(read_response_limit).await?;
340360
let response: Response = bincode::deserialize(&response_bytes)?;
361+
tracing::trace!(
362+
recv_id = %recv.id(),
363+
?response,
364+
"Received response"
365+
);
341366
Ok(Some(response))
342367
} else {
343368
Ok(None)
@@ -385,11 +410,18 @@ impl ConnectionTask {
385410
mut response_tx: SendStream,
386411
response: Response,
387412
) -> Result<(), ConnectionError> {
413+
tracing::trace!(
414+
?response,
415+
send_id = %response_tx.id(),
416+
"Sending response"
417+
);
388418
let response_bytes = bincode::serialize(&response)?;
389-
response_tx
390-
.write_all(&response_bytes)
391-
.await
392-
.map_err(ConnectionError::from)
419+
response_tx.write_all(&response_bytes).await.map_err(|err| {
420+
ConnectionError::Write {
421+
stream_id: response_tx.id(),
422+
source: err,
423+
}
424+
})
393425
}
394426

395427
/// Check if peer tip is better, requesting headers if necessary.

0 commit comments

Comments
 (0)