Skip to content

Commit

Permalink
Add BitcoinNetwork enum
Browse files Browse the repository at this point in the history
  • Loading branch information
thobson88 committed Dec 20, 2024
1 parent 44b0ce5 commit 02bdd6d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
38 changes: 16 additions & 22 deletions trustchain-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions trustchain-ion/src/data.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}

Expand Down
21 changes: 15 additions & 6 deletions trustchain-ion/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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)
}
Expand All @@ -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()
}

Expand Down

0 comments on commit 02bdd6d

Please sign in to comment.