Skip to content

Commit

Permalink
Merge pull request #803 from TheCharlatan/healthCheckArgs
Browse files Browse the repository at this point in the history
Grpc/Server: Add network args to health check
  • Loading branch information
h4sh3d authored Dec 6, 2022
2 parents 772d18e + 0ac0190 commit b4a8188
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 169 deletions.
2 changes: 1 addition & 1 deletion farcasterd.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bind_ip = "127.0.0.1"
# Electrum Server used by the Bitcoin syncer
electrum_server = "ssl://blockstream.info:700"
# Monero daemon used by the Monero syncer
monero_daemon = "http://node.monerooutreach.org:18081"
monero_daemon = "http://node.community.rino.io:18081"
# Monero Wallet RPC used by the Monero syncer
# Point to local running wallet
monero_rpc_wallet = "http://localhost:18083"
Expand Down
44 changes: 43 additions & 1 deletion src/bus/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::fmt::{self, Debug, Display, Formatter};
use std::{
fmt::{self, Debug, Display, Formatter},
str::FromStr,
};

use farcaster_core::{
blockchain::Network,
role::TradeRole,
swap::{btcxmr::PublicOffer, SwapId},
};
Expand Down Expand Up @@ -210,6 +214,30 @@ impl From<FailureCode> for rpc::FailureCode<FailureCode> {

impl rpc::FailureCodeExt for FailureCode {}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Display, From)]
pub enum HealthCheckSelector {
#[display(inner)]
Network(Network),
#[display("all")]
All,
}

impl FromStr for HealthCheckSelector {
type Err = farcaster_core::consensus::Error;
fn from_str(input: &str) -> Result<HealthCheckSelector, Self::Err> {
match Network::from_str(&input) {
Ok(n) => Ok(HealthCheckSelector::Network(n)),
Err(err) => {
if input == "all" || input == "All" {
Ok(HealthCheckSelector::All)
} else {
Err(err)
}
}
}
}
}

#[derive(Clone, Debug, Display, NetworkEncode, NetworkDecode)]
#[cfg_attr(
feature = "serde",
Expand All @@ -226,5 +254,19 @@ pub struct HealthReport {
pub monero_local_health: Health,
}

#[derive(Clone, Debug, Display, NetworkEncode, NetworkDecode)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate")
)]
#[display(ReducedHealthReport::to_yaml_string)]
pub struct ReducedHealthReport {
pub bitcoin_health: Health,
pub monero_health: Health,
}

#[cfg(feature = "serde")]
impl ToYamlString for HealthReport {}
#[cfg(feature = "serde")]
impl ToYamlString for ReducedHealthReport {}
212 changes: 128 additions & 84 deletions src/cli/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ use crate::bus::{
info::{Address, InfoMsg},
AddressSecretKey,
};
use crate::bus::{BusMsg, Failure, FailureCode, HealthReport};
use crate::bus::{
BusMsg, Failure, FailureCode, HealthCheckSelector, HealthReport, ReducedHealthReport,
};
use crate::cli::opts::CheckpointSelector;
use crate::client::Client;
use crate::syncerd::{SweepAddressAddendum, SweepBitcoinAddress, SweepMoneroAddress};
Expand Down Expand Up @@ -204,95 +206,137 @@ impl Exec for Command {
runtime.report_response_or_fail()?;
}

Command::HealthCheck => {
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Testnet),
)?;
let bitcoin_testnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
Command::HealthCheck { selector } => match selector {
HealthCheckSelector::Network(network) => {
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, network),
)?;
let bitcoin_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Mainnet),
)?;
let bitcoin_mainnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Local),
)?;
let bitcoin_local_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, network),
)?;
let monero_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Testnet),
)?;
let monero_testnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
let health = ReducedHealthReport {
bitcoin_health,
monero_health,
};
println!("{}", health);
}
HealthCheckSelector::All => {
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Testnet),
)?;
let bitcoin_testnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Mainnet),
)?;
let monero_mainnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Mainnet),
)?;
let bitcoin_mainnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Bitcoin, Network::Local),
)?;
let bitcoin_local_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Local),
)?;
let monero_local_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check".to_string(),
))
}
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Testnet),
)?;
let monero_testnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

