@@ -20,8 +20,8 @@ use std::ffi::OsStr;
20
20
use std:: net:: { Ipv4Addr , SocketAddrV4 , TcpListener } ;
21
21
use std:: path:: PathBuf ;
22
22
use std:: process:: { Child , Command , ExitStatus , Stdio } ;
23
- use std:: thread;
24
23
use std:: time:: Duration ;
24
+ use std:: { env, thread} ;
25
25
use tempfile:: TempDir ;
26
26
27
27
pub extern crate core_rpc as bitcoincore_rpc;
@@ -80,7 +80,9 @@ const LOCAL_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
80
80
81
81
/// The node configuration parameters, implements a convenient [Default] for most common use.
82
82
///
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.
84
86
///
85
87
/// Default values:
86
88
/// ```
@@ -89,6 +91,7 @@ const LOCAL_IP: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
89
91
/// conf.view_stdout = false;
90
92
/// conf.p2p = bitcoind::P2P::No;
91
93
/// conf.network = "regtest";
94
+ /// conf.tmpdir = None;
92
95
/// assert_eq!(conf, bitcoind::Conf::default());
93
96
/// ```
94
97
///
@@ -109,6 +112,13 @@ pub struct Conf<'a> {
109
112
/// Must match what specified in args without dashes, needed to locate the cookie file
110
113
/// directory with different/esoteric networks
111
114
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 > ,
112
122
}
113
123
114
124
impl Default for Conf < ' _ > {
@@ -118,6 +128,7 @@ impl Default for Conf<'_> {
118
128
view_stdout : false ,
119
129
p2p : P2P :: No ,
120
130
network : "regtest" ,
131
+ tmpdir : None ,
121
132
}
122
133
}
123
134
}
@@ -132,8 +143,14 @@ impl BitcoinD {
132
143
133
144
/// Launch the bitcoind process from the given `exe` executable with given [Conf] param
134
145
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 ( ) ;
137
154
let cookie_file = datadir. join ( conf. network ) . join ( ".cookie" ) ;
138
155
let rpc_port = get_available_port ( ) ?;
139
156
let rpc_socket = SocketAddrV4 :: new ( LOCAL_IP , rpc_port) ;
@@ -205,7 +222,7 @@ impl BitcoinD {
205
222
Ok ( BitcoinD {
206
223
process,
207
224
client,
208
- _work_dir,
225
+ _work_dir : work_dir ,
209
226
params : ConnectParams {
210
227
datadir,
211
228
cookie_file,
0 commit comments