|
| 1 | +use crate::daemon::{DaemonWrapper, DaemonHelper}; |
| 2 | +use crate::constants::env::{BITCOIND_EXEC, TEST_LEAVE_INTACT}; |
| 3 | + |
| 4 | +use anyhow::Context; |
| 5 | +use std::path::PathBuf; |
| 6 | +use std::env::VarError; |
| 7 | +use which::which; |
| 8 | +use tokio::sync::broadcast; |
| 9 | + |
| 10 | +pub struct BitcoinDHelper { |
| 11 | + name : String, |
| 12 | + bitcoind_exec: PathBuf, |
| 13 | + config: BitcoinDConfig, |
| 14 | + state: BitcoinDState, |
| 15 | +} |
| 16 | + |
| 17 | +pub struct BitcoinDConfig { |
| 18 | + pub datadir: PathBuf, |
| 19 | + pub txindex: bool, |
| 20 | + pub network: String, |
| 21 | +} |
| 22 | + |
| 23 | +impl Default for BitcoinDConfig { |
| 24 | + |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + datadir: PathBuf::from("~/.bitcoin"), |
| 28 | + txindex: false, |
| 29 | + network: String::from("regtest") |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Default)] |
| 35 | +pub struct BitcoinDState { |
| 36 | + rpc_port: Option<u16>, |
| 37 | + p2p_port: Option<u16>, |
| 38 | +} |
| 39 | + |
| 40 | +pub type BitcoinD = DaemonWrapper<BitcoinDHelper, BitcoinDState>; |
| 41 | + |
| 42 | +pub fn get_exe_path() -> anyhow::Result<PathBuf> { |
| 43 | + match std::env::var(&BITCOIND_EXEC) { |
| 44 | + Ok(var) => which(var).context("Failed to find binary path from `BITCOIND_EXEC`"), |
| 45 | + Err(VarError::NotPresent) => which("bitcoind").context("Failed to find `bitcoind` installation"), |
| 46 | + _ => anyhow::bail!("BITCOIND_EXEC is not valid unicode") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl BitcoinD { |
| 51 | + |
| 52 | + pub fn new(name: String, bitcoind_exec: PathBuf, config: BitcoinDConfig) -> Self { |
| 53 | + let state = BitcoinDState::default(); |
| 54 | + let inner = BitcoinDHelper { name, bitcoind_exec, config, state}; |
| 55 | + DaemonWrapper::wrap(inner) |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +impl DaemonHelper for BitcoinDHelper { |
| 61 | + type State = BitcoinDState; |
| 62 | + |
| 63 | + fn name(&self) -> &str { |
| 64 | + &self.name |
| 65 | + } |
| 66 | + |
| 67 | + async fn make_reservations(&mut self) -> anyhow::Result<()> { |
| 68 | + self.state.rpc_port = Some(portpicker::pick_unused_port().expect("A port is free")); |
| 69 | + self.state.p2p_port = Some(portpicker::pick_unused_port().expect("A port is free")); |
| 70 | + |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + async fn prepare(&self) -> anyhow::Result<()> { |
| 74 | + debug!("Creating bitcoind datadir in {:?}", self.config.datadir.clone()); |
| 75 | + std::fs::create_dir_all(self.config.datadir.clone())?; |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | + |
| 79 | + async fn get_command(&self) -> anyhow::Result<tokio::process::Command> { |
| 80 | + let mut cmd = tokio::process::Command::new(self.bitcoind_exec.clone()); |
| 81 | + cmd |
| 82 | + .arg(format!("--{}", self.config.network)) |
| 83 | + .arg(format!("-datadir={}", self.config.datadir.display().to_string())) |
| 84 | + .arg(format!("-txindex={}", self.config.txindex)) |
| 85 | + .arg(format!("-rpcport={}", self.state.rpc_port.expect("A port has been picked"))) |
| 86 | + .arg(format!("-port={}", self.state.p2p_port.expect("A port has been picked"))); |
| 87 | + |
| 88 | + Ok(cmd) |
| 89 | + } |
| 90 | + |
| 91 | + async fn request_stop(&self) -> anyhow::Result<()> { |
| 92 | + Ok(()) |
| 93 | + } |
| 94 | + |
| 95 | + async fn wait_for_init(&self, stdout_rx: &mut broadcast::Receiver::<String>, _: &mut broadcast::Receiver::<String>) -> anyhow::Result<()> { |
| 96 | + // TODO: We can find a better implementation here |
| 97 | + |
| 98 | + // Sleeping 2 seconds is always sufficient |
| 99 | + // However, this makes our tests slow |
| 100 | + tokio::time::sleep(std::time::Duration::from_secs(2)).await; |
| 101 | + |
| 102 | + // The bitcoind create tries to ping the json-rpc frequently |
| 103 | + // It will continue once a call to `getblockchaininfo` has succeeded |
| 104 | + |
| 105 | + // An other approach is to look at the logs and identify the correct port |
| 106 | + |
| 107 | + return Ok(()) |
| 108 | + } |
| 109 | +} |
0 commit comments