Skip to content

Commit

Permalink
Fix #84
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-gi committed Aug 29, 2022
1 parent f2e99fa commit 75bb616
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cln-plugin.yaml
Expand Up @@ -61,4 +61,4 @@ jobs:
- name: Run tests
run: |
cd watchtower-plugin/tests
DEVELOPER=1 SLOW_MACHINE=1 poetry run pytest test.py -s
DEVELOPER=1 SLOW_MACHINE=1 poetry run pytest test.py --log-cli-level=INFO -s
38 changes: 37 additions & 1 deletion watchtower-plugin/src/dbm.rs
Expand Up @@ -405,6 +405,22 @@ impl DBM {
appointments
}

/// Loads an appointment from the database.
pub fn load_appointment(&self, locator: Locator) -> Result<Appointment, Error> {
let mut stmt = self
.connection
.prepare("SELECT encrypted_blob, to_self_delay FROM appointments WHERE locator = ?")
.unwrap();

stmt.query_row(params![locator.to_vec()], |row| {
let encrypted_blob = row.get::<_, Vec<u8>>(0).unwrap();
let to_self_delay = row.get::<_, u32>(1).unwrap();

Ok(Appointment::new(locator, encrypted_blob, to_self_delay))
})
.map_err(|_| Error::NotFound)
}

/// Stores an appointment into the database.
///
/// Appointments are only stored as a whole when they are pending or invalid.
Expand Down Expand Up @@ -1010,7 +1026,27 @@ mod tests {
);
}

// `store_appointments` is implicitly tested by `store_pending_appointment` and `store_invalid_appointment`
#[test]
fn test_store_load_appointment() {
let mut dbm = DBM::in_memory().unwrap();

let appointment = generate_random_appointment(None);
let tx = dbm.get_mut_connection().transaction().unwrap();
DBM::store_appointment(&tx, &appointment).unwrap();
tx.commit().unwrap();

let loaded_appointment = dbm.load_appointment(appointment.locator);
assert_eq!(appointment, loaded_appointment.unwrap());
}

#[test]
fn test_store_load_appointment_inexistent() {
let dbm = DBM::in_memory().unwrap();

let locator = generate_random_appointment(None).locator;
let loaded_appointment = dbm.load_appointment(locator);
assert!(matches!(loaded_appointment, Err(Error::NotFound)));
}

#[test]
fn test_store_pending_appointment() {
Expand Down
53 changes: 39 additions & 14 deletions watchtower-plugin/src/main.rs
Expand Up @@ -326,10 +326,18 @@ async fn retry_tower(
));
}

state
.unreachable_towers
.send(tower_id)
.map_err(|e| anyhow!(e))?;
for locator in state
.towers
.get(&tower_id)
.unwrap()
.pending_appointments
.iter()
{
state
.unreachable_towers
.send((tower_id, *locator))
.map_err(|e| anyhow!(e))?;
}
Ok(json!(format!("Retrying {}", tower_id)))
} else {
Err(anyhow!("Unknown tower {}", tower_id))
Expand Down Expand Up @@ -421,17 +429,27 @@ async fn on_commitment_revocation(
state.set_tower_status(tower_id, TowerStatus::TemporaryUnreachable);
state.add_pending_appointment(tower_id, &appointment);

state.unreachable_towers.send(tower_id).unwrap();
state
.unreachable_towers
.send((tower_id, appointment.locator))
.unwrap();
}
}
AddAppointmentError::ApiError(e) => match e.error_code {
errors::INVALID_SIGNATURE_OR_SUBSCRIPTION_ERROR => {
log::warn!("There is a subscription issue with {}", tower_id);
log::warn!(
"There is a subscription issue with {}. Adding {} to pending",
tower_id,
appointment.locator
);
let mut state = plugin.state().lock().unwrap();
state.set_tower_status(tower_id, TowerStatus::SubscriptionError);
state.add_pending_appointment(tower_id, &appointment);

state.unreachable_towers.send(tower_id).unwrap();
state
.unreachable_towers
.send((tower_id, appointment.locator))
.unwrap();
}

_ => {
Expand Down Expand Up @@ -466,18 +484,25 @@ async fn on_commitment_revocation(
} else {
if status.is_subscription_error() {
log::warn!(
"There is a subscription issue with {}. Adding appointment to pending",
"There is a subscription issue with {}. Adding {} to pending",
tower_id,
appointment.locator
);
} else {
log::warn!("{} is {}. Adding appointment to pending", tower_id, status);
log::warn!(
"{} is {}. Adding {} to pending",
tower_id,
status,
appointment.locator,
);
}

plugin
.state()
.lock()
.unwrap()
.add_pending_appointment(tower_id, &appointment);
let mut state = plugin.state().lock().unwrap();
state.add_pending_appointment(tower_id, &appointment);
state
.unreachable_towers
.send((tower_id, appointment.locator))
.unwrap();
}
}

Expand Down

0 comments on commit 75bb616

Please sign in to comment.