@@ -28,6 +28,7 @@ pub extern crate bitcoincore_rpc;
28
28
pub extern crate tempfile;
29
29
pub extern crate which;
30
30
31
+ #[ derive( Debug ) ]
31
32
/// Struct representing the bitcoind process with related information
32
33
pub struct BitcoinD {
33
34
/// Process child handle, used to terminate the process when this struct is dropped
@@ -41,6 +42,7 @@ pub struct BitcoinD {
41
42
pub params : ConnectParams ,
42
43
}
43
44
45
+ #[ derive( Debug ) ]
44
46
/// The DataDir struct defining the kind of data directory the node
45
47
/// will contain. Data directory can be either persistent, or temporary.
46
48
pub enum DataDir {
@@ -225,18 +227,18 @@ impl BitcoinD {
225
227
/// Launch the bitcoind process from the given `exe` executable with default args.
226
228
///
227
229
/// Waits for the node to be ready to accept connections before returning
228
- pub fn new < S : AsRef < OsStr > > ( exe : S ) -> Result < BitcoinD , Error > {
230
+ pub fn new < S : AsRef < OsStr > > ( exe : S ) -> anyhow :: Result < BitcoinD > {
229
231
BitcoinD :: with_conf ( exe, & Conf :: default ( ) )
230
232
}
231
233
232
234
/// Launch the bitcoind process from the given `exe` executable with given [Conf] param
233
- pub fn with_conf < S : AsRef < OsStr > > ( exe : S , conf : & Conf ) -> Result < BitcoinD , Error > {
235
+ pub fn with_conf < S : AsRef < OsStr > > ( exe : S , conf : & Conf ) -> anyhow :: Result < BitcoinD > {
234
236
let tmpdir = conf
235
237
. tmpdir
236
238
. clone ( )
237
239
. or_else ( || env:: var ( "TEMPDIR_ROOT" ) . map ( PathBuf :: from) . ok ( ) ) ;
238
240
let work_dir = match ( & tmpdir, & conf. staticdir ) {
239
- ( Some ( _) , Some ( _) ) => return Err ( Error :: BothDirsSpecified ) ,
241
+ ( Some ( _) , Some ( _) ) => return Err ( Error :: BothDirsSpecified . into ( ) ) ,
240
242
( Some ( tmpdir) , None ) => DataDir :: Temporary ( TempDir :: new_in ( tmpdir) ?) ,
241
243
( None , Some ( workdir) ) => {
242
244
fs:: create_dir_all ( workdir) ?;
@@ -309,7 +311,7 @@ impl BitcoinD {
309
311
return Self :: with_conf ( exe, & conf) ;
310
312
} else {
311
313
error ! ( "early exit with: {:?}" , status) ;
312
- return Err ( Error :: EarlyExit ( status) ) ;
314
+ return Err ( Error :: EarlyExit ( status) . into ( ) ) ;
313
315
}
314
316
}
315
317
thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
@@ -381,15 +383,15 @@ impl BitcoinD {
381
383
}
382
384
383
385
/// Stop the node, waiting correct process termination
384
- pub fn stop ( & mut self ) -> Result < ExitStatus , Error > {
386
+ pub fn stop ( & mut self ) -> anyhow :: Result < ExitStatus > {
385
387
self . client . stop ( ) ?;
386
388
Ok ( self . process . wait ( ) ?)
387
389
}
388
390
389
391
#[ cfg( not( any( feature = "0_17_1" , feature = "0_18_0" , feature = "0_18_1" ) ) ) ]
390
392
/// Create a new wallet in the running node, and return an RPC client connected to the just
391
393
/// created wallet
392
- pub fn create_wallet < T : AsRef < str > > ( & self , wallet : T ) -> Result < Client , Error > {
394
+ pub fn create_wallet < T : AsRef < str > > ( & self , wallet : T ) -> anyhow :: Result < Client > {
393
395
let _ = self
394
396
. client
395
397
. create_wallet ( wallet. as_ref ( ) , None , None , None , None ) ?;
@@ -412,7 +414,7 @@ impl Drop for BitcoinD {
412
414
/// Returns a non-used local port if available.
413
415
///
414
416
/// Note there is a race condition during the time the method check availability and the caller
415
- pub fn get_available_port ( ) -> Result < u16 , Error > {
417
+ pub fn get_available_port ( ) -> anyhow :: Result < u16 > {
416
418
// using 0 as port let the system assign a port available
417
419
let t = TcpListener :: bind ( ( "127.0.0.1" , 0 ) ) ?; // 0 means the OS choose a free port
418
420
Ok ( t. local_addr ( ) . map ( |s| s. port ( ) ) ?)
@@ -431,7 +433,7 @@ impl From<bitcoincore_rpc::Error> for Error {
431
433
}
432
434
433
435
/// Provide the bitcoind executable path if a version feature has been specified
434
- pub fn downloaded_exe_path ( ) -> Result < String , Error > {
436
+ pub fn downloaded_exe_path ( ) -> anyhow :: Result < String > {
435
437
if versions:: HAS_FEATURE {
436
438
let mut path: PathBuf = env ! ( "OUT_DIR" ) . into ( ) ;
437
439
path. push ( "bitcoin" ) ;
@@ -446,25 +448,25 @@ pub fn downloaded_exe_path() -> Result<String, Error> {
446
448
447
449
Ok ( format ! ( "{}" , path. display( ) ) )
448
450
} else {
449
- Err ( Error :: NoFeature )
451
+ Err ( Error :: NoFeature . into ( ) )
450
452
}
451
453
}
452
454
453
455
/// Returns the daemon executable path if it's provided as a feature or as `BITCOIND_EXE` env var.
454
456
/// Returns error if none or both are set
455
- pub fn exe_path ( ) -> Result < String , Error > {
457
+ pub fn exe_path ( ) -> anyhow :: Result < String > {
456
458
match ( downloaded_exe_path ( ) , std:: env:: var ( "BITCOIND_EXE" ) ) {
457
- ( Ok ( _) , Ok ( _) ) => Err ( Error :: BothFeatureAndEnvVar ) ,
459
+ ( Ok ( _) , Ok ( _) ) => Err ( Error :: BothFeatureAndEnvVar . into ( ) ) ,
458
460
( Ok ( path) , Err ( _) ) => Ok ( path) ,
459
461
( Err ( _) , Ok ( path) ) => Ok ( path) ,
460
462
( Err ( _) , Err ( _) ) => which:: which ( "bitcoind" )
461
- . map_err ( |_| Error :: NoBitcoindExecutableFound )
463
+ . map_err ( |_| Error :: NoBitcoindExecutableFound . into ( ) )
462
464
. map ( |p| p. display ( ) . to_string ( ) ) ,
463
465
}
464
466
}
465
467
466
468
/// Validate the specified arg if there is any unavailable or deprecated one
467
- pub fn validate_args ( args : Vec < & str > ) -> Result < Vec < & str > , Error > {
469
+ pub fn validate_args ( args : Vec < & str > ) -> anyhow :: Result < Vec < & str > > {
468
470
args. iter ( ) . try_for_each ( |arg| {
469
471
// other kind of invalid arguments can be added into the list if needed
470
472
if INVALID_ARGS . iter ( ) . any ( |x| arg. starts_with ( x) ) {
@@ -676,7 +678,10 @@ mod test {
676
678
let bitcoind = BitcoinD :: with_conf ( exe, & conf) ;
677
679
678
680
assert ! ( bitcoind. is_err( ) ) ;
679
- assert ! ( matches!( bitcoind, Err ( Error :: RpcUserAndPasswordUsed ) ) ) ;
681
+ assert ! ( matches!(
682
+ bitcoind. unwrap_err( ) . downcast_ref( ) . unwrap( ) ,
683
+ Error :: RpcUserAndPasswordUsed
684
+ ) ) ;
680
685
}
681
686
682
687
#[ test]
0 commit comments