n0-computer / iroh

A toolkit for building distributed applications
https://iroh.computer
Apache License 2.0
2.58k stars 162 forks source link

don't get keys in a shared doc #2494

Open gaoming1998 opened 4 months ago

gaoming1998 commented 4 months ago

Hi, πŸ‘‹

Using the code below, I can consistently reproduce


// #![cfg(feature = "mem-db")]

use std::str::FromStr;

use anyhow::{anyhow, Result};
use futures:: TryStreamExt;

use iroh::docs::store::Query;
use iroh::docs::DocTicket;
use iroh::node;

fn main() -> Result<()> {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .thread_name("main-runtime")
        .worker_threads(2)
        .enable_all()
        .build()?;
    rt.block_on(main_impl())?;
    // give the runtime some time to finish, but do not wait indefinitely.
    // there are cases where the a runtime thread is blocked doing io.
    // e.g. reading from stdin.
    rt.shutdown_timeout(std::time::Duration::from_millis(500));
    Ok(())
}

async fn main_impl() -> Result<()> {
    let cmd_args: Vec<String> = std::env::args().collect();
    if cmd_args.len() != 2 {
        return Err(anyhow!("Usage: iroh-node <doc_ticket>"));
    }
    let store = node::Builder::default();
    let iroh_node = store.spawn().await.expect("failed to start store");
    let ticket = DocTicket::from_str(&cmd_args[1]).expect("failed to parse ticket");
    let docs = iroh_node.docs();
    let doc = docs.import(ticket).await.expect("failed to import doc");
    let doc = docs.open(doc.id()).await.expect("failed to open doc").expect("doc not found");
    let query = Query::all();
    let mut stream = doc.get_many(query).await?;
    let entry_one= stream.try_next().await?;
    println!("doc id: {}", doc.id());
    println!("entry one: {:?}", entry_one);
    Ok(())
}
[package]
name = "iroh_test"
version = "0.1.0"
edition = "2021"

[dependencies]
iroh = "0.20.0"
tokio = "1.38.0"
anyhow = "1.0.86"
futures = "0.3.30"

The output of the command is shown in the following figure πŸ‘

image

but on another terminal, use iroh console , the output of the command is right

image

Am I using it incorrectly? How can I use it correctly?

matheus23 commented 4 months ago

This may be related to #2319

matheus23 commented 4 months ago

What I tried to debug this was adding a subscription to live events:

let (doc, mut live_events) = docs.import_and_subscribe(ticket).await?;

loop {
    let event = live_events.try_next().await?.expect("Another live event");
    if let LiveEvent::SyncFinished(sync) = event {
        println!("Sync finished: {sync:#?}");
        break;
    }
}

But then I get this output:

Sync finished: SyncEvent {
    peer: PublicKey(uvpsmezolzb55a2n),
    origin: Connect(
        DirectJoin,
    ),
    finished: SystemTime {
        tv_sec: 1721035412,
        tv_nsec: 832947810,
    },
    started: SystemTime {
        tv_sec: 1721035412,
        tv_nsec: 767314187,
    },
    result: Ok(
        SyncDetails {
            entries_received: 0,
            entries_sent: 0,
        },
    ),
}
doc id: rs33g4qlsr7x5agyuq4gbw4ud64zsx74jdjcd3shrga5shbqz73q
entry one: None