1
//! Code implementing version 1 proof-of-work for onion service hosts.
2
//!
3
//! Spec links:
4
//! * <https://spec.torproject.org/hspow-spec/common-protocol.html>
5
//! * <https://spec.torproject.org/hspow-spec/v1-equix.html>
6

            
7
use std::{
8
    collections::{BTreeSet, HashMap, VecDeque},
9
    sync::{Arc, Mutex, RwLock},
10
    task::Waker,
11
};
12

            
13
use arrayvec::ArrayVec;
14
use equix::EquiXBuilder;
15
use futures::{SinkExt, StreamExt};
16
use futures::{Stream, channel::mpsc};
17
use num_traits::FromPrimitive;
18
use rand::CryptoRng;
19
use serde::{Deserialize, Serialize};
20
use thiserror::Error;
21
use tor_basic_utils::RngExt as _;
22
use tor_cell::relaycell::hs::pow::{ProofOfWork, v1::ProofOfWorkV1};
23
use tor_checkable::timed::TimeRangeBound;
24
use tor_error::warn_report;
25
use tor_hscrypto::{
26
    pk::HsBlindIdKey,
27
    pow::v1::{
28
        Effort, Instance, RuntimeOption, Seed, SeedHead, Solution, SolutionErrorV1, Verifier,
29
    },
30
    time::TimePeriod,
31
};
32
use tor_keymgr::KeyMgr;
33
use tor_netdir::{NetDirProvider, NetdirProviderShutdown, params::NetParameters};
34
use tor_netdoc::doc::hsdesc::pow::{PowParams, v1::PowParamsV1};
35
use tor_persist::{
36
    hsnickname::HsNickname,
37
    state_dir::{InstanceRawSubdir, StorageHandle},
38
};
39
use tor_rtcompat::Runtime;
40
use tor_rtcompat::SpawnExt;
41
use web_time_compat::{Duration, Instant, InstantExt, SystemTime, SystemTimeExt};
42

            
43
use crate::{
44
    BlindIdPublicKeySpecifier, OnionServiceConfig, RendRequest, ReplayError, StartupError,
45
    rend_handshake,
46
    replay::{OpenReplayLogError, PowNonceReplayLog},
47
    status::{PowManagerStatusSender, Problem, State as PowManagerState},
48
};
49

            
50
use super::NewPowManager;
51

            
52
/// Proof-of-Work manager type alias for production, using concrete [`RendRequest`].
53
pub(crate) type PowManager<R> = PowManagerGeneric<R, RendRequest>;
54

            
55
/// This is responsible for rotating Proof-of-Work seeds and doing verification of PoW solves.
56
pub(crate) struct PowManagerGeneric<R, Q>(RwLock<State<R, Q>>);
57

            
58
/// Internal state for [`PowManagerGeneric`].
59
struct State<R, Q> {
60
    /// The [`Seed`]s for a given [`TimePeriod`]
61
    ///
62
    /// The [`ArrayVec`] contains the current and previous seed, and the [`SystemTime`] is when the
63
    /// current seed will expire.
64
    seeds: HashMap<TimePeriod, SeedsForTimePeriod>,
65

            
66
    /// Verifiers for all the seeds that exist in `seeds`.
67
    verifiers: HashMap<SeedHead, (Verifier, Mutex<PowNonceReplayLog>)>,
68

            
69
    /// The nickname for this hidden service.
70
    ///
71
    /// We need this so we can get the blinded keys from the [`KeyMgr`].
72
    nickname: HsNickname,
73

            
74
    /// Directory used to store nonce replay log.
75
    instance_dir: InstanceRawSubdir,
76

            
77
    /// Key manager.
78
    keymgr: Arc<KeyMgr>,
79

            
80
    /// Current suggested effort that we publish in the pow-params line.
81
    ///
82
    /// This is only read by the PowManagerGeneric, and is written to by the [`RendRequestReceiver`].
83
    suggested_effort: Arc<Mutex<Effort>>,
84

            
85
    /// Runtime
86
    runtime: R,
87

            
88
    /// Handle for storing state we need to persist to disk.
89
    storage_handle: StorageHandle<PowManagerStateRecord>,
90

            
91
    /// Queue to tell the publisher to re-upload a descriptor for a given TP, since we've rotated
92
    /// that seed.
93
    publisher_update_tx: mpsc::Sender<TimePeriod>,
94

            
95
    /// The [`RendRequestReceiver`], which contains the queue of [`RendRequest`]s.
96
    ///
97
    /// We need a reference to this in order to tell it when to update the suggested_effort value.
98
    rend_request_rx: RendRequestReceiver<R, Q>,
99

            
100
    /// [`NetDirProvider`], used for getting consensus parameters for configuration values.
101
    netdir_provider: Arc<dyn NetDirProvider>,
102

            
103
    /// Sender for reporting back onion service status.
104
    status_tx: PowManagerStatusSender,
105

            
106
    /// Receiver for the current configuration.
107
    config_rx: postage::watch::Receiver<Arc<OnionServiceConfig>>,
108
}
109

            
110
#[derive(Serialize, Deserialize, Debug, Clone)]
111
/// Information about the current and previous [`Seed`] for a given [`TimePeriod`].
112
struct SeedsForTimePeriod {
113
    /// The previous and current [`Seed`].
114
    ///
115
    /// The last element in this array is the current seed.
116
    seeds: ArrayVec<Seed, 2>,
117

            
118
    /// When the current seed will expire.
119
    next_expiration_time: SystemTime,
120
}
121

            
122
#[derive(Debug)]
123
#[allow(unused)]
124
/// A PoW solve was invalid.
125
///
126
/// While this contains the reason for the failure, we probably just want to use that for
127
/// debugging, we shouldn't make any logical decisions based on what the particular error was.
128
pub(crate) enum PowSolveError {
129
    /// Seed head was not recognized, it may be expired.
130
    InvalidSeedHead,
131
    /// We have already seen a solve with this nonce
132
    NonceReplay(ReplayError),
133
    /// The bytes given as a solution do not form a valid Equi-X puzzle
134
    InvalidEquixSolution(SolutionErrorV1),
135
    /// The solution given was invalid.
136
    InvalidSolve(tor_hscrypto::pow::Error),
137
}
138

            
139
/// On-disk record of [`PowManagerGeneric`] state.
140
#[derive(Serialize, Deserialize, Debug, Default)]
141
pub(crate) struct PowManagerStateRecord {
142
    /// Seeds for each time period.
143
    ///
144
    /// Conceptually, this is a map between TimePeriod and SeedsForTimePeriod, but since TimePeriod
145
    /// can't be serialized to a string, it's not very simple to use serde to serialize it like
146
    /// that, so we instead store it as a list of tuples, and convert it to/from the map when
147
    /// saving/loading.
148
    seeds: Vec<(TimePeriod, SeedsForTimePeriod)>,
149

            
150
    /// Most recently published suggested_effort value.
151
    #[serde(default)]
152
    suggested_effort: Effort,
153
    // We don't persist any per-period state. While it might be sort of nice to, it's complex to
154
    // decide when to write the state out to disk. The disadvantage to not storing it is that when
155
    // we restart the process, we may be up to 5 minutes slower to update the suggested effort to a
156
    // new value, which isn't particularly bad. The only case it would be bad is if a attacker has
157
    // a way to cause the Arti process to restart (in which case they could do that just before the
158
    // update period to pin the suggested effort value at a specific value), but if they have that,
159
    // they have a much more valuable attack (including as a DoS vector) than just a PoW bypass.
160
}
161

            
162
impl<R: Runtime, Q> State<R, Q> {
163
    /// Make a [`PowManagerStateRecord`] for this state.
164
4
    pub(crate) fn to_record(&self) -> PowManagerStateRecord {
165
4
        PowManagerStateRecord {
166
4
            seeds: self.seeds.clone().into_iter().collect(),
167
4
            suggested_effort: *self.suggested_effort.lock().expect("Lock poisoned"),
168
4
        }
169
4
    }
170

            
171
    /// Remove a verifier and delete the backing file.
172
    fn remove_verifier(&mut self, seed: &SeedHead) {
173
        if let Some((_, replay_log)) = self.verifiers.remove(seed) {
174
            let _ = replay_log.into_inner().expect("Lock poisoned").delete();
175
        }
176
    }
177
}
178

            
179
/// How frequently the suggested effort should be recalculated.
180
const HS_UPDATE_PERIOD: Duration = Duration::from_secs(300);
181

            
182
/// When the suggested effort has changed by less than this much, we don't republish it.
183
///
184
/// Specified as "15 percent" in <https://spec.torproject.org/hspow-spec/common-protocol.html>
185
///
186
/// However, we may want to make this configurable in the future.
187
const SUGGESTED_EFFORT_DEADZONE: f64 = 0.15;
188

            
189
/// How soon before a seed's expiration time we should rotate it and publish a new seed.
190
const SEED_EARLY_ROTATION_TIME: Duration = Duration::from_secs(60 * 5);
191

            
192
/// Minimum seed expiration time in minutes. See:
193
/// <https://spec.torproject.org/hspow-spec/v1-equix.html#parameter-descriptor>
194
const EXPIRATION_TIME_MINS_MIN: u64 = 105;
195

            
196
/// Maximum seed expiration time in minutes. See:
197
/// <https://spec.torproject.org/hspow-spec/v1-equix.html#parameter-descriptor>
198
const EXPIRATION_TIME_MINS_MAX: u64 = 120;
199

            
200
/// Enforce that early rotation time is less than or equal to min expiration time.
201
const _: () = assert!(
202
    SEED_EARLY_ROTATION_TIME.as_secs() <= EXPIRATION_TIME_MINS_MIN * 60,
203
    "Early rotation time must be less than minimum expiration time"
204
);
205

            
206
/// Enforce that min expiration time is less than or equal to max.
207
const _: () = assert!(
208
    EXPIRATION_TIME_MINS_MIN <= EXPIRATION_TIME_MINS_MAX,
209
    "Minimum expiration time must be less than or equal to max"
210
);
211

            
212
/// Depth of the queue used to signal the publisher that it needs to update a given time period.
213
///
214
/// 32 is likely way larger than we need but the messages are tiny so we might as well.
215
const PUBLISHER_UPDATE_QUEUE_DEPTH: usize = 32;
216

            
217
#[derive(Error, Debug, Clone)]
218
#[allow(dead_code)] // We want to show fields in Debug even if we don't use them.
219
#[non_exhaustive]
220
/// Error within the PoW subsystem.
221
pub enum PowError {
222
    /// We don't have a key that is needed.
223
    #[error("Missing required key.")]
224
    MissingKey,
225
    /// Error in the underlying storage layer.
226
    #[error(transparent)]
227
    StorageError(#[from] tor_persist::Error),
228
    /// Error from the ReplayLog.
229
    #[error(transparent)]
230
    OpenReplayLog(#[from] OpenReplayLogError),
231
    /// NetDirProvider has shut down
232
    #[error(transparent)]
233
    NetdirProviderShutdown(#[from] NetdirProviderShutdown),
234
}
235

            
236
impl<R: Runtime, Q: MockableRendRequest + Send + 'static> PowManagerGeneric<R, Q> {
237
    /// Create a new [`PowManagerGeneric`].
238
    #[allow(clippy::new_ret_no_self, clippy::too_many_arguments)]
239
4
    pub(crate) fn new(
240
4
        runtime: R,
241
4
        nickname: HsNickname,
242
4
        instance_dir: InstanceRawSubdir,
243
4
        keymgr: Arc<KeyMgr>,
244
4
        storage_handle: StorageHandle<PowManagerStateRecord>,
245
4
        netdir_provider: Arc<dyn NetDirProvider>,
246
4
        status_tx: PowManagerStatusSender,
247
4
        config_rx: postage::watch::Receiver<Arc<OnionServiceConfig>>,
248
4
    ) -> Result<NewPowManager<R>, StartupError> {
249
4
        let on_disk_state = storage_handle
250
4
            .load()
251
4
            .map_err(StartupError::LoadState)?
252
4
            .unwrap_or_else(PowManagerStateRecord::default);
253

            
254
        let seeds: HashMap<TimePeriod, SeedsForTimePeriod> =
255
4
            on_disk_state.seeds.into_iter().collect();
256
4
        let suggested_effort = Arc::new(Mutex::new(on_disk_state.suggested_effort));
257

            
258
4
        let mut verifiers = HashMap::new();
259
4
        for (tp, seeds_for_tp) in seeds.clone().into_iter() {
260
            for seed in seeds_for_tp.seeds {
261
                let verifier = match Self::make_verifier(
262
                    &keymgr,
263
                    nickname.clone(),
264
                    tp,
265
                    seed.clone(),
266
                    &config_rx.borrow(),
267
                ) {
268
                    Some(verifier) => verifier,
269
                    None => {
270
                        tracing::warn!(
271
                            "Couldn't construct verifier (key not available?). We will continue without this key, but this may prevent clients from connecting..."
272
                        );
273
                        continue;
274
                    }
275
                };
276
                let replay_log = match PowNonceReplayLog::new_logged(&instance_dir, &seed) {
277
                    Ok(replay_log) => replay_log,
278
                    Err(err) => {
279
                        warn_report!(
280
                            err,
281
                            "Error constructing replay log. We will continue without the log, but be aware that this may allow attackers to bypass PoW defenses..."
282
                        );
283
                        continue;
284
                    }
285
                };
286
                verifiers.insert(seed.head(), (verifier, Mutex::new(replay_log)));
287
            }
288
        }
289

            
290
        // This queue is extremely small, and we only make one of it per onion service, so it's
291
        // fine to not use memquota tracking.
292
        let (publisher_update_tx, publisher_update_rx) =
293
4
            crate::mpsc_channel_no_memquota(PUBLISHER_UPDATE_QUEUE_DEPTH);
294

            
295
4
        let (rend_req_tx, rend_req_rx_channel) = super::make_rend_queue();
296
4
        let rend_req_rx = RendRequestReceiver::new(
297
4
            runtime.clone(),
298
4
            nickname.clone(),
299
4
            suggested_effort.clone(),
300
4
            netdir_provider.clone(),
301
4
            status_tx.clone(),
302
4
            config_rx.clone(),
303
4
        );
304

            
305
4
        let state = State {
306
4
            seeds,
307
4
            nickname,
308
4
            instance_dir,
309
4
            keymgr,
310
4
            publisher_update_tx,
311
4
            verifiers,
312
4
            suggested_effort: suggested_effort.clone(),
313
4
            runtime: runtime.clone(),
314
4
            storage_handle,
315
4
            rend_request_rx: rend_req_rx.clone(),
316
4
            netdir_provider,
317
4
            status_tx,
318
4
            config_rx,
319
4
        };
320
4
        let pow_manager = Arc::new(PowManagerGeneric(RwLock::new(state)));
321

            
322
4
        rend_req_rx.start_accept_thread(runtime, pow_manager.clone(), rend_req_rx_channel);
323

            
324
4
        Ok(NewPowManager {
325
4
            pow_manager,
326
4
            rend_req_tx,
327
4
            rend_req_rx: Box::pin(rend_req_rx),
328
4
            publisher_update_rx,
329
4
        })
330
4
    }
331

            
332
    /// Launch background task to rotate seeds.
333
    pub(crate) fn launch(self: &Arc<Self>) -> Result<(), StartupError> {
334
        let pow_manager = self.clone();
335
        let runtime = pow_manager.0.read().expect("Lock poisoned").runtime.clone();
336

            
337
        runtime
338
            .spawn(pow_manager.main_loop_error_wrapper())
339
            .map_err(|cause| StartupError::Spawn {
340
                spawning: "pow manager",
341
                cause: cause.into(),
342
            })?;
343

            
344
        self.0
345
            .write()
346
            .expect("Lock poisoned")
347
            .status_tx
348
            .send(PowManagerState::Running, None);
349
        Ok(())
350
    }
351

            
352
    /// Run [`Self::main_loop_task`], reporting any errors.
353
    async fn main_loop_error_wrapper(self: Arc<Self>) {
354
        if let Err(err) = self.clone().main_loop_task().await {
355
            self.0
356
                .write()
357
                .expect("Lock poisoned")
358
                .status_tx
359
                .send_broken(Problem::Pow(err));
360
        }
361
    }
362

            
363
    /// Main loop for rotating seeds.
364
    async fn main_loop_task(self: Arc<Self>) -> Result<(), PowError> {
365
        let runtime = self.0.write().expect("Lock poisoned").runtime.clone();
366

            
367
        let mut last_suggested_effort_update = runtime.now();
368
        let last_published_suggested_effort: u32 = (*self
369
            .0
370
            .read()
371
            .expect("Lock poisoned")
372
            .suggested_effort
373
            .lock()
374
            .expect("Lock poisoned"))
375
        .into();
376
        // Deal with effort values as i64 in this part of the code
377
        // to allow for subtracting them without worring about underflow.
378
        let mut last_published_suggested_effort: i64 = last_published_suggested_effort.into();
379

            
380
        let netdir_provider = self
381
            .0
382
            .read()
383
            .expect("Lock poisoned")
384
            .netdir_provider
385
            .clone();
386
        let net_params = netdir_provider
387
            .wait_for_netdir(tor_netdir::Timeliness::Timely)
388
            .await?
389
            .params()
390
            .clone();
391

            
392
        loop {
393
            let next_update_time = self.rotate_seeds_if_expiring().await;
394

            
395
            // Update the suggested effort, if needed
396
            if runtime.now() - last_suggested_effort_update >= HS_UPDATE_PERIOD {
397
                let (tps_to_update, mut publisher_update_tx) = {
398
                    let mut tps_to_update = vec![];
399

            
400
                    let inner = self.0.read().expect("Lock poisoned");
401

            
402
                    inner.rend_request_rx.update_suggested_effort(&net_params);
403
                    last_suggested_effort_update = runtime.now();
404
                    let new_suggested_effort: u32 =
405
                        (*inner.suggested_effort.lock().expect("Lock poisoned")).into();
406
                    let new_suggested_effort: i64 = new_suggested_effort.into();
407

            
408
                    let percent_change = (new_suggested_effort - last_published_suggested_effort)
409
                        as f64
410
                        / last_published_suggested_effort as f64;
411
                    if percent_change.abs() >= SUGGESTED_EFFORT_DEADZONE {
412
                        last_published_suggested_effort = new_suggested_effort;
413

            
414
                        tps_to_update = inner.seeds.iter().map(|x| *x.0).collect();
415
                    }
416

            
417
                    let publisher_update_tx = inner.publisher_update_tx.clone();
418
                    (tps_to_update, publisher_update_tx)
419
                };
420

            
421
                for time_period in tps_to_update {
422
                    let _ = publisher_update_tx.send(time_period).await;
423
                }
424
            }
425

            
426
            let suggested_effort_update_delay = HS_UPDATE_PERIOD.saturating_sub(
427
                runtime
428
                    .now()
429
                    .saturating_duration_since(last_suggested_effort_update),
430
            );
431

            
432
            // A new TimePeriod that we don't know about (and thus that isn't in next_update_time)
433
            // might get added at any point. Making sure that our maximum delay is the minimum
434
            // amount of time that it might take for a seed to expire means that we can be sure
435
            // that we will rotate newly-added seeds properly.
436
            const MAX_DELAY: Duration = Duration::from_secs(EXPIRATION_TIME_MINS_MIN * 60)
437
                .checked_sub(SEED_EARLY_ROTATION_TIME)
438
                .expect("SEED_EARLY_ROTATION_TIME too high, or EXPIRATION_TIME_MINS_MIN too low.");
439
            let delay = next_update_time
440
                .map(|x| x.duration_since(SystemTime::get()).unwrap_or(MAX_DELAY))
441
                .unwrap_or(MAX_DELAY)
442
                .min(MAX_DELAY)
443
                .min(suggested_effort_update_delay);
444

            
445
            tracing::debug!(next_wakeup = ?delay, "Recalculated PoW seeds.");
446

            
447
            runtime.sleep(delay).await;
448
        }
449
    }
450

            
451
    /// Make a randomized seed expiration time.
452
4
    fn make_next_expiration_time<Rng: rand::Rng + CryptoRng>(rng: &mut Rng) -> SystemTime {
453
4
        SystemTime::get()
454
4
            + Duration::from_secs(
455
4
                60 * rng
456
4
                    .gen_range_checked(EXPIRATION_TIME_MINS_MIN..=EXPIRATION_TIME_MINS_MAX)
457
4
                    .expect("Can't generate expiration_time"),
458
4
            )
459
4
    }
460

            
461
    /// Make a ner [`Verifier`] for a given [`TimePeriod`] and [`Seed`].
462
    ///
463
    /// If a key is not available for this TP, returns None.
464
    ///
465
    /// This takes individual arguments instead of `&self` to avoid getting into any trouble with
466
    /// locking.
467
4
    fn make_verifier(
468
4
        keymgr: &Arc<KeyMgr>,
469
4
        nickname: HsNickname,
470
4
        time_period: TimePeriod,
471
4
        seed: Seed,
472
4
        config: &OnionServiceConfig,
473
4
    ) -> Option<Verifier> {
474
4
        let blind_id_spec = BlindIdPublicKeySpecifier::new(nickname, time_period);
475
4
        let blind_id_key = match keymgr.get::<HsBlindIdKey>(&blind_id_spec) {
476
4
            Ok(blind_id_key) => blind_id_key,
477
            Err(err) => {
478
                warn_report!(err, "KeyMgr error when getting blinded ID key for PoW");
479
                None
480
            }
481
        };
482
4
        let instance = Instance::new(blind_id_key?.id(), seed);
483
4
        let mut equix = EquiXBuilder::default();
484
4
        if *config.disable_pow_compilation() {
485
            equix.runtime(RuntimeOption::InterpretOnly);
486
4
        }
487
4
        Some(Verifier::new_with_equix(instance, equix))
488
4
    }
489

            
490
    /// Calculate a time when we want to rotate a seed, slightly before it expires, in order to
491
    /// ensure that clients don't ever download a seed that is already out of date.
492
    fn calculate_early_rotation_time(expiration_time: SystemTime) -> SystemTime {
493
        // Underflow cannot happen because:
494
        //
495
        // * We set the expiration time to the current time plus at least the minimum
496
        //   expiration time
497
        // * We know (backed up by a compile-time assertion) that SEED_EARLY_ROTATION_TIME is
498
        //   less than the minimum expiration time.
499
        //
500
        // Thus, the only way this subtraction can underflow is if the system time at the
501
        // moment we set the expiration time was before the epoch, which is not possible on
502
        // reasonable platforms.
503
        expiration_time
504
            .checked_sub(SEED_EARLY_ROTATION_TIME)
505
            .expect("PoW seed expiration underflow")
506
    }
507

            
508
    /// Rotate any seeds that will expire soon.
509
    ///
510
    /// This also pokes the publisher when needed to cause rotated seeds to be published.
511
    ///
512
    /// Returns the next time this function should be called again.
513
    async fn rotate_seeds_if_expiring(&self) -> Option<SystemTime> {
514
        let mut expired_verifiers = vec![];
515
        let mut new_verifiers = vec![];
516

            
517
        let mut update_times = vec![];
518
        let mut updated_tps = vec![];
519
        let mut expired_tps = vec![];
520

            
521
        let mut publisher_update_tx = {
522
            let mut state = self.0.write().expect("Lock poisoned");
523

            
524
            let config = state.config_rx.borrow().clone();
525
            let keymgr = state.keymgr.clone();
526
            let nickname = state.nickname.clone();
527

            
528
            for (time_period, info) in state.seeds.iter_mut() {
529
                let rotation_time = Self::calculate_early_rotation_time(info.next_expiration_time);
530
                update_times.push(rotation_time);
531

            
532
                if rotation_time <= SystemTime::get() {
533
                    // This does not allow for easy testing, but because we're in a async function, it's
534
                    // non-trivial to pass in a Rng from the outside world. If we end up writing tests that
535
                    // require that, we can take a function to generate a Rng, but for now, just using the
536
                    // thread rng is fine.
537
                    let mut rng = rand::rng();
538

            
539
                    let seed = Seed::new(&mut rng, None);
540
                    let verifier = match Self::make_verifier(
541
                        &keymgr,
542
                        nickname.clone(),
543
                        *time_period,
544
                        seed.clone(),
545
                        &config,
546
                    ) {
547
                        Some(verifier) => verifier,
548
                        None => {
549
                            // We use not having a key for a given TP as the signal that we should
550
                            // stop keeping track of seeds for that TP.
551
                            expired_tps.push(*time_period);
552
                            continue;
553
                        }
554
                    };
555

            
556
                    let expired_seed = if info.seeds.is_full() {
557
                        info.seeds.pop_at(0)
558
                    } else {
559
                        None
560
                    };
561
                    // .push() is safe, since we just made space above.
562
                    info.seeds.push(seed.clone());
563
                    info.next_expiration_time = Self::make_next_expiration_time(&mut rng);
564
                    update_times.push(info.next_expiration_time);
565

            
566
                    // Make a note to add the new verifier and remove the old one.
567
                    new_verifiers.push((seed, verifier));
568
                    if let Some(expired_seed) = expired_seed {
569
                        expired_verifiers.push(expired_seed.head());
570
                    }
571

            
572
                    // Tell the publisher to update this TP
573
                    updated_tps.push(*time_period);
574

            
575
                    tracing::debug!(time_period = ?time_period, "Rotated PoW seed");
576
                }
577
            }
578

            
579
            for time_period in expired_tps {
580
                if let Some(seeds) = state.seeds.remove(&time_period) {
581
                    for seed in seeds.seeds {
582
                        state.remove_verifier(&seed.head());
583
                    }
584
                }
585
            }
586

            
587
            for (seed, verifier) in new_verifiers {
588
                let replay_log = Mutex::new(
589
                    PowNonceReplayLog::new_logged(&state.instance_dir, &seed)
590
                        .expect("Couldn't make ReplayLog."),
591
                );
592
                state.verifiers.insert(seed.head(), (verifier, replay_log));
593
            }
594

            
595
            for seed_head in expired_verifiers {
596
                state.remove_verifier(&seed_head);
597
            }
598

            
599
            let record = state.to_record();
600
            if let Err(err) = state.storage_handle.store(&record) {
601
                warn_report!(err, "Error saving PoW state");
602
            }
603

            
604
            state.publisher_update_tx.clone()
605
        };
606

            
607
        for time_period in updated_tps {
608
            if let Err(err) = publisher_update_tx.send(time_period).await {
609
                warn_report!(err, "Couldn't send update message to publisher");
610
            }
611
        }
612

            
613
        update_times.iter().min().cloned()
614
    }
615

            
616
    /// Get [`PowParams`] for a given [`TimePeriod`].
617
    ///
618
    /// If we don't have any [`Seed`]s for the requested period, generate them. This is the only
619
    /// way that [`PowManagerGeneric`] learns about new [`TimePeriod`]s.
620
72
    pub(crate) fn get_pow_params<Rng: rand::Rng + CryptoRng>(
621
72
        self: &Arc<Self>,
622
72
        time_period: TimePeriod,
623
72
        rng: &mut Rng,
624
72
    ) -> Result<PowParams, PowError> {
625
        let (seed_and_expiration, suggested_effort) = {
626
72
            let state = self.0.read().expect("Lock poisoned");
627
72
            let seed = state
628
72
                .seeds
629
72
                .get(&time_period)
630
68
                .and_then(|x| Some((x.seeds.last()?.clone(), x.next_expiration_time)));
631
72
            let suggested_effort = *state.suggested_effort.lock().expect("Lock poisoned");
632
72
            (seed, suggested_effort)
633
        };
634

            
635
72
        let (seed, expiration) = match seed_and_expiration {
636
68
            Some(seed) => seed,
637
            None => {
638
                // We don't have a seed for this time period, so we need to generate one.
639

            
640
4
                let seed = Seed::new(rng, None);
641
4
                let next_expiration_time = Self::make_next_expiration_time(rng);
642

            
643
4
                let mut seeds = ArrayVec::new();
644
4
                seeds.push(seed.clone());
645

            
646
4
                let mut state = self.0.write().expect("Lock poisoned");
647

            
648
4
                state.seeds.insert(
649
4
                    time_period,
650
4
                    SeedsForTimePeriod {
651
4
                        seeds,
652
4
                        next_expiration_time,
653
4
                    },
654
4
                );
655

            
656
4
                let verifier = Self::make_verifier(
657
4
                    &state.keymgr,
658
4
                    state.nickname.clone(),
659
4
                    time_period,
660
4
                    seed.clone(),
661
4
                    &state.config_rx.borrow(),
662
4
                )
663
4
                .ok_or(PowError::MissingKey)?;
664

            
665
                let replay_log =
666
4
                    Mutex::new(PowNonceReplayLog::new_logged(&state.instance_dir, &seed)?);
667
4
                state.verifiers.insert(seed.head(), (verifier, replay_log));
668

            
669
4
                let record = state.to_record();
670
4
                state.storage_handle.store(&record)?;
671

            
672
4
                (seed, next_expiration_time)
673
            }
674
        };
675

            
676
72
        Ok(PowParams::V1(PowParamsV1::new(
677
72
            TimeRangeBound::new(seed, ..expiration),
678
72
            suggested_effort,
679
72
        )))
680
72
    }
681

            
682
    /// Verify a PoW solve.
683
    fn check_solve(self: &Arc<Self>, solve: &ProofOfWorkV1) -> Result<(), PowSolveError> {
684
        // Note that we put the nonce into the replay log before we check the solve. While this
685
        // might not be ideal, it's not a problem and is probably the most reasonable thing to do.
686
        // See commit bc5b313028 for a more full explanation.
687
        {
688
            let state = self.0.write().expect("Lock poisoned");
689
            let mut replay_log = match state.verifiers.get(&solve.seed_head()) {
690
                Some((_, replay_log)) => replay_log.lock().expect("Lock poisoned"),
691
                None => return Err(PowSolveError::InvalidSeedHead),
692
            };
693
            replay_log
694
                .check_for_replay(solve.nonce())
695
                .map_err(PowSolveError::NonceReplay)?;
696
        }
697

            
698
        // TODO: Once RwLock::downgrade is stabilized, it would make sense to use it here...
699

            
700
        let state = self.0.read().expect("Lock poisoned");
701
        let verifier = match state.verifiers.get(&solve.seed_head()) {
702
            Some((verifier, _)) => verifier,
703
            None => return Err(PowSolveError::InvalidSeedHead),
704
        };
705

            
706
        let solution = match Solution::try_from_bytes(
707
            solve.nonce().clone(),
708
            solve.effort(),
709
            solve.seed_head(),
710
            solve.solution(),
711
        ) {
712
            Ok(solution) => solution,
713
            Err(err) => return Err(PowSolveError::InvalidEquixSolution(err)),
714
        };
715

            
716
        match verifier.check(&solution) {
717
            Ok(()) => Ok(()),
718
            Err(err) => Err(PowSolveError::InvalidSolve(err)),
719
        }
720
    }
721
}
722

            
723
/// Trait to allow mocking PowManagerGeneric in tests.
724
trait MockablePowManager {
725
    /// Verify a PoW solve.
726
    fn check_solve(self: &Arc<Self>, solve: &ProofOfWorkV1) -> Result<(), PowSolveError>;
727
}
728

            
729
impl<R: Runtime> MockablePowManager for PowManager<R> {
730
    fn check_solve(self: &Arc<Self>, solve: &ProofOfWorkV1) -> Result<(), PowSolveError> {
731
        PowManager::check_solve(self, solve)
732
    }
733
}
734

            
735
/// Trait to allow mocking RendRequest in tests.
736
pub(crate) trait MockableRendRequest {
737
    /// Get the proof-of-work extension associated with this request.
738
    fn proof_of_work(&self) -> Result<Option<&ProofOfWork>, rend_handshake::IntroRequestError>;
739
}
740

            
741
impl MockableRendRequest for RendRequest {
742
    fn proof_of_work(&self) -> Result<Option<&ProofOfWork>, rend_handshake::IntroRequestError> {
743
        Ok(self
744
            .intro_request()?
745
            .intro_payload()
746
            .proof_of_work_extension())
747
    }
748
}
749

            
750
/// Wrapper around [`RendRequest`] that implements [`std::cmp::Ord`] to sort by [`Effort`] and time.
751
#[derive(Debug)]
752
struct RendRequestOrdByEffort<Q> {
753
    /// The underlying request.
754
    request: Q,
755
    /// The proof-of-work options, if given.
756
    pow: Option<ProofOfWorkV1>,
757
    /// The maximum effort allowed. If the effort of this request is higher than this, it will be
758
    /// treated as though it is this value.
759
    max_effort: Effort,
760
    /// When this request was received, used for ordreing if the effort values are the same.
761
    recv_time: Instant,
762
    /// Unique number for this request, which is used for ordering among requests with the same
763
    /// timestamp.
764
    ///
765
    /// This is intended to be monotonically increasing, although it may overflow. Overflows are
766
    /// not handled in any special way, given that they are a edge case of an edge case, and
767
    /// ordering among requests that came in at the same instant is not important.
768
    request_num: u64,
769
}
770

            
771
impl<Q: MockableRendRequest> RendRequestOrdByEffort<Q> {
772
    /// Create a new [`RendRequestOrdByEffort`].
773
738
    fn new(
774
738
        request: Q,
775
738
        max_effort: Effort,
776
738
        request_num: u64,
777
738
    ) -> Result<Self, rend_handshake::IntroRequestError> {
778
738
        let pow = match request.proof_of_work()?.cloned() {
779
722
            Some(ProofOfWork::V1(pow)) => Some(pow),
780
16
            None | Some(_) => None,
781
        };
782

            
783
738
        Ok(Self {
784
738
            request,
785
738
            pow,
786
738
            max_effort,
787
738
            recv_time: Instant::get(),
788
738
            request_num,
789
738
        })
790
738
    }
791
}
792

            
793
impl<Q: MockableRendRequest> Ord for RendRequestOrdByEffort<Q> {
794
3390
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
795
3390
        let self_effort = self.pow.as_ref().map_or(Effort::zero(), |pow| {
796
3382
            Effort::min(pow.effort(), self.max_effort)
797
3382
        });
798
3390
        let other_effort = other.pow.as_ref().map_or(Effort::zero(), |pow| {
799
3382
            Effort::min(pow.effort(), other.max_effort)
800
3382
        });
801
3390
        match self_effort.cmp(&other_effort) {
802
            std::cmp::Ordering::Equal => {
803
                // Flip ordering, since we want the oldest ones to be handled first.
804
1484
                match other.recv_time.cmp(&self.recv_time) {
805
                    // Use request_num as a final tiebreaker, also flipping ordering (since
806
                    // lower-numbered requests should be older and thus come first)
807
                    std::cmp::Ordering::Equal => other.request_num.cmp(&self.request_num),
808
1484
                    not_equal => not_equal,
809
                }
810
            }
811
1906
            not_equal => not_equal,
812
        }
813
3390
    }
814
}
815

            
816
impl<Q: MockableRendRequest> PartialOrd for RendRequestOrdByEffort<Q> {
817
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
818
        Some(self.cmp(other))
819
    }
820
}
821

            
822
impl<Q: MockableRendRequest> PartialEq for RendRequestOrdByEffort<Q> {
823
    fn eq(&self, other: &Self) -> bool {
824
        let self_effort = self.pow.as_ref().map_or(Effort::zero(), |pow| {
825
            Effort::min(pow.effort(), self.max_effort)
826
        });
827
        let other_effort = other.pow.as_ref().map_or(Effort::zero(), |pow| {
828
            Effort::min(pow.effort(), other.max_effort)
829
        });
830
        self_effort == other_effort && self.recv_time == other.recv_time
831
    }
832
}
833

            
834
impl<Q: MockableRendRequest> Eq for RendRequestOrdByEffort<Q> {}
835

            
836
/// Implements [`Stream`] for incoming [`RendRequest`]s, using a priority queue system to dequeue
837
/// high-[`Effort`] requests first.
838
///
839
/// This is implemented on top of a [`mpsc::Receiver`]. There is a thread that dequeues from the
840
/// [`mpsc::Receiver`], checks the PoW solve, and if it is correct, adds it to a [`BTreeSet`],
841
/// which the [`Stream`] implementation reads from.
842
///
843
/// This is not particularly optimized — queueing and dequeuing use a [`Mutex`], so there may be
844
/// some contention there. It's possible there may be some fancy lockless (or more optimized)
845
/// priority queue that we could use, but we should properly benchmark things before trying to make
846
/// a optimization like that.
847
pub(crate) struct RendRequestReceiver<R, Q>(Arc<Mutex<RendRequestReceiverInner<R, Q>>>);
848

            
849
impl<R, Q> Clone for RendRequestReceiver<R, Q> {
850
60
    fn clone(&self) -> Self {
851
60
        Self(self.0.clone())
852
60
    }
853
}
854

            
855
/// Inner implementation for [`RendRequestReceiver`].
856
struct RendRequestReceiverInner<R, Q> {
857
    /// Internal priority queue of requests.
858
    queue: BTreeSet<RendRequestOrdByEffort<Q>>,
859

            
860
    /// Internal FIFO queue of requests used when PoW is disabled.
861
    ///
862
    /// We have this here to support switching back and forth between PoW enabled and disabled at
863
    /// runtime, although that isn't currently supported.
864
    queue_pow_disabled: VecDeque<Q>,
865

            
866
    /// Waker to inform async readers when there is a new message on the queue.
867
    waker: Option<Waker>,
868

            
869
    /// Runtime, used to get current time in a testable way.
870
    runtime: R,
871

            
872
    /// Nickname, use when reporting metrics.
873
    nickname: HsNickname,
874

            
875
    /// [`NetDirProvider`], for getting configuration values in consensus parameters.
876
    netdir_provider: Arc<dyn NetDirProvider>,
877

            
878
    /// Current configuration, used to see whether PoW is enabled or not.
879
    config_rx: postage::watch::Receiver<Arc<OnionServiceConfig>>,
880

            
881
    /// When the current update period started.
882
    update_period_start: Instant,
883
    /// Number of requests that were enqueued during the current update period, and had an effort
884
    /// greater than or equal to the suggested effort.
885
    num_enqueued_gte_suggested: usize,
886
    /// Number of requests that were dequeued during the current update period.
887
    num_dequeued: u32,
888
    /// Amount of time during the current update period that we spent with no requests in the
889
    /// queue.
890
    idle_time: Duration,
891
    /// Time that the queue last went from having items in it to not having items in it, or vice
892
    /// versa. This is used to update idle_time.
893
    last_transition: Instant,
894
    /// Sum of all effort values that were validated and enqueued during the current update period.
895
    total_effort: u64,
896

            
897
    /// Most recent published suggested effort value.
898
    ///
899
    /// We write to this, which is then published in the pow-params line by [`PowManagerGeneric`].
900
    suggested_effort: Arc<Mutex<Effort>>,
901

            
902
    /// Sender for reporting back onion service status.
903
    status_tx: PowManagerStatusSender,
904
}
905

            
906
impl<R: Runtime, Q: MockableRendRequest + Send + 'static> RendRequestReceiver<R, Q> {
907
    /// Create a new [`RendRequestReceiver`].
