Skip to content

Commit 806c46e

Browse files
committed
nostr: add Timestamp::as_secs and deprecate Timestamp::as_u64
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent ccbcbfc commit 806c46e

File tree

13 files changed

+31
-23
lines changed

13 files changed

+31
-23
lines changed

crates/nostr-relay-builder/src/local/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl InnerLocalRelay {
650650
let mut storage = NegentropyStorageVector::with_capacity(items.len());
651651
for (id, timestamp) in items.into_iter() {
652652
let id: Id = Id::from_byte_array(id.to_bytes());
653-
storage.insert(timestamp.as_u64(), id)?;
653+
storage.insert(timestamp.as_secs(), id)?;
654654
}
655655
storage.seal()?;
656656

crates/nostr-relay-builder/src/local/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Nip42Session {
5252

5353
// Check created_at
5454
let now = Timestamp::now();
55-
let diff: u64 = now.as_u64().abs_diff(event.created_at.as_u64());
55+
let diff: u64 = now.as_secs().abs_diff(event.created_at.as_secs());
5656
if diff > 120 {
5757
return Err(String::from("challenge is too old (max allowed 2 min)"));
5858
}

crates/nostr-relay-pool/src/relay/inner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl InnerRelay {
469469
return false;
470470
}
471471

472-
let idle_duration_secs: u64 = Timestamp::now().as_u64() - reference_time.as_u64();
472+
let idle_duration_secs: u64 = Timestamp::now().as_secs() - reference_time.as_secs();
473473
let idle_duration: Duration = Duration::from_secs(idle_duration_secs);
474474
idle_duration >= self.opts.idle_timeout
475475
}
@@ -2129,7 +2129,7 @@ fn prepare_negentropy_storage(
21292129
// Add items
21302130
for (id, timestamp) in items.into_iter() {
21312131
let id: Id = Id::from_byte_array(id.to_bytes());
2132-
storage.insert(timestamp.as_u64(), id)?;
2132+
storage.insert(timestamp.as_secs(), id)?;
21332133
}
21342134

21352135
// Seal

crates/nostr-relay-pool/src/relay/stats.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl RelayConnectionStats {
102102
/// Update last activity timestamp
103103
#[inline]
104104
pub(super) fn update_activity(&self) {
105-
let now: u64 = Timestamp::now().as_u64();
105+
let now: u64 = Timestamp::now().as_secs();
106106
self.inner.last_activity_at.store(now, Ordering::SeqCst);
107107
}
108108

@@ -115,7 +115,7 @@ impl RelayConnectionStats {
115115
/// Update the wake-up timestamp
116116
#[inline]
117117
pub(super) fn just_woke_up(&self) {
118-
let now: u64 = Timestamp::now().as_u64();
118+
let now: u64 = Timestamp::now().as_secs();
119119
self.inner.woke_up_at.store(now, Ordering::SeqCst);
120120
}
121121

@@ -142,7 +142,7 @@ impl RelayConnectionStats {
142142
pub(super) fn new_success(&self) {
143143
self.inner.success.fetch_add(1, Ordering::SeqCst);
144144

145-
let now: u64 = Timestamp::now().as_u64();
145+
let now: u64 = Timestamp::now().as_secs();
146146

147147
self.inner.connected_at.store(now, Ordering::SeqCst);
148148

@@ -190,8 +190,8 @@ mod tests {
190190
assert_eq!(stats.attempts(), 1);
191191
assert_eq!(stats.success(), 1);
192192
assert_eq!(stats.success_rate(), 1.0);
193-
assert!(stats.connected_at().as_u64() > 0);
194-
assert!(stats.first_connection_timestamp().as_u64() > 0);
193+
assert!(stats.connected_at().as_secs() > 0);
194+
assert!(stats.first_connection_timestamp().as_secs() > 0);
195195
}
196196

197197
#[test]

crates/nostr/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- Add NIP-60 support (https://github.com/rust-nostr/nostr/pull/1092)
4545
- Implement `ToBech32` trait for `Nip21`
4646
- Add `CowTag::kind` and `CowTag::content`
47+
- Add `Timestamp::as_secs`
4748
- Add `Kind::CashuNutZapInfo` (10019) and `Kind::CashuNutZap` (9321) variants
4849
- Add nip47 holdinvoice methods and notification (https://github.com/rust-nostr/nostr/pull/1019)
4950
- Add `TransactionState` to `LookupInvoiceResponse` and `PaymentNotification` (https://github.com/rust-nostr/nostr/pull/1045)
@@ -71,6 +72,7 @@
7172

7273
### Deprecated
7374

75+
- Deprecate `Timestamp::as_u64`
7476
- Deprecate `kind` field in `CommentTarget::Coordinate` variant (https://github.com/rust-nostr/nostr/pull/1035)
7577

7678
### Breaking changes

crates/nostr/src/event/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ mod tests {
431431
#[cfg(feature = "std")]
432432
fn test_event_not_expired() {
433433
let now = Timestamp::now();
434-
let expiry_date: u64 = now.as_u64() * 2;
434+
let expiry_date: u64 = now.as_secs() * 2;
435435

436436
let my_keys = Keys::generate();
437437
let event = EventBuilder::text_note("my content")

crates/nostr/src/nips/nip98.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,12 @@ impl TimeDelta {
420420
pub fn subtracting(t1: Timestamp, t2: Timestamp) -> TimeDelta {
421421
if t1 > t2 {
422422
TimeDelta {
423-
delta_abs_seconds: (t1 - t2).as_u64(),
423+
delta_abs_seconds: (t1 - t2).as_secs(),
424424
negative: false,
425425
}
426426
} else {
427427
TimeDelta {
428-
delta_abs_seconds: (t2 - t1).as_u64(),
428+
delta_abs_seconds: (t2 - t1).as_secs(),
429429
negative: true,
430430
}
431431
}

crates/nostr/src/types/time/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,18 @@ impl Timestamp {
120120
self.0 = self.0.saturating_sub(secs);
121121
}
122122

123-
/// Get timestamp as [`u64`]
123+
/// Get timestamp as seconds
124124
#[inline]
125-
pub fn as_u64(&self) -> u64 {
125+
pub const fn as_secs(&self) -> u64 {
126126
self.0
127127
}
128128

129+
/// Get timestamp as [`u64`]
130+
#[deprecated(since = "0.44.0", note = "Use `as_secs` instead")]
131+
pub fn as_u64(&self) -> u64 {
132+
self.as_secs()
133+
}
134+
129135
/// Check if timestamp is `0`
130136
#[inline]
131137
pub fn is_zero(&self) -> bool {
@@ -134,7 +140,7 @@ impl Timestamp {
134140

135141
/// Convert [`Timestamp`] to human datetime
136142
pub fn to_human_datetime(&self) -> String {
137-
let timestamp: u64 = self.as_u64();
143+
let timestamp: u64 = self.as_secs();
138144

139145
if timestamp >= 253_402_300_800 {
140146
// Year 9999
@@ -241,14 +247,14 @@ impl fmt::Display for Timestamp {
241247
impl Add<Timestamp> for Timestamp {
242248
type Output = Self;
243249
fn add(self, rhs: Timestamp) -> Self::Output {
244-
Self::from_secs(self.0.saturating_add(rhs.as_u64()))
250+
Self::from_secs(self.0.saturating_add(rhs.as_secs()))
245251
}
246252
}
247253

248254
impl Sub<Timestamp> for Timestamp {
249255
type Output = Self;
250256
fn sub(self, rhs: Timestamp) -> Self::Output {
251-
Self::from_secs(self.0.saturating_sub(rhs.as_u64()))
257+
Self::from_secs(self.0.saturating_sub(rhs.as_secs()))
252258
}
253259
}
254260

database/nostr-database/src/flatbuffers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl FlatBufferEncode for Event {
105105
let args = event_fbs::EventArgs {
106106
id: Some(&id),
107107
pubkey: Some(&pubkey),
108-
created_at: self.created_at.as_u64(),
108+
created_at: self.created_at.as_secs(),
109109
kind: self.kind.as_u16() as u64,
110110
tags: Some(fbb.create_vector(&tags)),
111111
content: Some(fbb.create_string(&self.content)),

database/nostr-lmdb/src/store/lmdb/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TAG_VALUE_PAD_LEN: usize = 182;
1616
#[inline]
1717
fn reverse_and_conv_to_be64(created_at: &Timestamp) -> [u8; 8] {
1818
// Reverse
19-
let created_at: u64 = u64::MAX - created_at.as_u64();
19+
let created_at: u64 = u64::MAX - created_at.as_secs();
2020

2121
// Convert to big-endian
2222
created_at.to_be_bytes()

0 commit comments

Comments
 (0)