Skip to content

Commit

Permalink
Fixes statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-gi committed Sep 14, 2022
1 parent cfdd3fb commit 56ec2eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
9 changes: 7 additions & 2 deletions watchtower-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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());
}

Expand Down
15 changes: 9 additions & 6 deletions watchtower-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
));
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
38 changes: 17 additions & 21 deletions watchtower-plugin/src/retrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions watchtower-plugin/src/wt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 56ec2eb

Please sign in to comment.