908
14
    fn new(
909
14
        runtime: R,
910
14
        nickname: HsNickname,
911
14
        suggested_effort: Arc<Mutex<Effort>>,
912
14
        netdir_provider: Arc<dyn NetDirProvider>,
913
14
        status_tx: PowManagerStatusSender,
914
14
        config_rx: postage::watch::Receiver<Arc<OnionServiceConfig>>,
915
14
    ) -> Self {
916
14
        let now = runtime.now();
917
14
        RendRequestReceiver(Arc::new(Mutex::new(RendRequestReceiverInner {
918
14
            queue: BTreeSet::new(),
919
14
            queue_pow_disabled: VecDeque::new(),
920
14
            waker: None,
921
14
            runtime,
922
14
            nickname,
923
14
            netdir_provider,
924
14
            config_rx,
925
14
            update_period_start: now,
926
14
            num_enqueued_gte_suggested: 0,
927
14
            num_dequeued: 0,
928
14
            idle_time: Duration::new(0, 0),
929
14
            last_transition: now,
930
14
            total_effort: 0,
931
14
            suggested_effort,
932
14
            status_tx,
933
14
        })))
934
14
    }
935

            
936
    // spawn_blocking executes immediately, but some of our abstractions make clippy not
937
    // realize this.
938
    #[allow(clippy::let_underscore_future)]
