Skip to content

Commit 4278331

Browse files
committed
chore: enable auto push by default and announcing to mesh by default; add provider preload list
1 parent 38d68ed commit 4278331

File tree

5 files changed

+56
-39
lines changed

5 files changed

+56
-39
lines changed

extensions/warp-ipfs/hotspot/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1313

1414
clap = { version = "4.4", features = ["derive"] }
1515
zeroize.workspace = true
16-
base64 = "0.21"
16+
base64 = "0.22"
1717

1818
tokio = { workspace = true }
19-
toml = "0.5.11"
19+
toml = "0.8.19"

extensions/warp-ipfs/src/config.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub struct IpfsSetting {
107107
/// Used for testing with a memory transport
108108
pub memory_transport: bool,
109109
pub dht_client: bool,
110+
pub preload: Vec<Multiaddr>,
110111
}
111112

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

@@ -133,8 +128,6 @@ pub struct StoreSetting {
133128
pub friend_request_response_duration: Option<Duration>,
134129
/// Disable providing images for identities
135130
pub disable_images: bool,
136-
/// Announce to mesh network
137-
pub announce_to_mesh: bool,
138131
/// Function to call to provide data for a default profile picture if one is not apart of the identity
139132
pub default_profile_picture: Option<DefaultPfpFn>,
140133
}
@@ -148,7 +141,6 @@ impl std::fmt::Debug for StoreSetting {
148141
impl Default for StoreSetting {
149142
fn default() -> Self {
150143
Self {
151-
auto_push: None,
152144
discovery: Discovery::Namespace {
153145
namespace: None,
154146
discovery_type: Default::default(),
@@ -158,7 +150,6 @@ impl Default for StoreSetting {
158150
disable_images: false,
159151
with_friends: false,
160152
default_profile_picture: None,
161-
announce_to_mesh: false,
162153
}
163154
}
164155
}

extensions/warp-ipfs/src/hotspot/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ impl HotspotTask {
157157
// TODO: Maybe perform in match condition to prevent nesdless update if the document entry is new?
158158
user.update_identity_document(document.clone());
159159

160+
// TODO: manually propagate initial message to mesh network
161+
160162
Ok(())
161163
}
162164
}
@@ -253,6 +255,7 @@ impl HotspotUser {
253255
self.identity = identity_document;
254256
self.last_seen = Utc::now();
255257
self.last_seen_timer = Delay::new(Duration::from_secs(2 * 60));
258+
tracing::info!(identity = %self.identity.did, last_seen = %self.last_seen, "last seen identity.");
256259
}
257260
}
258261

extensions/warp-ipfs/src/lib.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ipfs::{DhtMode, Ipfs, Keypair, Protocol, UninitializedIpfs};
1313
use parking_lot::RwLock;
1414
use rust_ipfs as ipfs;
1515
use rust_ipfs::p2p::{RequestResponseConfig, UpgradeVersion};
16+
use rust_ipfs::AddPeerOpt;
1617
use std::any::Any;
1718
use std::collections::HashSet;
1819
use std::ffi::OsStr;
@@ -525,6 +526,7 @@ impl WarpIpfs {
525526
}
526527
}
527528

529+
528530
if let config::Discovery::Shuttle { addresses } =
529531
self.inner.config.store_setting().discovery.clone()
530532
{
@@ -633,6 +635,31 @@ impl WarpIpfs {
633635
}
634636
}
635637

638+
let preload = self.inner.config.ipfs_setting().preload.clone();
639+
640+
self.executor.dispatch({
641+
let ipfs = ipfs.clone();
642+
async move {
643+
for addr in preload {
644+
let Some(peer_id) = addr.peer_id() else {
645+
tracing::warn!("{addr} does not contain a peer id. Skipping");
646+
continue;
647+
};
648+
649+
let opt = AddPeerOpt::with_peer_id(peer_id).add_address(addr.clone()).set_dial();
650+
651+
if let Err(e) = ipfs.add_peer(addr.clone()).await {
652+
tracing::error!(error = %e, "unable to add {addr} to address book");
653+
continue;
654+
}
655+
656+
if !ipfs.is_connected(peer_id).await.unwrap_or_default() {
657+
let _ = ipfs.connect(peer_id).await;
658+
}
659+
}
660+
}
661+
});
662+
636663
let discovery =
637664
Discovery::new(&ipfs, &self.inner.config.store_setting().discovery, &relays);
638665

