Commit 13af6134 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Rename Guard=>FirstHop, GuardId=>FirstHopId

This is preparation for having separate GuardId and FirstHopId types
that distinguish which back-end they index.
parent 9803b645
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ use crate::isolation::StreamIsolation;
use crate::preemptive::PreemptiveCircuitPredictor;
use usage::TargetCircUsage;

pub use tor_guardmgr::{ExternalActivity, GuardId};
pub use tor_guardmgr::{ExternalActivity, FirstHopId};
use tor_persist::{FsStateMgr, StateMgr};
use tor_rtcompat::scheduler::{TaskHandle, TaskSchedule};

@@ -695,7 +695,7 @@ impl<R: Runtime> CircMgr<R> {

    /// Record that a failure occurred on a circuit with a given guard, in a way
    /// that makes us unwilling to use that guard for future circuits.
    pub fn note_external_failure(&self, id: &GuardId, external_failure: ExternalActivity) {
    pub fn note_external_failure(&self, id: &FirstHopId, external_failure: ExternalActivity) {
        self.mgr
            .peek_builder()
            .guardmgr()
@@ -704,7 +704,7 @@ impl<R: Runtime> CircMgr<R> {

    /// Record that a success occurred on a circuit with a given guard, in a way
    /// that makes us possibly willing to use that guard for future circuits.
    pub fn note_external_success(&self, id: &GuardId, external_activity: ExternalActivity) {
    pub fn note_external_success(&self, id: &FirstHopId, external_activity: ExternalActivity) {
        self.mgr
            .peek_builder()
            .guardmgr()
+4 −4
Original line number Diff line number Diff line
@@ -981,7 +981,7 @@ impl<R: Runtime> DirMgr<R> {

    /// Record that a problem has occurred because of a failure in an answer from `source`.
    fn note_cache_error(&self, source: &tor_dirclient::SourceInfo, problem: &Error) {
        use tor_circmgr::{ExternalActivity, GuardId};
        use tor_circmgr::{ExternalActivity, FirstHopId};

        if !problem.indicates_cache_failure() {
            return;
@@ -989,7 +989,7 @@ impl<R: Runtime> DirMgr<R> {

        if let Some(circmgr) = &self.circmgr {
            info!("Marking {:?} as failed: {}", source, problem);
            let guard_id = GuardId::from_chan_target(source.cache_id());
            let guard_id = FirstHopId::from_chan_target(source.cache_id());
            circmgr.note_external_failure(&guard_id, ExternalActivity::DirCache);
            circmgr.retire_circ(source.unique_circ_id());
        }
@@ -997,11 +997,11 @@ impl<R: Runtime> DirMgr<R> {

    /// Record that `source` has successfully given us some directory info.
    fn note_cache_success(&self, source: &tor_dirclient::SourceInfo) {
        use tor_circmgr::{ExternalActivity, GuardId};
        use tor_circmgr::{ExternalActivity, FirstHopId};

        if let Some(circmgr) = &self.circmgr {
            trace!("Marking {:?} as successful", source);
            let guard_id = GuardId::from_chan_target(source.cache_id());
            let guard_id = FirstHopId::from_chan_target(source.cache_id());
            circmgr.note_external_success(&guard_id, ExternalActivity::DirCache);
        }
    }
+3 −3
Original line number Diff line number Diff line
@@ -51,9 +51,9 @@ impl FallbackDir {
    }

    /// Return a copy of this FallbackDir as a [`Guard`](crate::Guard)
    pub fn as_guard(&self) -> crate::Guard {
        crate::Guard {
            id: crate::GuardId::from_chan_target(self),
    pub fn as_guard(&self) -> crate::FirstHop {
        crate::FirstHop {
            id: crate::FirstHopId::from_chan_target(self),
            orports: self.orports.clone(),
        }
    }
+12 −12
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ use rand::seq::IteratorRandom;
use std::time::Instant;

use super::{FallbackDir, Status};
use crate::{GuardId, PickGuardError};
use crate::{FirstHopId, PickGuardError};
use serde::Deserialize;

/// A list of fallback directories.
@@ -62,7 +62,7 @@ pub(crate) struct FallbackState {
#[derive(Debug, Clone)]
pub(super) struct Entry {
    /// The inner fallback directory.
    pub(super) fallback: crate::Guard,
    pub(super) fallback: crate::FirstHop,
    /// The status for the fallback directory.
    pub(super) status: Status,
}
@@ -77,7 +77,7 @@ impl From<FallbackDir> for Entry {

impl Entry {
    /// Return the identity for this fallback entry.
    fn id(&self) -> &GuardId {
    fn id(&self) -> &FirstHopId {
        self.fallback.id()
    }
}
@@ -97,7 +97,7 @@ impl FallbackState {
        &self,
        rng: &mut R,
        now: Instant,
    ) -> Result<&crate::Guard, PickGuardError> {
    ) -> Result<&crate::FirstHop, PickGuardError> {
        if self.fallbacks.is_empty() {
            return Err(PickGuardError::NoCandidatesAvailable);
        }
@@ -123,7 +123,7 @@ impl FallbackState {
    }

    /// Return a mutable reference to the entry whose identity is `id`, if there is one.
    fn get_mut(&mut self, id: &GuardId) -> Option<&mut Entry> {
    fn get_mut(&mut self, id: &FirstHopId) -> Option<&mut Entry> {
        match self.fallbacks.binary_search_by(|e| e.id().cmp(id)) {
            Ok(idx) => Some(&mut self.fallbacks[idx]),
            Err(_) => None,
@@ -135,7 +135,7 @@ impl FallbackState {
    ///
    /// Be aware that for fallbacks, we only count a successful directory
    /// operation as a success: a circuit success is not enough.
    pub(crate) fn note_success(&mut self, id: &GuardId) {
    pub(crate) fn note_success(&mut self, id: &FirstHopId) {
        if let Some(entry) = self.get_mut(id) {
            entry.status.note_success();
        }
@@ -143,7 +143,7 @@ impl FallbackState {

    /// Record that a failure has occurred for the fallback with the given
    /// identity.
    pub(crate) fn note_failure(&mut self, id: &GuardId, now: Instant) {
    pub(crate) fn note_failure(&mut self, id: &FirstHopId, now: Instant) {
        if let Some(entry) = self.get_mut(id) {
            entry.status.note_failure(now);
        }
@@ -197,7 +197,7 @@ mod test {
        // fabricate some fallbacks.
        let fbs = vec![rand_fb(), rand_fb(), rand_fb(), rand_fb()];
        let fb_other = rand_fb();
        let id_other = GuardId::from_chan_target(&fb_other);
        let id_other = FirstHopId::from_chan_target(&fb_other);

        // basic case: construct a set
        let list: FallbackList = fbs.clone().into();
@@ -213,7 +213,7 @@ mod test {

        // use the constructed set a little.
        for fb in fbs.iter() {
            let id = GuardId::from_chan_target(fb);
            let id = FirstHopId::from_chan_target(fb);
            assert_eq!(set.get_mut(&id).unwrap().id(), &id);
        }
        assert!(set.get_mut(&id_other).is_none());
@@ -246,7 +246,7 @@ mod test {
        let mut rng = rand::thread_rng();
        let now = Instant::now();

        fn lookup_idx(set: &FallbackState, id: &GuardId) -> Option<usize> {
        fn lookup_idx(set: &FallbackState, id: &FirstHopId) -> Option<usize> {
            set.fallbacks.binary_search_by(|ent| ent.id().cmp(id)).ok()
        }
        // Basic case: everybody is up.
@@ -333,7 +333,7 @@ mod test {
        let mut fbs2: Vec<_> = fbs
            .into_iter()
            // (Remove the fallback with id==ids[2])
            .filter(|fb| GuardId::from_chan_target(fb) != ids[2])
            .filter(|fb| FirstHopId::from_chan_target(fb) != ids[2])
            .collect();
        // add 2 new ones.
        let fbs_new = [rand_fb(), rand_fb(), rand_fb()];
@@ -352,7 +352,7 @@ mod test {
        // Make sure that the new fbs are there.
        for new_fb in fbs_new {
            assert!(set2
                .get_mut(&GuardId::from_chan_target(&new_fb))
                .get_mut(&FirstHopId::from_chan_target(&new_fb))
                .unwrap()
                .status
                .usable_at(now));
+21 −13
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ use std::time::{Duration, Instant, SystemTime};
use tracing::{trace, warn};

use crate::util::randomize_time;
use crate::{GuardId, GuardParams, GuardRestriction, GuardUsage};
use crate::{FirstHopId, GuardParams, GuardRestriction, GuardUsage};
use tor_persist::{Futureproof, JsonValue};

/// Tri-state to represent whether a guard is believed to be reachable or not.
@@ -83,7 +83,7 @@ impl CrateId {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct Guard {
    /// The identity keys for this guard.
    id: GuardId, // TODO: Maybe refactor this out as redundant someday.
    id: FirstHopId, // TODO: Maybe refactor this out as redundant someday.

    /// The most recently seen addresses for making OR connections to this
    /// guard.
@@ -195,14 +195,14 @@ impl Guard {
        );

        Self::new(
            GuardId::from_chan_target(relay),
            FirstHopId::from_chan_target(relay),
            relay.addrs().into(),
            added_at,
        )
    }

    /// Return a new, manually constructed [`Guard`].
    fn new(id: GuardId, orports: Vec<SocketAddr>, added_at: SystemTime) -> Self {
    fn new(id: FirstHopId, orports: Vec<SocketAddr>, added_at: SystemTime) -> Self {
        Guard {
            id,
            orports,
@@ -225,7 +225,7 @@ impl Guard {
    }

    /// Return the identity of this Guard.
    pub(crate) fn guard_id(&self) -> &GuardId {
    pub(crate) fn guard_id(&self) -> &FirstHopId {
        &self.id
    }

@@ -574,8 +574,8 @@ impl Guard {
    }

    /// Return a [`crate::Guard`] object to represent this guard.
    pub(crate) fn get_external_rep(&self) -> crate::Guard {
        crate::Guard {
    pub(crate) fn get_external_rep(&self) -> crate::FirstHop {
        crate::FirstHop {
            id: self.id.clone(),
            orports: self.orports.clone(),
        }
@@ -687,8 +687,8 @@ mod test {
        assert_eq!(Some(id.version.as_ref()), option_env!("CARGO_PKG_VERSION"));
    }

    fn basic_id() -> GuardId {
        GuardId::new([13; 32].into(), [37; 20].into())
    fn basic_id() -> FirstHopId {
        FirstHopId::new([13; 32].into(), [37; 20].into())
    }
    fn basic_guard() -> Guard {
        let id = basic_id();
@@ -919,7 +919,7 @@ mod test {

        // Now try a guard that isn't in the netdir.
        let guard255 = Guard::new(
            GuardId::new([255; 32].into(), [255; 20].into()),
            FirstHopId::new([255; 32].into(), [255; 20].into()),
            vec![],
            now,
        );
@@ -960,7 +960,7 @@ mod test {

        // Try a guard that isn't in the netdir at all.
        let mut guard255 = Guard::new(
            GuardId::new([255; 32].into(), [255; 20].into()),
            FirstHopId::new([255; 32].into(), [255; 20].into()),
            vec!["8.8.8.8:53".parse().unwrap()],
            now,
        );
@@ -974,7 +974,11 @@ mod test {
        assert!(!guard255.orports.is_empty());

        // Try a guard that is in netdir, but not netdir2.
        let mut guard22 = Guard::new(GuardId::new([22; 32].into(), [22; 20].into()), vec![], now);
        let mut guard22 = Guard::new(
            FirstHopId::new([22; 32].into(), [22; 20].into()),
            vec![],
            now,
        );
        let relay22 = guard22.id.get_relay(&netdir).unwrap();
        assert_eq!(guard22.listed_in(&netdir), Some(true));
        guard22.update_from_netdir(&netdir);
@@ -990,7 +994,11 @@ mod test {
        assert!(!guard22.microdescriptor_missing);

        // Now see what happens for a guard that's in the consensus, but missing an MD.
        let mut guard23 = Guard::new(GuardId::new([23; 32].into(), [23; 20].into()), vec![], now);
        let mut guard23 = Guard::new(
            FirstHopId::new([23; 32].into(), [23; 20].into()),
            vec![],
            now,
        );
        assert_eq!(guard23.listed_in(&netdir2), Some(true));
        assert_eq!(guard23.listed_in(&netdir3), None);
        guard23.update_from_netdir(&netdir3);
Loading