Skip to content

Commit 2b0f5bc

Browse files
committed
Reconnect to LND on transport error
1 parent e41a23b commit 2b0f5bc

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub async fn api_v1_reply(
506506
});
507507

508508
let helipad_config = state.helipad_config.clone();
509-
let lightning = match lightning::connect_to_lnd(helipad_config.node_address, helipad_config.cert_path, helipad_config.macaroon_path).await {
509+
let lightning = match lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await {
510510
Some(lndconn) => lndconn,
511511
None => {
512512
return (StatusCode::INTERNAL_SERVER_ERROR, "** Error connecting to LND.").into_response();

src/lightning.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ impl std::fmt::Display for BoostError {
126126

127127
impl std::error::Error for BoostError {}
128128

129-
pub async fn connect_to_lnd(node_address: String, cert_path: String, macaroon_path: String) -> Option<lnd::Lnd> {
130-
let cert: Vec<u8> = match fs::read(cert_path.clone()) {
129+
pub async fn connect_to_lnd(node_address: &str, cert_path: &str, macaroon_path: &str) -> Option<lnd::Lnd> {
130+
let cert: Vec<u8> = match fs::read(cert_path) {
131131
Ok(cert_content) => cert_content,
132132
Err(_) => {
133133
eprintln!("Cannot find a valid tls.cert file");
134134
return None;
135135
}
136136
};
137137

138-
let macaroon: Vec<u8> = match fs::read(macaroon_path.clone()) {
138+
let macaroon: Vec<u8> = match fs::read(macaroon_path) {
139139
Ok(macaroon_content) => macaroon_content,
140140
Err(_) => {
141141
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
144144
};
145145

146146
//Make the connection to LND
147-
let lightning = lnd::Lnd::connect_with_macaroon(node_address.clone(), &cert, &macaroon).await;
147+
let address = String::from(node_address);
148+
let lightning = lnd::Lnd::connect_with_macaroon(address.clone(), &cert, &macaroon).await;
148149

149150
if lightning.is_err() {
150-
println!("Could not connect to: [{}] using tls: [{}] and macaroon: [{}]", node_address, cert_path, macaroon_path);
151+
println!("Could not connect to: [{}] using tls: [{}] and macaroon: [{}]", address, cert_path, macaroon_path);
151152
eprintln!("{:#?}", lightning.err());
152153
return None;
153154
}

src/main.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ async fn lnd_poller(helipad_config: HelipadConfig) {
361361
//Make the connection to LND
362362
println!("\nConnecting to LND node address...");
363363
let mut lightning;
364-
match lightning::connect_to_lnd(helipad_config.node_address, helipad_config.cert_path, helipad_config.macaroon_path).await {
364+
match lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await {
365365
Some(lndconn) => {
366366
println!(" - Success.");
367367
lightning = lndconn;
@@ -402,21 +402,33 @@ async fn lnd_poller(helipad_config: HelipadConfig) {
402402
let mut updated = false;
403403

404404
//Get lnd node channel balance
405-
match lnd::Lnd::channel_balance(&mut lightning).await {
406-
Ok(balance) => {
407-
let mut current_balance: i64 = 0;
408-
if let Some(bal) = balance.local_balance {
409-
println!("LND node local balance: {:#?}", bal.sat);
410-
current_balance = bal.sat as i64;
411-
}
405+
let balance = lnd::Lnd::channel_balance(&mut lightning).await;
406+
407+
if let Err(status) = balance {
408+
eprintln!("Error getting LND wallet balance: {:#?}", status);
412409

413-
if dbif::add_wallet_balance_to_db(&db_filepath, current_balance).is_err() {
414-
println!("Error adding wallet balance to the database.");
410+
if status.message() == "transport error" {
411+
// Attempt reconnect to LND
412+
if let Some(lndconn) = lightning::connect_to_lnd(&helipad_config.node_address, &helipad_config.cert_path, &helipad_config.macaroon_path).await {
413+
println!(" - Reconnected.");
414+
lightning = lndconn;
415415
}
416416
}
417-
Err(e) => {
418-
eprintln!("Error getting LND wallet balance: {:#?}", e);
419-
}
417+
418+
tokio::time::sleep(tokio::time::Duration::from_millis(9000)).await;
419+
continue;
420+
}
421+
422+
let balance = balance.unwrap();
423+
let mut current_balance: i64 = 0;
424+
425+
if let Some(bal) = balance.local_balance {
426+
println!("LND node local balance: {:#?}", bal.sat);
427+
current_balance = bal.sat as i64;
428+
}
429+
430+
if dbif::add_wallet_balance_to_db(&db_filepath, current_balance).is_err() {
431+
println!("Error adding wallet balance to the database.");
420432
}
421433

422434
//Get a list of invoices

0 commit comments

Comments
 (0)