Revert "Use coarsetime to build an incoming traffic timestamp."

This reverts commit f30b22802a.

It turns out (see torspec#57) that there is no need for us to keep
this timestamp -- at least, not for the use we had in mind in
circuit timeouts.
This commit is contained in:
Nick Mathewson 2021-07-13 13:18:10 -04:00
parent f30b22802a
commit 3a730de601
6 changed files with 0 additions and 150 deletions

View File

@ -13,7 +13,6 @@ repository="https://gitlab.torproject.org/tpo/core/arti.git/"
[features]
default = []
hs = []
traffic-timestamp = [ "coarsetime" ]
[dependencies]
tor-llcrypto = { path="../tor-llcrypto", version="0.0.0" }
@ -43,8 +42,6 @@ thiserror = "1.0.24"
typenum = "1.13.0"
zeroize = "1.3.0"
coarsetime = { version = "0.1.19", optional = true }
[dev-dependencies]
futures-await-test = "0.3.0"
hex-literal = "0.3.1"

View File

@ -386,14 +386,6 @@ impl<T: AsyncRead + AsyncWrite + Send + Unpin + 'static> VerifiedChannel<T> {
Arc<super::Channel>,
super::reactor::Reactor<stream::SplitStream<CellFrame<T>>>,
)> {
// We treat a completed channel -- that is to say, one where the
// authentication is finished -- as incoming traffic.
//
// TODO: conceivably we should remember the time when we _got_ the
// final cell on the handshake, and update the channel completion
// time to be no earlier than _that_ timestamp.
crate::note_incoming_traffic();
trace!("{}: Sending netinfo cell.", self.unique_id);
let netinfo = msg::Netinfo::for_client(self.target_addr.as_ref().map(SocketAddr::ip));
self.tls.send(netinfo.into()).await?;

View File

@ -163,7 +163,6 @@ where
None => return Err(ReactorError::Shutdown), // the TLS connection closed.
Some(r) => r.map_err(Error::CellErr)?, // it's a cell.
};
crate::note_incoming_traffic();
self.handle_cell(item).await?;
}

View File

@ -122,36 +122,3 @@ type SecretBytes = zeroize::Zeroizing<Vec<u8>>;
/// A Result type for this crate.
pub type Result<T> = std::result::Result<T, Error>;
/// Timestamp object that we update whenever we get incoming traffic.
///
/// Used to implement [`time_since_last_incoming_traffic`]
#[cfg(feature = "traffic-timestamp")]
static LAST_INCOMING_TRAFFIC: util::ts::Timestamp = util::ts::Timestamp::new();
/// Called whenever we receive incoming traffic.
///
/// Used to implement [`time_since_last_incoming_traffic`]
#[inline]
pub(crate) fn note_incoming_traffic() {
#[cfg(feature = "traffic-timestamp")]
{
LAST_INCOMING_TRAFFIC.update();
}
}
/// Return the amount of time since we last received "incoming traffic".
///
/// Requires that the `traffic-timestamp` feature is enabled.
///
/// This is a global counter, and is subject to interference from
/// other users of the `tor_proto`. Its only permissible use is for
/// checking how recently we have been definitely able to receive
/// incoming traffic.
///
/// When enabled, this timestamp is updated whenever we receive a valid
/// cell, and whenever we complete a channel handshake.
#[cfg(feature = "traffic-timestamp")]
pub fn time_since_last_incoming_traffic() -> coarsetime::Duration {
LAST_INCOMING_TRAFFIC.time_since_update()
}

View File

@ -2,5 +2,3 @@
pub(crate) mod ct;
pub(crate) mod err;
#[cfg(feature = "traffic-timestamp")]
pub(crate) mod ts;

View File

@ -1,103 +0,0 @@
//! Implement a fast 'timestamp' for determining when an event last
//! happened.
use std::sync::atomic::{AtomicU64, Ordering};
/// An object for determining when an event last happened.
///
/// Every `Timestamp` has internal mutability. A timestamp can move
/// forward in time, but never backwards.
///
/// Internally, it uses the `coarsetime` crate to represent times in a way
/// that lets us do atomic updates.
#[derive(Default, Debug)]
pub(crate) struct Timestamp {
/// A timestamp (from `coarsetime`) describing when this timestamp
/// was last updated.
///
/// I'd rather just use [`coarsetime::Instant`], but that doesn't have
/// an atomic form.
latest: AtomicU64,
}
impl Timestamp {
/// Construct a new timestamp that has never been updated.
pub(crate) const fn new() -> Self {
Timestamp {
latest: AtomicU64::new(0),
}
}
/// Update this timestamp to (at least) the current time.
pub(crate) fn update(&self) {
// TODO: Do we want to use 'Instant::recent() instead,' and
// add an updater thread?
self.update_to(coarsetime::Instant::now())
}
/// Update this timestamp to (at least) the time `now`.
#[inline]
pub(crate) fn update_to(&self, now: coarsetime::Instant) {
// TODO: This is using an undocumented API from coarsetime.
// We should talk to the coarsetime folks and promote some way
// to do this using only a public API.
self.latest.fetch_max(now.as_u64(), Ordering::Relaxed);
}
/// Return the time since `update` was last called.
///
/// Returns 0 if update was never called.
pub(crate) fn time_since_update(&self) -> coarsetime::Duration {
self.time_since_update_at(coarsetime::Instant::now())
}
/// Return the time between the time when `update` was last
/// called, and the time `now`.
///
/// Returns 0 if `update` was never called, or if `now` is before
/// that time.
#[inline]
pub(crate) fn time_since_update_at(&self, now: coarsetime::Instant) -> coarsetime::Duration {
let earlier = self.latest.load(Ordering::Relaxed);
let now = now.as_u64();
if now >= earlier && earlier != 0 {
// TODO: This is also an undocumented API.
coarsetime::Duration::from_u64(now - earlier)
} else {
coarsetime::Duration::from_secs(0)
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn timestamp() {
use coarsetime::{Duration, Instant};
let ts = Timestamp::new();
let zero = Duration::from_secs(0);
let one_sec = Duration::from_secs(1);
let first = Instant::now();
let in_a_bit = first + one_sec * 10;
let even_later = first + one_sec * 25;
assert_eq!(ts.time_since_update_at(first), zero);
ts.update_to(first);
assert_eq!(ts.time_since_update_at(first), zero);
assert_eq!(ts.time_since_update_at(in_a_bit), one_sec * 10);
ts.update_to(in_a_bit);
assert_eq!(ts.time_since_update_at(first), zero);
assert_eq!(ts.time_since_update_at(in_a_bit), zero);
assert_eq!(ts.time_since_update_at(even_later), one_sec * 15);
// Make sure we can't move backwards.
ts.update_to(first);
assert_eq!(ts.time_since_update_at(first), zero);
assert_eq!(ts.time_since_update_at(in_a_bit), zero);
assert_eq!(ts.time_since_update_at(even_later), one_sec * 15);
}
}