1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
//! Async (tokio) io, written in fsm style
//!
//! IO ops are written as async state machines that thread the state through the
//! futures to avoid being encumbered by lifetimes.
//!
//! This makes them occasionally a bit verbose to use, but allows being generic
//! without having to box the futures.
use std::{io, result};
use crate::{
blake3, hash_subtree,
iter::ResponseIter,
rec::{encode_selected_rec, truncate_ranges, truncate_ranges_owned},
ChunkRanges, ChunkRangesRef,
};
use blake3::guts::parent_cv;
use bytes::{Bytes, BytesMut};
use futures::{future::LocalBoxFuture, Future, FutureExt};
use iroh_io::AsyncStreamWriter;
use smallvec::SmallVec;
use tokio::io::{AsyncRead, AsyncReadExt};
use crate::{
io::{
error::EncodeError,
outboard::{PostOrderOutboard, PreOrderOutboard},
Leaf, Parent,
},
iter::BaoChunk,
BaoTree, BlockSize, ByteNum, TreeNode,
};
pub use iroh_io::{AsyncSliceReader, AsyncSliceWriter};
use super::{DecodeError, StartDecodeError};
/// An item of bao content
///
/// We know that we are not going to get headers after the first item.
#[derive(Debug)]
pub enum BaoContentItem {
/// a parent node, to update the outboard
Parent(Parent),
/// a leaf node, to write to the file
Leaf(Leaf),
}
impl From<Parent> for BaoContentItem {
fn from(p: Parent) -> Self {
Self::Parent(p)
}
}
impl From<Leaf> for BaoContentItem {
fn from(l: Leaf) -> Self {
Self::Leaf(l)
}
}
/// A binary merkle tree for blake3 hashes of a blob.
///
/// This trait contains information about the geometry of the tree, the root hash,
/// and a method to load the hashes at a given node.
///
/// It is up to the implementor to decide how to store the hashes.
///
/// In the original bao crate, the hashes are stored in a file in pre order.
/// This is implemented for a generic io object in [super::outboard::PreOrderOutboard]
/// and for a memory region in [super::outboard::PreOrderMemOutboard].
///
/// For files that grow over time, it is more efficient to store the hashes in post order.
/// This is implemented for a generic io object in [super::outboard::PostOrderOutboard]
/// and for a memory region in [super::outboard::PostOrderMemOutboard].
///
/// If you use a different storage engine, you can implement this trait for it. E.g.
/// you could store the hashes in a database and use the node number as the key.
///
/// The async version takes a mutable reference to load, not because it mutates
/// the outboard (it doesn't), but to ensure that there is at most one outstanding
/// load at a time.
///
/// Dropping the load future without polling it to completion is safe, but will
/// possibly render the outboard unusable.
pub trait Outboard {
/// The root hash
fn root(&self) -> blake3::Hash;
/// The tree. This contains the information about the size of the file and the block size.
fn tree(&self) -> BaoTree;
/// load the hash pair for a node
///
/// This takes a &mut self not because it mutates the outboard (it doesn't),
/// but to ensure that there is only one outstanding load at a time.
fn load(
&mut self,
node: TreeNode,
) -> impl Future<Output = io::Result<Option<(blake3::Hash, blake3::Hash)>>>;
}
/// A mutable outboard.
///
/// This trait extends [Outboard] with methods to save a hash pair for a node and to set the
/// length of the data file.
///
/// This trait can be used to incrementally save an outboard when receiving data.
/// If you want to just ignore outboard data, there is a special placeholder outboard
/// implementation [super::outboard::EmptyOutboard].
pub trait OutboardMut: Sized {
/// Save a hash pair for a node
fn save(
&mut self,
node: TreeNode,
hash_pair: &(blake3::Hash, blake3::Hash),
) -> impl Future<Output = io::Result<()>>;
/// sync to disk
fn sync(&mut self) -> impl Future<Output = io::Result<()>>;
}
impl<'b, O: Outboard> Outboard for &'b mut O {
fn root(&self) -> blake3::Hash {
(**self).root()
}
fn tree(&self) -> BaoTree {
(**self).tree()
}
async fn load(&mut self, node: TreeNode) -> io::Result<Option<(blake3::Hash, blake3::Hash)>> {
(**self).load(node).await
}
}
impl<R: AsyncSliceReader> Outboard for PreOrderOutboard<R> {
fn root(&self) -> blake3::Hash {
self.root
}
fn tree(&self) -> BaoTree {
self.tree
}
async fn load(&mut self, node: TreeNode) -> io::Result<Option<(blake3::Hash, blake3::Hash)>> {
let Some(offset) = self.tree.pre_order_offset(node) else {
return Ok(None);
};
let offset = offset * 64 + 8;
let content = self.data.read_at(offset, 64).await?;
Ok(Some(if content.len() != 64 {
(blake3::Hash::from([0; 32]), blake3::Hash::from([0; 32]))
} else {
parse_hash_pair(content)?
}))
}
}
impl<'b, O: OutboardMut> OutboardMut for &'b mut O {
async fn save(
&mut self,
node: TreeNode,
hash_pair: &(blake3::Hash, blake3::Hash),
) -> io::Result<()> {
(**self).save(node, hash_pair).await
}
async fn sync(&mut self) -> io::Result<()> {
(**self).sync().await
}
}
impl<W: AsyncSliceWriter> OutboardMut for PreOrderOutboard<W> {
async fn save(
&mut self,
node: TreeNode,
hash_pair: &(blake3::Hash, blake3::Hash),
) -> io::Result<()> {
let Some(offset) = self.tree.pre_order_offset(node) else {
return Ok(());
};
let offset = offset * 64 + 8;
let mut buf = [0u8; 64];
buf[..32].copy_from_slice(hash_pair.0.as_bytes());
buf[32..].copy_from_slice(hash_pair.1.as_bytes());
self.data.write_at(offset, &buf).await?;
Ok(())
}
async fn sync(&mut self) -> io::Result<()> {
self.data.sync().await
}
}
impl<R: AsyncSliceReader> Outboard for PostOrderOutboard<R> {
fn root(&self) -> blake3::Hash {
self.root
}
fn tree(&self) -> BaoTree {
self.tree
}
async fn load(&mut self, node: TreeNode) -> io::Result<Option<(blake3::Hash, blake3::Hash)>> {
let Some(offset) = self.tree.post_order_offset(node) else {
return Ok(None);
};
let offset = offset.value() * 64;
let content = self.data.read_at(offset, 64).await?;
Ok(Some(if content.len() != 64 {
(blake3::Hash::from([0; 32]), blake3::Hash::from([0; 32]))
} else {
parse_hash_pair(content)?
}))
}
}
pub(crate) fn parse_hash_pair(buf: Bytes) -> io::Result<(blake3::Hash, blake3::Hash)> {
if buf.len() != 64 {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"hash pair must be 64 bytes",
));
}
let l_hash = blake3::Hash::from(<[u8; 32]>::try_from(&buf[..32]).unwrap());
let r_hash = blake3::Hash::from(<[u8; 32]>::try_from(&buf[32..]).unwrap());
Ok((l_hash, r_hash))
}
pub(crate) fn combine_hash_pair(l: &blake3::Hash, r: &blake3::Hash) -> [u8; 64] {
let mut res = [0u8; 64];
let lb: &mut [u8; 32] = (&mut res[0..32]).try_into().unwrap();
*lb = *l.as_bytes();
let rb: &mut [u8; 32] = (&mut res[32..]).try_into().unwrap();
*rb = *r.as_bytes();
res
}
/// Response decoder state machine, at the start of a stream
#[derive(Debug)]
pub struct ResponseDecoderStart<R> {
ranges: ChunkRanges,
block_size: BlockSize,
hash: blake3::Hash,
encoded: R,
}
impl<'a, R: AsyncRead + Unpin> ResponseDecoderStart<R> {
/// Create a new response decoder state machine, at the start of a stream
/// where you don't yet know the size.
pub fn new(hash: blake3::Hash, ranges: ChunkRanges, block_size: BlockSize, encoded: R) -> Self {
Self {
ranges,
block_size,
hash,
encoded,
}
}
/// Immediately finish decoding the stream, returning the underlying reader
pub fn finish(self) -> R {
self.encoded
}
/// Read the size and go into the next state
///
/// The only thing that can go wrong here is an io error when reading the size.
pub async fn next(
self,
) -> std::result::Result<(ResponseDecoderReading<R>, u64), StartDecodeError> {
let Self {
ranges,
block_size,
hash,
mut encoded,
} = self;
let size = ByteNum(
encoded
.read_u64_le()
.await
.map_err(StartDecodeError::maybe_not_found)?,
);
let tree = BaoTree::new(size, block_size);
let state = ResponseDecoderReading(Box::new(ResponseDecoderReadingInner::new(
tree, hash, ranges, encoded,
)));
Ok((state, size.0))
}
/// Hash of the blob we are currently getting
pub fn hash(&self) -> &blake3::Hash {
&self.hash
}
/// The ranges we requested
pub fn ranges(&self) -> &ChunkRanges {
&self.ranges
}
}
#[derive(Debug)]
struct ResponseDecoderReadingInner<R> {
iter: ResponseIter,
stack: SmallVec<[blake3::Hash; 10]>,
encoded: R,
buf: BytesMut,
}
impl<R> ResponseDecoderReadingInner<R> {
fn new(tree: BaoTree, hash: blake3::Hash, ranges: ChunkRanges, encoded: R) -> Self {
// now that we know the size, we can canonicalize the ranges
let ranges = truncate_ranges_owned(ranges, tree.size());
let mut res = Self {
iter: ResponseIter::new(tree, ranges),
stack: SmallVec::new(),
encoded,
buf: BytesMut::with_capacity(tree.chunk_group_bytes().to_usize()),
};
res.stack.push(hash);
res
}
}
/// Response decoder state machine, after reading the size
#[derive(Debug)]
pub struct ResponseDecoderReading<R>(Box<ResponseDecoderReadingInner<R>>);
/// Next type for ResponseDecoderReading.
#[derive(Debug)]
pub enum ResponseDecoderReadingNext<R> {
/// One more item, and you get back the state machine in the next state
More(
(
ResponseDecoderReading<R>,
std::result::Result<BaoContentItem, DecodeError>,
),
),
/// The stream is done, you get back the underlying reader
Done(R),
}
impl<R: AsyncRead + Unpin> ResponseDecoderReading<R> {
/// Create a new response decoder state machine, when you have already read the size.
///
/// The size as well as the chunk size is given in the `tree` parameter.
pub fn new(hash: blake3::Hash, ranges: ChunkRanges, tree: BaoTree, encoded: R) -> Self {
let mut stack = SmallVec::new();
stack.push(hash);
Self(Box::new(ResponseDecoderReadingInner {
iter: ResponseIter::new(tree, ranges),
stack,
encoded,
buf: BytesMut::new(),
}))
}
/// Proceed to the next state by reading the next chunk from the stream.
pub async fn next(mut self) -> ResponseDecoderReadingNext<R> {
if let Some(chunk) = self.0.iter.next() {
let item = self.next0(chunk).await;
ResponseDecoderReadingNext::More((self, item))
} else {
ResponseDecoderReadingNext::Done(self.0.encoded)
}
}
/// Immediately return the underlying reader
pub fn finish(self) -> R {
self.0.encoded
}
/// The tree geometry
pub fn tree(&self) -> BaoTree {
self.0.iter.tree()
}
/// Hash of the blob we are currently getting
pub fn hash(&self) -> &blake3::Hash {
&self.0.stack[0]
}
async fn next0(&mut self, chunk: BaoChunk) -> std::result::Result<BaoContentItem, DecodeError> {
Ok(match chunk {
BaoChunk::Parent {
is_root,
right,
left,
node,
..
} => {
let mut buf = [0u8; 64];
let this = &mut self.0;
this.encoded
.read_exact(&mut buf)
.await
.map_err(|e| DecodeError::maybe_parent_not_found(e, node))?;
let pair @ (l_hash, r_hash) = read_parent(&buf);
let parent_hash = this.stack.pop().unwrap();
let actual = parent_cv(&l_hash, &r_hash, is_root);
// Push the children in reverse order so they are popped in the correct order
// only push right if the range intersects with the right child
if right {
this.stack.push(r_hash);
}
// only push left if the range intersects with the left child
if left {
this.stack.push(l_hash);
}
// Validate after pushing the children so that we could in principle continue
if parent_hash != actual {
return Err(DecodeError::ParentHashMismatch(node));
}
Parent { pair, node }.into()
}
BaoChunk::Leaf {
size,
is_root,
start_chunk,
..
} => {
// this will resize always to chunk group size, except for the last chunk
let this = &mut self.0;
this.buf.resize(size, 0u8);
this.encoded
.read_exact(&mut this.buf)
.await
.map_err(|e| DecodeError::maybe_leaf_not_found(e, start_chunk))?;
let leaf_hash = this.stack.pop().unwrap();
let actual = hash_subtree(start_chunk.0, &this.buf, is_root);
if leaf_hash != actual {
return Err(DecodeError::LeafHashMismatch(start_chunk));
}
Leaf {
offset: start_chunk.to_bytes(),
data: self.0.buf.split().freeze(),
}
.into()
}
})
}
}
/// Encode ranges relevant to a query from a reader and outboard to a writer
///
/// This will not validate on writing, so data corruption will be detected on reading
///
/// It is possible to encode ranges from a partial file and outboard.
/// This will either succeed if the requested ranges are all present, or fail
/// as soon as a range is missing.
pub async fn encode_ranges<D, O, W>(
mut data: D,
mut outboard: O,
ranges: &ChunkRangesRef,
encoded: W,
) -> result::Result<(), EncodeError>
where
D: AsyncSliceReader,
O: Outboard,
W: AsyncStreamWriter,
{
let mut encoded = encoded;
let tree = outboard.tree();
// write header
encoded.write(tree.size.0.to_le_bytes().as_slice()).await?;
for item in tree.ranges_pre_order_chunks_iter_ref(ranges, 0) {
match item {
BaoChunk::Parent { node, .. } => {
let (l_hash, r_hash) = outboard.load(node).await?.unwrap();
let pair = combine_hash_pair(&l_hash, &r_hash);
encoded
.write(&pair)
.await
.map_err(|e| EncodeError::maybe_parent_write(e, node))?;
}
BaoChunk::Leaf {
start_chunk, size, ..
} => {
let start = start_chunk.to_bytes();
let bytes = data.read_at(start.0, size).await?;
encoded
.write(&bytes)
.await
.map_err(|e| EncodeError::maybe_leaf_write(e, start_chunk))?;
}
}
}
Ok(())
}
/// Encode ranges relevant to a query from a reader and outboard to a writer
///
/// This function validates the data before writing
///
/// It is possible to encode ranges from a partial file and outboard.
/// This will either succeed if the requested ranges are all present, or fail
/// as soon as a range is missing.
pub async fn encode_ranges_validated<D, O, W>(
mut data: D,
mut outboard: O,
ranges: &ChunkRangesRef,
encoded: W,
) -> result::Result<(), EncodeError>
where
D: AsyncSliceReader,
O: Outboard,
W: AsyncStreamWriter,
{
// buffer for writing incomplete subtrees.
// for queries that don't have incomplete subtrees, this will never be used.
let mut out_buf = Vec::new();
let mut stack = SmallVec::<[blake3::Hash; 10]>::new();
stack.push(outboard.root());
let mut encoded = encoded;
let tree = outboard.tree();
let ranges = truncate_ranges(ranges, tree.size());
// write header
encoded.write(tree.size.0.to_le_bytes().as_slice()).await?;
for item in tree.ranges_pre_order_chunks_iter_ref(ranges, 0) {
match item {
BaoChunk::Parent {
is_root,
left,
right,
node,
..
} => {
let (l_hash, r_hash) = outboard.load(node).await?.unwrap();
let actual = parent_cv(&l_hash, &r_hash, is_root);
let expected = stack.pop().unwrap();
if actual != expected {
return Err(EncodeError::ParentHashMismatch(node));
}
if right {
stack.push(r_hash);
}
if left {
stack.push(l_hash);
}
let pair = combine_hash_pair(&l_hash, &r_hash);
encoded
.write(&pair)
.await
.map_err(|e| EncodeError::maybe_parent_write(e, node))?;
}
BaoChunk::Leaf {
start_chunk,
size,
is_root,
ranges,
..
} => {
let expected = stack.pop().unwrap();
let start = start_chunk.to_bytes();
let bytes = data.read_at(start.0, size).await?;
let (actual, to_write) = if !ranges.is_all() {
// we need to encode just a part of the data
//
// write into an out buffer to ensure we detect mismatches
// before writing to the output.
out_buf.clear();
let actual = encode_selected_rec(
start_chunk,
&bytes,
is_root,
ranges,
tree.block_size.to_u32(),
true,
&mut out_buf,
);
(actual, &out_buf[..])
} else {
let actual = hash_subtree(start_chunk.0, &bytes, is_root);
(actual, &bytes[..])
};
if actual != expected {
return Err(EncodeError::LeafHashMismatch(start_chunk));
}
encoded
.write(to_write)
.await
.map_err(|e| EncodeError::maybe_leaf_write(e, start_chunk))?;
}
}
}
Ok(())
}
/// Decode a response into a file while updating an outboard.
///
/// If you do not want to update an outboard, use [super::outboard::EmptyOutboard] as
/// the outboard.
pub async fn decode_response_into<R, O, W, F, Fut>(
root: blake3::Hash,
block_size: BlockSize,
ranges: ChunkRanges,
encoded: R,
create: F,
mut target: W,
) -> io::Result<Option<O>>
where
O: OutboardMut,
R: AsyncRead + Unpin,
W: AsyncSliceWriter,
F: FnOnce(blake3::Hash, BaoTree) -> Fut,
Fut: Future<Output = io::Result<O>>,
{
let start = ResponseDecoderStart::new(root, ranges, block_size, encoded);
let (mut reading, _size) = start.next().await?;
let mut outboard = None;
let mut create = Some(create);
loop {
let item = match reading.next().await {
ResponseDecoderReadingNext::Done(_reader) => break,
ResponseDecoderReadingNext::More((next, item)) => {
reading = next;
item?
}
};
match item {
BaoContentItem::Parent(Parent { node, pair }) => {
let outboard = if let Some(outboard) = outboard.as_mut() {
outboard
} else {
let tree = reading.tree();
let create = create.take().unwrap();
let new = create(root, tree).await?;
outboard = Some(new);
outboard.as_mut().unwrap()
};
outboard.save(node, &pair).await?;
}
BaoContentItem::Leaf(Leaf { offset, data }) => {
target.write_bytes_at(offset.0, data).await?;
}
}
}
Ok(outboard)
}
fn read_parent(buf: &[u8]) -> (blake3::Hash, blake3::Hash) {
let l_hash = blake3::Hash::from(<[u8; 32]>::try_from(&buf[..32]).unwrap());
let r_hash = blake3::Hash::from(<[u8; 32]>::try_from(&buf[32..64]).unwrap());
(l_hash, r_hash)
}
/// Given an outboard, return a range set of all valid ranges
pub async fn valid_ranges<O>(outboard: &mut O) -> io::Result<ChunkRanges>
where
O: Outboard,
{
struct RecursiveValidator<'a, O: Outboard> {
tree: BaoTree,
shifted_filled_size: TreeNode,
res: ChunkRanges,
outboard: &'a mut O,
}
impl<'a, O: Outboard> RecursiveValidator<'a, O> {
fn validate_rec<'b>(
&'b mut self,
parent_hash: &'b blake3::Hash,
shifted: TreeNode,
is_root: bool,
) -> LocalBoxFuture<'b, io::Result<()>> {
async move {
let node = shifted.subtract_block_size(self.tree.block_size.0);
// if there is an IO error reading the hash, we simply continue without adding the range
let (l_hash, r_hash) =
if let Some((l_hash, r_hash)) = self.outboard.load(node).await? {
let actual = parent_cv(&l_hash, &r_hash, is_root);
if &actual != parent_hash {
// we got a validation error. Simply continue without adding the range
return Ok(());
}
(l_hash, r_hash)
} else {
(*parent_hash, blake3::Hash::from([0; 32]))
};
if shifted.is_leaf() {
let start = node.chunk_range().start;
let end = (start + self.tree.chunk_group_chunks() * 2).min(self.tree.chunks());
self.res |= ChunkRanges::from(start..end);
} else {
// recurse
let left = shifted.left_child().unwrap();
self.validate_rec(&l_hash, left, false).await?;
let right = shifted.right_descendant(self.shifted_filled_size).unwrap();
self.validate_rec(&r_hash, right, false).await?;
}
Ok(())
}
.boxed_local()
}
}
let tree = outboard.tree();
let root_hash = outboard.root();
let (shifted_root, shifted_filled_size) = tree.shifted();
let mut validator = RecursiveValidator {
tree,
shifted_filled_size,
res: ChunkRanges::empty(),
outboard,
};
validator
.validate_rec(&root_hash, shifted_root, true)
.await?;
Ok(validator.res)
}