From 56ec2eb8be0cb90d99f7f091fdbd59f3dcc3ea7b Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Wed, 14 Sep 2022 18:38:04 +0200 Subject: [PATCH] Fixes statuses --- watchtower-plugin/src/lib.rs | 9 +++++-- watchtower-plugin/src/main.rs | 15 +++++++----- watchtower-plugin/src/retrier.rs | 38 +++++++++++++----------------- watchtower-plugin/src/wt_client.rs | 6 ++--- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/watchtower-plugin/src/lib.rs b/watchtower-plugin/src/lib.rs index e23563df..dc7804c6 100755 --- a/watchtower-plugin/src/lib.rs +++ b/watchtower-plugin/src/lib.rs @@ -71,9 +71,14 @@ impl TowerStatus { *self == TowerStatus::Reachable } + /// Whether the tower is unreachable or not. + pub fn is_temporary_unreachable(&self) -> bool { + *self == TowerStatus::TemporaryUnreachable + } + /// Whether the tower is unreachable or not. pub fn is_unreachable(&self) -> bool { - *self == TowerStatus::TemporaryUnreachable || *self == TowerStatus::Unreachable + *self == TowerStatus::Unreachable } /// Whether the tower is misbehaving or not. @@ -411,7 +416,7 @@ mod tests { Vec::new(), ); - assert_eq!(tower_info.status, TowerStatus::Reachable); + assert!(tower_info.status.is_reachable()); assert!(tower_info.misbehaving_proof.is_none()); } diff --git a/watchtower-plugin/src/main.rs b/watchtower-plugin/src/main.rs index 8f9226d6..74195127 100755 --- a/watchtower-plugin/src/main.rs +++ b/watchtower-plugin/src/main.rs @@ -318,9 +318,9 @@ async fn retry_tower( let tower_id = TowerId::try_from(v).map_err(|e| anyhow!(e))?; let state = plugin.state().lock().unwrap(); if let Some(tower) = state.towers.get(&tower_id) { - if tower.status == TowerStatus::TemporaryUnreachable { + if tower.status.is_temporary_unreachable() { return Err(anyhow!("{} is already being retried", tower_id)); - } else if tower.status != TowerStatus::Unreachable { + } else if !tower.status.is_unreachable() { return Err(anyhow!( "Tower status must be unreachable to manually retry", )); @@ -499,10 +499,13 @@ async fn on_commitment_revocation( let mut state = plugin.state().lock().unwrap(); state.add_pending_appointment(tower_id, &appointment); - state - .unreachable_towers - .send((tower_id, appointment.locator)) - .unwrap(); + + if status.is_temporary_unreachable() { + state + .unreachable_towers + .send((tower_id, appointment.locator)) + .unwrap(); + } } } diff --git a/watchtower-plugin/src/retrier.rs b/watchtower-plugin/src/retrier.rs index 598a05e4..cfc5afd3 100644 --- a/watchtower-plugin/src/retrier.rs +++ b/watchtower-plugin/src/retrier.rs @@ -94,7 +94,7 @@ impl RetryManager { // Notice we'll end up here after a permanent error. That is, either after finishing the backoff strategy // unsuccessfully or by manually raising such an error (like when facing a tower misbehavior) if let Some(tower) = state.towers.get_mut(&tower_id) { - if tower.status.is_unreachable() { + if tower.status.is_temporary_unreachable() { log::warn!("Setting {} as unreachable", tower_id); state.set_tower_status( tower_id, @@ -414,29 +414,25 @@ mod tests { // Wait for the elapsed time and check how the tower status changed tokio::time::sleep(Duration::from_secs(max_elapsed_time as u64 / 3)).await; - assert_eq!( - wt_client - .lock() - .unwrap() - .towers - .get(&tower_id) - .unwrap() - .status, - TowerStatus::TemporaryUnreachable - ); + assert!(wt_client + .lock() + .unwrap() + .towers + .get(&tower_id) + .unwrap() + .status + .is_temporary_unreachable()); // Wait until the task gives up and check again tokio::time::sleep(Duration::from_secs(max_elapsed_time as u64)).await; - assert_eq!( - wt_client - .lock() - .unwrap() - .towers - .get(&tower_id) - .unwrap() - .status, - TowerStatus::Unreachable - ); + assert!(wt_client + .lock() + .unwrap() + .towers + .get(&tower_id) + .unwrap() + .status + .is_unreachable()); task.abort(); } diff --git a/watchtower-plugin/src/wt_client.rs b/watchtower-plugin/src/wt_client.rs index 78a6d7f9..e42ac369 100644 --- a/watchtower-plugin/src/wt_client.rs +++ b/watchtower-plugin/src/wt_client.rs @@ -59,7 +59,7 @@ impl WTClient { let towers = dbm.load_towers(); for (tower_id, tower) in towers.iter() { - if tower.status.is_unreachable() { + if tower.status.is_temporary_unreachable() { for locator in tower.pending_appointments.iter() { unreachable_towers.send((*tower_id, *locator)).unwrap(); } @@ -734,11 +734,11 @@ mod tests { // Check data in memory let tower_summary = wt_client.towers.get(&tower_id); assert!(tower_summary.is_some()); - assert_eq!(tower_summary.unwrap().status, TowerStatus::Misbehaving); + assert!(tower_summary.unwrap().status.is_misbehaving()); // Check data in DB let loaded_info = wt_client.load_tower_info(tower_id).unwrap(); - assert_eq!(loaded_info.status, TowerStatus::Misbehaving); + assert!(loaded_info.status.is_misbehaving()); assert_eq!(loaded_info.misbehaving_proof, Some(proof)); assert!(loaded_info.appointments.contains_key(&appointment.locator)); }