Example using iroh Sender and Receiver

Open
#79 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Active
Tech stack
rust

Research direction

Start with the bao-tree documentation and the iroh-blobs usage referenced in the issue, then compare those materials with the shown send_file and receive_file functions. Done means the project has a clear, working example or documented guidance for using Sender and Receiver with bao-tree verification.

Written by the indexing model from the issue text.

Description

Hey everyone,

I am looking for using bao-tree in a project that I am developing using Iroh.

The project at the moment is quite simple: just sending files from one node to another. At the moment, I have a piece of code working, mainly using irpc, but I am missing the hashing part. I was checking iroh-blobs, and I saw that they are using this library, but for me it's not very clear how to implement it.

I was playing a bit with blake3 for doing the hashing, but after reading some of your docs, I think I am doing it wrong. I mainly have two functions:

pub async fn send_file(file: &mut File, tx: Sender<FileChunk>) -> Result<()> {
    let mut file_reader = BufReader::new(file);
    let mut buf = vec![0; FILE_CHUNK_SIZE];

    loop {
        tokio::select! {
           result = file_reader.read(&mut buf) => {
               match result {
                   Ok(bytes_read_len) => {
                        if bytes_read_len == 0 {
                            break;
                        }
                        let hash = blake3::hash(bytes_read);


                        tx.send(FileChunk {
                            data: bytes_read.to_vec(),
                            hash: hash.to_string()
                        }).await?;
                   },
                   Err(err) => {
                       bail!(err);
                   }
               }
           },
           _ = tx.closed() => {
               break;
           }
        }
    }

    Ok(())
}

send_file just sents a file over the network using a Sender object. As you can see, I am using blake3:hash to create a hash of every chunk, and sending it to the other node, but as far as I understood there is a better way to do it using bao_tree.

Same think with the receive_file function:

pub async fn receive_file(file: &mut File, mut rx: Receiver<FileChunk>) -> Result<()> {
    let mut file_writer = BufWriter::new(file);
    let mut hasher = Hasher::new();

    while let Ok(Some(chunk)) = rx.recv().await {
        let hash = blake3::hash(&chunk.data);

        if hash.to_string() != chunk.hash {
            let err_message = "UploadFile Error: Invalid hash";

            bail!(err_message);
        }

        file_writer
            .write(chunk.data.as_slice())
            .await
            .context("UploadFile Error: Failed to write data into file")?;

        hasher.update(&chunk.data);
    }

    file_writer
        .flush()
        .await
        .context("UploadFile Error: Failed to flush {:?} file")?;

    Ok(())
}

Here I am just generating a hash with the chunk of data sent by the client, and checking if it's the same. I suppose that here it also would be easier to use bao_tree.

Do you have any example or idea how it could be implement it? I would appreciate any guidance here.

Thanks a lot in advance.

Dominant language
Rust
Stars
40
Forks
12
Avg merge
7h 45m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from n0-computer/bao-tree

All issues in n0-computer/bao-tree

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.