Skip to content

Commit

Permalink
feat: asserting the channel was correctly opened
Browse files Browse the repository at this point in the history
We need to wait until the node announcement was processed correctly by lnd. This can be checked if lnd is aware of the coordinator's `node_alias`
  • Loading branch information
bonomat committed Jul 27, 2023
1 parent 84ad247 commit 3583062
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
61 changes: 61 additions & 0 deletions crates/tests-e2e/examples/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::Parser;
use ln_dlc_node::node::NodeInfo;
use local_ip_address::local_ip;
use reqwest::Response;
use reqwest::StatusCode;
use serde::Deserialize;
use std::time::Duration;
use tests_e2e::coordinator::Coordinator;
Expand Down Expand Up @@ -39,6 +40,10 @@ async fn main() {
}

async fn fund_everything(faucet: &str, coordinator: &str) -> Result<()> {
// let node_info = get_node_info(faucet).await?;
// dbg!(node_info);
// return Ok(());

let coordinator = Coordinator::new(init_reqwest(), coordinator);
let coord_addr = coordinator.get_new_address().await?;
fund(&coord_addr, Amount::ONE_BTC, faucet).await?;
Expand Down Expand Up @@ -81,6 +86,27 @@ async fn fund_everything(faucet: &str, coordinator: &str) -> Result<()> {
)
.await?;

// wait until channel has `peer_alias` set correctly
tracing::info!("Waiting until channel is has correct peer_alias set");
let mut counter = 0;
loop {
if counter == 3 {
bail!("Could not verify channel is open. Please wipe and try again");
}
counter += 1;

let node_info = get_node_info(faucet).await?;
if let Some(node_info) = node_info {
if node_info.num_channels > 0 && node_info.node.alias == "10101.finance" {
break;
}
}

tracing::info!("Manually broadcasting node announcement and waiting for a few seconds...");
coordinator.broadcast_node_announcement().await?;
tokio::time::sleep(Duration::from_secs(5)).await;
}

let lnd_channels = get_text(&format!("{faucet}/lnd/v1/channels")).await?;
tracing::info!("open LND channels: {}", lnd_channels);
Ok(())
Expand Down Expand Up @@ -153,6 +179,40 @@ async fn post_query(path: &str, body: String, faucet: &str) -> Result<Response>
Ok(response)
}

async fn get_query(path: &str, faucet: &str) -> Result<Response> {
let faucet = faucet.to_string();
let client = init_reqwest();
let response = client.get(format!("{faucet}/{path}")).send().await?;

Ok(response)
}

#[derive(Deserialize, Debug, Clone)]
pub struct LndNodeInfo {
node: Node,
num_channels: u32,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Node {
alias: String,
}

async fn get_node_info(faucet: &str) -> Result<Option<LndNodeInfo>> {
let response = get_query(
"lnd/v1/graph/node/02dd6abec97f9a748bf76ad502b004ce05d1b2d1f43a9e76bd7d85e767ffb022c9",
faucet,
)
.await?;
if response.status() == StatusCode::NOT_FOUND {
tracing::warn!("Node info not yet found.");
return Ok(None);
}

let node_info = response.json().await?;
Ok(Some(node_info))
}

/// Instructs lnd to open a public channel with the target node.
/// 1. Connect to the target node.
/// 2. Open channel to the target node.
Expand Down Expand Up @@ -196,6 +256,7 @@ async fn open_channel(node_info: &NodeInfo, amount: Amount, faucet: &str) -> Res
.await?;

mine(10, faucet).await?;

tracing::info!("connected to channel");

tracing::info!("You can now use the lightning faucet {faucet}/faucet/");
Expand Down
8 changes: 8 additions & 0 deletions crates/tests-e2e/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ impl Coordinator {
.context("could not parse json")
}

pub async fn broadcast_node_announcement(&self) -> Result<reqwest::Response> {
let status = self
.post("/api/admin/broadcast_announcement")
.await?
.error_for_status()?;
Ok(status)
}

async fn get(&self, path: &str) -> Result<reqwest::Response> {
self.client
.get(format!("{0}{path}", self.host))
Expand Down

0 comments on commit 3583062

Please sign in to comment.