regtest_mint.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use std::env;
  2. use anyhow::Result;
  3. use cdk::cdk_database::mint_memory::MintMemoryDatabase;
  4. use cdk_integration_tests::init_regtest::{
  5. create_cln_backend, create_mint, fund_ln, generate_block, get_bitcoin_dir, get_cln_dir,
  6. get_lnd_cert_file_path, get_lnd_dir, get_lnd_macaroon_path, get_temp_dir, init_bitcoin_client,
  7. init_bitcoind, init_lnd, open_channel, BITCOIN_RPC_PASS, BITCOIN_RPC_USER, LND_ADDR,
  8. LND_RPC_ADDR, LND_TWO_ADDR, LND_TWO_RPC_ADDR,
  9. };
  10. use cdk_redb::MintRedbDatabase;
  11. use cdk_sqlite::MintSqliteDatabase;
  12. use ln_regtest_rs::cln::Clnd;
  13. use ln_regtest_rs::ln_client::{ClnClient, LightningClient, LndClient};
  14. use tracing_subscriber::EnvFilter;
  15. const CLN_ADDR: &str = "127.0.0.1:19846";
  16. const CLN_TWO_ADDR: &str = "127.0.0.1:19847";
  17. #[tokio::main]
  18. async fn main() -> Result<()> {
  19. let default_filter = "debug";
  20. let sqlx_filter = "sqlx=warn";
  21. let hyper_filter = "hyper=warn";
  22. let h2_filter = "h2=warn";
  23. let env_filter = EnvFilter::new(format!(
  24. "{},{},{},{}",
  25. default_filter, sqlx_filter, hyper_filter, h2_filter
  26. ));
  27. tracing_subscriber::fmt().with_env_filter(env_filter).init();
  28. let mut bitcoind = init_bitcoind();
  29. bitcoind.start_bitcoind()?;
  30. let bitcoin_client = init_bitcoin_client()?;
  31. bitcoin_client.create_wallet().ok();
  32. bitcoin_client.load_wallet()?;
  33. let new_add = bitcoin_client.get_new_address()?;
  34. bitcoin_client.generate_blocks(&new_add, 200).unwrap();
  35. let cln_one_dir = get_cln_dir("one");
  36. let mut clnd = Clnd::new(
  37. get_bitcoin_dir(),
  38. cln_one_dir.clone(),
  39. CLN_ADDR.into(),
  40. BITCOIN_RPC_USER.to_string(),
  41. BITCOIN_RPC_PASS.to_string(),
  42. );
  43. clnd.start_clnd()?;
  44. let cln_client = ClnClient::new(cln_one_dir.clone(), None).await?;
  45. cln_client.wait_chain_sync().await.unwrap();
  46. fund_ln(&bitcoin_client, &cln_client).await.unwrap();
  47. // Create second cln
  48. let cln_two_dir = get_cln_dir("two");
  49. let mut clnd_two = Clnd::new(
  50. get_bitcoin_dir(),
  51. cln_two_dir.clone(),
  52. CLN_TWO_ADDR.into(),
  53. BITCOIN_RPC_USER.to_string(),
  54. BITCOIN_RPC_PASS.to_string(),
  55. );
  56. clnd_two.start_clnd()?;
  57. let cln_two_client = ClnClient::new(cln_two_dir.clone(), None).await?;
  58. cln_two_client.wait_chain_sync().await.unwrap();
  59. fund_ln(&bitcoin_client, &cln_two_client).await.unwrap();
  60. let lnd_dir = get_lnd_dir("one");
  61. println!("{}", lnd_dir.display());
  62. let mut lnd = init_lnd(lnd_dir.clone(), LND_ADDR, LND_RPC_ADDR).await;
  63. lnd.start_lnd().unwrap();
  64. tracing::info!("Started lnd node");
  65. let lnd_client = LndClient::new(
  66. format!("https://{}", LND_RPC_ADDR),
  67. get_lnd_cert_file_path(&lnd_dir),
  68. get_lnd_macaroon_path(&lnd_dir),
  69. )
  70. .await?;
  71. lnd_client.wait_chain_sync().await.unwrap();
  72. fund_ln(&bitcoin_client, &lnd_client).await.unwrap();
  73. // create second lnd node
  74. let lnd_two_dir = get_lnd_dir("two");
  75. let mut lnd_two = init_lnd(lnd_two_dir.clone(), LND_TWO_ADDR, LND_TWO_RPC_ADDR).await;
  76. lnd_two.start_lnd().unwrap();
  77. tracing::info!("Started second lnd node");
  78. let lnd_two_client = LndClient::new(
  79. format!("https://{}", LND_TWO_RPC_ADDR),
  80. get_lnd_cert_file_path(&lnd_two_dir),
  81. get_lnd_macaroon_path(&lnd_two_dir),
  82. )
  83. .await?;
  84. lnd_two_client.wait_chain_sync().await.unwrap();
  85. fund_ln(&bitcoin_client, &lnd_two_client).await.unwrap();
  86. // Open channels concurrently
  87. // Open channels
  88. {
  89. open_channel(&cln_client, &lnd_client).await.unwrap();
  90. tracing::info!("Opened channel between cln and lnd one");
  91. generate_block(&bitcoin_client)?;
  92. // open_channel(&bitcoin_client, &cln_client, &cln_two_client)
  93. // .await
  94. // .unwrap();
  95. // tracing::info!("Opened channel between cln and cln two");
  96. open_channel(&lnd_client, &lnd_two_client).await.unwrap();
  97. tracing::info!("Opened channel between lnd and lnd two");
  98. generate_block(&bitcoin_client)?;
  99. // open_channel(&cln_client, &lnd_two_client).await.unwrap();
  100. // tracing::info!("Opened channel between cln and lnd two");
  101. open_channel(&cln_two_client, &lnd_client).await.unwrap();
  102. tracing::info!("Opened channel between cln two and lnd");
  103. generate_block(&bitcoin_client)?;
  104. open_channel(&cln_client, &lnd_two_client).await.unwrap();
  105. tracing::info!("Opened channel between cln and lnd two");
  106. generate_block(&bitcoin_client)?;
  107. cln_client.wait_channels_active().await?;
  108. cln_two_client.wait_channels_active().await?;
  109. lnd_client.wait_channels_active().await?;
  110. lnd_two_client.wait_channels_active().await?;
  111. }
  112. let addr = "127.0.0.1";
  113. let port = 8085;
  114. let mint_db_kind = env::var("MINT_DATABASE")?;
  115. let db_path = get_temp_dir().join("mint");
  116. let cln_backend = create_cln_backend(&cln_client).await?;
  117. match mint_db_kind.as_str() {
  118. "MEMORY" => {
  119. create_mint(addr, port, MintMemoryDatabase::default(), cln_backend).await?;
  120. }
  121. "SQLITE" => {
  122. let sqlite_db = MintSqliteDatabase::new(&db_path).await?;
  123. sqlite_db.migrate().await;
  124. create_mint(addr, port, sqlite_db, cln_backend).await?;
  125. }
  126. "REDB" => {
  127. let redb_db = MintRedbDatabase::new(&db_path).unwrap();
  128. create_mint(addr, port, redb_db, cln_backend).await?;
  129. }
  130. _ => panic!("Unknown mint db type: {}", mint_db_kind),
  131. };
  132. Ok(())
  133. }