Skip to content

Commit b916441

Browse files
committed
Allow to specify temp root directory
1 parent b506528 commit b916441

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
Cargo.lock
33
.idea/
4+
bin/

src/lib.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use std::ffi::OsStr;
2020
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
2121
use std::path::PathBuf;
2222
use std::process::{Child, Command, ExitStatus, Stdio};
23-
use std::thread;
2423
use std::time::Duration;
24+
use std::{env, thread};
2525
use tempfile::TempDir;
2626

2727
pub extern crate core_rpc as bitcoincore_rpc;
@@ -80,7 +80,9 @@ const LOCAL_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
8080

8181
/// The node configuration parameters, implements a convenient [Default] for most common use.
8282
///
83-
/// `#[non_exhaustive]` allows adding new parameters without breaking downstream users
83+
/// `#[non_exhaustive]` allows adding new parameters without breaking downstream users.
84+
/// Users cannot instantiate the struct directly, they need to create it via the `default()` method
85+
/// and mutate fields according to their preference.
8486
///
8587
/// Default values:
8688
/// ```
@@ -89,6 +91,7 @@ const LOCAL_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
8991
/// conf.view_stdout = false;
9092
/// conf.p2p = bitcoind::P2P::No;
9193
/// conf.network = "regtest";
94+
/// conf.tmpdir = None;
9295
/// assert_eq!(conf, bitcoind::Conf::default());
9396
/// ```
9497
///
@@ -109,6 +112,13 @@ pub struct Conf<'a> {
109112
/// Must match what specified in args without dashes, needed to locate the cookie file
110113
/// directory with different/esoteric networks
111114
pub network: &'a str,
115+
116+
/// Optionally specify the root of where the temporary directories will be created.
117+
/// If none and the env var `TEMPDIR_ROOT` is set, the env var is used.
118+
/// If none and the env var `TEMPDIR_ROOT` is not set, the default temp dir of the OS is used.
119+
/// It may be useful for example to set to a ramdisk so that bitcoin nodes spawn very fast
120+
/// because their datadirs are in RAM
121+
pub tmpdir: Option<PathBuf>,
112122
}
113123

114124
impl Default for Conf<'_> {
@@ -118,6 +128,7 @@ impl Default for Conf<'_> {
118128
view_stdout: false,
119129
p2p: P2P::No,
120130
network: "regtest",
131+
tmpdir: None,
121132
}
122133
}
123134
}
@@ -132,8 +143,14 @@ impl BitcoinD {
132143

133144
/// Launch the bitcoind process from the given `exe` executable with given [Conf] param
134145
pub fn with_conf<S: AsRef<OsStr>>(exe: S, conf: &Conf) -> Result<BitcoinD, Error> {
135-
let _work_dir = TempDir::new()?;
136-
let datadir = _work_dir.path().to_path_buf();
146+
let work_dir = match &conf.tmpdir {
147+
Some(path) => TempDir::new_in(path),
148+
None => match env::var("TEMPDIR_ROOT") {
149+
Ok(env_path) => TempDir::new_in(env_path),
150+
Err(_) => TempDir::new(),
151+
},
152+
}?;
153+
let datadir = work_dir.path().to_path_buf();
137154
let cookie_file = datadir.join(conf.network).join(".cookie");
138155
let rpc_port = get_available_port()?;
139156
let rpc_socket = SocketAddrV4::new(LOCAL_IP, rpc_port);
@@ -205,7 +222,7 @@ impl BitcoinD {
205222
Ok(BitcoinD {
206223
process,
207224
client,
208-
_work_dir,
225+
_work_dir: work_dir,
209226
params: ConnectParams {
210227
datadir,
211228
cookie_file,

0 commit comments

Comments
 (0)