diff --git a/trustchain-cli/src/lib.rs b/trustchain-cli/src/lib.rs index 40479de9..37a2fdd7 100644 --- a/trustchain-cli/src/lib.rs +++ b/trustchain-cli/src/lib.rs @@ -8,44 +8,38 @@ pub mod config; /// Prints the current status of ION and its dependencies. pub async fn print_status() { - let bitcoind_status = bitcoind_status().await; - let mut is_mainnet = false; - if let BitcoindStatus::Ok(x) = bitcoind_status { - is_mainnet = x; - } - let str = "IPFS......... ".to_string(); let msg = Some("IPFS daemon not found"); println!("{}", status_str(str, ipfs_ok().await, msg)); - let str = "MongoDB...... ".to_string(); - let msg = Some("Mongo daemon not found"); - println!("{}", status_str(str, mongodb_ok(is_mainnet).await, msg)); - - let str = "Bitcoin...... ".to_string(); + // Check bitcoind status to determine network (mainnet or testnet). + let bitcoind_status = bitcoind_status().await; + let bitcoin_str = "Bitcoin...... ".to_string(); match bitcoind_status { - BitcoindStatus::Ok(_) => { - println!("{}", status_str(str, true, None)); + BitcoindStatus::Ok(network) => { + let mongo_str = "MongoDB...... ".to_string(); + let msg = Some("Mongo daemon not found"); + println!("{}", status_str(mongo_str, mongodb_ok(&network).await, msg)); + + println!("{}", status_str(bitcoin_str, true, None)); + + let ion_str = "ION.......... ".to_string(); + let msg = Some("ION DID resolution attempt failed"); + let is_ok = ion_ok(&network, cli_config().ion_endpoint.port).await; + println!("{}", status_str(ion_str, is_ok, msg)); } BitcoindStatus::Synching(blocks, headers) => { let msg = Some(format!("Synching blocks: {}/{}", blocks, headers)); - println!("{}", status_str(str, false, msg.as_deref())); + println!("{}", status_str(bitcoin_str, false, msg.as_deref())); } BitcoindStatus::Error(e) => { let msg = match e { err @ TrustchainBitcoinError::BitcoinCoreRPCError(_) => err.to_string(), _ => "Bitcoin RPC returned an error".to_string(), }; - println!("{}", status_str(str, false, Some(&msg))); + println!("{}", status_str(bitcoin_str, false, Some(&msg))); } }; - - let str = "ION.......... ".to_string(); - let msg = Some("ION DID resolution attempt failed"); - let is_ok = ion_ok(is_mainnet, cli_config().ion_endpoint.port).await; - println!("{}", status_str(str, is_ok, msg)); - - // TODO: check trustchain-http server status (report only if positive). } pub fn status_str(mut str: String, is_ok: bool, details: Option<&str>) -> String { diff --git a/trustchain-ion/src/data.rs b/trustchain-ion/src/data.rs index 9fec579d..f181e3eb 100644 --- a/trustchain-ion/src/data.rs +++ b/trustchain-ion/src/data.rs @@ -1,16 +1,18 @@ //! Test fixtures for crate. #![allow(dead_code)] +use crate::utils::BitcoinNetwork; + pub(crate) const SAMPLE_CID: &str = "QmRvgZm4J3JSxfk4wRjE2u2Hi2U7VmobYnpqhqH5QP6J97"; pub(crate) const SAMPLE_DID_TESTNET: &str = "did:ion:test:EiCClfEdkTv_aM3UnBBhlOV89LlGhpQAbfeZLFdFxVFkEg"; pub(crate) const SAMPLE_DID_MAINNET: &str = "did:ion:EiClkZMDxPKqC9c-umQfTkR8vvZ9JPhl_xLDI9Nfk38w5w"; -pub fn sample_did(is_mainnet: bool) -> String { - match is_mainnet { - true => SAMPLE_DID_MAINNET.to_string(), - false => SAMPLE_DID_TESTNET.to_string(), +pub fn sample_did(network: &BitcoinNetwork) -> String { + match network { + BitcoinNetwork::Mainnet => SAMPLE_DID_MAINNET.to_string(), + BitcoinNetwork::Testnet3 => SAMPLE_DID_TESTNET.to_string(), } } diff --git a/trustchain-ion/src/utils.rs b/trustchain-ion/src/utils.rs index e8e73464..cd7f9b06 100644 --- a/trustchain-ion/src/utils.rs +++ b/trustchain-ion/src/utils.rs @@ -409,9 +409,15 @@ pub fn block_height_range_on_date( Ok((first_block, last_block)) } +#[derive(Debug, Clone)] +pub enum BitcoinNetwork { + Mainnet, + Testnet3, +} + #[derive(Debug)] pub enum BitcoindStatus { - Ok(bool), + Ok(BitcoinNetwork), Synching(u64, u64), Error(TrustchainBitcoinError), } @@ -424,7 +430,10 @@ pub async fn bitcoind_status() -> BitcoindStatus { } let info = info.unwrap(); if info.blocks == info.headers { - return BitcoindStatus::Ok(info.chain == "main"); + if info.chain == "main" { + return BitcoindStatus::Ok(BitcoinNetwork::Mainnet); + } + return BitcoindStatus::Ok(BitcoinNetwork::Testnet3); } BitcoindStatus::Synching(info.blocks, info.headers) } @@ -435,16 +444,16 @@ pub async fn ipfs_ok() -> bool { } /// Returns true if the MongoDB daemon is running on the expected port. -pub async fn mongodb_ok(is_mainnet: bool) -> bool { - query_mongodb(get_did_suffix(&sample_did(is_mainnet))) +pub async fn mongodb_ok(network: &BitcoinNetwork) -> bool { + query_mongodb(get_did_suffix(&sample_did(network))) .await .is_ok() } /// Returns true if the ION Core microservice is running on the expected port. -pub async fn ion_ok(is_mainnet: bool, ion_port: u16) -> bool { +pub async fn ion_ok(network: &BitcoinNetwork, ion_port: u16) -> bool { let resolver = trustchain_resolver(&format!("http://localhost:{}/", ion_port)); - let result = resolver.resolve_as_result(&sample_did(is_mainnet)).await; + let result = resolver.resolve_as_result(&sample_did(network)).await; result.is_ok() }