chore: connect bootstrap relays on demand #25

Merged
reya merged 3 commits from refactor-bootstrap into master 2026-09-27 07:20:55 +00:00
Showing only changes of commit bdf0e63279 - Show all commits
+35 -42
View File
@@ -151,9 +151,10 @@ impl Backend {
pump.detach(); pump.detach();
cx.defer(move |cx| { cx.defer(move |cx| {
if let Err(error) = weak.update(cx, |this, cx| this.bootstrap(cx)) { weak.update(cx, |this, cx| {
log::warn!("backend dropped before bootstrap could run: {error}"); this.restore_session(cx);
} })
.ok();
}); });
Self { Self {
@@ -167,42 +168,6 @@ impl Backend {
} }
} }
fn bootstrap(&mut self, cx: &mut Context<Self>) {
let client = self.client.clone();
let task = cx.background_spawn(async move {
for url in BOOTSTRAP_RELAYS {
client.add_relay(url).await?;
}
for url in INDEXER_RELAYS {
client
.add_relay(url)
.capabilities(RelayCapabilities::DISCOVERY)
.await?;
}
client.connect().await;
Ok::<(), Error>(())
});
let notify_task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
match task.await {
Ok(()) => {
this.update(cx, |this, cx| {
this.restore_session(cx);
})?;
}
Err(e) => {
this.update(cx, |_this, cx| cx.emit(BackendEvent::error(e.to_string())))?;
}
}
Ok::<(), Error>(())
});
notify_task.detach();
}
/// Restore the saved session from the Keyring. /// Restore the saved session from the Keyring.
/// ///
/// - Emits [`BackendEvent::SignerRequired`] when no credential is stored. /// - Emits [`BackendEvent::SignerRequired`] when no credential is stored.
@@ -227,7 +192,9 @@ impl Backend {
let result = async { let result = async {
if content.starts_with("nsec1") { if content.starts_with("nsec1") {
let keys = Keys::new(SecretKey::parse(&content)?); let keys = Keys::new(SecretKey::parse(&content)?);
this.update(cx, |this, cx| this.set_signer(keys, cx))?; this.update(cx, |this, cx| {
this.set_signer(keys, cx);
})?;
} else if content.starts_with("bunker://") { } else if content.starts_with("bunker://") {
let (base, keys) = extract_master_key(&content); let (base, keys) = extract_master_key(&content);
let uri = NostrConnectUri::parse(base)?; let uri = NostrConnectUri::parse(base)?;
@@ -238,14 +205,19 @@ impl Backend {
None, None,
)?; )?;
signer.auth_url_handler(SignedAuthUrlHandler); signer.auth_url_handler(SignedAuthUrlHandler);
this.update(cx, |this, cx| this.set_signer(signer, cx))?;
this.update(cx, |this, cx| {
this.set_signer(signer, cx);
})?;
} else if content.starts_with("ncryptsec1") { } else if content.starts_with("ncryptsec1") {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.passphrase_required = true; this.passphrase_required = true;
cx.emit(BackendEvent::PassphraseRequired); cx.emit(BackendEvent::PassphraseRequired);
})?; })?;
} else { } else {
this.update(cx, |_this, cx| cx.emit(BackendEvent::SignerRequired))?; this.update(cx, |_this, cx| {
cx.emit(BackendEvent::SignerRequired);
})?;
} }
Ok::<_, Error>(()) Ok::<_, Error>(())
@@ -1424,10 +1396,29 @@ async fn connect_repo_relays(
Ok(()) Ok(())
} }
/// Add and connect the startup relays.
async fn ensure_bootstrap_relays(client: &Client) -> Result<(), Error> {
for url in BOOTSTRAP_RELAYS {
client.add_relay(url).and_connect().await?;
}
for url in INDEXER_RELAYS {
client
.add_relay(url)
.capabilities(RelayCapabilities::DISCOVERY)
.and_connect()
.await?;
}
Ok(())
}
pub(crate) async fn subscribe_bootstrap_only( pub(crate) async fn subscribe_bootstrap_only(
client: &Client, client: &Client,
filters: Vec<Filter>, filters: Vec<Filter>,
) -> Result<(), Error> { ) -> Result<(), Error> {
ensure_bootstrap_relays(client).await?;
let opts = SubscribeAutoCloseOptions::default() let opts = SubscribeAutoCloseOptions::default()
.exit_policy(ReqExitPolicy::ExitOnEOSE) .exit_policy(ReqExitPolicy::ExitOnEOSE)
.timeout(Some(Duration::from_secs(10))); .timeout(Some(Duration::from_secs(10)));
@@ -1447,6 +1438,8 @@ pub(crate) async fn sync_bootstrap_only(
filter: Filter, filter: Filter,
opts: SyncOptions, opts: SyncOptions,
) -> Result<SyncSummary, Error> { ) -> Result<SyncSummary, Error> {
ensure_bootstrap_relays(client).await?;
let output = client let output = client
.sync(filter) .sync(filter)
.with(BOOTSTRAP_RELAYS) .with(BOOTSTRAP_RELAYS)