This commit is contained in:
Sebastian Klähn
2024-02-21 16:42:24 +01:00
parent 8385ba92c7
commit 29581c5ed9
3 changed files with 27 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ use crate::tools::time;
use crate::webxdc::StatusUpdateItem;
use anyhow::{anyhow, Context as _, Result};
use image::EncodableLayout;
use iroh_base::base32;
use iroh_gossip::net::{Gossip, GOSSIP_ALPN};
use iroh_gossip::proto::{Event as IrohEvent, TopicId};
use iroh_net::magic_endpoint::accept_conn;
@@ -19,6 +20,8 @@ impl Context {
pub async fn create_gossip(&self) -> Result<()> {
let secret_key: SecretKey = self.get_or_create_iroh_keypair().await?;
println!("> our secret key: {}", base32::fmt(secret_key.to_bytes()));
if self.endpoint.lock().await.is_some() {
warn!(
self,
@@ -32,12 +35,6 @@ impl Context {
.secret_key(secret_key)
.alpns(vec![GOSSIP_ALPN.to_vec()])
.derp_mode(DerpMode::Default)
.peers_data_path(
self.blobdir
.parent()
.context("Can't get parent of blob dir")?
.to_path_buf(),
)
.bind(0)
.await?;
@@ -72,6 +69,17 @@ impl Context {
warn!(self, "joining gossip with zero peers");
} else {
info!(self, "joining gossip with peers: {peers:?}");
info!(
self,
"{:?}",
self.endpoint
.lock()
.await
.as_ref()
.unwrap()
.my_addr()
.await?
);
}
// TODO: add timeout as the returned future might be pending forever
@@ -139,7 +147,8 @@ impl Context {
/// Get the iroh gossip secret key from the database or create one.
pub async fn get_or_create_iroh_keypair(&self) -> Result<SecretKey> {
match self.get_config_parsed(Config::IrohSecretKey).await? {
Ok(SecretKey::generate())
/* match self.get_config_parsed(Config::IrohSecretKey).await? {
Some(key) => Ok(key),
None => {
let key = SecretKey::generate();
@@ -147,7 +156,7 @@ impl Context {
.await?;
Ok(key)
}
}
} */
}
}

View File

@@ -457,9 +457,15 @@ pub(crate) async fn receive_imf_inner(
let node_id = node_addr.node_id;
for topic in topics {
println!("Adding peer: {:?}", node_id);
context
.add_peer_for_topic(instance_id, topic, node_id)
.await?;
println!(
"New peer topics: {:?}",
context.get_peers_for_topic(topic).await?
);
}
}
Err(err) => {

View File

@@ -500,8 +500,6 @@ impl Context {
let mut ephemeral = status_update.gossip_topic.is_some();
if send_now {
if let Some(ref topic) = status_update.gossip_topic {
// find out if any row with `topic = topic` exists in the gossip_peers table
let topic = TopicId::from_str(&iroh_base::base32::fmt(
topic.get(0..32).context("Can't get 32 bytes from topic")?,
))?;
@@ -539,6 +537,10 @@ impl Context {
ephemeral = false;
} else {
if let Some(ref gossip) = *self.gossip.lock().await {
println!(
"sending to topic {topic} with peers: {:?}",
self.get_peers_for_topic(topic).await?
);
gossip
.broadcast(topic, serde_json::to_string(&status_update)?.into())
.await?;