Skip to content

Commit

Permalink
Minor bugs fixed 2
Browse files Browse the repository at this point in the history
  • Loading branch information
1010adigupta committed Sep 11, 2023
1 parent 41dd201 commit 9012aef
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
17 changes: 6 additions & 11 deletions watchtower-plugin/src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,11 @@ mod tests {

#[cfg(feature = "accountable")]
let appointment_receipt = get_random_appointment_receipt(tower_sk);
#[cfg(feature = "accountable")]
// #[cfg(feature = "accountable")]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &appointment_receipt);
#[cfg(not(feature = "accountable"))]
let add_appointment_response = get_dummy_add_appointment_response(appointment.locator);
get_dummy_add_appointment_response(appointment.locator, #[cfg(feature = "accountable")]
&appointment_receipt);


let mut server = mockito::Server::new_async().await;
let api_mock = server
Expand All @@ -455,7 +455,7 @@ mod tests {
.await
.unwrap();
#[cfg(not(feature = "accountable"))]
let (response) = add_appointment(
let response = add_appointment(
TowerId(tower_pk),
&NetAddr::new(server.url()),
&None,
Expand Down Expand Up @@ -521,17 +521,13 @@ mod tests {
}

#[tokio::test]
#[cfg(feature = "accountable")]
async fn test_send_appointment_misbehaving() {
let (sybil_tower_sk, sibyl_tower_pk) = cryptography::get_random_keypair();
let appointment = generate_random_appointment(None);

#[cfg(feature = "accountable")]
let appointment_receipt = get_random_appointment_receipt(tower_sk);
#[cfg(feature = "accountable")]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &appointment_receipt);
#[cfg(not(feature = "accountable"))]
let add_appointment_response = get_dummy_add_appointment_response(appointment.locator);

let mut server = mockito::Server::new_async().await;
let api_mock = server
Expand All @@ -554,7 +550,6 @@ mod tests {
.unwrap_err();

api_mock.assert_async().await;
#[cfg(feature = "accountable")]
if let AddAppointmentError::SignatureError(proof) = error {
assert_eq!(
MisbehaviorProof::new(
Expand Down
18 changes: 10 additions & 8 deletions watchtower-plugin/src/retrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use teos_common::UserId as TowerId;

use crate::net::http::{self, AddAppointmentError};
use crate::wt_client::{RevocationData, WTClient};
#[cfg(not(feature = "accountable"))]
use crate::TowerStatus;
#[cfg(feature = "accountable")]
use crate::{MisbehaviorProof, TowerStatus};
use crate::MisbehaviorProof;

const POLLING_TIME: u64 = 1;

Expand Down Expand Up @@ -48,7 +47,7 @@ impl RetryError {
fn is_permanent(&self) -> bool {
matches!(
self,
RetryError::Subscription(_, true) | RetryError::Misbehaving(_) | RetryError::Abandoned
RetryError::Subscription(_, true) | RetryError::Abandoned | RetryError::Misbehaving(_)
)
}
}
Expand Down Expand Up @@ -520,6 +519,7 @@ impl Retrier {
tower_id,
appointment.locator,
slots,
#[cfg(feature = "accountable")]
&receipt,
);
wt_client.remove_pending_appointment(tower_id, appointment.locator);
Expand All @@ -529,7 +529,7 @@ impl Retrier {
Ok(slots) => {
self.pending_appointments.lock().unwrap().remove(&locator);
let mut wt_client = self.wt_client.lock().unwrap();
wt_client.add_accepted_appointment(tower_id, appointment.locator, slots);
wt_client.add_appointment_receipt(tower_id, appointment.locator, slots);
wt_client.remove_pending_appointment(tower_id, appointment.locator);
log::debug!("Response verified and data stored in the database");
}
Expand Down Expand Up @@ -652,6 +652,8 @@ mod tests {
}

#[tokio::test]
#[cfg(feature = "accountable")]
// #[cfg(feature = "accountable")]
async fn test_manage_retry_reachable() {
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
Expand All @@ -678,19 +680,19 @@ mod tests {
.unwrap()
.add_pending_appointment(tower_id, &appointment);

#[cfg(not(feature = "accountable"))]
// Prepare the mock response
let mut add_appointment_receipt = AppointmentReceipt::new(
cryptography::sign(&appointment.to_vec(), &wt_client.lock().unwrap().user_sk).unwrap(),
42,
);
#[cfg(not(feature = "accountable"))]

add_appointment_receipt.sign(&tower_sk);
#[cfg(feature = "accountable")]

let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &add_appointment_receipt);

#[cfg(not(feature = "accountable"))]

let add_appointment_response = get_dummy_add_appointment_response(appointment.locator);
let api_mock = server
.mock("POST", Endpoint::AddAppointment.path().as_str())
Expand Down
23 changes: 8 additions & 15 deletions watchtower-plugin/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ use teos_common::protos as common_msgs;
#[cfg(feature = "accountable")]
use teos_common::receipts::AppointmentReceipt;

#[cfg(feature = "accountable")]
pub fn get_dummy_add_appointment_response(
locator: Locator,
receipt: &AppointmentReceipt,
) -> common_msgs::AddAppointmentResponse {
common_msgs::AddAppointmentResponse {
locator: locator.to_vec(),
start_block: receipt.start_block(),
signature: receipt.signature().unwrap(),
available_slots: 21,
subscription_expiry: 1000,
}
}

#[cfg(not(feature = "accountable"))]
pub fn get_dummy_add_appointment_response(locator: Locator) -> common_msgs::AddAppointmentResponse {
// #[cfg(not(feature = "accountable"))]
pub fn get_dummy_add_appointment_response(locator: Locator, #[cfg(feature = "accountable")]receipt: &AppointmentReceipt,) -> common_msgs::AddAppointmentResponse {
common_msgs::AddAppointmentResponse {
locator: locator.to_vec(),
#[cfg(feature = "accountable")]
start_block: receipt.start_block(),
#[cfg(not(feature = "accountable"))]
start_block: 0,
#[cfg(feature = "accountable")]
signature: receipt.signature().unwrap(),
#[cfg(not(feature = "accountable"))]
signature: "None".to_string(),
available_slots: 21,
subscription_expiry: 1000,
Expand Down
29 changes: 8 additions & 21 deletions watchtower-plugin/src/wt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use teos_common::appointment::{Appointment, Locator};
use teos_common::cryptography;
use teos_common::dbm::Error as DBError;
#[cfg(not(feature = "accountable"))]
use teos_common::receipts::RegistrationReceipt;
#[cfg(feature = "accountable")]
use teos_common::receipts::{AppointmentReceipt, RegistrationReceipt};
use teos_common::receipts::AppointmentReceipt;
use teos_common::{TowerId, UserId};

use crate::dbm::DBM;
use crate::net::ProxyInfo;
use crate::retrier::RetrierStatus;
#[cfg(feature = "accountable")]
use crate::{MisbehaviorProof, SubscriptionError, TowerInfo, TowerStatus, TowerSummary};
#[cfg(not(feature = "accountable"))]
use crate::MisbehaviorProof;
use crate::{SubscriptionError, TowerInfo, TowerStatus, TowerSummary};

#[derive(Eq, PartialEq)]
Expand Down Expand Up @@ -219,40 +217,29 @@ impl WTClient {
}

/// Adds an appointment receipt to the tower record.
#[cfg(feature = "accountable")]
pub fn add_appointment_receipt(
&mut self,
tower_id: TowerId,
locator: Locator,
available_slots: u32,
#[cfg(feature = "accountable")]
receipt: &AppointmentReceipt,
) {
if let Some(tower) = self.towers.get_mut(&tower_id) {
// DISCUSS: It may be nice to independently compute the slots and compare
tower.available_slots = available_slots;

#[cfg(feature = "accountable")]
self.dbm
.store_appointment_receipt(tower_id, locator, available_slots, receipt)
.unwrap();
} else {
log::error!("Cannot add appointment receipt to tower. Unknown tower_id: {tower_id}");
}
}
#[cfg(not(feature = "accountable"))]
pub fn add_accepted_appointment(
&mut self,
tower_id: TowerId,
locator: Locator,
available_slots: u32,
) {
if let Some(tower) = self.towers.get_mut(&tower_id) {
// DISCUSS: It may be nice to independently compute the slots and compare
tower.available_slots = available_slots;

#[cfg(not(feature = "accountable"))]
self.dbm
.store_accepted_appointments(tower_id, locator, available_slots)
.unwrap();
} else {
#[cfg(feature = "accountable")]
log::error!("Cannot add appointment receipt to tower. Unknown tower_id: {tower_id}");
#[cfg(not(feature = "accountable"))]
log::error!("Cannot add accepted appointment to tower. Unknown tower_id: {tower_id}");
}
}
Expand Down

0 comments on commit 9012aef

Please sign in to comment.