Skip to content

Commit

Permalink
chore: enable auto push by default and announcing to mesh by default;…
Browse files Browse the repository at this point in the history
… add provider preload list
  • Loading branch information
dariusc93 committed Dec 19, 2024
1 parent 38d68ed commit 4278331
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 39 deletions.
4 changes: 2 additions & 2 deletions extensions/warp-ipfs/hotspot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }

clap = { version = "4.4", features = ["derive"] }
zeroize.workspace = true
base64 = "0.21"
base64 = "0.22"

tokio = { workspace = true }
toml = "0.5.11"
toml = "0.8.19"
11 changes: 1 addition & 10 deletions extensions/warp-ipfs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct IpfsSetting {
/// Used for testing with a memory transport
pub memory_transport: bool,
pub dht_client: bool,
pub preload: Vec<Multiaddr>,
}

pub type DefaultPfpFn = std::sync::Arc<
Expand All @@ -118,12 +119,6 @@ pub struct StoreSetting {
/// Allow only interactions with friends
/// Note: This is ignored when it comes to chating between group chat recipients
pub with_friends: bool,
/// Interval for broadcasting out identity (cannot be less than 3 minutes)
/// Note:
/// - If `None`, this will be disabled
/// - Will default to 3 minutes if less than
/// - This may be removed in the future
pub auto_push: Option<Duration>,
/// Discovery type
pub discovery: Discovery,

Expand All @@ -133,8 +128,6 @@ pub struct StoreSetting {
pub friend_request_response_duration: Option<Duration>,
/// Disable providing images for identities
pub disable_images: bool,
/// Announce to mesh network
pub announce_to_mesh: bool,
/// Function to call to provide data for a default profile picture if one is not apart of the identity
pub default_profile_picture: Option<DefaultPfpFn>,
}
Expand All @@ -148,7 +141,6 @@ impl std::fmt::Debug for StoreSetting {
impl Default for StoreSetting {
fn default() -> Self {
Self {
auto_push: None,
discovery: Discovery::Namespace {
namespace: None,
discovery_type: Default::default(),
Expand All @@ -158,7 +150,6 @@ impl Default for StoreSetting {
disable_images: false,
with_friends: false,
default_profile_picture: None,
announce_to_mesh: false,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions extensions/warp-ipfs/src/hotspot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ impl HotspotTask {
// TODO: Maybe perform in match condition to prevent nesdless update if the document entry is new?
user.update_identity_document(document.clone());

// TODO: manually propagate initial message to mesh network

Ok(())
}
}
Expand Down Expand Up @@ -253,6 +255,7 @@ impl HotspotUser {
self.identity = identity_document;
self.last_seen = Utc::now();
self.last_seen_timer = Delay::new(Duration::from_secs(2 * 60));
tracing::info!(identity = %self.identity.did, last_seen = %self.last_seen, "last seen identity.");
}
}

Expand Down
35 changes: 31 additions & 4 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ipfs::{DhtMode, Ipfs, Keypair, Protocol, UninitializedIpfs};
use parking_lot::RwLock;
use rust_ipfs as ipfs;
use rust_ipfs::p2p::{RequestResponseConfig, UpgradeVersion};
use rust_ipfs::AddPeerOpt;
use std::any::Any;
use std::collections::HashSet;
use std::ffi::OsStr;
Expand Down Expand Up @@ -525,6 +526,7 @@ impl WarpIpfs {
}
}


if let config::Discovery::Shuttle { addresses } =
self.inner.config.store_setting().discovery.clone()
{
Expand Down Expand Up @@ -633,6 +635,31 @@ impl WarpIpfs {
}
}

let preload = self.inner.config.ipfs_setting().preload.clone();

self.executor.dispatch({
let ipfs = ipfs.clone();
async move {
for addr in preload {
let Some(peer_id) = addr.peer_id() else {
tracing::warn!("{addr} does not contain a peer id. Skipping");
continue;
};

let opt = AddPeerOpt::with_peer_id(peer_id).add_address(addr.clone()).set_dial();

if let Err(e) = ipfs.add_peer(addr.clone()).await {
tracing::error!(error = %e, "unable to add {addr} to address book");
continue;
}

if !ipfs.is_connected(peer_id).await.unwrap_or_default() {
let _ = ipfs.connect(peer_id).await;
}
}
}
});

let discovery =
Discovery::new(&ipfs, &self.inner.config.store_setting().discovery, &relays);

Expand All @@ -647,7 +674,7 @@ impl WarpIpfs {
&discovery,
&span,
)
.await?;
.await?;

tracing::info!("Identity initialized");

Expand All @@ -660,7 +687,7 @@ impl WarpIpfs {
self.constellation_tx.clone(),
&span,
)
.await;
.await;

let message_store = MessageStore::new(
&ipfs,
Expand All @@ -669,7 +696,7 @@ impl WarpIpfs {
self.raygun_tx.clone(),
&identity_store,
)
.await;
.await;

tracing::info!("Messaging store initialized");

Expand Down Expand Up @@ -1125,7 +1152,7 @@ impl LocalIdentity for WarpIpfs {
format.into(),
Some(MAX_IMAGE_SIZE),
)
.await?;
.await?;

tracing::debug!("Image cid: {cid}");

Expand Down
42 changes: 19 additions & 23 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,7 @@ impl IdentityStore {
futures::pin_mut!(event_stream);
futures::pin_mut!(friend_stream);

let auto_push = store.config.store_setting().auto_push.is_some();

let interval = store
.config
.store_setting()
.auto_push
.unwrap_or(Duration::from_millis(300000));
let interval = Duration::from_secs(60);

let mut tick = Delay::new(interval);

Expand All @@ -472,7 +466,13 @@ impl IdentityStore {
};

let identity = payload.message().clone();


if identity.verify().is_err() {
tracing::warn!(from = %from_did, "invalid identity document");
//TODO: Blacklist?
continue;
}

//Maybe establish a connection?
//Note: Although it would be prefer not to establish a connection, it may be ideal to check to determine
// the actual source of the payload to determine if its a message propagated over the mesh from the peer
Expand Down Expand Up @@ -595,9 +595,7 @@ impl IdentityStore {
}
}
_ = &mut tick => {
if auto_push {
store.push_to_all().await;
}
store.push_to_all().await;
tick.reset(interval)
}
}
Expand Down Expand Up @@ -878,18 +876,16 @@ impl IdentityStore {
}

pub async fn announce_identity_to_mesh(&self) -> Result<(), Error> {
if self.config.store_setting().announce_to_mesh {
let kp = self.ipfs.keypair();
let document = self.own_identity_document().await?;
tracing::debug!("announcing identity to mesh");
let payload = PayloadBuilder::new(kp, document)
.from_ipfs(&self.ipfs)
.await?;
let bytes = payload.to_bytes()?;
match self.ipfs.pubsub_publish(IDENTITY_ANNOUNCEMENT, bytes).await {
Ok(_) => tracing::debug!("identity announced to mesh"),
Err(_) => tracing::warn!("unable to announce identity to mesh"),
}
let kp = self.ipfs.keypair();
let document = self.own_identity_document().await?;
tracing::debug!("announcing identity to mesh");
let payload = PayloadBuilder::new(kp, document)
.from_ipfs(&self.ipfs)
.await?;
let bytes = payload.to_bytes()?;
match self.ipfs.pubsub_publish(IDENTITY_ANNOUNCEMENT, bytes).await {
Ok(_) => tracing::debug!("identity announced to mesh"),
Err(_) => tracing::warn!("unable to announce identity to mesh"),
}

Ok(())
Expand Down

0 comments on commit 4278331

Please sign in to comment.