Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure LSP url is a valid url when switching #1217

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mutiny-core/src/lsp/voltage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ impl LspClient {
}

/// Get the pubkey and connection string from the LSP from the /info endpoint
async fn fetch_connection_info(
pub(crate) async fn fetch_connection_info(
http_client: &Client,
url: &str,
logger: &MutinyLogger,
) -> Result<(PublicKey, String), MutinyError> {
let builder = http_client.get(format!("{}{}", url, GET_INFO_PATH));
let builder = http_client.get(format!("{}{}", url.trim(), GET_INFO_PATH));
let request = add_x_auth_token_if_needed(url, builder)?;

let response: reqwest::Response = utils::fetch_with_timeout(http_client, request)
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Lsp for LspClient {

let builder = self
.http_client
.post(format!("{}{}", &self.url, PROPOSAL_PATH))
.post(format!("{}{}", &self.url.trim(), PROPOSAL_PATH))
.json(&payload);

let request = add_x_auth_token_if_needed(&self.url, builder)?;
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Lsp for LspClient {
async fn get_lsp_fee_msat(&self, fee_request: FeeRequest) -> Result<FeeResponse, MutinyError> {
let builder = self
.http_client
.post(format!("{}{}", &self.url, FEE_PATH))
.post(format!("{}{}", &self.url.trim(), FEE_PATH))
.json(&fee_request);

let request = add_x_auth_token_if_needed(&self.url, builder)?;
Expand Down
46 changes: 42 additions & 4 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::labels::LabelStorage;
use crate::ldkstorage::CHANNEL_CLOSURE_PREFIX;
use crate::logging::LOGGING_KEY;
use crate::lsp::voltage;
use crate::utils::{sleep, spawn};
use crate::MutinyInvoice;
use crate::MutinyWalletConfig;
Expand Down Expand Up @@ -59,6 +60,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, ops::Deref, sync::Arc};
use url::Url;
#[cfg(target_arch = "wasm32")]
use web_time::Instant;

Expand Down Expand Up @@ -347,7 +349,15 @@ impl<S: MutinyStorage> NodeManagerBuilder<S> {
let lsp_config = if c.safe_mode {
None
} else {
create_lsp_config(c.lsp_url, c.lsp_connection_string, c.lsp_token)?
create_lsp_config(c.lsp_url, c.lsp_connection_string, c.lsp_token).unwrap_or_else(
|_| {
log_warn!(
logger,
"Failed to create lsp config, falling back to no LSP configured"
);
None
},
)
};
log_trace!(logger, "finished creating lsp config");

Expand Down Expand Up @@ -1338,7 +1348,7 @@ impl<S: MutinyStorage> NodeManager<S> {
/// current LSP, it will fail to change the LSP.
///
/// Requires a restart of the node manager to take effect.
pub async fn change_lsp(&self, lsp_config: Option<LspConfig>) -> Result<(), MutinyError> {
pub async fn change_lsp(&self, mut lsp_config: Option<LspConfig>) -> Result<(), MutinyError> {
log_trace!(self.logger, "calling change_lsp");

// if we are in safe mode we don't load the lightning state so we can't know if it is safe to change the LSP.
Expand All @@ -1362,6 +1372,28 @@ impl<S: MutinyStorage> NodeManager<S> {
}
drop(nodes);

// verify that the LSP config is valid
match lsp_config.as_mut() {
Some(LspConfig::VoltageFlow(config)) => {
let http_client = Client::new();

// try to connect to the LSP, update the config if successful
let (pk, str) = voltage::LspClient::fetch_connection_info(
&http_client,
&config.url,
&self.logger,
)
.await?;
config.pubkey = Some(pk);
config.connection_string = Some(str);
}
Some(LspConfig::Lsps(config)) => {
// make sure a valid connection string was provided
PubkeyConnectionInfo::new(&config.connection_string)?;
}
None => {} // Nothing to verify
}

// edit node storage
let mut node_storage = self.node_storage.write().await;
node_storage.nodes.iter_mut().for_each(|(_, n)| {
Expand Down Expand Up @@ -2036,8 +2068,14 @@ pub fn create_lsp_config(
) -> Result<Option<LspConfig>, MutinyError> {
match (lsp_url.clone(), lsp_connection_string.clone()) {
(Some(lsp_url), None) => {
if !lsp_url.is_empty() {
Ok(Some(LspConfig::new_voltage_flow(lsp_url)))
let trimmed = lsp_url.trim().to_string();
if !trimmed.is_empty() {
// make sure url is valid
if Url::parse(&trimmed).is_err() {
return Err(MutinyError::InvalidArgumentsError);
}

Ok(Some(LspConfig::new_voltage_flow(trimmed)))
} else {
Ok(None)
}
Expand Down
Loading