Skip to content

Commit

Permalink
set lsp id and pubkey in the same function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JssDWt committed Aug 15, 2024
1 parent 1fd05f3 commit 985bfed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
12 changes: 5 additions & 7 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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(()),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<dyn Receiver> = Arc::new(PaymentReceiver {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions libs/sdk-core/src/persist/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> 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<Option<String>> {
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<Option<String>> {
self.get_setting("lsp-pubkey".to_string())
}
Expand Down

0 comments on commit 985bfed

Please sign in to comment.