-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathretrier.rs
1566 lines (1413 loc) · 61.6 KB
/
retrier.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::{HashMap, HashSet};
use std::fmt::Display;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::sync::mpsc::{error::TryRecvError, UnboundedReceiver};
use backoff::future::retry_notify;
use backoff::{Error, ExponentialBackoff};
use teos_common::appointment::Locator;
use teos_common::cryptography;
use teos_common::errors;
use teos_common::UserId as TowerId;
use crate::net::http::{self, AddAppointmentError};
use crate::wt_client::{RevocationData, WTClient};
use crate::{MisbehaviorProof, TowerStatus};
#[derive(Eq, PartialEq, Debug)]
enum RetryError {
// bool marks whether the Subscription error is permanent or not
Subscription(String, bool),
Unreachable,
Misbehaving(MisbehaviorProof),
Abandoned,
}
impl Display for RetryError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RetryError::Subscription(r, _) => write!(f, "{}", r),
RetryError::Unreachable => write!(f, "Tower cannot be reached"),
RetryError::Misbehaving(_) => write!(f, "Tower misbehaved"),
RetryError::Abandoned => write!(f, "Tower was abandoned. Skipping retry"),
}
}
}
impl RetryError {
fn is_permanent(&self) -> bool {
matches!(
self,
RetryError::Subscription(_, true) | RetryError::Misbehaving(_) | RetryError::Abandoned
)
}
}
pub struct RetryManager {
wt_client: Arc<Mutex<WTClient>>,
unreachable_towers: UnboundedReceiver<(TowerId, RevocationData)>,
max_elapsed_time_secs: u16,
auto_retry_delay: u32,
max_interval_time_secs: u16,
retriers: HashMap<TowerId, Arc<Retrier>>,
}
impl RetryManager {
pub fn new(
wt_client: Arc<Mutex<WTClient>>,
unreachable_towers: UnboundedReceiver<(TowerId, RevocationData)>,
max_elapsed_time_secs: u16,
auto_retry_delay: u32,
max_interval_time_secs: u16,
) -> Self {
RetryManager {
wt_client,
unreachable_towers,
max_elapsed_time_secs,
auto_retry_delay,
max_interval_time_secs,
retriers: HashMap::new(),
}
}
/// Starts the retry manager's main logic loop.
/// This method will keep running until the `unreachable_towers` sender disconnects.
///
/// It will receive a `(tower_id, revocation_data)` pair and try to send all the appointments contained
/// in `revocation_data` (identified by `locator`) to the tower with `tower_id`. This is done by spawning
/// a tokio thread for each `tower_id` that tries to send all the pending appointments.
///
/// The content of [RevocationData] will depend on who called `unreachable_towers.send`:
/// - If it was called by `on_commitment_revocation`, the data will be fresh and contain a single locator
/// - If it was called by the [WTClient] constructor, or by manually retrying, then the data will the stale
/// and contain a `HashSet<locator>` with, potentially, many locators.
pub async fn manage_retry(&mut self) {
log::info!("Starting retry manager");
loop {
match self.unreachable_towers.try_recv() {
Ok((tower_id, data)) => {
// Not start a retry if the tower is flagged to be abandoned
if !self
.wt_client
.lock()
.unwrap()
.towers
.contains_key(&tower_id)
{
log::info!("Skipping retrying abandoned tower {}", tower_id);
} else if let Some(retrier) = self.retriers.get(&tower_id) {
if retrier.is_idle() {
if !data.is_none() {
log::error!("Data was send to an idle retier. This should have never happened. Please report! ({:?})", data);
continue;
}
log::info!(
"Manually finished idling. Flagging {} for retry",
retrier.tower_id
);
// While a retrier is idle data is not kept in memory.
// Load the pending appointments from the DB and feed them to the retrier
retrier.set_status(RetrierStatus::Stopped);
retrier.pending_appointments.lock().unwrap().extend(
self.wt_client
.lock()
.unwrap()
.dbm
.load_appointment_locators(
retrier.tower_id,
crate::AppointmentStatus::Pending,
),
);
} else {
self.add_pending_appointments(tower_id, data.into());
}
} else {
self.add_pending_appointments(tower_id, data.into());
}
}
Err(TryRecvError::Empty) => {
// Keep only running retriers and retriers ready to be started/re-started.
// This will remove failed ones and ones finished successfully and have no pending appointments.
//
// Note that a failed retrier could have received some new appointments to retry. In this case, we don't try to send
// them because we know that that tower is unreachable. We most likely received these new appointments while the tower
// was still flagged as temporarily unreachable when cleaning up after giving up retrying.
self.retriers.retain(|_, retrier| {
retrier.remove_if_failed();
retrier.should_start() || retrier.is_running() || retrier.is_idle()
});
// Start all the ready retriers.
for retrier in self.retriers.values() {
if retrier.should_start() {
self.start_retrying(retrier.clone());
// Effectively this is the same as `if retrier.is_idle` plus returning for how long is true.
} else if let Some(t) = retrier.get_elapsed_time() {
if t > self.auto_retry_delay as u64 {
log::info!(
"Finished idling. Flagging {} for retry",
retrier.tower_id
);
// While a retrier is idle data is not kept in memory.
// Load the pending appointments from the DB and feed them to the retrier
retrier.set_status(RetrierStatus::Stopped);
retrier.pending_appointments.lock().unwrap().extend(
self.wt_client
.lock()
.unwrap()
.dbm
.load_appointment_locators(
retrier.tower_id,
crate::AppointmentStatus::Pending,
),
);
}
}
}
// Sleep to not waste a lot of CPU cycles.
tokio::time::sleep(Duration::from_secs(1)).await;
}
Err(TryRecvError::Disconnected) => break,
}
}
}
/// Adds an appointment to pending for a given tower.
///
/// If the tower is not currently being retried, a new entry for it is created, otherwise, the data is appended to the existing entry.
fn add_pending_appointments(&mut self, tower_id: TowerId, locators: HashSet<Locator>) {
if let std::collections::hash_map::Entry::Vacant(e) = self.retriers.entry(tower_id) {
log::debug!("Creating a new entry for tower {} ", tower_id);
e.insert(Arc::new(Retrier::new(
self.wt_client.clone(),
tower_id,
locators,
)));
} else {
let mut pending_appointments = self
.retriers
.get(&tower_id)
.unwrap()
.pending_appointments
.lock()
.unwrap();
for locator in locators {
log::debug!(
"Adding pending appointment {} to existing tower {}",
locator,
tower_id
);
pending_appointments.insert(locator);
}
}
}
fn start_retrying(&self, retrier: Arc<Retrier>) {
log::info!("Retrying tower {}", retrier.tower_id);
retrier.start(self.max_elapsed_time_secs, self.max_interval_time_secs);
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum RetrierStatus {
/// Retrier is stopped. This could happen if the retrier was never started or it started and
/// finished successfully. If a retrier is stopped and has some pending appointments, it should be
/// started/re-started, otherwise, it can be deleted safely.
Stopped,
/// Retrier is currently retrying the tower. If the retrier receives new appointments, it will
/// **try** to send them along (but it might not send them).
///
/// If a retrier status is `Running`, then its associated tower is either temporary unreachable or subscription error.
Running,
/// Retrier failed retrying the tower. Should not be re-started.
///
/// If a retrier status is `Failed`, then its associated tower is neither reachable nor temporary unreachable.
Failed,
/// Retrier is currently idle waiting for a signal to start working again. An Idle retrier can be forced to start
/// working again by the user by manually calling `retrytower`.
///
/// If a retrier status is `Idle`, then its associated tower is unreachable.
Idle(Instant),
}
impl RetrierStatus {
/// Check whether the status is [Running](RetrierStatus::Stopped).
pub fn is_stopped(&self) -> bool {
*self == RetrierStatus::Stopped
}
/// Check whether the status is [Running](RetrierStatus::Running).
pub fn is_running(&self) -> bool {
*self == RetrierStatus::Running
}
/// Check whether the status is [Idle](RetrierStatus::Idle).
pub fn is_idle(&self) -> bool {
matches!(self, RetrierStatus::Idle { .. })
}
/// Check whether the status is [Failed](RetrierStatus::Failed).
pub fn failed(&self) -> bool {
*self == RetrierStatus::Failed
}
/// Gets the elapsed time of an [Idle](RetrierStatus::Idle) status, [None] otherwise.
pub fn get_elapsed_time(&self) -> Option<u64> {
if let RetrierStatus::Idle(x) = *self {
Some(x.elapsed().as_secs())
} else {
None
}
}
}
pub struct Retrier {
wt_client: Arc<Mutex<WTClient>>,
tower_id: TowerId,
pending_appointments: Mutex<HashSet<Locator>>,
status: Mutex<RetrierStatus>,
}
impl Retrier {
pub fn new(
wt_client: Arc<Mutex<WTClient>>,
tower_id: TowerId,
locators: HashSet<Locator>,
) -> Self {
Self {
wt_client,
tower_id,
pending_appointments: Mutex::new(locators),
status: Mutex::new(RetrierStatus::Stopped),
}
}
fn has_pending_appointments(&self) -> bool {
!self.pending_appointments.lock().unwrap().is_empty()
}
fn set_status(&self, status: RetrierStatus) {
*self.status.lock().unwrap() = status.clone();
// Add or remove retriers from WTClient based on the RetrierStatus
if self.is_running() || self.is_idle() {
log::debug!("Adding {} to active retriers", self.tower_id);
self.wt_client
.lock()
.unwrap()
.retriers
.insert(self.tower_id, status);
} else if self.is_stopped() {
// We are not removing failed retriers here to prevent a manual retry until the retrier is removed from
// the manager
log::debug!("Removing retrier {} from active retriers", self.tower_id);
self.wt_client
.lock()
.unwrap()
.retriers
.remove(&self.tower_id);
}
}
/// Maps [RetrierStatus::is_stopped]
pub fn is_stopped(&self) -> bool {
self.status.lock().unwrap().is_stopped()
}
/// Maps [RetrierStatus::is_running]
pub fn is_running(&self) -> bool {
self.status.lock().unwrap().is_running()
}
/// Maps [RetrierStatus::is_idle]
pub fn is_idle(&self) -> bool {
self.status.lock().unwrap().is_idle()
}
/// Maps [RetrierStatus::failed]
pub fn failed(&self) -> bool {
self.status.lock().unwrap().failed()
}
/// Maps [RetrierStatus::get_elapsed_time]
pub fn get_elapsed_time(&self) -> Option<u64> {
self.status.lock().unwrap().get_elapsed_time()
}
pub fn should_start(&self) -> bool {
// A retrier can be started/re-started if it is stopped (i.e. not running and not failed)
// and has some pending appointments.
self.is_stopped() && self.has_pending_appointments()
}
pub fn start(self: Arc<Self>, max_elapsed_time_secs: u16, max_interval_time_secs: u16) {
// We shouldn't be retrying failed and running retriers.
debug_assert_eq!(*self.status.lock().unwrap(), RetrierStatus::Stopped);
// When manually retrying the tower may be in either SubscriptionError or Unreachable state.
// Flag this as TemporaryUnreachable only if there is no SubscriptionError.
// Rationale: if there is a subscription error that needs to be handled first, otherwise we'll
// waste a retry cycle with a request that will always fail.
{
let mut state = self.wt_client.lock().unwrap();
if !state
.get_tower_status(&self.tower_id)
.unwrap()
.is_subscription_error()
{
state.set_tower_status(self.tower_id, TowerStatus::TemporaryUnreachable);
}
}
self.set_status(RetrierStatus::Running);
tokio::spawn(async move {
let r = retry_notify(
ExponentialBackoff {
max_elapsed_time: Some(Duration::from_secs(max_elapsed_time_secs as u64)),
max_interval: Duration::from_secs(max_interval_time_secs as u64),
..ExponentialBackoff::default()
},
|| async { self.run().await },
|err, _| {
log::warn!("Retry error happened with {}. {}", self.tower_id, err);
},
)
.await;
match r {
Ok(_) => {
log::info!("Retry strategy succeeded for {}", self.tower_id);
// Set the tower status now so new appointment doesn't go to the retry manager.
self.wt_client
.lock()
.unwrap()
.set_tower_status(self.tower_id, TowerStatus::Reachable);
// Retrier succeeded and can be re-used by re-starting it.
self.set_status(RetrierStatus::Stopped);
}
Err(e) => {
// 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).
log::warn!("Retry strategy gave up for {}. {}", self.tower_id, e);
if e.is_permanent() {
self.set_status(RetrierStatus::Failed);
}
match e {
RetryError::Subscription(_, true) => {
log::info!("Setting {} status as subscription error", self.tower_id);
self.wt_client
.lock()
.unwrap()
.set_tower_status(self.tower_id, TowerStatus::SubscriptionError)
}
RetryError::Misbehaving(p) => {
log::warn!("Cannot recover known tower_id from the appointment receipt. Flagging tower as misbehaving");
self.wt_client
.lock()
.unwrap()
.flag_misbehaving_tower(self.tower_id, p);
}
RetryError::Abandoned => {
log::info!("Skipping retrying abandoned tower {}", self.tower_id)
}
// This covers `RetryError::Unreachable` and `RetryError::Subscription(_, false)`
_ => {
log::debug!("Starting to idle");
self.set_status(RetrierStatus::Idle(Instant::now()));
// Clear all pending appointments so they do not waste any memory while idling
self.pending_appointments.lock().unwrap().clear();
self.wt_client
.lock()
.unwrap()
.set_tower_status(self.tower_id, TowerStatus::Unreachable);
}
}
}
}
});
}
async fn run(&self) -> Result<(), Error<RetryError>> {
// Create a new scope so we can get all the data only locking the WTClient once.
let (tower_id, status, net_addr, user_id, user_sk, proxy) = {
let wt_client = self.wt_client.lock().unwrap();
if wt_client.towers.get(&self.tower_id).is_none() {
return Err(Error::permanent(RetryError::Abandoned));
}
let tower = wt_client.towers.get(&self.tower_id).unwrap();
(
self.tower_id,
tower.status,
tower.net_addr.clone(),
wt_client.user_id,
wt_client.user_sk,
wt_client.proxy.clone(),
)
};
// If the tower state is subscription_error we need to re-register first. If we cannot, then the retry is aborted.
if status.is_subscription_error() {
let receipt = http::register(tower_id, user_id, &net_addr, &proxy)
.await
.map_err(|e| {
log::debug!("Cannot renew registration with tower. Error: {:?}", e);
Error::transient(RetryError::Subscription(
"Cannot renew registration with tower".to_owned(),
false,
))
})?;
if !receipt.verify(&tower_id) {
return Err(Error::permanent(RetryError::Subscription("Registration receipt contains bad signature. Are you using the right tower_id?".to_owned(), true)));
}
self.wt_client
.lock()
.unwrap()
.add_update_tower(tower_id, net_addr.net_addr(), &receipt)
.map_err(|e| {
let reason = if e.is_expiry() {
"Registration receipt contains a subscription expiry that is not higher than the one we are currently registered for"
} else {
"Registration receipt does not contain more slots than the ones we are currently registered for"
};
Error::permanent(RetryError::Subscription(reason.to_owned(), true))
})?;
}
while self.has_pending_appointments() {
let locators = self.pending_appointments.lock().unwrap().clone();
for locator in locators.into_iter() {
let appointment = self
.wt_client
.lock()
.unwrap()
.dbm
.load_appointment(locator)
.unwrap();
match http::add_appointment(
tower_id,
&net_addr,
&proxy,
&appointment,
&cryptography::sign(&appointment.to_vec(), &user_sk).unwrap(),
)
.await
{
Ok((slots, receipt)) => {
self.pending_appointments.lock().unwrap().remove(&locator);
let mut wt_client = self.wt_client.lock().unwrap();
wt_client.add_appointment_receipt(
tower_id,
appointment.locator,
slots,
&receipt,
);
wt_client.remove_pending_appointment(tower_id, appointment.locator);
log::debug!("Response verified and data stored in the database");
}
Err(e) => {
match e {
AddAppointmentError::RequestError(e) => {
if e.is_connection() {
log::warn!(
"{} cannot be reached. Tower will be retried later",
tower_id,
);
return Err(Error::transient(RetryError::Unreachable));
}
}
AddAppointmentError::ApiError(e) => match e.error_code {
errors::INVALID_SIGNATURE_OR_SUBSCRIPTION_ERROR => {
log::warn!("There is a subscription issue with {}", tower_id);
self.wt_client
.lock()
.unwrap()
.set_tower_status(tower_id, TowerStatus::SubscriptionError);
return Err(Error::transient(RetryError::Subscription(
"Subscription error".to_owned(),
false,
)));
}
_ => {
log::warn!(
"{} rejected the appointment. Error: {}, error_code: {}",
tower_id,
e.error,
e.error_code
);
// We need to move the appointment from pending to invalid
// Add it first to invalid and remove it from pending later so a cascade delete is not triggered
self.pending_appointments.lock().unwrap().remove(&locator);
let mut wt_client = self.wt_client.lock().unwrap();
wt_client.add_invalid_appointment(tower_id, &appointment);
wt_client
.remove_pending_appointment(tower_id, appointment.locator);
}
},
AddAppointmentError::SignatureError(proof) => {
return Err(Error::permanent(RetryError::Misbehaving(proof)));
}
}
}
}
}
}
Ok(())
}
/// Removed our retrier identifier from the WTClient if the retrier has failed
pub fn remove_if_failed(&self) {
if self.failed() {
log::debug!(
"Removing failed retrier {} from active retriers",
self.tower_id
);
self.wt_client
.lock()
.unwrap()
.retriers
.remove(&self.tower_id);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use httpmock::prelude::*;
use serde_json::json;
use tempdir::TempDir;
use tokio::sync::mpsc::unbounded_channel;
use teos_common::errors;
use teos_common::net::http::Endpoint;
use teos_common::receipts::{AppointmentReceipt, RegistrationReceipt};
use teos_common::test_utils::{
generate_random_appointment, get_random_registration_receipt, get_random_user_id,
get_registration_receipt_from_previous,
};
use crate::net::http::ApiError;
use crate::test_utils::get_dummy_add_appointment_response;
const LONG_AUTO_RETRY_DELAY: u32 = 60;
const SHORT_AUTO_RETRY_DELAY: u32 = 3;
const API_DELAY: f64 = 0.5;
const MAX_ELAPSED_TIME: u16 = 2;
const MAX_INTERVAL_TIME: u16 = 1;
impl Retrier {
fn empty(wt_client: Arc<Mutex<WTClient>>, tower_id: TowerId) -> Self {
Self {
wt_client,
tower_id,
pending_appointments: Mutex::new(HashSet::new()),
status: Mutex::new(RetrierStatus::Stopped),
}
}
}
#[tokio::test]
// TODO: It'll be nice to toggle the mock on and off instead of having it always on. Not sure MockServer allows that though:
// https://github.com/alexliesenfeld/httpmock/issues/67
async fn test_manage_retry_reachable() {
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();
// Add a tower with pending appointments
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let receipt = get_random_registration_receipt();
wt_client
.lock()
.unwrap()
.add_update_tower(tower_id, &server.base_url(), &receipt)
.unwrap();
// Add appointment to pending
let appointment = generate_random_appointment(None);
wt_client
.lock()
.unwrap()
.add_pending_appointment(tower_id, &appointment);
// Prepare the mock response
let mut add_appointment_receipt = AppointmentReceipt::new(
cryptography::sign(&appointment.to_vec(), &wt_client.lock().unwrap().user_sk).unwrap(),
42,
);
add_appointment_receipt.sign(&tower_sk);
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &add_appointment_receipt);
let api_mock = server.mock(|when, then| {
when.method(POST).path(Endpoint::AddAppointment.path());
then.status(200)
.delay(Duration::from_secs_f64(API_DELAY))
.header("content-type", "application/json")
.json_body(json!(add_appointment_response));
});
// Start the task and send the tower to the channel for retry
let wt_client_clone = wt_client.clone();
let task = tokio::spawn(async move {
RetryManager::new(
wt_client_clone,
rx,
MAX_ELAPSED_TIME,
LONG_AUTO_RETRY_DELAY,
MAX_INTERVAL_TIME,
)
.manage_retry()
.await
});
tx.send((tower_id, RevocationData::Fresh(appointment.locator)))
.unwrap();
// Wait for the elapsed time and check how the tower status changed
tokio::time::sleep(Duration::from_secs((API_DELAY / 2.0) as u64)).await;
assert!(wt_client
.lock()
.unwrap()
.get_retrier_status(&tower_id)
.unwrap()
.is_running());
tokio::time::sleep(Duration::from_secs(MAX_ELAPSED_TIME as u64)).await;
let state = wt_client.lock().unwrap();
assert_eq!(
state.get_tower_status(&tower_id).unwrap(),
TowerStatus::Reachable
);
assert!(!state.retriers.contains_key(&tower_id));
assert!(!state
.towers
.get(&tower_id)
.unwrap()
.pending_appointments
.contains(&appointment.locator));
api_mock.assert();
task.abort();
}
#[tokio::test]
async fn test_manage_retry_unreachable() {
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
// Add a tower with pending appointments
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let receipt = get_random_registration_receipt();
wt_client
.lock()
.unwrap()
.add_update_tower(tower_id, "http://unreachable.tower", &receipt)
.unwrap();
// Add appointment to pending
let appointment = generate_random_appointment(None);
wt_client
.lock()
.unwrap()
.add_pending_appointment(tower_id, &appointment);
// Start the task and send the tower to the channel for retry
let wt_client_clone = wt_client.clone();
let mut retry_manager = RetryManager::new(
wt_client_clone,
rx,
MAX_ELAPSED_TIME + 1,
SHORT_AUTO_RETRY_DELAY,
MAX_INTERVAL_TIME,
);
let task = tokio::spawn(async move { retry_manager.manage_retry().await });
tx.send((tower_id, RevocationData::Fresh(appointment.locator)))
.unwrap();
// Wait for the elapsed time and check how the tower status changed
tokio::time::sleep(Duration::from_secs_f64(
(MAX_ELAPSED_TIME as f64 + 1.0) / 2.0,
))
.await;
assert!(wt_client
.lock()
.unwrap()
.get_tower_status(&tower_id)
.unwrap()
.is_temporary_unreachable());
assert!(wt_client
.lock()
.unwrap()
.get_retrier_status(&tower_id)
.unwrap()
.is_running());
// Wait until the task gives up and check again (this gives up due to accumulation of transient errors,
// so the retiers will be idle).
tokio::time::sleep(Duration::from_secs(MAX_ELAPSED_TIME as u64)).await;
assert!(wt_client
.lock()
.unwrap()
.get_tower_status(&tower_id)
.unwrap()
.is_unreachable());
assert!(wt_client
.lock()
.unwrap()
.get_retrier_status(&tower_id)
.unwrap()
.is_idle());
// Add a proper server and check that the auto-retry works
// Prepare the mock response
let server = MockServer::start();
let mut add_appointment_receipt = AppointmentReceipt::new(
cryptography::sign(&appointment.to_vec(), &wt_client.lock().unwrap().user_sk).unwrap(),
42,
);
add_appointment_receipt.sign(&tower_sk);
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &add_appointment_receipt);
let api_mock = server.mock(|when, then| {
when.method(POST).path(Endpoint::AddAppointment.path());
then.status(200)
.header("content-type", "application/json")
.json_body(json!(add_appointment_response));
});
// Update the tower details
wt_client
.lock()
.unwrap()
.add_update_tower(
tower_id,
&server.base_url(),
&get_registration_receipt_from_previous(&receipt),
)
.unwrap();
// Wait and check. We wait twice the short retry delay because it can be the case that the first auto retry
// is performed while we are patching the mock.
tokio::time::sleep(Duration::from_secs((SHORT_AUTO_RETRY_DELAY * 2) as u64)).await;
assert_eq!(
wt_client
.lock()
.unwrap()
.get_tower_status(&tower_id)
.unwrap(),
TowerStatus::Reachable
);
assert!(!wt_client
.lock()
.unwrap()
.towers
.get(&tower_id)
.unwrap()
.pending_appointments
.contains(&appointment.locator));
assert!(!wt_client.lock().unwrap().retriers.contains_key(&tower_id));
api_mock.assert();
task.abort();
}
#[tokio::test]
async fn test_manage_retry_rejected() {
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();
// Add a tower with pending appointments
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let receipt = get_random_registration_receipt();
wt_client
.lock()
.unwrap()
.add_update_tower(tower_id, &server.base_url(), &receipt)
.unwrap();
// Add appointment to pending
let appointment = generate_random_appointment(None);
wt_client
.lock()
.unwrap()
.add_pending_appointment(tower_id, &appointment);
// Prepare the mock response
let api_mock = server.mock(|when, then| {
when.method(POST).path(Endpoint::AddAppointment.path());
then.status(400)
.delay(Duration::from_secs_f64(API_DELAY))
.header("content-type", "application/json")
.json_body(json!(ApiError {
error: "error_msg".to_owned(),
error_code: 1,
}));
});
// Start the task and send the tower to the channel for retry
let wt_client_clone = wt_client.clone();
let task = tokio::spawn(async move {
RetryManager::new(
wt_client_clone,
rx,
MAX_ELAPSED_TIME,
LONG_AUTO_RETRY_DELAY,
MAX_INTERVAL_TIME,
)
.manage_retry()
.await
});
tx.send((tower_id, RevocationData::Fresh(appointment.locator)))
.unwrap();
// Wait for the elapsed time and check how the tower status changed
tokio::time::sleep(Duration::from_secs((API_DELAY / 2.0) as u64)).await;
assert!(wt_client
.lock()
.unwrap()
.get_retrier_status(&tower_id)
.unwrap()
.is_running());
tokio::time::sleep(Duration::from_secs(MAX_ELAPSED_TIME as u64)).await;
assert_eq!(
wt_client
.lock()
.unwrap()
.get_tower_status(&tower_id)
.unwrap(),
TowerStatus::Reachable
);
assert!(!wt_client.lock().unwrap().retriers.contains_key(&tower_id));
assert!(!wt_client
.lock()
.unwrap()
.towers
.get(&tower_id)
.unwrap()
.pending_appointments
.contains(&appointment.locator));
assert!(wt_client
.lock()
.unwrap()
.towers
.get(&tower_id)
.unwrap()
.invalid_appointments
.contains(&appointment.locator));
api_mock.assert();
task.abort();
}
#[tokio::test]
async fn test_manage_retry_misbehaving() {
let tmp_path = TempDir::new(&format!("watchtower_{}", get_random_user_id())).unwrap();
let (tx, rx) = unbounded_channel();
let wt_client = Arc::new(Mutex::new(
WTClient::new(tmp_path.path().to_path_buf(), tx.clone()).await,
));
let server = MockServer::start();
// Add a tower with pending appointments
let (_, tower_pk) = cryptography::get_random_keypair();
let tower_id = TowerId(tower_pk);
let receipt = get_random_registration_receipt();
wt_client
.lock()
.unwrap()
.add_update_tower(tower_id, &server.base_url(), &receipt)
.unwrap();
// Add appointment to pending
let appointment = generate_random_appointment(None);
wt_client
.lock()
.unwrap()
.add_pending_appointment(tower_id, &appointment);
// Prepare the mock response
let mut add_appointment_receipt = AppointmentReceipt::new(
cryptography::sign(&appointment.to_vec(), &wt_client.lock().unwrap().user_sk).unwrap(),
42,
);
// Sign with a random key so it counts as misbehaving
add_appointment_receipt.sign(&cryptography::get_random_keypair().0);
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &add_appointment_receipt);
let api_mock = server.mock(|when, then| {
when.method(POST).path(Endpoint::AddAppointment.path());
then.status(200)
.delay(Duration::from_secs_f64(API_DELAY))
.header("content-type", "application/json")
.json_body(json!(add_appointment_response));
});
// Start the task and send the tower to the channel for retry
let wt_client_clone = wt_client.clone();
let task = tokio::spawn(async move {
RetryManager::new(
wt_client_clone,
rx,
MAX_ELAPSED_TIME,
LONG_AUTO_RETRY_DELAY,
MAX_INTERVAL_TIME,
)
.manage_retry()
.await
});
tx.send((tower_id, RevocationData::Fresh(appointment.locator)))
.unwrap();
// Wait for the elapsed time and check how the tower status changed
tokio::time::sleep(Duration::from_secs_f64(API_DELAY / 2.0)).await;
assert!(wt_client
.lock()
.unwrap()
.get_retrier_status(&tower_id)
.unwrap()
.is_running());
tokio::time::sleep(Duration::from_secs(MAX_ELAPSED_TIME as u64)).await;
assert!(wt_client
.lock()
.unwrap()
.get_tower_status(&tower_id)
.unwrap()
.is_misbehaving());
assert!(!wt_client.lock().unwrap().retriers.contains_key(&tower_id));
api_mock.assert();