From 9012aef0ae442d486e8f5ac2f7eb0859abb268f5 Mon Sep 17 00:00:00 2001 From: 1010adigupta <1010adigupta@gmail.com> Date: Mon, 11 Sep 2023 11:43:48 +0530 Subject: [PATCH] Minor bugs fixed 2 --- watchtower-plugin/src/net/http.rs | 17 ++++++----------- watchtower-plugin/src/retrier.rs | 18 ++++++++++-------- watchtower-plugin/src/test_utils.rs | 23 ++++++++--------------- watchtower-plugin/src/wt_client.rs | 29 ++++++++--------------------- 4 files changed, 32 insertions(+), 55 deletions(-) diff --git a/watchtower-plugin/src/net/http.rs b/watchtower-plugin/src/net/http.rs index 7d442a38..6a9fc360 100644 --- a/watchtower-plugin/src/net/http.rs +++ b/watchtower-plugin/src/net/http.rs @@ -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 @@ -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, @@ -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 @@ -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( diff --git a/watchtower-plugin/src/retrier.rs b/watchtower-plugin/src/retrier.rs index 488dcfd3..db3d769e 100644 --- a/watchtower-plugin/src/retrier.rs +++ b/watchtower-plugin/src/retrier.rs @@ -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; @@ -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(_) ) } } @@ -520,6 +519,7 @@ impl Retrier { tower_id, appointment.locator, slots, + #[cfg(feature = "accountable")] &receipt, ); wt_client.remove_pending_appointment(tower_id, appointment.locator); @@ -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"); } @@ -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(); @@ -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()) diff --git a/watchtower-plugin/src/test_utils.rs b/watchtower-plugin/src/test_utils.rs index 37bbed49..37704a0d 100644 --- a/watchtower-plugin/src/test_utils.rs +++ b/watchtower-plugin/src/test_utils.rs @@ -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, diff --git a/watchtower-plugin/src/wt_client.rs b/watchtower-plugin/src/wt_client.rs index 4016e488..f36cb9bd 100644 --- a/watchtower-plugin/src/wt_client.rs +++ b/watchtower-plugin/src/wt_client.rs @@ -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)] @@ -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}"); } }