939
    /// Start helper thread to accept and validate [`RendRequest`]s.
940
14
    fn start_accept_thread<P: MockablePowManager + Send + Sync + 'static>(
941
14
        &self,
942
14
        runtime: R,
943
14
        pow_manager: Arc<P>,
944
14
        inner_receiver: mpsc::Receiver<Q>,
945
14
    ) {
946
14
        let receiver = self.clone();
947
14
        let runtime_clone = runtime.clone();
948
14
        let _ = runtime.clone().spawn_blocking(move || {
949
14
            if let Err(err) =
950
14
                receiver
951
14
                    .clone()
952
14
                    .accept_loop(&runtime_clone, &pow_manager, inner_receiver)
953
            {
954
                warn_report!(err, "PoW accept loop error!");
955
                receiver
956
                    .0
957
                    .lock()
958
                    .expect("Lock poisoned")
959
                    .status_tx
960
                    .send_broken(Problem::Pow(err));
961
14
            }
962
14
        });
963

            
964
14
        let receiver = self.clone();
965
14
        let _ = runtime.clone().spawn_blocking(move || {
966
14
            if let Err(err) = receiver.clone().expire_old_requests_loop(&runtime) {
967
14
                warn_report!(err, "PoW request expiration loop error!");
968
14
                receiver
969
14
                    .0
970
14
                    .lock()
971
14
                    .expect("Lock poisoned")
972
14
                    .status_tx
973
14
                    .send_broken(Problem::Pow(err));
974
            }
975
14
        });
976
14
    }
