Skip to content

Commit

Permalink
refactor: Track the actual bytes written.
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Sep 4, 2023
1 parent 9835b69 commit ab54a35
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
18 changes: 5 additions & 13 deletions car-mirror/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,6 @@ pub async fn block_send(
Vec::new(),
);

writer
.write_header()
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?;

write_blocks_into_car(
&mut writer,
subgraph_roots,
Expand Down Expand Up @@ -310,14 +305,11 @@ async fn write_blocks_into_car<W: tokio::io::AsyncWrite + Unpin + Send>(
"writing block to CAR",
);

writer
block_bytes += writer
.write(cid, &block)
.await
.map_err(|e| Error::CarFileError(anyhow!(e)))?;

// TODO(matheus23): Count the actual bytes sent?
// At the moment, this is a rough estimate. iroh-car could be improved to return the written bytes.
block_bytes += block.len();
if block_bytes > send_minimum {
break;
}
Expand All @@ -333,7 +325,7 @@ async fn read_and_verify_blocks<R: tokio::io::AsyncRead + Unpin>(
store: &impl BlockStore,
cache: &impl Cache,
) -> Result<(), Error> {
let mut block_bytes = 0;
let mut bytes_read = 0;
while let Some((cid, vec)) = reader
.next_block()
.await
Expand All @@ -347,10 +339,10 @@ async fn read_and_verify_blocks<R: tokio::io::AsyncRead + Unpin>(
"reading block from CAR",
);

block_bytes += block.len();
if block_bytes > receive_maximum {
bytes_read += block.len();
if bytes_read > receive_maximum {
return Err(Error::TooManyBytes {
block_bytes,
bytes_read,
receive_maximum,
});
}
Expand Down
4 changes: 2 additions & 2 deletions car-mirror/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use crate::incremental_verification::BlockState;
pub enum Error {
/// An error raised during receival of blocks, when more than the configured maximum
/// bytes are received in a single batch. See the `Config` type.
#[error("Received more than {receive_maximum} bytes ({block_bytes}), aborting request.")]
#[error("Expected to receive no more than {receive_maximum} bytes, but got at least {bytes_read}, aborting request.")]
TooManyBytes {
/// The configured amount of maximum bytes to receive
receive_maximum: usize,
/// The actual amount of bytes received so far
block_bytes: usize,
bytes_read: usize,
},

/// This library only supports a subset of default codecs, including DAG-CBOR, DAG-JSON, DAG-PB and more.g
Expand Down

0 comments on commit ab54a35

Please sign in to comment.