diff --git a/crates/net/hlfs/src/lib.rs b/crates/net/hlfs/src/lib.rs index 45155180f..02ad42462 100644 --- a/crates/net/hlfs/src/lib.rs +++ b/crates/net/hlfs/src/lib.rs @@ -91,7 +91,12 @@ impl Client { pub fn new(root: impl Into, peers: Vec) -> Self { let root: PathBuf = root.into(); let n = find_max_number_file(&root).unwrap(); - Self { root, peers: Arc::new(Mutex::new(peers)), timeout: Duration::from_secs(3), max_block: n } + Self { + root, + peers: Arc::new(Mutex::new(peers)), + timeout: Duration::from_secs(3), + max_block: n, + } } pub fn update_peers(&self, peers: Vec) { *self.peers.lock() = peers; @@ -187,7 +192,7 @@ fn find_max_number_file(root: &Path) -> Result { Ok(()) } - let mut best = None; + let mut best = Some(0); walk(root, &mut best)?; Ok(best.expect("cannot find block files")) } @@ -253,11 +258,19 @@ async fn handle_conn( ) -> Result<(), HlfsError> { let mut op = [0u8; 1]; sock.read_exact(&mut op).await?; - if op[0] != OP_REQ_BLOCK { + if op[0] != OP_REQ_BLOCK && op[0] != OP_REQ_MAX_BLOCK { warn!(%addr, "hlfs: bad op"); return Err(HlfsError::Proto); } + if op[0] == OP_REQ_MAX_BLOCK { + let mut b = BytesMut::with_capacity(1 + 8); + b.put_u8(OP_RES_MAX_BLOCK); + put_u64(&mut b, max_block); + let _ = sock.write_all(&b).await; + return Ok(()); + } + let mut num = [0u8; 8]; sock.read_exact(&mut num).await?; let number = u64::from_le_bytes(num);