init_regtest.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. use std::env;
  2. use std::path::{Path, PathBuf};
  3. use std::sync::Arc;
  4. use anyhow::Result;
  5. use bip39::Mnemonic;
  6. use cdk::cdk_database::{self, MintDatabase};
  7. use cdk::cdk_lightning::{self, MintLightning};
  8. use cdk::mint::{FeeReserve, MintBuilder, MintMeltLimits};
  9. use cdk::nuts::{CurrencyUnit, PaymentMethod};
  10. use cdk_cln::Cln as CdkCln;
  11. use cdk_lnd::Lnd as CdkLnd;
  12. use ln_regtest_rs::bitcoin_client::BitcoinClient;
  13. use ln_regtest_rs::bitcoind::Bitcoind;
  14. use ln_regtest_rs::ln_client::{ClnClient, LightningClient, LndClient};
  15. use ln_regtest_rs::lnd::Lnd;
  16. use tracing::instrument;
  17. use crate::init_mint::start_mint;
  18. pub const BITCOIND_ADDR: &str = "127.0.0.1:18443";
  19. pub const ZMQ_RAW_BLOCK: &str = "tcp://127.0.0.1:28332";
  20. pub const ZMQ_RAW_TX: &str = "tcp://127.0.0.1:28333";
  21. pub const BITCOIN_RPC_USER: &str = "testuser";
  22. pub const BITCOIN_RPC_PASS: &str = "testpass";
  23. const BITCOIN_DIR: &str = "bitcoin";
  24. pub const LND_ADDR: &str = "0.0.0.0:18449";
  25. pub const LND_RPC_ADDR: &str = "localhost:10009";
  26. pub const LND_TWO_ADDR: &str = "0.0.0.0:18410";
  27. pub const LND_TWO_RPC_ADDR: &str = "localhost:10010";
  28. pub fn get_mint_addr() -> String {
  29. env::var("cdk_itests_mint_addr").expect("Temp dir set")
  30. }
  31. pub fn get_mint_port() -> u16 {
  32. let dir = env::var("cdk_itests_mint_port").expect("Temp dir set");
  33. dir.parse().unwrap()
  34. }
  35. pub fn get_mint_url() -> String {
  36. format!("http://{}:{}", get_mint_addr(), get_mint_port())
  37. }
  38. pub fn get_mint_ws_url() -> String {
  39. format!("ws://{}:{}/v1/ws", get_mint_addr(), get_mint_port())
  40. }
  41. pub fn get_temp_dir() -> PathBuf {
  42. let dir = env::var("cdk_itests").expect("Temp dir set");
  43. std::fs::create_dir_all(&dir).unwrap();
  44. dir.parse().expect("Valid path buf")
  45. }
  46. pub fn get_bitcoin_dir() -> PathBuf {
  47. let dir = get_temp_dir().join(BITCOIN_DIR);
  48. std::fs::create_dir_all(&dir).unwrap();
  49. dir
  50. }
  51. pub fn init_bitcoind() -> Bitcoind {
  52. Bitcoind::new(
  53. get_bitcoin_dir(),
  54. BITCOIND_ADDR.parse().unwrap(),
  55. BITCOIN_RPC_USER.to_string(),
  56. BITCOIN_RPC_PASS.to_string(),
  57. ZMQ_RAW_BLOCK.to_string(),
  58. ZMQ_RAW_TX.to_string(),
  59. )
  60. }
  61. pub fn init_bitcoin_client() -> Result<BitcoinClient> {
  62. BitcoinClient::new(
  63. "wallet".to_string(),
  64. BITCOIND_ADDR.into(),
  65. None,
  66. Some(BITCOIN_RPC_USER.to_string()),
  67. Some(BITCOIN_RPC_PASS.to_string()),
  68. )
  69. }
  70. pub fn get_cln_dir(name: &str) -> PathBuf {
  71. let dir = get_temp_dir().join("cln").join(name);
  72. std::fs::create_dir_all(&dir).unwrap();
  73. dir
  74. }
  75. pub fn get_lnd_dir(name: &str) -> PathBuf {
  76. let dir = get_temp_dir().join("lnd").join(name);
  77. std::fs::create_dir_all(&dir).unwrap();
  78. dir
  79. }
  80. pub fn get_lnd_cert_file_path(lnd_dir: &Path) -> PathBuf {
  81. lnd_dir.join("tls.cert")
  82. }
  83. pub fn get_lnd_macaroon_path(lnd_dir: &Path) -> PathBuf {
  84. lnd_dir.join("data/chain/bitcoin/regtest/admin.macaroon")
  85. }
  86. pub async fn init_lnd(lnd_dir: PathBuf, lnd_addr: &str, lnd_rpc_addr: &str) -> Lnd {
  87. Lnd::new(
  88. get_bitcoin_dir(),
  89. lnd_dir,
  90. lnd_addr.parse().unwrap(),
  91. lnd_rpc_addr.to_string(),
  92. BITCOIN_RPC_USER.to_string(),
  93. BITCOIN_RPC_PASS.to_string(),
  94. ZMQ_RAW_BLOCK.to_string(),
  95. ZMQ_RAW_TX.to_string(),
  96. )
  97. }
  98. pub fn generate_block(bitcoin_client: &BitcoinClient) -> Result<()> {
  99. let mine_to_address = bitcoin_client.get_new_address()?;
  100. bitcoin_client.generate_blocks(&mine_to_address, 10)?;
  101. Ok(())
  102. }
  103. pub async fn create_cln_backend(cln_client: &ClnClient) -> Result<CdkCln> {
  104. let rpc_path = cln_client.rpc_path.clone();
  105. let fee_reserve = FeeReserve {
  106. min_fee_reserve: 1.into(),
  107. percent_fee_reserve: 1.0,
  108. };
  109. Ok(CdkCln::new(rpc_path, fee_reserve).await?)
  110. }
  111. pub async fn create_lnd_backend(lnd_client: &LndClient) -> Result<CdkLnd> {
  112. let fee_reserve = FeeReserve {
  113. min_fee_reserve: 1.into(),
  114. percent_fee_reserve: 1.0,
  115. };
  116. Ok(CdkLnd::new(
  117. lnd_client.address.clone(),
  118. lnd_client.cert_file.clone(),
  119. lnd_client.macaroon_file.clone(),
  120. fee_reserve,
  121. )
  122. .await?)
  123. }
  124. #[instrument(skip_all)]
  125. pub async fn create_mint<D, L>(addr: &str, port: u16, database: D, lighting: L) -> Result<()>
  126. where
  127. D: MintDatabase<Err = cdk_database::Error> + Send + Sync + 'static,
  128. L: MintLightning<Err = cdk_lightning::Error> + Send + Sync + 'static,
  129. {
  130. let mut mint_builder = MintBuilder::new();
  131. mint_builder = mint_builder.with_localstore(Arc::new(database));
  132. mint_builder = mint_builder.add_ln_backend(
  133. CurrencyUnit::Sat,
  134. PaymentMethod::Bolt11,
  135. MintMeltLimits::new(1, 5_000),
  136. Arc::new(lighting),
  137. );
  138. let mnemonic = Mnemonic::generate(12)?;
  139. mint_builder = mint_builder
  140. .with_name("regtest mint".to_string())
  141. .with_description("regtest mint".to_string())
  142. .with_quote_ttl(10000, 10000)
  143. .with_seed(mnemonic.to_seed_normalized("").to_vec());
  144. let mint = mint_builder.build().await?;
  145. start_mint(addr, port, mint).await?;
  146. Ok(())
  147. }
  148. pub async fn fund_ln<C>(bitcoin_client: &BitcoinClient, ln_client: &C) -> Result<()>
  149. where
  150. C: LightningClient,
  151. {
  152. let ln_address = ln_client.get_new_onchain_address().await?;
  153. bitcoin_client.send_to_address(&ln_address, 5_000_000)?;
  154. ln_client.wait_chain_sync().await?;
  155. let mine_to_address = bitcoin_client.get_new_address()?;
  156. bitcoin_client.generate_blocks(&mine_to_address, 10)?;
  157. ln_client.wait_chain_sync().await?;
  158. Ok(())
  159. }
  160. pub async fn open_channel<C1, C2>(cln_client: &C1, lnd_client: &C2) -> Result<()>
  161. where
  162. C1: LightningClient,
  163. C2: LightningClient,
  164. {
  165. let cln_info = cln_client.get_connect_info().await?;
  166. let cln_pubkey = cln_info.pubkey;
  167. let cln_address = cln_info.address;
  168. let cln_port = cln_info.port;
  169. lnd_client
  170. .connect_peer(cln_pubkey.to_string(), cln_address.to_string(), cln_port)
  171. .await
  172. .unwrap();
  173. cln_client.wait_chain_sync().await?;
  174. lnd_client.wait_chain_sync().await?;
  175. lnd_client
  176. .open_channel(1_500_000, &cln_pubkey.to_string(), Some(750_000))
  177. .await
  178. .unwrap();
  179. Ok(())
  180. }