|
5 | 5 |
|
6 | 6 | use std::collections::HashMap; |
7 | 7 | use std::sync::Arc; |
8 | | -use std::time::{Duration, SystemTime}; |
| 8 | +use std::time::{Duration, Instant, SystemTime}; |
9 | 9 |
|
10 | 10 | use dashcore::ephemerealdata::instant_lock::InstantLock; |
11 | 11 | use dashcore::hashes::Hash; |
@@ -49,8 +49,9 @@ pub(super) struct PendingInstantLock { |
49 | 49 | /// The InstantLock data. |
50 | 50 | instant_lock: InstantLock, |
51 | 51 | /// When the lock was first received, used to expire locks that never |
52 | | - /// become verifiable. |
53 | | - first_seen: SystemTime, |
| 52 | + /// become verifiable. Uses a monotonic `Instant` so the elapsed-time TTL |
| 53 | + /// check is unaffected by wall-clock adjustments. |
| 54 | + first_seen: Instant, |
54 | 55 | } |
55 | 56 |
|
56 | 57 | /// InstantSend manager. |
@@ -122,7 +123,7 @@ impl InstantSendManager { |
122 | 123 | } else { |
123 | 124 | self.queue_pending(PendingInstantLock { |
124 | 125 | instant_lock: instantlock.clone(), |
125 | | - first_seen: SystemTime::now(), |
| 126 | + first_seen: Instant::now(), |
126 | 127 | }); |
127 | 128 | self.progress.update_pending(self.pending_instantlocks.len()); |
128 | 129 | } |
@@ -239,8 +240,7 @@ impl InstantSendManager { |
239 | 240 | let txid = pending_lock.instant_lock.txid; |
240 | 241 |
|
241 | 242 | // Drop locks that have been awaiting quorum data for too long. |
242 | | - let expired = |
243 | | - pending_lock.first_seen.elapsed().map(|age| age > PENDING_TTL).unwrap_or(false); |
| 243 | + let expired = pending_lock.first_seen.elapsed() > PENDING_TTL; |
244 | 244 | if expired { |
245 | 245 | tracing::warn!( |
246 | 246 | "Dropping InstantLock for txid {} after awaiting quorum data for over {}s", |
@@ -308,8 +308,7 @@ impl InstantSendManager { |
308 | 308 | pub(super) fn expire_pending(&mut self) -> usize { |
309 | 309 | let before = self.pending_instantlocks.len(); |
310 | 310 | self.pending_instantlocks.retain(|pending| { |
311 | | - let expired = |
312 | | - pending.first_seen.elapsed().map(|age| age > PENDING_TTL).unwrap_or(false); |
| 311 | + let expired = pending.first_seen.elapsed() > PENDING_TTL; |
313 | 312 | if expired { |
314 | 313 | tracing::warn!( |
315 | 314 | "Dropping InstantLock for txid {} after awaiting quorum data for over {}s", |
@@ -404,14 +403,14 @@ mod tests { |
404 | 403 | fn expired_pending(txid: Txid) -> PendingInstantLock { |
405 | 404 | PendingInstantLock { |
406 | 405 | instant_lock: create_test_instantlock(txid), |
407 | | - first_seen: SystemTime::now() - Duration::from_secs(PENDING_TTL.as_secs() + 60), |
| 406 | + first_seen: Instant::now() - Duration::from_secs(PENDING_TTL.as_secs() + 60), |
408 | 407 | } |
409 | 408 | } |
410 | 409 |
|
411 | 410 | fn fresh_pending(txid: Txid) -> PendingInstantLock { |
412 | 411 | PendingInstantLock { |
413 | 412 | instant_lock: create_test_instantlock(txid), |
414 | | - first_seen: SystemTime::now(), |
| 413 | + first_seen: Instant::now(), |
415 | 414 | } |
416 | 415 | } |
417 | 416 |
|
|
0 commit comments