@@ -28,11 +28,12 @@ use crate::bus::{
2828 AddressSecretKey ,
2929} ;
3030use crate :: bus:: {
31- BusMsg , Failure , FailureCode , HealthCheckSelector , HealthReport , ReducedHealthReport ,
31+ BusMsg , CompleteHealthReport , DefaultHealthReport , Failure , FailureCode , HealthCheckSelector ,
32+ ReducedHealthReport ,
3233} ;
3334use crate :: cli:: opts:: CheckpointSelector ;
3435use crate :: client:: Client ;
35- use crate :: syncerd:: { SweepAddressAddendum , SweepBitcoinAddress , SweepMoneroAddress } ;
36+ use crate :: syncerd:: { Health , SweepAddressAddendum , SweepBitcoinAddress , SweepMoneroAddress } ;
3637use crate :: { Error , LogStyle , ServiceId } ;
3738
3839impl Exec for Command {
@@ -197,135 +198,66 @@ impl Exec for Command {
197198 runtime. report_response_or_fail ( ) ?;
198199 }
199200
200- Command :: HealthCheck { selector } => match selector {
201- HealthCheckSelector :: Network ( network) => {
202- runtime. request_ctl (
203- ServiceId :: Farcasterd ,
204- CtlMsg :: HealthCheck ( Blockchain :: Bitcoin , network) ,
205- ) ?;
206- let bitcoin_health = match runtime. response ( ) ? {
207- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
208- _ => {
209- return Err ( Error :: Other (
210- "Server returned unexpected response for call health check"
211- . to_string ( ) ,
212- ) )
213- }
214- } ;
201+ Command :: HealthCheck { ref selector } => match selector {
202+ // no selector, check only mainnet and testnet
203+ None => {
204+ use Blockchain :: * ;
205+ use Network :: * ;
215206
216- runtime. request_ctl (
217- ServiceId :: Farcasterd ,
218- CtlMsg :: HealthCheck ( Blockchain :: Monero , network) ,
219- ) ?;
220- let monero_health = match runtime. response ( ) ? {
221- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
222- _ => {
223- return Err ( Error :: Other (
224- "Server returned unexpected response for call health check"
225- . to_string ( ) ,
226- ) )
227- }
228- } ;
207+ let bitcoin_testnet_health = self . check_health ( runtime, Bitcoin , Testnet ) ?;
208+ let bitcoin_mainnet_health = self . check_health ( runtime, Bitcoin , Mainnet ) ?;
229209
230- let health = ReducedHealthReport {
231- bitcoin_health,
232- monero_health,
233- } ;
234- println ! ( "{}" , health) ;
235- }
236- HealthCheckSelector :: All => {
237- runtime. request_ctl (
238- ServiceId :: Farcasterd ,
239- CtlMsg :: HealthCheck ( Blockchain :: Bitcoin , Network :: Testnet ) ,
240- ) ?;
241- let bitcoin_testnet_health = match runtime. response ( ) ? {
242- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
243- _ => {
244- return Err ( Error :: Other (
245- "Server returned unexpected response for call health check"
246- . to_string ( ) ,
247- ) )
248- }
249- } ;
210+ let monero_testnet_health = self . check_health ( runtime, Monero , Testnet ) ?;
211+ let monero_mainnet_health = self . check_health ( runtime, Monero , Mainnet ) ?;
250212
251- runtime. request_ctl (
252- ServiceId :: Farcasterd ,
253- CtlMsg :: HealthCheck ( Blockchain :: Bitcoin , Network :: Mainnet ) ,
254- ) ?;
255- let bitcoin_mainnet_health = match runtime. response ( ) ? {
256- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
257- _ => {
258- return Err ( Error :: Other (
259- "Server returned unexpected response for call health check"
260- . to_string ( ) ,
261- ) )
262- }
263- } ;
264- runtime. request_ctl (
265- ServiceId :: Farcasterd ,
266- CtlMsg :: HealthCheck ( Blockchain :: Bitcoin , Network :: Local ) ,
267- ) ?;
268- let bitcoin_local_health = match runtime. response ( ) ? {
269- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
270- _ => {
271- return Err ( Error :: Other (
272- "Server returned unexpected response for call health check"
273- . to_string ( ) ,
274- ) )
213+ println ! (
214+ "{}" ,
215+ DefaultHealthReport {
216+ bitcoin_testnet_health,
217+ bitcoin_mainnet_health,
218+ monero_testnet_health,
219+ monero_mainnet_health,
275220 }
276- } ;
221+ ) ;
222+ }
223+ // user selected a specific network
224+ Some ( HealthCheckSelector :: Network ( network) ) => {
225+ use Blockchain :: * ;
277226
278- runtime. request_ctl (
279- ServiceId :: Farcasterd ,
280- CtlMsg :: HealthCheck ( Blockchain :: Monero , Network :: Testnet ) ,
281- ) ?;
282- let monero_testnet_health = match runtime. response ( ) ? {
283- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
284- _ => {
285- return Err ( Error :: Other (
286- "Server returned unexpected response for call health check"
287- . to_string ( ) ,
288- ) )
227+ let bitcoin_health = self . check_health ( runtime, Bitcoin , * network) ?;
228+ let monero_health = self . check_health ( runtime, Monero , * network) ?;
229+ println ! (
230+ "{}" ,
231+ ReducedHealthReport {
232+ bitcoin_health,
233+ monero_health,
289234 }
290- } ;
235+ ) ;
236+ }
237+ // check all networks
238+ Some ( HealthCheckSelector :: All ) => {
239+ use Blockchain :: * ;
240+ use Network :: * ;
291241
292- runtime. request_ctl (
293- ServiceId :: Farcasterd ,
294- CtlMsg :: HealthCheck ( Blockchain :: Monero , Network :: Mainnet ) ,
295- ) ?;
296- let monero_mainnet_health = match runtime. response ( ) ? {
297- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
298- _ => {
299- return Err ( Error :: Other (
300- "Server returned unexpected response for call health check"
301- . to_string ( ) ,
302- ) )
303- }
304- } ;
242+ let bitcoin_testnet_health = self . check_health ( runtime, Bitcoin , Testnet ) ?;
243+ let bitcoin_mainnet_health = self . check_health ( runtime, Bitcoin , Mainnet ) ?;
244+ let bitcoin_local_health = self . check_health ( runtime, Bitcoin , Local ) ?;
305245
306- runtime. request_ctl (
307- ServiceId :: Farcasterd ,
308- CtlMsg :: HealthCheck ( Blockchain :: Monero , Network :: Local ) ,
309- ) ?;
310- let monero_local_health = match runtime. response ( ) ? {
311- BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => health,
312- _ => {
313- return Err ( Error :: Other (
314- "Server returned unexpected response for call health check"
315- . to_string ( ) ,
316- ) )
246+ let monero_testnet_health = self . check_health ( runtime, Monero , Testnet ) ?;
247+ let monero_mainnet_health = self . check_health ( runtime, Monero , Mainnet ) ?;
248+ let monero_local_health = self . check_health ( runtime, Monero , Local ) ?;
249+
250+ println ! (
251+ "{}" ,
252+ CompleteHealthReport {
253+ bitcoin_testnet_health,
254+ bitcoin_mainnet_health,
255+ bitcoin_local_health,
256+ monero_testnet_health,
257+ monero_mainnet_health,
258+ monero_local_health,
317259 }
318- } ;
319-
320- let report = HealthReport {
321- bitcoin_testnet_health,
322- bitcoin_mainnet_health,
323- bitcoin_local_health,
324- monero_testnet_health,
325- monero_mainnet_health,
326- monero_local_health,
327- } ;
328- println ! ( "{}" , report) ;
260+ ) ;
329261 }
330262 } ,
331263
@@ -634,6 +566,27 @@ impl Exec for Command {
634566 }
635567}
636568
569+ impl Command {
570+ /// Check syncer (coin, net) health via farcasterd and return a [`Health`] result
571+ fn check_health (
572+ & self ,
573+ runtime : & mut Client ,
574+ blockchain : Blockchain ,
575+ network : Network ,
576+ ) -> Result < Health , Error > {
577+ runtime. request_ctl (
578+ ServiceId :: Farcasterd ,
579+ CtlMsg :: HealthCheck ( blockchain, network) ,
580+ ) ?;
581+ match runtime. response ( ) ? {
582+ BusMsg :: Ctl ( CtlMsg :: HealthResult ( health) ) => Ok ( health) ,
583+ _ => Err ( Error :: Other (
584+ "Server returned unexpected response for call health check" . to_string ( ) ,
585+ ) ) ,
586+ }
587+ }
588+ }
589+
637590fn take_deal ( ) -> bool {
638591 println ! ( "Deal or No Deal? [y/n]" ) ;
639592 let mut input = [ 0u8 ; 1 ] ;
0 commit comments