Skip to content

Commit 58509bb

Browse files
committed
Finish it
1 parent cfce2a2 commit 58509bb

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

src/app/docs/quickstart/page.mdx

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export const metadata = {
77

88
# Quickstart
99

10-
Let's dive into iroh by building a simple peer-to-peer file transfer tool in rust. {{ className: 'lead' }}
10+
Let's dive into iroh by building a simple peer-to-peer file transfer tool in rust! {{ className: 'lead' }}
1111

1212

1313
## What we'll build
1414

15-
At the end we should be able to transfer a file from one device that has it by running this:
15+
At the end we should be able to transfer a file from one device by running this:
1616

1717
```sh
1818
$ cargo run -- send ./file.txt
@@ -21,7 +21,7 @@ File analyzed. Fetch this file by running:
2121
cargo run -- receive blobabvojvy[...] file.txt
2222
```
2323

24-
And then on any other device running this:
24+
And then fetch it on any other device like so:
2525
```sh
2626
$ cargo run -- receive blobabvojvy[...] file.txt
2727
Starting download.
@@ -31,7 +31,7 @@ Finished copying.
3131
Shutting down.
3232
```
3333

34-
In this guide we'll be omitting the imports statements required to get this working.
34+
In this guide we'll be omitting the import statements required to get this working.
3535
If you're ever confused about what to import, take a look at the imports in the [complete example](https://github.com/n0-computer/iroh-blobs/blob/main/examples/transfer.rs).
3636

3737

@@ -66,9 +66,9 @@ There we go, this is all we need to [open connections](https://docs.rs/iroh/late
6666

6767
<Note>
6868
Here, we're specifically configuring the `Endpoint`'s builder to include "number 0 discovery".
69-
This makes it connect to DNS servers that number 0 runs to find which relay to talk to for specific `NodeId`s.
69+
This makes it connect to DNS servers that [number 0](https://n0.computer) runs to find which relay to talk to for specific `NodeId`s.
7070
It's a great default!
71-
But if you want to, you can add other discovery types like [`discovery_local_network`](https://docs.rs/iroh/latest/iroh/endpoint/struct.Builder.html#method.discovery_local_network) based on mDNS, or [`discovery_dht`](https://docs.rs/iroh/latest/iroh/endpoint/struct.Builder.html#method.discovery_dht) for discovery based on the bit-torrent mainline DHT.
71+
But if you want to, you can add other discovery types like [`discovery_local_network`](https://docs.rs/iroh/latest/iroh/endpoint/struct.Builder.html#method.discovery_local_network) based on mDNS, or [`discovery_dht`](https://docs.rs/iroh/latest/iroh/endpoint/struct.Builder.html#method.discovery_dht) for discovery based on the bittorrent mainline DHT.
7272

7373
If all of this is too much magic for your taste, it's possible for the endpoint to work entirely without any discovery services.
7474
In that case, you'll need to make sure you're not only dialing by `NodeId`, but also help the `Endpoint` out with giving it the whole [`NodeAddr`](https://docs.rs/iroh/latest/iroh/struct.NodeAddr.html) when connecting.
@@ -189,7 +189,15 @@ Now all we need to do is fill in the `todo!()`s one-by-one:
189189

190190
### Getting ready to send
191191

192+
If we want to make a file available over the network with iroh-blobs, we first need to index this file.
192193

194+
<Note>
195+
What does this step do?
196+
197+
It hashes the file using BLAKE3 and stores a so-called "outboard" for that file.
198+
This outboard file contains information about hashes for parts of this file.
199+
All of this enables some extra features with iroh-blobs like automatically verifying the integrity of the file *during* streaming, verified range downloads and download resumption.
200+
</Note>
193201

194202
```rust
195203
let abs_path = PathBuf::from_str(path)?.canonicalize()?;
@@ -201,7 +209,15 @@ let blob = blobs
201209
.await?
202210
.finish()
203211
.await?;
212+
```
213+
214+
The `WrapOption::NoWrap` is just an indicator that we don't want to wrap the file with some metadata information about its file name.
215+
We keep it simple here for now!
204216

217+
Now, we'll print a `BlobTicket`.
218+
This ticket contains the `NodeId` of our `Endpoint` as well as the file's BLAKE3 hash.
219+
220+
```rust
205221
let node_id = router.endpoint().node_id();
206222
let ticket = BlobTicket::new(node_id.into(), blob.hash, blob.format)?;
207223

@@ -211,9 +227,13 @@ println!("cargo run --example transfer -- receive {ticket} {path}");
211227
tokio::signal::ctrl_c().await?;
212228
```
213229

230+
And as you can see, as a final step we wait for the user to stop the file providing side by hitting `Ctrl+C` in the console.
231+
214232
### Connecting to the other side to receive
215233

216-
TODO
234+
On the connection side, we got the `ticket` and the `path` from the CLI arguments and we can parse them into their `struct` versions.
235+
236+
With them parsed, we can call `blobs.download` with the information contained in the ticket and wait for the download to finish:
217237

218238
```rust
219239
let path_buf = PathBuf::from_str(path)?;
@@ -228,6 +248,11 @@ blobs
228248
.await?;
229249

230250
println!("Finished download.");
251+
```
252+
253+
As a final step, we'll copy the file we just downloaded to the desired file path:
254+
255+
```rust
231256
println!("Copying to destination.");
232257

233258
let mut file = tokio::fs::File::create(path_buf).await?;
@@ -237,6 +262,13 @@ tokio::io::copy(&mut reader, &mut file).await?;
237262
println!("Finished copying.");
238263
```
239264

265+
<Note>
266+
This first download the file completely into memory, then copy that memory into a file in two steps.
267+
268+
There's ways to make this work without having to store the whole file in memory, but that involves setting up `Blobs::persistent` instead of `Blobs::memory` and using `blobs.export` with `EntryMode::TryReference`.
269+
We'll leave these changes as an excercise to the reader 😉
270+
</Note>
271+
240272

241273
## That's it!
242274

0 commit comments

Comments
 (0)