diff --git a/src/handler.rs b/src/handler.rs index d952dfa..926e13c 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -506,7 +506,7 @@ pub async fn api_v1_reply( }); let helipad_config = state.helipad_config.clone(); - let lightning = match lightning::connect_to_lnd(helipad_config.node_address, helipad_config.cert_path, helipad_config.macaroon_path).await { + let lightning = match lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await { Some(lndconn) => lndconn, None => { return (StatusCode::INTERNAL_SERVER_ERROR, "** Error connecting to LND.").into_response(); diff --git a/src/lightning.rs b/src/lightning.rs index 7711076..bee712f 100644 --- a/src/lightning.rs +++ b/src/lightning.rs @@ -126,8 +126,8 @@ impl std::fmt::Display for BoostError { impl std::error::Error for BoostError {} -pub async fn connect_to_lnd(node_address: String, cert_path: String, macaroon_path: String) -> Option { - let cert: Vec = match fs::read(cert_path.clone()) { +pub async fn connect_to_lnd(node_address: &str, cert_path: &str, macaroon_path: &str) -> Option { + let cert: Vec = match fs::read(cert_path) { Ok(cert_content) => cert_content, Err(_) => { eprintln!("Cannot find a valid tls.cert file"); @@ -135,7 +135,7 @@ pub async fn connect_to_lnd(node_address: String, cert_path: String, macaroon_pa } }; - let macaroon: Vec = match fs::read(macaroon_path.clone()) { + let macaroon: Vec = match fs::read(macaroon_path) { Ok(macaroon_content) => macaroon_content, Err(_) => { eprintln!("Cannot find a valid admin.macaroon file"); @@ -144,10 +144,11 @@ pub async fn connect_to_lnd(node_address: String, cert_path: String, macaroon_pa }; //Make the connection to LND - let lightning = lnd::Lnd::connect_with_macaroon(node_address.clone(), &cert, &macaroon).await; + let address = String::from(node_address); + let lightning = lnd::Lnd::connect_with_macaroon(address.clone(), &cert, &macaroon).await; if lightning.is_err() { - println!("Could not connect to: [{}] using tls: [{}] and macaroon: [{}]", node_address, cert_path, macaroon_path); + println!("Could not connect to: [{}] using tls: [{}] and macaroon: [{}]", address, cert_path, macaroon_path); eprintln!("{:#?}", lightning.err()); return None; } diff --git a/src/main.rs b/src/main.rs index 6c6deaa..f7c2a38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -361,7 +361,7 @@ async fn lnd_poller(helipad_config: HelipadConfig) { //Make the connection to LND println!("\nConnecting to LND node address..."); let mut lightning; - match lightning::connect_to_lnd(helipad_config.node_address, helipad_config.cert_path, helipad_config.macaroon_path).await { + match lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await { Some(lndconn) => { println!(" - Success."); lightning = lndconn; @@ -402,21 +402,33 @@ async fn lnd_poller(helipad_config: HelipadConfig) { let mut updated = false; //Get lnd node channel balance - match lnd::Lnd::channel_balance(&mut lightning).await { - Ok(balance) => { - let mut current_balance: i64 = 0; - if let Some(bal) = balance.local_balance { - println!("LND node local balance: {:#?}", bal.sat); - current_balance = bal.sat as i64; - } + let balance = lnd::Lnd::channel_balance(&mut lightning).await; + + if let Err(status) = balance { + eprintln!("Error getting LND wallet balance: {:#?}", status); - if dbif::add_wallet_balance_to_db(&db_filepath, current_balance).is_err() { - println!("Error adding wallet balance to the database."); + if status.message() == "transport error" { + // Attempt reconnect to LND + if let Some(lndconn) = lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await { + println!(" - Reconnected."); + lightning = lndconn; } } - Err(e) => { - eprintln!("Error getting LND wallet balance: {:#?}", e); - } + + tokio::time::sleep(tokio::time::Duration::from_millis(9000)).await; + continue; + } + + let balance = balance.unwrap(); + let mut current_balance: i64 = 0; + + if let Some(bal) = balance.local_balance { + println!("LND node local balance: {:#?}", bal.sat); + current_balance = bal.sat as i64; + } + + if dbif::add_wallet_balance_to_db(&db_filepath, current_balance).is_err() { + println!("Error adding wallet balance to the database."); } //Get a list of invoices