use core::net::SocketAddr;
use core::net::SocketAddrV4;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Child;
use std::process::Command;
use std::process::ExitStatus;
use std::process::Stdio;
use std::thread;
use std::time::Duration;
use std::time::Instant;
use corepc_client::client_sync::Auth;
use corepc_client::client_sync::v17::AddNodeCommand;
use corepc_client::client_sync::v17::Client;
use tempfile::TempDir;
use crate::DataDir;
use crate::Error;
use crate::LOCALHOST;
use crate::NODE_BUILDING_MAX_RETRIES;
use crate::get_available_port;
mod versions;
const RPC_USER: &str = "halfin";
const RPC_PASS: &str = "halfin";
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct UtreexoDConf<'a> {
pub args: Vec<&'a str>,
pub tmpdir: Option<PathBuf>,
pub staticdir: Option<PathBuf>,
pub max_retries: u8,
}
impl Default for UtreexoDConf<'_> {
fn default() -> Self {
UtreexoDConf {
args: vec![
"--regtest",
"--notls",
"--nodnsseed",
"--noassumeutreexo",
"--miningaddr=bcrt1qusgerygumpd0ztn735s5pypq6wsv2zzhuc4yak",
],
tmpdir: None,
staticdir: None,
max_retries: NODE_BUILDING_MAX_RETRIES,
}
}
}
#[derive(Debug)]
pub struct UtreexoD {
process: Child,
rpc_client: Client,
working_directory: DataDir,
rpc_socket: SocketAddr,
p2p_socket: SocketAddr,
}
impl Drop for UtreexoD {
fn drop(&mut self) {
let _ = self.process.kill();
}
}
impl UtreexoD {
pub fn download_new() -> Result<UtreexoD, Error> {
UtreexoD::from_bin(get_utreexod_path()?)
}
pub fn from_downloaded_with_conf(conf: &UtreexoDConf) -> Result<UtreexoD, Error> {
UtreexoD::from_bin_with_conf(get_utreexod_path()?, conf)
}
pub fn from_bin<P: AsRef<Path>>(utreexod_bin: P) -> Result<UtreexoD, Error> {
UtreexoD::from_bin_with_conf(utreexod_bin, &UtreexoDConf::default())
}
pub fn from_bin_with_conf<P: AsRef<Path>>(
utreexod_bin: P,
conf: &UtreexoDConf,
) -> Result<UtreexoD, Error> {
for _attempt in 0..conf.max_retries {
let working_directory = Self::init_work_dir(conf)?;
let rpc_port = get_available_port();
let rpc_socket = SocketAddr::V4(SocketAddrV4::new(LOCALHOST, rpc_port));
let rpc_url = format!("http://{}", rpc_socket);
let p2p_port = get_available_port();
let p2p_socket = SocketAddr::V4(SocketAddrV4::new(LOCALHOST, p2p_port));
let datadir_arg = format!("--datadir={}", working_directory.path().display());
let rpclisten_arg = format!("--rpclisten=127.0.0.1:{}", rpc_port);
let rpcuser_arg = format!("--rpcuser={}", RPC_USER);
let rpcpass_arg = format!("--rpcpass={}", RPC_PASS);
let listen_arg = format!("--listen=127.0.0.1:{}", p2p_port);
let mut process = Command::new(utreexod_bin.as_ref())
.args(&conf.args)
.arg(&datadir_arg)
.arg(&rpclisten_arg)
.arg(&rpcuser_arg)
.arg(&rpcpass_arg)
.arg(&listen_arg)
.arg("--flatutreexoproofindex")
.arg("--utreexoproofindexmaxmemory=512")
.arg("--v2transport")
.stdout(Stdio::null())
.spawn()
.map_err(Error::FailedToSpawn)?;
thread::sleep(Duration::from_millis(100));
match process.try_wait() {
Ok(Some(_)) | Err(_) => {
let _ = process.kill();
continue;
}
Ok(None) => {}
}
let auth = Auth::UserPass(RPC_USER.to_string(), RPC_PASS.to_string());
match Self::wait_for_client(&rpc_url, &auth, Duration::from_secs(10)) {
Ok(rpc_client) => {
return Ok(UtreexoD {
process,
rpc_client,
working_directory,
rpc_socket,
p2p_socket,
});
}
Err(_) => {
let _ = process.kill();
continue;
}
}
}
Err(Error::ExhaustedNodeBuildingRetries)
}
pub fn stop(&mut self) -> Result<ExitStatus, Error> {
let _ = self.rpc_client.stop().map_err(Error::FailedToStop)?;
let exit_status = self.process.wait().map_err(Error::Io)?;
Ok(exit_status)
}
pub fn get_pid(&self) -> u32 {
self.process.id()
}
pub fn get_working_directory(&self) -> PathBuf {
self.working_directory.path()
}
pub fn get_rpc_client(&self) -> &Client {
&self.rpc_client
}
pub fn get_p2p_socket(&self) -> SocketAddr {
self.p2p_socket
}
pub fn rpc_socket(&self) -> SocketAddr {
self.rpc_socket
}
pub fn get_height(&self) -> Result<u32, Error> {
let height = self
.rpc_client
.call::<serde_json::Value>("getblockchaininfo", &[])
.map_err(Error::JsonRpc)?["blocks"]
.as_u64()
.ok_or(Error::UnexpectedResponse)? as u32;
Ok(height)
}
pub fn add_peer(&self, socket: SocketAddr) -> Result<(), Error> {
self.rpc_client
.add_node(&socket.to_string(), AddNodeCommand::Add)
.map_err(Error::JsonRpc)?;
let mut delay = Duration::from_millis(100);
let timeout = Duration::from_secs(5);
let start = Instant::now();
while start.elapsed() < timeout {
let peers = self
.rpc_client
.call::<serde_json::Value>("getpeerinfo", &[])
.map_err(Error::JsonRpc)?;
if peers
.as_array()
.map(|v| {
v.iter().any(|p| {
p["addr"]
.as_str()
.map(|a| a.contains(&socket.to_string()))
.unwrap_or(false)
})
})
.unwrap_or(false)
{
return Ok(());
}
thread::sleep(delay);
delay = (delay * 2).min(Duration::from_secs(1));
}
Err(Error::PeerConnectionTimeout((
self.get_p2p_socket(),
socket,
)))
}
pub fn get_peer_count(&self) -> Result<u32, Error> {
let peers = self
.rpc_client
.call::<serde_json::Value>("getpeerinfo", &[])
.map_err(Error::JsonRpc)?;
let peer_count = peers.as_array().ok_or(Error::UnexpectedResponse)?.len() as u32;
Ok(peer_count)
}
pub fn generate(&self, count: u32) -> Result<(), Error> {
self.rpc_client
.call::<serde_json::Value>("generate", &[serde_json::to_value(count).unwrap()])
.map_err(Error::JsonRpc)?;
Ok(())
}
fn init_work_dir(conf: &UtreexoDConf) -> Result<DataDir, Error> {
let tmpdir = conf
.tmpdir
.clone()
.or_else(|| env::var("TEMPDIR_ROOT").map(PathBuf::from).ok());
let work_dir = match (&tmpdir, &conf.staticdir) {
(Some(_), Some(_)) => return Err(Error::BothDirsSpecified),
(None, Some(workdir)) => {
fs::create_dir_all(workdir).map_err(Error::Io)?;
DataDir::Persistent(workdir.to_owned())
}
(Some(tmpdir), None) => DataDir::Temporary(TempDir::new_in(tmpdir).map_err(Error::Io)?),
(None, None) => DataDir::Temporary(TempDir::new().map_err(Error::Io)?),
};
Ok(work_dir)
}
fn wait_for_client(rpc_url: &str, auth: &Auth, timeout: Duration) -> Result<Client, Error> {
let start = Instant::now();
while start.elapsed() < timeout {
if let Ok(client) = Client::new_with_auth(rpc_url, auth.clone()) {
if client
.call::<serde_json::Value>("getblockchaininfo", &[])
.is_ok()
{
return Ok(client);
}
}
thread::sleep(Duration::from_millis(200));
}
Err(Error::RpcClientSetupTimeout)
}
}
pub fn get_utreexod_path() -> Result<PathBuf, Error> {
use versions::UTREEXOD_VERSION;
let mut bin_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("bin");
bin_path.push(format!("utreexod-{}", UTREEXOD_VERSION));
bin_path.push("utreexod");
match bin_path.exists() {
true => Ok(bin_path),
false => Err(Error::BinaryNotFound(bin_path)),
}
}
#[cfg(test)]
mod test {
use crate::wait_for_height;
use super::*;
#[test]
fn test_utreexod_starts() {
let bin = get_utreexod_path().unwrap();
let utreexod = UtreexoD::from_bin(bin).unwrap();
println!("PID: {}", utreexod.get_pid());
println!("Working Directory: {:?}", utreexod.get_working_directory());
println!("P2P Socket: {}", utreexod.get_p2p_socket());
}
#[test]
fn test_utreexod_generate() {
let utreexod = UtreexoD::download_new().unwrap();
let height = utreexod.get_height().unwrap();
assert_eq!(height, 0);
utreexod.generate(10).unwrap();
let height = utreexod.get_height().unwrap();
assert_eq!(height, 10);
}
#[test]
fn test_utreexod_addnode() {
let utreexod_alpha = UtreexoD::download_new().unwrap();
let utreexod_beta = UtreexoD::download_new().unwrap();
assert_eq!(utreexod_alpha.get_peer_count().unwrap(), 0);
assert_eq!(utreexod_beta.get_peer_count().unwrap(), 0);
utreexod_beta
.add_peer(utreexod_alpha.get_p2p_socket())
.unwrap();
assert_eq!(utreexod_alpha.get_peer_count().unwrap(), 1);
assert_eq!(utreexod_beta.get_peer_count().unwrap(), 1);
}
#[test]
fn test_utreexod_blocks_propagate() {
let utreexod_alpha = UtreexoD::download_new().unwrap();
let utreexod_beta = UtreexoD::download_new().unwrap();
utreexod_alpha.generate(21).unwrap();
assert_eq!(utreexod_alpha.get_height().unwrap(), 21);
assert_eq!(utreexod_beta.get_height().unwrap(), 0);
utreexod_alpha
.add_peer(utreexod_beta.get_p2p_socket())
.unwrap();
wait_for_height(&utreexod_beta, 21).unwrap();
assert_eq!(utreexod_beta.get_height().unwrap(), 21);
utreexod_beta.generate(21).unwrap();
wait_for_height(&utreexod_alpha, 42).unwrap();
assert_eq!(utreexod_alpha.get_height().unwrap(), 42);
}
}