977

            
978
    /// Update the suggested effort value, as per the algorithm in prop362
979
16
    fn update_suggested_effort(&self, net_params: &NetParameters) {
980
16
        let mut inner = self.0.lock().expect("Lock poisoned");
981

            
982
16
        let decay_adjustment_fraction = net_params.hs_pow_v1_default_decay_adjustment.as_fraction();
983

            
984
16
        let update_period_duration = inner.runtime.now() - inner.update_period_start;
985
16
        if inner.num_dequeued != 0 && update_period_duration.as_millis() != 0 {
986
16
            let avg_request_duration = update_period_duration / inner.num_dequeued;
987
16
            let num_dequeued = f64::from(inner.num_dequeued);
988
16
            if inner.queue.is_empty() {
989
12
                let now = inner.runtime.now();
990
12
                let last_transition = inner.last_transition;
991
12
                inner.idle_time += now - last_transition;
992
4
            }
993
16
            let adjusted_idle_time = Duration::saturating_sub(
994
16
                inner.idle_time,
995
16
                avg_request_duration * inner.queue.len().try_into().expect("Queue too large."),
996
16
            );
997
            // TODO: use as_millis_f64 when stable
998
16
            let idle_fraction = f64::from_u128(adjusted_idle_time.as_millis())
999
16
                .expect("Conversion error")
16
                / f64::from_u128(update_period_duration.as_millis()).expect("Conversion error");
16
            let busy_fraction = 1.0 - idle_fraction;
16
            let mut suggested_effort = inner.suggested_effort.lock().expect("Lock poisoned");
16
            let suggested_effort_inner: u32 = (*suggested_effort).into();
16
            let suggested_effort_inner_f64 = f64::from(suggested_effort_inner);
16
            if busy_fraction == 0.0 {
                // Infallible, this takes the previous suggested effort and
                // reduces it, so overflow is not possible, since the netdir
                // code enforces that it's in the range of 0.0 - 0.99.
                debug_assert!(decay_adjustment_fraction < 1.0);
                let new_suggested_effort =
                    u32::from_f64(suggested_effort_inner_f64 * decay_adjustment_fraction)
                        .expect("Conversion error");
                *suggested_effort = Effort::from(new_suggested_effort);
            } else {
                // The rust `as` operator is used here to provide saturating conversions,
                // to avoid panicking if the effort calculation would be lossy due to extremely
                // large values. In practice, the values should be low enough that this shouldn't
                // come up, but it is worth being defensive.
16
                let theoretical_num_dequeued = num_dequeued * (1.0 / busy_fraction);
16
                let num_enqueued_gte_suggested_f64 = inner.num_enqueued_gte_suggested as f64;
16
                if num_enqueued_gte_suggested_f64 >= theoretical_num_dequeued {
4
                    let effort_per_dequeued = (inner.total_effort as f64 / num_dequeued) as u32;
4
                    *suggested_effort = Effort::from(std::cmp::max(
4
                        effort_per_dequeued,
4
                        suggested_effort_inner + 1,
4
                    ));
                } else {
12
                    let decay = num_enqueued_gte_suggested_f64 / theoretical_num_dequeued;
12
                    let adjusted_decay = decay + ((1.0 - decay) * decay_adjustment_fraction);
12
                    let new_suggested_effort = (suggested_effort_inner_f64 * adjusted_decay) as u32;
12
                    *suggested_effort = Effort::from(new_suggested_effort);
                }
            }
16
            drop(suggested_effort);
        }
16
        let now = inner.runtime.now();
16
        inner.update_period_start = now;
16
        inner.num_enqueued_gte_suggested = 0;
16
        inner.num_dequeued = 0;
16
        inner.idle_time = Duration::new(0, 0);
16
        inner.last_transition = now;
16
        inner.total_effort = 0;
16
    }
    /// Loop to accept message from the wrapped [`mpsc::Receiver`], validate PoW solves, and
    /// enqueue onto the priority queue.
