|
| 1 | +#![warn(missing_docs)] |
| 2 | + |
| 3 | +//! |
| 4 | +//! Bitcoind |
| 5 | +//! |
| 6 | +//! Utility to run a regtest bitcoind process, useful in integration testing environment |
| 7 | +//! |
| 8 | +//! ```no_run |
| 9 | +//! use bitcoincore_rpc::RpcApi; |
| 10 | +//! let bitcoind = bitcoind::BitcoinD::new("/usr/local/bin/bitcoind").unwrap(); |
| 11 | +//! assert_eq!(0, bitcoind.client.get_blockchain_info().unwrap().blocks); |
| 12 | +//! ``` |
| 13 | +
|
| 14 | +use bitcoincore_rpc::{Auth, Client, RpcApi}; |
| 15 | +use std::ffi::OsStr; |
| 16 | +use std::net::TcpListener; |
| 17 | +use std::path::PathBuf; |
| 18 | +use std::process::{Child, Command, ExitStatus}; |
| 19 | +use std::thread; |
| 20 | +use std::time::Duration; |
| 21 | +use tempfile::TempDir; |
| 22 | + |
| 23 | +/// Struct representing the bitcoind process with related information |
| 24 | +pub struct BitcoinD { |
| 25 | + /// Process child handle, used to terminate the process when this struct is dropped |
| 26 | + process: Child, |
| 27 | + /// Rpc client linked to this bitcoind process |
| 28 | + pub client: Client, |
| 29 | + /// Work directory, where the node store blocks and other stuff. It is kept in the struct so that |
| 30 | + /// directory is deleted only when this struct is dropped |
| 31 | + _work_dir: TempDir, |
| 32 | + /// Path to the node cookie file, useful for other client to connect to the node |
| 33 | + pub cookie_file: PathBuf, |
| 34 | + /// Url of the rpc of the node, useful for other client to connect to the node |
| 35 | + pub url: String, |
| 36 | +} |
| 37 | + |
| 38 | +/// All the possible error in this crate |
| 39 | +#[derive(Debug)] |
| 40 | +pub enum Error { |
| 41 | + /// No port available on the system |
| 42 | + PortUnavailable, |
| 43 | + /// Wrapper of io Error |
| 44 | + Io(std::io::Error), |
| 45 | + /// Wrapper of bitcoincore_rpc Error |
| 46 | + Rpc(bitcoincore_rpc::Error), |
| 47 | +} |
| 48 | + |
| 49 | +impl BitcoinD { |
| 50 | + /// Launch the bitcoind process from the given `exe` executable. |
| 51 | + /// Waits for the node to be ready before returning |
| 52 | + pub fn new<S: AsRef<OsStr>>(exe: S) -> Result<BitcoinD, Error> { |
| 53 | + let _work_dir = TempDir::new()?; |
| 54 | + let cookie_file = _work_dir.path().join("regtest").join(".cookie"); |
| 55 | + let rpc_port = get_available_port().ok_or(Error::PortUnavailable)?; |
| 56 | + let url = format!("http://127.0.0.1:{}", rpc_port); |
| 57 | + |
| 58 | + let process = Command::new(exe) |
| 59 | + .arg(format!("-datadir={}", _work_dir.path().display())) |
| 60 | + .arg(format!("-rpcport={}", rpc_port)) |
| 61 | + .arg("-regtest") |
| 62 | + .arg("-listen=0") // do not connect to p2p |
| 63 | + .arg("-fallbackfee=0.0001") |
| 64 | + .spawn()?; |
| 65 | + |
| 66 | + let node_url_default = format!("{}/wallet/default", url); |
| 67 | + // wait bitcoind is ready, use default wallet |
| 68 | + let client = loop { |
| 69 | + thread::sleep(Duration::from_millis(500)); |
| 70 | + assert!(process.stderr.is_none()); |
| 71 | + let client_result = Client::new(url.clone(), Auth::CookieFile(cookie_file.clone())); |
| 72 | + if let Ok(client_base) = client_result { |
| 73 | + if client_base.get_blockchain_info().is_ok() { |
| 74 | + client_base |
| 75 | + .create_wallet("default", None, None, None, None) |
| 76 | + .unwrap(); |
| 77 | + break Client::new(node_url_default, Auth::CookieFile(cookie_file.clone())) |
| 78 | + .unwrap(); |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + Ok(BitcoinD { |
| 84 | + process, |
| 85 | + client, |
| 86 | + _work_dir, |
| 87 | + cookie_file, |
| 88 | + url, |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + /// Stop the node, waiting correct process termination |
| 93 | + pub fn stop(&mut self) -> Result<ExitStatus, Error> { |
| 94 | + self.client.stop()?; |
| 95 | + Ok(self.process.wait()?) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl Drop for BitcoinD { |
| 100 | + fn drop(&mut self) { |
| 101 | + let _ = self.process.kill(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn get_available_port() -> Option<u16> { |
| 106 | + (1025..65535).find(|port| TcpListener::bind(("127.0.0.1", *port)).is_ok()) |
| 107 | +} |
| 108 | + |
| 109 | +impl From<std::io::Error> for Error { |
| 110 | + fn from(e: std::io::Error) -> Self { |
| 111 | + Error::Io(e) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +impl From<bitcoincore_rpc::Error> for Error { |
| 116 | + fn from(e: bitcoincore_rpc::Error) -> Self { |
| 117 | + Error::Rpc(e) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(test)] |
| 122 | +mod test { |
| 123 | + use crate::BitcoinD; |
| 124 | + use bitcoincore_rpc::RpcApi; |
| 125 | + use std::env; |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_bitcoind() { |
| 129 | + let exe = env::var("BITCOIND_EXE").expect("BITCOIND_EXE env var must be set"); |
| 130 | + let bitcoind = BitcoinD::new(exe).unwrap(); |
| 131 | + let info = bitcoind.client.get_blockchain_info().unwrap(); |
| 132 | + assert_eq!(0, info.blocks); |
| 133 | + let address = bitcoind.client.get_new_address(None, None).unwrap(); |
| 134 | + let _ = bitcoind.client.generate_to_address(1, &address).unwrap(); |
| 135 | + let info = bitcoind.client.get_blockchain_info().unwrap(); |
| 136 | + assert_eq!(1, info.blocks); |
| 137 | + } |
| 138 | +} |
0 commit comments