#Required wait between iroh `Endpoint::bind` and `Endpoint::connect`

5 messages · Page 1 of 1 (latest)

fossil relic
#

Hello, I'm trying to test a basic iroh setup which is just a direct 1-host 1-client setup. I have the following in my test:

#[tokio::test]
async fn send_recv() {
    const PAYLOAD: [u8; 8] = [1,2,3,4,5,6,7,8];
    let (server, server_addr) = Node::host().await.unwrap();
    // Needed so discovery doesn't fail
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    tokio::join!{
        async {
            let mut client = Node::connect(server_addr).await.unwrap();
            client.write(&PAYLOAD).await.unwrap();
            client // return this so it isn't dropped
        },
        async move {
            let mut server = server.await.unwrap();
            let recv = server.read().await.unwrap();
            assert_eq!(PAYLOAD, recv.as_slice());
        }
    };
}

Node::host is as follows:

pub async fn host() -> Result<(impl Future<Output = Result<Self, HostError>>, NodeId), HostError> {
    let endpoint = Endpoint::builder()
        .alpns(vec![ALPN.to_vec()])
        .discovery_n0()
        .bind().await?;
    let id = endpoint.node_id();
    let s = async move {
        let conn = endpoint.accept().await
            .expect("endpoint closed")
            .await?;
        println!("accepted");
        let (send, recv) = conn.accept_bi().await?;
        println!("accepted bi");
        Ok(Self::new(send, recv, endpoint))
    };

    Ok((s, id))
}

If I remove (or reduce) the sleep that I marked in the first codeblock then the test fails. I assume this is due to needing adequate time to register the iroh node (however that works behind the scenes), but I feel like there should be some awaitable way to do that without sleep guesswork. Am I missing something here?

fossil relic
#

Bump

true pecan
#

Heyo ameowmeltwave, iroh team member here ✌️ every now and then I look for iroh messages in other discords, but I don't actively watch this discord.
Feel free to jump into our discord, it's much more likely you'll get a response there! I hope we're just as friendly and well-moderated as this discord is 🙂
(I can't post a link to the discord here, but it's reachable from our iroh.computer website)

#

If I remove (or reduce) the sleep that I marked in the first codeblock then the test fails. I assume this is due to needing adequate time to register the iroh node (however that works behind the scenes), but I feel like there should be some awaitable way to do that without sleep guesswork. Am I missing something here?
There are some hints in the examples in the iroh repository!
In the newer versions you can e.g. use endpoint.online().await.

#

What happens in the background is that the node will register itself as available in our DNS server. This makes it possible to dial the node without needing to know it's IP addresses, only from knowing the Endpoint Id. (We've renamed NodeId to EndpointId recently)