From 985bfedac357ca435f1a1a2bfff32a11ab9700e5 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 15 Aug 2024 10:25:45 +0200 Subject: [PATCH] set lsp id and pubkey in the same function This update ensures that lsp pubkey and lsp id are always set in the same call. Sometimes the lsp pubkey is not yet available. Then only the lsp id will be set. If the lsp id changes, the pubkey is then removed. If the lsp id remains the same, the node pubkey will still be persisted. --- libs/sdk-core/src/breez_services.rs | 12 +++++------- libs/sdk-core/src/persist/settings.rs | 28 ++++++++++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/libs/sdk-core/src/breez_services.rs b/libs/sdk-core/src/breez_services.rs index 8ed964118..1818a7b43 100644 --- a/libs/sdk-core/src/breez_services.rs +++ b/libs/sdk-core/src/breez_services.rs @@ -755,8 +755,7 @@ impl BreezServices { } }; - self.persister.set_lsp_id(lsp_id)?; - self.persister.set_lsp_pubkey(lsp_pubkey)?; + self.persister.set_lsp(lsp_id, Some(lsp_pubkey))?; self.sync().await?; if let Some(webhook_url) = self.persister.get_webhook_url()? { self.register_payment_notifications(webhook_url).await? @@ -1257,8 +1256,7 @@ impl BreezServices { None => return Ok(()), }; - self.persister.set_lsp_id(lsp.id)?; - self.persister.set_lsp_pubkey(lsp.pubkey.clone())?; + self.persister.set_lsp(lsp.id, Some(lsp.pubkey.clone()))?; let node_state = match self.node_info() { Ok(node_state) => node_state, Err(_) => return Ok(()), @@ -2409,7 +2407,7 @@ impl BreezServicesBuilder { let current_lsp_id = persister.get_lsp_id()?; if current_lsp_id.is_none() && self.config.default_lsp_id.is_some() { - persister.set_lsp_id(self.config.default_lsp_id.clone().unwrap())?; + persister.set_lsp(self.config.default_lsp_id.clone().unwrap(), None)?; } let payment_receiver = Arc::new(PaymentReceiver { @@ -3233,7 +3231,7 @@ pub(crate) mod tests { let node_api = Arc::new(MockNodeAPI::new(dummy_node_state.clone())); let breez_server = Arc::new(MockBreezServer {}); - persister.set_lsp_id(breez_server.lsp_id()).unwrap(); + persister.set_lsp(breez_server.lsp_id(), None).unwrap(); persister.set_node_state(&dummy_node_state).unwrap(); let receiver: Arc = Arc::new(PaymentReceiver { @@ -3340,7 +3338,7 @@ pub(crate) mod tests { let persister = Arc::new(create_test_persister(test_config.clone())); persister.init()?; persister.insert_or_update_payments(&known_payments, false)?; - persister.set_lsp_id(MockBreezServer {}.lsp_id())?; + persister.set_lsp(MockBreezServer {}.lsp_id(), None)?; let mut builder = BreezServicesBuilder::new(test_config.clone()); let breez_services = builder diff --git a/libs/sdk-core/src/persist/settings.rs b/libs/sdk-core/src/persist/settings.rs index 2272a7ddd..6dccc5996 100644 --- a/libs/sdk-core/src/persist/settings.rs +++ b/libs/sdk-core/src/persist/settings.rs @@ -51,19 +51,33 @@ impl SqliteStorage { Ok(vec) } - pub fn set_lsp_id(&self, lsp_id: String) -> PersistResult<()> { - self.update_setting("lsp".to_string(), lsp_id) + pub fn set_lsp(&self, lsp_id: String, pubkey: Option) -> PersistResult<()> { + if let Some(pubkey) = pubkey { + self.update_setting("lsp".to_string(), lsp_id)?; + self.update_setting("lsp-pubkey".to_string(), pubkey)?; + return Ok(()); + } + + match self.get_setting("lsp".to_string())? { + Some(old_lsp_id) => { + if old_lsp_id != lsp_id { + self.update_setting("lsp".to_string(), lsp_id)?; + self.delete_setting("lsp-pubkey".to_string())?; + } + } + None => { + self.update_setting("lsp".to_string(), lsp_id)?; + self.delete_setting("lsp-pubkey".to_string())?; + } + }; + + Ok(()) } pub fn get_lsp_id(&self) -> PersistResult> { self.get_setting("lsp".to_string()) } - pub fn set_lsp_pubkey(&self, pubkey: String) -> PersistResult<()> { - self.update_setting("lsp-pubkey".to_string(), pubkey) - } - - #[allow(dead_code)] pub fn get_lsp_pubkey(&self) -> PersistResult> { self.get_setting("lsp-pubkey".to_string()) }