14
    fn accept_loop<P: MockablePowManager>(
14
        self,
14
        runtime: &R,
14
        pow_manager: &Arc<P>,
14
        mut receiver: mpsc::Receiver<Q>,
14
    ) -> Result<(), PowError> {
14
        let mut request_num = 0;
14
        let netdir_provider = self
14
            .0
14
            .lock()
14
            .expect("Lock poisoned")
14
            .netdir_provider
14
            .clone();
14
        let net_params = runtime
14
            .reenter_block_on(netdir_provider.wait_for_netdir(tor_netdir::Timeliness::Timely))?
            .params()
            .clone();
14
        let max_effort: u32 = net_params
14
            .hs_pow_v1_max_effort
14
            .get()
14
            .try_into()
14
            .expect("Bounded i32 not in range of u32?!");
14
        let max_effort = Effort::from(max_effort);
14
        let config_rx = self.0.lock().expect("Lock poisoned").config_rx.clone();
14
        let nickname = self.0.lock().expect("Lock poisoned").nickname.to_string();
        cfg_if::cfg_if! {
            if #[cfg(feature = "metrics")] {
14
                let counter_rendrequest_error_total = metrics::counter!(
                    description: "Number of errors processing rendezvous requests in the PoW subsystem.",
                    unit: metrics::Unit::Count,
14
                    "arti_hss_pow_rendrequest_error_total",
14
                    "nickname" => nickname.clone()
                );
14
                let counter_rendrequest_verification_failure = metrics::counter!(
                    description: "Number of PoW verification failures.",
                    unit: metrics::Unit::Count,
14
                    "arti_hss_pow_rendrequest_verification_failure_total",
14
                    "nickname" => nickname.clone()
                );
14
                let counter_rend_queue_overflow = metrics::counter!(
                    description: "Number of times the PoW rendezvous request queue overflowed, leading to dropped requests.",
                    unit: metrics::Unit::Count,
14
                    "arti_hss_pow_rend_queue_overflow_total",
14
                    "nickname" => nickname.clone()
                );
14
                let counter_rendrequest_enqueued = metrics::counter!(
                    description: "Number of rendezvous requests enqueued in the PoW subsystem.",
                    unit: metrics::Unit::Count,
14
                    "arti_hss_pow_rendrequest_enqueued_total",
14
                    "nickname" => nickname.clone()
                );
14
                let histogram_rendrequest_effort = metrics::histogram!(
                    description: "Histogram of effort values seen for incoming PoW requests.",
14
                    "arti_hss_pow_rendrequest_effort_hist",
14
                    "nickname" => nickname.clone()
                );
            }
        }
        loop {
760
            let rend_request = if let Some(rend_request) = runtime.reenter_block_on(receiver.next())
            {
746
                rend_request
            } else {
14
                self.0
14
                    .lock()
14
                    .expect("Lock poisoned")
14
                    .status_tx
14
                    .send_shutdown();
14
                return Ok(());
            };
746
            if config_rx.borrow().enable_pow {
                let rend_request =
738
                    match RendRequestOrdByEffort::new(rend_request, max_effort, request_num) {
738
                        Ok(rend_request) => rend_request,
                        Err(err) => {
                            #[cfg(feature = "metrics")]
                            counter_rendrequest_error_total.increment(1);
                            tracing::trace!(?err, "Error processing RendRequest");
                            continue;
                        }
                    };
738
                request_num = request_num.wrapping_add(1);
738
                if let Some(ref pow) = rend_request.pow {
722
                    if let Err(err) = pow_manager.check_solve(pow) {
2
                        tracing::debug!(?err, "PoW verification failed");
                        #[cfg(feature = "metrics")]
2
                        counter_rendrequest_verification_failure.increment(1);
2
                        continue;
                    } else {
                        #[cfg(feature = "metrics")]
                        {
720
                            let effort: u32 = pow.effort().into();
720
                            histogram_rendrequest_effort.record(effort);
                        }
                    }
16
                }
736
                let mut inner = self.0.lock().expect("Lock poisoned");
736
                if inner.queue.is_empty() {
26
                    let now = runtime.now();
26
                    let last_transition = inner.last_transition;
26
                    inner.idle_time += now - last_transition;
26
                    inner.last_transition = now;
710
                }
736
                if let Some(ref request_pow) = rend_request.pow {
720
                    if request_pow.effort()
720
                        >= *inner.suggested_effort.lock().expect("Lock poisoned")
                    {
720
                        inner.num_enqueued_gte_suggested += 1;
720
                        let effort: u32 = request_pow.effort().into();
720
                        if let Some(total_effort) = inner.total_effort.checked_add(effort.into()) {
720
                            inner.total_effort = total_effort;
                        } else {
                            tracing::warn!(
                                "PoW total_effort would overflow. The total effort has been capped, but this is not expected to happen - please file a bug report with logs and information about the circumstances under which this occurred."
                            );
                            inner.total_effort = u64::MAX;
                        }
                    }
16
                }
736
                inner.queue.insert(rend_request);
736
                if inner.queue.len() > config_rx.borrow().pow_rend_queue_depth {
2
                    let dropped_request = inner.queue.pop_first();
                    #[cfg(feature = "metrics")]
2
                    counter_rend_queue_overflow.increment(1);
2
                    tracing::debug!(
2
                        dropped_effort = ?dropped_request.map(|x| x.pow.map(|x| x.effort())),
                        "RendRequest queue full, dropping request."
                    );
734
                }
                #[cfg(feature = "metrics")]
736
                counter_rendrequest_enqueued.increment(1);
736
                if let Some(waker) = &inner.waker {
18
                    waker.wake_by_ref();
718
                }
            } else {
                // TODO (#2082): when allowing enable_pow to be toggled at runtime, we will need to
                // do bookkeeping here, as above. Perhaps it can be refactored nicely so the
                // bookkeeping code can be the same in both cases.
8
                let mut inner = self.0.lock().expect("Lock poisoned");
8
                inner.queue_pow_disabled.push_back(rend_request);
                #[cfg(feature = "metrics")]
8
                counter_rendrequest_enqueued.increment(1);
8
                if let Some(waker) = &inner.waker {
                    waker.wake_by_ref();
8
                }
            }
        }