let report = HealthReport {
bitcoin_testnet_health,
bitcoin_mainnet_health,
bitcoin_local_health,
monero_testnet_health,
monero_mainnet_health,
monero_local_health,
};
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Mainnet),
)?;
let monero_mainnet_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

println!("{}", report);
}
runtime.request_ctl(
ServiceId::Farcasterd,
CtlMsg::HealthCheck(Blockchain::Monero, Network::Local),
)?;
let monero_local_health = match runtime.response()? {
BusMsg::Ctl(CtlMsg::HealthResult(health)) => health,
_ => {
return Err(Error::Other(
"Server returned unexpected response for call health check"
.to_string(),
))
}
};

let report = HealthReport {
bitcoin_testnet_health,
bitcoin_mainnet_health,
bitcoin_local_health,
monero_testnet_health,
monero_mainnet_health,
monero_local_health,
};
println!("{}", report);
}
},

Command::Make {
network,
Expand Down
10 changes: 9 additions & 1 deletion src/cli/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use farcaster_core::{
swap::{btcxmr::PublicOffer, SwapId},
};

use crate::bus::HealthCheckSelector;

/// Command-line tool for working with Farcaster node
#[derive(Parser, Clone, PartialEq, Eq, Debug)]
#[clap(name = "swap-cli", bin_name = "swap-cli", author, version)]
Expand Down Expand Up @@ -111,7 +113,13 @@ pub enum Command {

/// Checks the health of the syncers
#[clap(aliases = &["hc"])]
HealthCheck,
HealthCheck {
#[clap(
default_value = "all",
possible_values = &["Mainnet", "mainnet", "Testnet", "testnet", "Local", "local", "all", "All"]
)]
selector: HealthCheckSelector,
},

/// Restores saved checkpoint of a swap
#[clap(aliases = &["r"])]
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::str::FromStr;
use serde::{Deserialize, Serialize};

pub const FARCASTER_MAINNET_ELECTRUM_SERVER: &str = "ssl://blockstream.info:700";
pub const FARCASTER_MAINNET_MONERO_DAEMON: &str = "http://node.monerooutreach.org:18081";
pub const FARCASTER_MAINNET_MONERO_DAEMON: &str = "http://node.community.rino.io:18081";
pub const FARCASTER_MAINNET_MONERO_RPC_WALLET: &str = "http://localhost:18083";

pub const FARCASTER_TESTNET_ELECTRUM_SERVER: &str = "ssl://blockstream.info:993";
Expand Down
22 changes: 21 additions & 1 deletion src/grpcd/proto/farcaster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,36 @@ service Farcaster {

message HealthCheckRequest {
uint32 id = 1;
HealthCheckSelector selector = 2;
}

enum HealthCheckSelector {
CHECK_ALL = 0;
CHECK_MAINNET = 1;
CHECK_TESTNET = 2;
CHECK_LOCAL = 3;
}

message HealthCheckResponse {
uint32 id = 1;
oneof health_report {
CompleteHealthReport complete_health_report = 2;
ReducedHealthReport reduced_health_report = 3;
}
}

message CompleteHealthReport {
string bitcoin_mainnet_health = 2;
string bitcoin_testnet_health = 3;
string bitcoin_local_health = 4;
string monero_mainnet_health = 5;
string monero_testnet_health = 6;
string monero_local_health = 7;
string monero_local_health = 7;
}

message ReducedHealthReport {
string bitcoin_health = 2;
string monero_health = 7;
}

message InfoRequest {
Expand Down
Loading

0 comments on commit b4a8188

Please sign in to comment.