Skip to content

Commit d4b5d3e

Browse files
committed
Merge remote-tracking branch 'upstream/main' into cacheLatestSyncerEvents
2 parents 3e0d535 + 5684960 commit d4b5d3e

23 files changed

Lines changed: 204 additions & 238 deletions

.github/workflows/binaries.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616

1717
env:
1818
CARGO_TERM_COLOR: always
19+
RUSTFLAGS: "-Dwarnings"
1920

2021
jobs:
2122
binaries:

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616

1717
env:
1818
CARGO_TERM_COLOR: always
19+
RUSTFLAGS: "-Dwarnings"
1920

2021
jobs:
2122
rust_format:

src/bus/types.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,22 @@ impl FromStr for HealthCheckSelector {
273273
derive(Serialize, Deserialize),
274274
serde(crate = "serde_crate")
275275
)]
276-
#[display(HealthReport::to_yaml_string)]
277-
pub struct HealthReport {
276+
#[display(DefaultHealthReport::to_yaml_string)]
277+
pub struct DefaultHealthReport {
278+
pub bitcoin_mainnet_health: Health,
279+
pub bitcoin_testnet_health: Health,
280+
pub monero_mainnet_health: Health,
281+
pub monero_testnet_health: Health,
282+
}
283+
284+
#[derive(Clone, Debug, Display, NetworkEncode, NetworkDecode)]
285+
#[cfg_attr(
286+
feature = "serde",
287+
derive(Serialize, Deserialize),
288+
serde(crate = "serde_crate")
289+
)]
290+
#[display(CompleteHealthReport::to_yaml_string)]
291+
pub struct CompleteHealthReport {
278292
pub bitcoin_mainnet_health: Health,
279293
pub bitcoin_testnet_health: Health,
280294
pub bitcoin_local_health: Health,
@@ -296,6 +310,8 @@ pub struct ReducedHealthReport {
296310
}
297311

298312
#[cfg(feature = "serde")]
299-
impl ToYamlString for HealthReport {}
313+
impl ToYamlString for DefaultHealthReport {}
314+
#[cfg(feature = "serde")]
315+
impl ToYamlString for CompleteHealthReport {}
300316
#[cfg(feature = "serde")]
301317
impl ToYamlString for ReducedHealthReport {}

src/cli/command.rs

Lines changed: 75 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ use crate::bus::{
2828
AddressSecretKey,
2929
};
3030
use crate::bus::{
31-
BusMsg, Failure, FailureCode, HealthCheckSelector, HealthReport, ReducedHealthReport,
31+
BusMsg, CompleteHealthReport, DefaultHealthReport, Failure, FailureCode, HealthCheckSelector,
32+
ReducedHealthReport,
3233
};
3334
use crate::cli::opts::CheckpointSelector;
3435
use crate::client::Client;
35-
use crate::syncerd::{SweepAddressAddendum, SweepBitcoinAddress, SweepMoneroAddress};
36+
use crate::syncerd::{Health, SweepAddressAddendum, SweepBitcoinAddress, SweepMoneroAddress};
3637
use crate::{Error, LogStyle, ServiceId};
3738

3839
impl 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+
637590
fn take_deal() -> bool {
638591
println!("Deal or No Deal? [y/n]");
639592
let mut input = [0u8; 1];

src/cli/opts.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,16 @@ pub enum Command {
104104
select: CheckpointSelector,
105105
},
106106

107-
/// Checks the health of the syncers
107+
/// Checks the health of the syncers. By default 'mainnet' and 'testnet' are checked, use the
108+
/// selector to change this behavior.
108109
#[clap(aliases = &["hc"])]
109110
HealthCheck {
110111
#[clap(
111-
default_value = "all",
112+
short,
113+
long,
112114
possible_values = &["Mainnet", "mainnet", "Testnet", "testnet", "Local", "local", "all", "All"]
113115
)]
114-
selector: HealthCheckSelector,
116+
selector: Option<HealthCheckSelector>,
115117
},
116118

117119
/// Restores saved checkpoint of a swap