14
    }
    /// Loop to check for messages that are older than our timeout and remove them from the queue.
14
    fn expire_old_requests_loop(self, runtime: &R) -> Result<(), PowError> {
14
        let netdir_provider = self
14
            .0
14
            .lock()
14
            .expect("Lock poisoned")
14
            .netdir_provider
14
            .clone();
14
        let net_params = runtime
14
            .reenter_block_on(netdir_provider.wait_for_netdir(tor_netdir::Timeliness::Timely))?
            .params()
            .clone();
14
        let max_age: Duration = net_params
14
            .hs_pow_v1_service_intro_timeout
14
            .try_into()
14
            .expect(
14
                "Couldn't convert HiddenServiceProofOfWorkV1ServiceIntroTimeoutSeconds to Duration",
14
            );
14
        let nickname = self.0.lock().expect("Lock poisoned").nickname.to_string();
        #[cfg(feature = "metrics")]
14
        let counter_rendrequest_expired = metrics::counter!("arti_hss_pow_rendrequest_expired_total", "nickname" => nickname.clone());
        loop {
114
            let inner = self.0.lock().expect("Lock poisoned");
            // Wake up when the oldest request will reach the expiration age, or, if there are no
            // items currently in the queue, wait for the maximum age.
114
            let wait_time = inner
114
                .queue
114
                .first()
2
                .map(|r| {
2
                    max_age.saturating_sub(runtime.now().saturating_duration_since(r.recv_time))
2
                })
114
                .unwrap_or(max_age);
114
            drop(inner);
114
            runtime.reenter_block_on(runtime.sleep(wait_time));
114
            let mut inner = self.0.lock().expect("Lock poisoned");
114
            let now = runtime.now();
114
            let prev_len = inner.queue.len();
114
            inner.queue.retain(|r| now - r.recv_time < max_age);
114
            let dropped = prev_len - inner.queue.len();
114
            tracing::trace!(dropped, "Expired timed out RendRequests");
            #[cfg(feature = "metrics")]
100
            counter_rendrequest_expired
100
                .increment(dropped.try_into().expect("usize overflowed u64!"));
        }
    }
}
impl<R: Runtime, Q: MockableRendRequest> Stream for RendRequestReceiver<R, Q> {
    type Item = Q;
754
    fn poll_next(
754
        self: std::pin::Pin<&mut Self>,
754
        cx: &mut std::task::Context<'_>,
754
    ) -> std::task::Poll<Option<Self::Item>> {
754
        let mut inner = self.get_mut().0.lock().expect("Lock poisoned");
754
        if inner.config_rx.borrow().enable_pow {
746
            match inner.queue.pop_last() {
                Some(item) => {
732
                    inner.num_dequeued += 1;
732
                    if inner.queue.is_empty() {
24
                        inner.last_transition = inner.runtime.now();
708
                    }
732
                    std::task::Poll::Ready(Some(item.request))
                }
                None => {
14
                    inner.waker = Some(cx.waker().clone());
14
                    std::task::Poll::Pending
                }
            }
8
        } else if let Some(request) = inner.queue_pow_disabled.pop_front() {
            // TODO (#2082): when we allow changing enable_pow at runtime, we will need to do
            // bookkeeping here.
8
            std::task::Poll::Ready(Some(request))
        } else {
            inner.waker = Some(cx.waker().clone());
            std::task::Poll::Pending
        }
754
    }
}
#[cfg(test)]
mod test {
    #![allow(clippy::unwrap_used)]
    use crate::config::OnionServiceConfigBuilder;
    use crate::status::{OnionServiceStatus, StatusSender};
    use super::*;
    use futures::FutureExt;
    use tor_hscrypto::pow::v1::{Nonce, SolutionByteArray};
    use tor_netdir::{testnet, testprovider::TestNetDirProvider};
    use tor_rtmock::MockRuntime;
    struct MockPowManager;
    #[derive(Debug)]
    struct MockRendRequest {
        id: usize,
        pow: Option<ProofOfWork>,
    }
    impl MockablePowManager for MockPowManager {
        fn check_solve(self: &Arc<Self>, solve: &ProofOfWorkV1) -> Result<(), PowSolveError> {
            // For testing, treat all zeros as the only valid solve. Error is chosen arbitrarily.
            if solve.solution() == &[0; 16] {
                Ok(())
            } else {
                Err(PowSolveError::InvalidSeedHead)
            }
        }
    }
    impl MockableRendRequest for MockRendRequest {
        fn proof_of_work(&self) -> Result<Option<&ProofOfWork>, rend_handshake::IntroRequestError> {
            Ok(self.pow.as_ref())
        }
    }
    fn make_req(id: usize, effort: Option<u32>) -> MockRendRequest {
        MockRendRequest {
            id,
            pow: effort.map(|e| {
                ProofOfWork::V1(ProofOfWorkV1::new(
                    Nonce::from([0; 16]),
                    Effort::from(e),
                    SeedHead::from([0; 4]),
                    SolutionByteArray::from([0; 16]),
                ))
            }),
        }
    }
    fn make_req_invalid(id: usize, effort: u32) -> MockRendRequest {
        MockRendRequest {
            id,
            pow: Some(ProofOfWork::V1(ProofOfWorkV1::new(
                Nonce::from([0; 16]),
                Effort::from(effort),
                SeedHead::from([0; 4]),
                SolutionByteArray::from([1; 16]),
            ))),
        }
    }
    #[allow(clippy::type_complexity)]
    fn make_test_receiver(
        runtime: &MockRuntime,
        netdir_params: Vec<(String, i32)>,
        config: Option<OnionServiceConfig>,
    ) -> (
        RendRequestReceiver<MockRuntime, MockRendRequest>,
        mpsc::Sender<MockRendRequest>,
        Arc<Mutex<Effort>>,
        NetParameters,
        postage::watch::Sender<Arc<OnionServiceConfig>>,
    ) {
        let pow_manager = Arc::new(MockPowManager);
        let suggested_effort = Arc::new(Mutex::new(Effort::zero()));
        let netdir = testnet::construct_custom_netdir_with_params(
            testnet::simple_net_func,
            netdir_params,
            None,
        )
        .unwrap()
        .unwrap_if_sufficient()
        .unwrap();
        let net_params = netdir.params().clone();
        let netdir_provider: Arc<TestNetDirProvider> = Arc::new(netdir.into());
        let status_tx = StatusSender::new(OnionServiceStatus::new_shutdown()).into();
        let nickname = HsNickname::new("test-hs".to_string()).unwrap();
        let (config_tx, config_rx) =
            postage::watch::channel_with(Arc::new(config.unwrap_or_else(|| {
                OnionServiceConfigBuilder::default()
                    .nickname(nickname.clone())
                    .enable_pow(true)
                    .build()
                    .unwrap()
            })));
        let receiver: RendRequestReceiver<_, MockRendRequest> = RendRequestReceiver::new(
            runtime.clone(),
            nickname.clone(),
            suggested_effort.clone(),
            netdir_provider,
            status_tx,
            config_rx,
        );
        let (tx, rx) = mpsc::channel(32);
        receiver.start_accept_thread(runtime.clone(), pow_manager, rx);
        (receiver, tx, suggested_effort, net_params, config_tx)
    }
    #[test]
    fn test_basic_pow_ordering() {
        MockRuntime::test_with_various(|runtime| async move {
            let (mut receiver, mut tx, _suggested_effort, _net_params, _config_tx) =
                make_test_receiver(&runtime, vec![], None);
            // Request with no PoW
            tx.send(make_req(0, None)).await.unwrap();
            assert_eq!(receiver.next().await.unwrap().id, 0);
            // Request with PoW
            tx.send(make_req(1, Some(0))).await.unwrap();
            assert_eq!(receiver.next().await.unwrap().id, 1);
            // Request with effort is before request with zero effort
            tx.send(make_req(2, Some(0))).await.unwrap();
            tx.send(make_req(3, Some(16))).await.unwrap();
            runtime.progress_until_stalled().await;
            assert_eq!(receiver.next().await.unwrap().id, 3);
            assert_eq!(receiver.next().await.unwrap().id, 2);
            // Invalid solves are dropped
            tx.send(make_req_invalid(4, 32)).await.unwrap();
            tx.send(make_req(5, Some(16))).await.unwrap();
            runtime.progress_until_stalled().await;
            assert_eq!(receiver.next().await.unwrap().id, 5);
            assert_eq!(receiver.0.lock().unwrap().queue.len(), 0);
        });
    }
    #[test]
    fn test_suggested_effort_increase() {
        MockRuntime::test_with_various(|runtime| async move {
            let (mut receiver, mut tx, suggested_effort, net_params, _config_tx) =
                make_test_receiver(
                    &runtime,
                    vec![(
                        "HiddenServiceProofOfWorkV1ServiceIntroTimeoutSeconds".to_string(),
                        60000,
                    )],
                    None,
                );
            // Get through all the requests in plenty of time, no increase
            for n in 0..128 {
                tx.send(make_req(n, Some(0))).await.unwrap();
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
            for _ in 0..128 {
                receiver.next().await.unwrap();
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
            receiver.update_suggested_effort(&net_params);
            assert_eq!(suggested_effort.lock().unwrap().clone(), Effort::zero());
            // Requests left in the queue with zero suggested effort, suggested effort should
            // increase
            for n in 0..128 {
                tx.send(make_req(n, Some(0))).await.unwrap();
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
            for _ in 0..64 {
                receiver.next().await.unwrap();
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
            receiver.update_suggested_effort(&net_params);
            let mut new_suggested_effort = *suggested_effort.lock().unwrap();
            assert!(new_suggested_effort > Effort::zero());
            // We keep on being behind, effort should increase again.
            for n in 0..64 {
                tx.send(make_req(n, Some(new_suggested_effort.into())))
                    .await
                    .unwrap();
            }
            receiver.next().await.unwrap();
            runtime.advance_by(HS_UPDATE_PERIOD).await;
            receiver.update_suggested_effort(&net_params);
            let mut old_suggested_effort = new_suggested_effort;
            new_suggested_effort = *suggested_effort.lock().unwrap();
            assert!(new_suggested_effort > old_suggested_effort);
            // We catch up now, effort should start dropping, but not be zero immediately.
            for n in 0..32 {
                tx.send(make_req(n, Some(new_suggested_effort.into())))
                    .await
                    .unwrap();
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 16 * 15).await;
            while receiver.next().now_or_never().is_some() {
                // Keep going...
            }
            runtime.advance_by(HS_UPDATE_PERIOD / 16).await;
            receiver.update_suggested_effort(&net_params);
            old_suggested_effort = new_suggested_effort;
            new_suggested_effort = *suggested_effort.lock().unwrap();
            assert!(new_suggested_effort < old_suggested_effort);
            assert!(new_suggested_effort > Effort::zero());
            // Effort will drop to zero eventually
            let mut num_loops = 0;
            loop {
                tx.send(make_req(0, Some(new_suggested_effort.into())))
                    .await
                    .unwrap();
                runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
                while receiver.next().now_or_never().is_some() {
                    // Keep going...
                }
                runtime.advance_by(HS_UPDATE_PERIOD / 2).await;
                receiver.update_suggested_effort(&net_params);
                old_suggested_effort = new_suggested_effort;
                new_suggested_effort = *suggested_effort.lock().unwrap();
                assert!(new_suggested_effort < old_suggested_effort);
                if new_suggested_effort == Effort::zero() {
                    break;
                }
                num_loops += 1;
                if num_loops > 5 {
                    panic!("Took too long for suggested effort to fall!");
                }
            }
        });
    }
    #[test]
    fn test_rendrequest_timeout() {
        MockRuntime::test_with_various(|runtime| async move {
            let (receiver, mut tx, _suggested_effort, net_params, _config_tx) =
                make_test_receiver(&runtime, vec![], None);
            let r0 = MockRendRequest { id: 0, pow: None };
            tx.send(r0).await.unwrap();
            let max_age: Duration = net_params
                .hs_pow_v1_service_intro_timeout
                .try_into()
                .unwrap();
            runtime.advance_by(max_age * 2).await;
            // Waited too long, request has been dropped
            assert_eq!(receiver.0.lock().unwrap().queue.len(), 0);
        });
    }
    #[test]
    fn test_pow_disabled() {
        MockRuntime::test_with_various(|runtime| async move {
            let (mut receiver, mut tx, _suggested_effort, _net_params, _config_tx) =
                make_test_receiver(
                    &runtime,
                    vec![],
                    Some(
                        OnionServiceConfigBuilder::default()
                            .nickname(HsNickname::new("test-hs".to_string()).unwrap())
                            .enable_pow(false)
                            .build()
                            .unwrap(),
                    ),
                );
            // Request with no PoW
            tx.send(make_req(0, None)).await.unwrap();
            tx.send(make_req(1, Some(0))).await.unwrap();
            tx.send(make_req(2, Some(20))).await.unwrap();
            tx.send(make_req(3, Some(10))).await.unwrap();
            runtime.progress_until_stalled().await;
            // Requests are FIFO, since PoW is disabled
            assert_eq!(receiver.next().await.unwrap().id, 0);
            assert_eq!(receiver.next().await.unwrap().id, 1);
            assert_eq!(receiver.next().await.unwrap().id, 2);
            assert_eq!(receiver.next().await.unwrap().id, 3);
        });
    }
    #[test]
    fn test_rend_queue_max_depth() {
        MockRuntime::test_with_various(|runtime| async move {
            let (mut receiver, mut tx, _suggested_effort, _net_params, mut config_tx) =
                make_test_receiver(
                    &runtime,
                    vec![],
                    Some(
                        OnionServiceConfigBuilder::default()
                            .nickname(HsNickname::new("test-hs".to_string()).unwrap())
                            .enable_pow(true)
                            .pow_rend_queue_depth(2)
                            .build()
                            .unwrap(),
                    ),
                );
            tx.send(make_req(0, None)).await.unwrap();
            tx.send(make_req(1, None)).await.unwrap();
            tx.send(make_req(2, None)).await.unwrap();
            runtime.progress_until_stalled().await;
            assert!(receiver.next().await.is_some());
            assert!(receiver.next().await.is_some());
            assert_eq!(receiver.0.lock().unwrap().queue.len(), 0);
            // Check that increasing queue size at runtime works...
            config_tx
                .send(Arc::new(
                    OnionServiceConfigBuilder::default()
                        .nickname(HsNickname::new("test-hs".to_string()).unwrap())
                        .enable_pow(true)
                        .pow_rend_queue_depth(8)
                        .build()
                        .unwrap(),
                ))
                .await
                .unwrap();
            tx.send(make_req(0, None)).await.unwrap();
            tx.send(make_req(1, None)).await.unwrap();
            tx.send(make_req(2, None)).await.unwrap();
            runtime.progress_until_stalled().await;
            assert!(receiver.next().await.is_some());
            assert!(receiver.next().await.is_some());
            assert!(receiver.next().await.is_some());
        });
    }
}