Skip to content

Commit

Permalink
feat(pkarr): add monotonic Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Sep 25, 2024
1 parent b8d1597 commit 1ee9ff8
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 51 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pkarr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ simple-dns = "0.6.1"
thiserror = "1.0.49"
tracing = "0.1.40"
dyn-clone = "1.0.17"
once_cell = {version = "1.19.0", default-features = false }
lru = { version = "0.12.3", default-features = false }
document-features = "0.2.8"

# optional dependencies
serde = { version = "1.0.209", features = ["derive"], optional = true }
rand = { version = "0.8.5", optional = true }

document-features = "0.2.8"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Dht client dependencies:
Expand Down
2 changes: 1 addition & 1 deletion pkarr/src/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
pub mod cache;
pub mod keys;
pub mod signed_packet;
pub mod time;
pub mod timestamp;
36 changes: 18 additions & 18 deletions pkarr/src/base/signed_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
net::{Ipv4Addr, Ipv6Addr},
};

use crate::system_time;
use crate::Timestamp;

const DOT: char = '.';

Expand Down Expand Up @@ -60,7 +60,7 @@ impl Inner {
/// Signed DNS packet
pub struct SignedPacket {
inner: Inner,
last_seen: u64,
last_seen: Timestamp,
}

impl SignedPacket {
Expand Down Expand Up @@ -102,16 +102,16 @@ impl SignedPacket {

Ok(SignedPacket {
inner: Inner::try_from_bytes(bytes)?,
last_seen: system_time(),
last_seen: Timestamp::now(),
})
}

/// Useful for cloning a [SignedPacket], or cerating one from a previously checked bytes,
/// like ones stored on disk or in a database.
pub fn from_bytes_unchecked(bytes: &Bytes, last_seen: u64) -> SignedPacket {
pub fn from_bytes_unchecked(bytes: &Bytes, last_seen: impl Into<Timestamp>) -> SignedPacket {
SignedPacket {
inner: Inner::try_from_bytes(bytes).unwrap(),
last_seen,
last_seen: last_seen.into(),
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ impl SignedPacket {
return Err(Error::PacketTooLarge(encoded_packet.len()));
}

let timestamp = system_time();
let timestamp = Timestamp::now().into();

let signature = keypair.sign(&signable(timestamp, &encoded_packet));

Expand All @@ -183,7 +183,7 @@ impl SignedPacket {
timestamp,
&encoded_packet,
)?,
last_seen: system_time(),
last_seen: Timestamp::now(),
})
}

Expand Down Expand Up @@ -236,22 +236,22 @@ impl SignedPacket {
}

/// Unix last_seen time in microseconds
pub fn last_seen(&self) -> &u64 {
pub fn last_seen(&self) -> &Timestamp {
&self.last_seen
}

// === Setters ===

/// Set the [Self::last_seen] property
pub fn set_last_seen(&mut self, last_seen: &u64) {
self.last_seen = *last_seen;
pub fn set_last_seen(&mut self, last_seen: &Timestamp) {
self.last_seen = last_seen.into();
}

// === Public Methods ===

/// Set the [Self::last_seen] to the current system time
pub fn refresh(&mut self) {
self.last_seen = system_time();
self.last_seen = Timestamp::now();
}

/// Return whether this [SignedPacket] is more recent than the given one.
Expand Down Expand Up @@ -329,7 +329,7 @@ impl SignedPacket {

/// Time since the [Self::last_seen] in seconds
fn elapsed(&self) -> u32 {
((system_time() - self.last_seen) / 1_000_000) as u32
((Timestamp::now().into_u64() - self.last_seen.into_u64()) / 1_000_000) as u32
}
}

Expand Down Expand Up @@ -369,7 +369,7 @@ impl TryFrom<&MutableItem> for SignedPacket {

Ok(Self {
inner: Inner::try_from_parts(&public_key, &signature, seq, i.value())?,
last_seen: system_time(),
last_seen: Timestamp::now(),
})
}
}
Expand All @@ -384,7 +384,7 @@ impl AsRef<[u8]> for SignedPacket {

impl Clone for SignedPacket {
fn clone(&self) -> Self {
Self::from_bytes_unchecked(self.as_bytes(), self.last_seen)
Self::from_bytes_unchecked(self.as_bytes(), &self.last_seen)
}
}

Expand Down Expand Up @@ -651,7 +651,7 @@ mod tests {
let bytes = signed.as_bytes();
let from_bytes = SignedPacket::from_bytes(bytes).unwrap();
assert_eq!(signed.as_bytes(), from_bytes.as_bytes());
let from_bytes2 = SignedPacket::from_bytes_unchecked(bytes, signed.last_seen);
let from_bytes2 = SignedPacket::from_bytes_unchecked(bytes, &signed.last_seen);
assert_eq!(signed.as_bytes(), from_bytes2.as_bytes());

let public_key = keypair.public_key();
Expand Down Expand Up @@ -690,7 +690,7 @@ mod tests {

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

signed.last_seen = system_time() - (20 * 1_000_000);
signed.last_seen -= 20 * 1_000_000_u64;

assert!(
signed.expires_in(30, u32::MAX) > 0,
Expand All @@ -716,7 +716,7 @@ mod tests {

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

signed.last_seen = system_time() - (2 * (DEFAULT_MAXIMUM_TTL as u64) * 1_000_000);
signed.last_seen -= 2 * (DEFAULT_MAXIMUM_TTL as u64) * 1_000_000;

assert!(
signed.expires_in(0, DEFAULT_MAXIMUM_TTL) == 0,
Expand Down Expand Up @@ -748,7 +748,7 @@ mod tests {

let mut signed = SignedPacket::from_packet(&keypair, &packet).unwrap();

signed.last_seen = system_time() - (30 * 1_000_000);
signed.last_seen -= 30 * 1_000_000;

assert_eq!(signed.fresh_resource_records("_foo").count(), 1);
}
Expand Down
20 changes: 0 additions & 20 deletions pkarr/src/base/time.rs

This file was deleted.

Loading

0 comments on commit 1ee9ff8

Please sign in to comment.