Forráskód Böngészése

feat: second lnd node

thesimplekid 2 hónapja
szülő
commit
72c66af8b7

+ 2 - 1
crates/cdk-integration-tests/Cargo.toml

@@ -21,6 +21,7 @@ bip39 = { version = "2.0", features = ["rand"] }
 anyhow = "1"
 cdk = { path = "../cdk", features = ["mint", "wallet"] }
 cdk-cln = { path = "../cdk-cln" }
+cdk-lnd = { path = "../cdk-lnd" }
 cdk-axum = { path = "../cdk-axum" }
 cdk-sqlite = { path = "../cdk-sqlite" }
 cdk-redb = { path = "../cdk-redb" }
@@ -34,7 +35,7 @@ uuid = { version = "1", features = ["v4"] }
 serde = "1"
 serde_json = "1"
 # ln-regtest-rs = { path = "../../../../ln-regtest-rs" }
-ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "166038b5" }
+ln-regtest-rs = { git = "https://github.com/thesimplekid/ln-regtest-rs", rev = "f9e7bbbb" }
 lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
 tracing = { version = "0.1", default-features = false, features = [
     "attributes",

+ 63 - 14
crates/cdk-integration-tests/src/bin/regtest_mint.rs

@@ -3,14 +3,15 @@ use std::env;
 use anyhow::Result;
 use cdk::cdk_database::mint_memory::MintMemoryDatabase;
 use cdk_integration_tests::init_regtest::{
-    create_cln_backend, create_mint, fund_ln, get_bitcoin_dir, get_cln_dir, get_temp_dir,
-    init_bitcoin_client, init_bitcoind, init_lnd, init_lnd_client, open_channel, BITCOIN_RPC_PASS,
-    BITCOIN_RPC_USER,
+    create_cln_backend, create_mint, fund_ln, generate_block, get_bitcoin_dir, get_cln_dir,
+    get_lnd_cert_file_path, get_lnd_dir, get_lnd_macaroon_path, get_temp_dir, init_bitcoin_client,
+    init_bitcoind, init_lnd, open_channel, BITCOIN_RPC_PASS, BITCOIN_RPC_USER, LND_ADDR,
+    LND_RPC_ADDR, LND_TWO_ADDR, LND_TWO_RPC_ADDR,
 };
 use cdk_redb::MintRedbDatabase;
 use cdk_sqlite::MintSqliteDatabase;
 use ln_regtest_rs::cln::Clnd;
-use ln_regtest_rs::ln_client::{ClnClient, LightningClient};
+use ln_regtest_rs::ln_client::{ClnClient, LightningClient, LndClient};
 use tracing_subscriber::EnvFilter;
 
 const CLN_ADDR: &str = "127.0.0.1:19846";
@@ -70,34 +71,82 @@ async fn main() -> Result<()> {
 
     let cln_two_client = ClnClient::new(cln_two_dir.clone(), None).await?;
 
-    cln_client.wait_chain_sync().await.unwrap();
+    cln_two_client.wait_chain_sync().await.unwrap();
 
     fund_ln(&bitcoin_client, &cln_two_client).await.unwrap();
 
-    let mut lnd = init_lnd().await;
+    let lnd_dir = get_lnd_dir("one");
+    println!("{}", lnd_dir.display());
+
+    let mut lnd = init_lnd(lnd_dir.clone(), LND_ADDR, LND_RPC_ADDR).await;
     lnd.start_lnd().unwrap();
     tracing::info!("Started lnd node");
 
-    let lnd_client = init_lnd_client().await.unwrap();
+    let lnd_client = LndClient::new(
+        format!("https://{}", LND_RPC_ADDR),
+        get_lnd_cert_file_path(&lnd_dir),
+        get_lnd_macaroon_path(&lnd_dir),
+    )
+    .await?;
 
     lnd_client.wait_chain_sync().await.unwrap();
 
     fund_ln(&bitcoin_client, &lnd_client).await.unwrap();
 
-    open_channel(&bitcoin_client, &cln_client, &lnd_client)
-        .await
-        .unwrap();
+    // create second lnd node
+    let lnd_two_dir = get_lnd_dir("two");
+    let mut lnd_two = init_lnd(lnd_two_dir.clone(), LND_TWO_ADDR, LND_TWO_RPC_ADDR).await;
+    lnd_two.start_lnd().unwrap();
+    tracing::info!("Started second lnd node");
+
+    let lnd_two_client = LndClient::new(
+        format!("https://{}", LND_TWO_RPC_ADDR),
+        get_lnd_cert_file_path(&lnd_two_dir),
+        get_lnd_macaroon_path(&lnd_two_dir),
+    )
+    .await?;
+
+    lnd_two_client.wait_chain_sync().await.unwrap();
+
+    fund_ln(&bitcoin_client, &lnd_two_client).await.unwrap();
+
+    // Open channels concurrently
+    // Open channels
+    {
+        open_channel(&cln_client, &lnd_client).await.unwrap();
+        tracing::info!("Opened channel between cln and lnd one");
+        generate_block(&bitcoin_client)?;
+        // open_channel(&bitcoin_client, &cln_client, &cln_two_client)
+        //     .await
+        //     .unwrap();
+        // tracing::info!("Opened channel between cln and cln two");
+
+        open_channel(&lnd_client, &lnd_two_client).await.unwrap();
+        tracing::info!("Opened channel between lnd and lnd two");
+        generate_block(&bitcoin_client)?;
+
+        // open_channel(&cln_client, &lnd_two_client).await.unwrap();
+        // tracing::info!("Opened channel between cln and lnd two");
+        open_channel(&cln_two_client, &lnd_client).await.unwrap();
+        tracing::info!("Opened channel between cln two and lnd");
+        generate_block(&bitcoin_client)?;
+
+        open_channel(&cln_client, &lnd_two_client).await.unwrap();
+        tracing::info!("Opened channel between cln and lnd two");
+        generate_block(&bitcoin_client)?;
+
+        cln_client.wait_channels_active().await?;
+        cln_two_client.wait_channels_active().await?;
+        lnd_client.wait_channels_active().await?;
+        lnd_two_client.wait_channels_active().await?;
+    }
 
     let addr = "127.0.0.1";
     let port = 8085;
 
     let mint_db_kind = env::var("MINT_DATABASE")?;
 
-    let temp_dir_path = get_temp_dir();
     let db_path = get_temp_dir().join("mint");
-    let cln_path = temp_dir_path.join("one");
-
-    let cln_client = ClnClient::new(cln_path, None).await?;
 
     let cln_backend = create_cln_backend(&cln_client).await?;
 

+ 47 - 35
crates/cdk-integration-tests/src/init_regtest.rs

@@ -9,6 +9,7 @@ use cdk::cdk_lightning::{self, MintLightning};
 use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
 use cdk::nuts::{CurrencyUnit, PaymentMethod};
 use cdk_cln::Cln as CdkCln;
+use cdk_lnd::Lnd as CdkLnd;
 use ln_regtest_rs::bitcoin_client::BitcoinClient;
 use ln_regtest_rs::bitcoind::Bitcoind;
 use ln_regtest_rs::ln_client::{ClnClient, LightningClient, LndClient};
@@ -21,11 +22,14 @@ pub const ZMQ_RAW_BLOCK: &str = "tcp://127.0.0.1:28332";
 pub const ZMQ_RAW_TX: &str = "tcp://127.0.0.1:28333";
 pub const BITCOIN_RPC_USER: &str = "testuser";
 pub const BITCOIN_RPC_PASS: &str = "testpass";
-const LND_ADDR: &str = "0.0.0.0:18449";
-const LND_RPC_ADDR: &str = "localhost:10009";
 
 const BITCOIN_DIR: &str = "bitcoin";
-const LND_DIR: &str = "lnd";
+
+pub const LND_ADDR: &str = "0.0.0.0:18449";
+pub const LND_RPC_ADDR: &str = "localhost:10009";
+
+pub const LND_TWO_ADDR: &str = "0.0.0.0:18410";
+pub const LND_TWO_RPC_ADDR: &str = "localhost:10010";
 
 pub fn get_mint_addr() -> String {
     env::var("cdk_itests_mint_addr").expect("Temp dir set")
@@ -78,23 +82,31 @@ pub fn init_bitcoin_client() -> Result<BitcoinClient> {
 }
 
 pub fn get_cln_dir(name: &str) -> PathBuf {
-    let dir = get_temp_dir().join(name);
+    let dir = get_temp_dir().join("cln").join(name);
     std::fs::create_dir_all(&dir).unwrap();
     dir
 }
 
-pub fn get_lnd_dir() -> PathBuf {
-    let dir = get_temp_dir().join(LND_DIR);
+pub fn get_lnd_dir(name: &str) -> PathBuf {
+    let dir = get_temp_dir().join("lnd").join(name);
     std::fs::create_dir_all(&dir).unwrap();
     dir
 }
 
-pub async fn init_lnd() -> Lnd {
+pub fn get_lnd_cert_file_path(lnd_dir: &PathBuf) -> PathBuf {
+    lnd_dir.join("tls.cert")
+}
+
+pub fn get_lnd_macaroon_path(lnd_dir: &PathBuf) -> PathBuf {
+    lnd_dir.join("data/chain/bitcoin/regtest/admin.macaroon")
+}
+
+pub async fn init_lnd(lnd_dir: PathBuf, lnd_addr: &str, lnd_rpc_addr: &str) -> Lnd {
     Lnd::new(
         get_bitcoin_dir(),
-        get_lnd_dir(),
-        LND_ADDR.parse().unwrap(),
-        LND_RPC_ADDR.to_string(),
+        lnd_dir,
+        lnd_addr.parse().unwrap(),
+        lnd_rpc_addr.to_string(),
         BITCOIN_RPC_USER.to_string(),
         BITCOIN_RPC_PASS.to_string(),
         ZMQ_RAW_BLOCK.to_string(),
@@ -102,16 +114,11 @@ pub async fn init_lnd() -> Lnd {
     )
 }
 
-pub async fn init_lnd_client() -> Result<LndClient> {
-    let lnd_dir = get_lnd_dir();
-    let cert_file = lnd_dir.join("tls.cert");
-    let macaroon_file = lnd_dir.join("data/chain/bitcoin/regtest/admin.macaroon");
-    LndClient::new(
-        format!("https://{}", LND_RPC_ADDR).parse().unwrap(),
-        cert_file,
-        macaroon_file,
-    )
-    .await
+pub fn generate_block(bitcoin_client: &BitcoinClient) -> Result<()> {
+    let mine_to_address = bitcoin_client.get_new_address()?;
+    bitcoin_client.generate_blocks(&mine_to_address, 10)?;
+
+    Ok(())
 }
 
 pub async fn create_cln_backend(cln_client: &ClnClient) -> Result<CdkCln> {
@@ -125,6 +132,21 @@ pub async fn create_cln_backend(cln_client: &ClnClient) -> Result<CdkCln> {
     Ok(CdkCln::new(rpc_path, fee_reserve).await?)
 }
 
+pub async fn create_lnd_backend(lnd_client: &LndClient) -> Result<CdkLnd> {
+    let fee_reserve = FeeReserve {
+        min_fee_reserve: 1.into(),
+        percent_fee_reserve: 1.0,
+    };
+
+    Ok(CdkLnd::new(
+        lnd_client.address.clone(),
+        lnd_client.cert_file.clone(),
+        lnd_client.macaroon_file.clone(),
+        fee_reserve,
+    )
+    .await?)
+}
+
 pub async fn create_mint<D, L>(addr: &str, port: u16, database: D, lighting: L) -> Result<()>
 where
     D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
@@ -163,7 +185,7 @@ where
 {
     let ln_address = ln_client.get_new_onchain_address().await?;
 
-    bitcoin_client.send_to_address(&ln_address, 2_000_000)?;
+    bitcoin_client.send_to_address(&ln_address, 5_000_000)?;
 
     ln_client.wait_chain_sync().await?;
 
@@ -175,11 +197,7 @@ where
     Ok(())
 }
 
-pub async fn open_channel<C1, C2>(
-    bitcoin_client: &BitcoinClient,
-    cln_client: &C1,
-    lnd_client: &C2,
-) -> Result<()>
+pub async fn open_channel<C1, C2>(cln_client: &C1, lnd_client: &C2) -> Result<()>
 where
     C1: LightningClient,
     C2: LightningClient,
@@ -195,19 +213,13 @@ where
         .await
         .unwrap();
 
+    cln_client.wait_chain_sync().await?;
+    lnd_client.wait_chain_sync().await?;
+
     lnd_client
         .open_channel(1_500_000, &cln_pubkey.to_string(), Some(750_000))
         .await
         .unwrap();
 
-    let mine_to_address = bitcoin_client.get_new_address()?;
-    bitcoin_client.generate_blocks(&mine_to_address, 10)?;
-
-    cln_client.wait_chain_sync().await?;
-    lnd_client.wait_chain_sync().await?;
-
-    cln_client.wait_channels_active().await?;
-    lnd_client.wait_channels_active().await?;
-
     Ok(())
 }

+ 23 - 8
crates/cdk-integration-tests/tests/regtest.rs

@@ -15,17 +15,31 @@ use cdk::nuts::{
 use cdk::wallet::client::{HttpClient, MintConnector};
 use cdk::wallet::{Wallet, WalletSubscription};
 use cdk_integration_tests::init_regtest::{
-    get_cln_dir, get_mint_url, get_mint_ws_url, init_lnd_client,
+    get_cln_dir, get_lnd_dir, get_mint_url, get_mint_ws_url, LND_RPC_ADDR,
 };
 use futures::{SinkExt, StreamExt};
 use lightning_invoice::Bolt11Invoice;
-use ln_regtest_rs::ln_client::{ClnClient, LightningClient};
+use ln_regtest_rs::ln_client::{ClnClient, LightningClient, LndClient};
 use ln_regtest_rs::InvoiceStatus;
 use serde_json::json;
 use tokio::time::timeout;
 use tokio_tungstenite::connect_async;
 use tokio_tungstenite::tungstenite::protocol::Message;
 
+// This is the ln wallet we use to send/receive ln payements as the wallet
+async fn init_lnd_client() -> LndClient {
+    let lnd_dir = get_lnd_dir("one");
+    let cert_file = lnd_dir.join("tls.cert");
+    let macaroon_file = lnd_dir.join("data/chain/bitcoin/regtest/admin.macaroon");
+    LndClient::new(
+        format!("https://{}", LND_RPC_ADDR),
+        cert_file,
+        macaroon_file,
+    )
+    .await
+    .unwrap()
+}
+
 async fn get_notification<T: StreamExt<Item = Result<Message, E>> + Unpin, E: Debug>(
     reader: &mut T,
     timeout_to_wait: Duration,
@@ -60,7 +74,7 @@ async fn get_notification<T: StreamExt<Item = Result<Message, E>> + Unpin, E: De
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_regtest_mint_melt_round_trip() -> Result<()> {
-    let lnd_client = init_lnd_client().await.unwrap();
+    let lnd_client = init_lnd_client().await;
 
     let wallet = Wallet::new(
         &get_mint_url(),
@@ -143,7 +157,7 @@ async fn test_regtest_mint_melt_round_trip() -> Result<()> {
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_regtest_mint_melt() -> Result<()> {
-    let lnd_client = init_lnd_client().await?;
+    let lnd_client = init_lnd_client().await;
 
     let wallet = Wallet::new(
         &get_mint_url(),
@@ -174,7 +188,7 @@ async fn test_regtest_mint_melt() -> Result<()> {
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_restore() -> Result<()> {
-    let lnd_client = init_lnd_client().await?;
+    let lnd_client = init_lnd_client().await;
 
     let seed = Mnemonic::generate(12)?.to_seed_normalized("");
     let wallet = Wallet::new(
@@ -231,7 +245,8 @@ async fn test_restore() -> Result<()> {
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_pay_invoice_twice() -> Result<()> {
-    let lnd_client = init_lnd_client().await?;
+    let lnd_client = init_lnd_client().await;
+
     let seed = Mnemonic::generate(12)?.to_seed_normalized("");
     let wallet = Wallet::new(
         &get_mint_url(),
@@ -284,7 +299,7 @@ async fn test_pay_invoice_twice() -> Result<()> {
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_internal_payment() -> Result<()> {
-    let lnd_client = init_lnd_client().await?;
+    let lnd_client = init_lnd_client().await;
 
     let seed = Mnemonic::generate(12)?.to_seed_normalized("");
     let wallet = Wallet::new(
@@ -356,7 +371,7 @@ async fn test_internal_payment() -> Result<()> {
 
 #[tokio::test(flavor = "multi_thread", worker_threads = 1)]
 async fn test_cached_mint() -> Result<()> {
-    let lnd_client = init_lnd_client().await.unwrap();
+    let lnd_client = init_lnd_client().await;
 
     let wallet = Wallet::new(
         &get_mint_url(),

+ 4 - 4
misc/itests.sh

@@ -12,11 +12,11 @@ cleanup() {
     wait $CDK_ITEST_MINT_BIN_PID
 
     echo "Mint binary terminated"
-    
     # Kill processes
-    lncli --lnddir="$cdk_itests/lnd" --network=regtest stop
-    lightning-cli --regtest --lightning-dir="$cdk_itests/one/" stop
-    lightning-cli --regtest --lightning-dir="$cdk_itests/two/" stop
+    lncli --lnddir="$cdk_itests/lnd/one" --network=regtest stop
+    lncli --lnddir="$cdk_itests/lnd/two" --network=regtest --rpcserver=localhost:10010 stop
+    lightning-cli --regtest --lightning-dir="$cdk_itests/cln/one/" stop
+    lightning-cli --regtest --lightning-dir="$cdk_itests/cln/two/" stop
     bitcoin-cli --datadir="$cdk_itests/bitcoin"  -rpcuser=testuser -rpcpassword=testpass -rpcport=18443 stop
 
     # Remove the temporary directory