rng ranges: Use gen_range_infallible() for Duration::ZERO..=T
This commit is contained in:
parent
09f0ecaa71
commit
b151237a7f
|
@ -330,6 +330,7 @@ dependencies = [
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tor-basic-utils",
|
||||||
"tor-checkable",
|
"tor-checkable",
|
||||||
"tor-config",
|
"tor-config",
|
||||||
"tor-dirmgr",
|
"tor-dirmgr",
|
||||||
|
|
|
@ -37,6 +37,7 @@ pin-project = "1"
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
serde = { version = "1.0.103", features = ["derive"] }
|
serde = { version = "1.0.103", features = ["derive"] }
|
||||||
tokio = { version = "1.7", features = ["signal", "macros"] }
|
tokio = { version = "1.7", features = ["signal", "macros"] }
|
||||||
|
tor-basic-utils = { path = "../tor-basic-utils", version = "0.7.1" }
|
||||||
tor-checkable = { path = "../tor-checkable", version = "0.5.1", features = ["experimental-api"] }
|
tor-checkable = { path = "../tor-checkable", version = "0.5.1", features = ["experimental-api"] }
|
||||||
tor-config = { path = "../tor-config", version = "0.9.2" }
|
tor-config = { path = "../tor-config", version = "0.9.2" }
|
||||||
tor-dirmgr = { package = "tor-dirmgr", path = "../tor-dirmgr", version = "0.10.2", features = ["dirfilter"] }
|
tor-dirmgr = { package = "tor-dirmgr", path = "../tor-dirmgr", version = "0.10.2", features = ["dirfilter"] }
|
||||||
|
|
|
@ -8,7 +8,7 @@ use anyhow::anyhow;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures::io::{AsyncRead, AsyncWrite};
|
use futures::io::{AsyncRead, AsyncWrite};
|
||||||
use pin_project::pin_project;
|
use pin_project::pin_project;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::thread_rng;
|
||||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult};
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Result as IoResult};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
@ -16,6 +16,7 @@ use std::str::FromStr;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use tor_basic_utils::RngExt as _;
|
||||||
|
|
||||||
/// An action that we can take upon trying to make a TCP connection.
|
/// An action that we can take upon trying to make a TCP connection.
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
@ -154,7 +155,7 @@ impl<R: Runtime> TcpProvider for BrokenTcpProvider<R> {
|
||||||
Ok(BreakableTcpStream::Present(conn))
|
Ok(BreakableTcpStream::Present(conn))
|
||||||
}
|
}
|
||||||
Action::Fail(dur, kind) => {
|
Action::Fail(dur, kind) => {
|
||||||
let d = thread_rng().gen_range(Duration::from_secs(0)..=dur);
|
let d = thread_rng().gen_range_infallible(..=dur);
|
||||||
self.inner.sleep(d).await;
|
self.inner.sleep(d).await;
|
||||||
Err(IoError::new(kind, anyhow::anyhow!("intentional failure")))
|
Err(IoError::new(kind, anyhow::anyhow!("intentional failure")))
|
||||||
}
|
}
|
||||||
|
|
|
@ -657,7 +657,7 @@ mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::timeouts::TimeoutEstimator;
|
use crate::timeouts::TimeoutEstimator;
|
||||||
use tor_basic_utils::test_rng::testing_rng;
|
use tor_basic_utils::test_rng::testing_rng;
|
||||||
use tor_basic_utils::{RngExt as _};
|
use tor_basic_utils::RngExt as _;
|
||||||
|
|
||||||
/// Return an action to build a 3-hop circuit.
|
/// Return an action to build a 3-hop circuit.
|
||||||
fn b3() -> Action {
|
fn b3() -> Action {
|
||||||
|
|
|
@ -10,13 +10,13 @@
|
||||||
//! [`bootstrap`](crate::bootstrap) module for functions that actually
|
//! [`bootstrap`](crate::bootstrap) module for functions that actually
|
||||||
//! load or download directory information.
|
//! load or download directory information.
|
||||||
|
|
||||||
use rand::Rng;
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use time::OffsetDateTime;
|
use time::OffsetDateTime;
|
||||||
|
use tor_basic_utils::RngExt as _;
|
||||||
use tor_error::{internal, warn_report};
|
use tor_error::{internal, warn_report};
|
||||||
use tor_netdir::{MdReceiver, NetDir, PartialNetDir};
|
use tor_netdir::{MdReceiver, NetDir, PartialNetDir};
|
||||||
use tor_netdoc::doc::authcert::UncheckedAuthCert;
|
use tor_netdoc::doc::authcert::UncheckedAuthCert;
|
||||||
|
@ -1151,8 +1151,7 @@ impl<R: Runtime> DirState for GetMicrodescsState<R> {
|
||||||
/// is `lifetime`.
|
/// is `lifetime`.
|
||||||
fn pick_download_time(lifetime: &Lifetime) -> SystemTime {
|
fn pick_download_time(lifetime: &Lifetime) -> SystemTime {
|
||||||
let (lowbound, uncertainty) = client_download_range(lifetime);
|
let (lowbound, uncertainty) = client_download_range(lifetime);
|
||||||
let zero = Duration::new(0, 0);
|
lowbound + rand::thread_rng().gen_range_infallible(..=uncertainty)
|
||||||
lowbound + rand::thread_rng().gen_range(zero..=uncertainty)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Based on the lifetime for a consensus, return the time range during which
|
/// Based on the lifetime for a consensus, return the time range during which
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
|
use tor_basic_utils::RngExt as _;
|
||||||
|
|
||||||
/// Return a random time within the range `when-max ..= when`.
|
/// Return a random time within the range `when-max ..= when`.
|
||||||
///
|
///
|
||||||
|
@ -12,7 +13,7 @@ use std::time::{Duration, SystemTime};
|
||||||
/// an attempt to make some kinds of traffic analysis attacks a bit
|
/// an attempt to make some kinds of traffic analysis attacks a bit
|
||||||
/// harder for an attacker who can read our state after the fact.
|
/// harder for an attacker who can read our state after the fact.
|
||||||
pub(crate) fn randomize_time<R: Rng>(rng: &mut R, when: SystemTime, max: Duration) -> SystemTime {
|
pub(crate) fn randomize_time<R: Rng>(rng: &mut R, when: SystemTime, max: Duration) -> SystemTime {
|
||||||
let offset = rng.gen_range(Duration::ZERO..=max);
|
let offset = rng.gen_range_infallible(..=max);
|
||||||
let random = when
|
let random = when
|
||||||
.checked_sub(offset)
|
.checked_sub(offset)
|
||||||
.unwrap_or(SystemTime::UNIX_EPOCH)
|
.unwrap_or(SystemTime::UNIX_EPOCH)
|
||||||
|
|
Loading…
Reference in New Issue