Switch to HashMap

This commit is contained in:
Neel Chauhan 2021-11-22 20:22:44 -08:00
parent e69d66233e
commit 16e8489abb
3 changed files with 11 additions and 10 deletions

View File

@ -3,6 +3,7 @@
use super::TorPath;
use crate::{DirInfo, Error, PathConfig, Result, TargetPort};
use rand::Rng;
use std::collections::HashSet;
use tor_guardmgr::{GuardMgr, GuardMonitor, GuardUsable};
use tor_linkspec::ChanTarget;
use tor_netdir::{NetDir, Relay, SubnetConfig, WeightRole};
@ -141,17 +142,17 @@ impl<'a> ExitPathBuilder<'a> {
Some(guardmgr) => {
let mut b = tor_guardmgr::GuardUsageBuilder::default();
b.kind(tor_guardmgr::GuardUsageKind::Data);
let mut restrictions: Vec<tor_guardmgr::GuardRestriction> = Vec::new();
let mut restrictions: HashSet<tor_guardmgr::GuardRestriction> = HashSet::new();
guardmgr.update_network(netdir); // possibly unnecessary.
if let Some(exit_relay) = chosen_exit {
let id = exit_relay.ed_identity();
restrictions.push(tor_guardmgr::GuardRestriction::AvoidId(*id));
restrictions.insert(tor_guardmgr::GuardRestriction::AvoidId(*id));
for rsaid in exit_relay.family().members() {
let relay = netdir.by_rsa_id(rsaid);
if let Some(r) = relay {
for fam_relay in r.family().members() {
if fam_relay == exit_relay.rsa_identity() {
restrictions.push(tor_guardmgr::GuardRestriction::AvoidId(
restrictions.insert(tor_guardmgr::GuardRestriction::AvoidId(
*r.ed_identity(),
));
}

View File

@ -5,7 +5,7 @@ use tor_llcrypto::pk::{ed25519::Ed25519Identity, rsa::RsaIdentity};
use tor_netdir::{NetDir, Relay, RelayWeight};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::time::{Duration, Instant, SystemTime};
use tracing::{trace, warn};
@ -297,7 +297,7 @@ impl Guard {
}
/// Return true if this guard obeys the restriction in `rest`.
fn obeys_restriction(&self, rest: &[GuardRestriction]) -> bool {
fn obeys_restriction(&self, rest: &HashSet<GuardRestriction>) -> bool {
for r in rest {
match r {
GuardRestriction::AvoidId(ed) => {
@ -694,11 +694,11 @@ mod test {
use crate::GuardUsageBuilder;
let usage1 = GuardUsageBuilder::new()
.restriction(vec![GuardRestriction::AvoidId([22; 32].into())])
.restriction(HashSet::from([GuardRestriction::AvoidId([22; 32].into())]))
.build()
.unwrap();
let usage2 = GuardUsageBuilder::new()
.restriction(vec![GuardRestriction::AvoidId([13; 32].into())])
.restriction(HashSet::from([GuardRestriction::AvoidId([13; 32].into())]))
.build()
.unwrap();
let usage3 = GuardUsage::default();

View File

@ -132,7 +132,7 @@
use futures::channel::mpsc;
use futures::task::{SpawnError, SpawnExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::convert::{TryFrom, TryInto};
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
@ -878,7 +878,7 @@ pub struct GuardUsage {
///
/// (Eventually, multiple restrictions may be supported.)
#[builder(default, setter(strip_option))]
restriction: Option<Vec<GuardRestriction>>,
restriction: Option<HashSet<GuardRestriction>>,
}
impl GuardUsageBuilder {
@ -895,7 +895,7 @@ impl GuardUsageBuilder {
/// They're suitable for things like making sure that we don't start
/// and end a circuit at the same relay, or requiring a specific
/// subprotocol version for certain kinds of requests.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[non_exhaustive]
pub enum GuardRestriction {
/// Don't pick a guard with the provided Ed25519 identity.