src/config.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,31 +153,35 @@ impl Config {
153153
.bitcoin
154154
.get_for_network(network)
155155
.or_else(|| ArbConfig::get(arb, network))
156-
.ok_or(config::ConfigError::Message(
157-
"No configuration nor defaults founds!".to_string(),
158-
))?,
156+
.ok_or_else(|| {
157+
config::ConfigError::Message(
158+
"No configuration nor defaults founds!".to_string(),
159+
)
160+
})?,
159161
};
160162
let accordant = match acc {
161163
AccordantBlockchain::Monero => swap
162164
.monero
163165
.get_for_network(network)
164166
.or_else(|| AccConfig::get(acc, network))
165-
.ok_or(config::ConfigError::Message(
166-
"No configuration nor defaults founds!".to_string(),
167-
))?,
167+
.ok_or_else(|| {
168+
config::ConfigError::Message(
169+
"No configuration nor defaults founds!".to_string(),
170+
)
171+
})?,
168172
};
169173
Ok(ParsedSwapConfig {
170174
arbitrating,
171175
accordant,
172176
})
173177
}
174178
None => {
175-
let arbitrating = ArbConfig::get(arb, network).ok_or(
176-
config::ConfigError::Message("No defaults founds!".to_string()),
177-
)?;
178-
let accordant = AccConfig::get(acc, network).ok_or(
179-
config::ConfigError::Message("No defaults founds!".to_string()),
180-
)?;
179+
let arbitrating = ArbConfig::get(arb, network).ok_or_else(|| {
180+
config::ConfigError::Message("No defaults founds!".to_string())
181+
})?;
182+
let accordant = AccConfig::get(acc, network).ok_or_else(|| {
183+
config::ConfigError::Message("No defaults founds!".to_string())
184+
})?;
181185
Ok(ParsedSwapConfig {
182186
arbitrating,
183187
accordant,

src/databased/runtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ fn test_lmdb_state() {
697697
service_id: ServiceId::Database,
698698
};
699699
let path = std::env::current_dir().unwrap();
700-
let mut database = Database::new(path.to_path_buf()).unwrap();
700+
let mut database = Database::new(path).unwrap();
701701
database.set_checkpoint_state(&key1, &val1).unwrap();
702702
let res = database.get_checkpoint_state(&key1).unwrap();
703703
assert_eq!(val1, res);
@@ -713,7 +713,7 @@ fn test_lmdb_state() {
713713

714714
let key_info = SwapId(Uuid::new());
715715
let val_info = CheckpointEntry {
716-
swap_id: key_info.clone(),
716+
swap_id: key_info,
717717
trade_role: TradeRole::Maker,
718718
deal: Deal::from_str("Deal:Cke4ftrP5A781Vq85dgBQJNwYgBS4nuUV1LQM2fvVdFMNR4h5TrWhRR11111uMFuZTAsNgpdK8DiK11111TB9zym113GTvtvqfD1111114A4TTfFfmZoWyvpcjDBtTZCdWFSUWcRKYfEC3Y17hqaXZ3dWz11111111111111111111111111111111111111111AfZ113SEBTEspU3a").unwrap(),
719719
expected_counterparty_node_id: None,
@@ -736,7 +736,7 @@ fn test_lmdb_state() {
736736
let val_retrieved = database.get_bitcoin_address_secret_key(&addr).unwrap();
737737
assert_eq!(addr_info, val_retrieved);
738738
let addrs = database.get_all_bitcoin_addresses().unwrap();
739-
assert!(addrs.iter().find(|(a, _)| *a == addr).is_some());
739+
assert!(addrs.iter().any(|(a, _)| *a == addr));
740740
let key_pair = monero::KeyPair {
741741
spend: monero::PrivateKey::from_str(
742742
"77916d0cd56ed1920aef6ca56d8a41bac915b68e4c46a589e0956e27a7b77404",
@@ -759,7 +759,7 @@ fn test_lmdb_state() {
759759
let val_retrieved = database.get_monero_address_secret_key(&addr).unwrap();
760760
assert_eq!(addr_info, val_retrieved);
761761
let addrs = database.get_all_monero_addresses().unwrap();
762-
assert!(addrs.iter().find(|(a, _)| *a == addr).is_some());
762+
assert!(addrs.iter().any(|(a, _)| *a == addr));
763763

764764
let deal_1 = Deal::from_str("Deal:Cke4ftrP5A7MgLMaQZLZUMTC6TfkqUKBu1LQM2fvVdFMNR4gmBqNCsR11111uMFuZTAsNgpdK8DiK11111TB9zym113GTvtvqfD1111114A4TTF4h53Tv4MR6eS9sdDxV5JCH9xZcKejCqKShnphqndeeD11111111111111111111111111111111111111111AfZ113XRBtrLeA3t").unwrap();
765765
let deal_2 = Deal::from_str("Deal:Cke4ftrP5A7Km9Kmc2UDBePio1p7wM56P1LQM2fvVdFMNR4gmBqNCsR11111uMFuZTAsNgpdK8DiK11111TB9zym113GTvtvqfD1111114A4TTF4h53Tv4MR6eS9sdDxV5JCH9xZcKejCqKShnphqndeeD11111111111111111111111111111111111111111AfZ113XRBuLWyw3M").unwrap();

src/farcasterd/runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,9 @@ impl Runtime {
11191119
);
11201120

11211121
let address = bind_addr.address();
1122-
let port = bind_addr.port().ok_or(Error::Farcaster(
1123-
"listen requires the port to listen on".to_string(),
1124-
))?;
1122+
let port = bind_addr
1123+
.port()
1124+
.ok_or_else(|| Error::Farcaster("listen requires the port to listen on".to_string()))?;
11251125

11261126
debug!("Instantiating peerd...");
11271127
let child = launch(

0 commit comments

Comments
 (0)