Skip to content

Commit

Permalink
Reconnect to LND on transport error
Browse files Browse the repository at this point in the history
  • Loading branch information
ericpp committed Dec 22, 2024
1 parent e41a23b commit 2b0f5bc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ 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<lnd::Lnd> {
let cert: Vec<u8> = match fs::read(cert_path.clone()) {
pub async fn connect_to_lnd(node_address: &str, cert_path: &str, macaroon_path: &str) -> Option<lnd::Lnd> {
let cert: Vec<u8> = match fs::read(cert_path) {
Ok(cert_content) => cert_content,
Err(_) => {
eprintln!("Cannot find a valid tls.cert file");
return None;
}
};

let macaroon: Vec<u8> = match fs::read(macaroon_path.clone()) {
let macaroon: Vec<u8> = match fs::read(macaroon_path) {
Ok(macaroon_content) => macaroon_content,
Err(_) => {
eprintln!("Cannot find a valid admin.macaroon file");
Expand All @@ -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;
}
Expand Down
38 changes: 25 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b0f5bc

Please sign in to comment.