@@ -647,7 +674,7 @@ impl WarpIpfs {
647674
&discovery,
648675
&span,
649676
)
650-
.await?;
677+
.await?;
651678

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

@@ -660,7 +687,7 @@ impl WarpIpfs {
660687
self.constellation_tx.clone(),
661688
&span,
662689
)
663-
.await;
690+
.await;
664691

665692
let message_store = MessageStore::new(
666693
&ipfs,
@@ -669,7 +696,7 @@ impl WarpIpfs {
669696
self.raygun_tx.clone(),
670697
&identity_store,
671698
)
672-
.await;
699+
.await;
673700

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

@@ -1125,7 +1152,7 @@ impl LocalIdentity for WarpIpfs {
11251152
format.into(),
11261153
Some(MAX_IMAGE_SIZE),
11271154
)
1128-
.await?;
1155+
.await?;
11291156

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

extensions/warp-ipfs/src/store/identity.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -439,13 +439,7 @@ impl IdentityStore {
439439
futures::pin_mut!(event_stream);
440440
futures::pin_mut!(friend_stream);
441441

442-
let auto_push = store.config.store_setting().auto_push.is_some();
443-
444-
let interval = store
445-
.config
446-
.store_setting()
447-
.auto_push
448-
.unwrap_or(Duration::from_millis(300000));
442+
let interval = Duration::from_secs(60);
449443

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

@@ -472,7 +466,13 @@ impl IdentityStore {
472466
};
473467

474468
let identity = payload.message().clone();
475-
469+
470+
if identity.verify().is_err() {
471+
tracing::warn!(from = %from_did, "invalid identity document");
472+
//TODO: Blacklist?
473+
continue;
474+
}
475+
476476
//Maybe establish a connection?
477477
//Note: Although it would be prefer not to establish a connection, it may be ideal to check to determine
478478
// the actual source of the payload to determine if its a message propagated over the mesh from the peer
@@ -595,9 +595,7 @@ impl IdentityStore {
595595
}
596596
}
597597
_ = &mut tick => {
598-
if auto_push {
599-
store.push_to_all().await;
600-
}
598+
store.push_to_all().await;
601599
tick.reset(interval)
602600
}
603601
}
@@ -878,18 +876,16 @@ impl IdentityStore {
878876
}
879877

880878
pub async fn announce_identity_to_mesh(&self) -> Result<(), Error> {
881-
if self.config.store_setting().announce_to_mesh {
882-
let kp = self.ipfs.keypair();
883-
let document = self.own_identity_document().await?;
884-
tracing::debug!("announcing identity to mesh");
885-
let payload = PayloadBuilder::new(kp, document)
886-
.from_ipfs(&self.ipfs)
887-
.await?;
888-
let bytes = payload.to_bytes()?;
889-
match self.ipfs.pubsub_publish(IDENTITY_ANNOUNCEMENT, bytes).await {
890-
Ok(_) => tracing::debug!("identity announced to mesh"),
891-
Err(_) => tracing::warn!("unable to announce identity to mesh"),
892-
}
879+
let kp = self.ipfs.keypair();
880+
let document = self.own_identity_document().await?;
881+
tracing::debug!("announcing identity to mesh");
882+
let payload = PayloadBuilder::new(kp, document)
883+
.from_ipfs(&self.ipfs)
884+
.await?;
885+
let bytes = payload.to_bytes()?;
886+
match self.ipfs.pubsub_publish(IDENTITY_ANNOUNCEMENT, bytes).await {
887+
Ok(_) => tracing::debug!("identity announced to mesh"),
888+
Err(_) => tracing::warn!("unable to announce identity to mesh"),
893889
}
894890

895891
Ok(())

0 commit comments

Comments
 (0)