node: Type-safe timestamps
Use a struct to represent timestamps, to improve type safety.
This commit is contained in:
parent
0fcfbb3ecf
commit
2b771921d5
|
|
@ -1221,7 +1221,7 @@ fn rad_clone_partial_fail() {
|
||||||
node::Features::SEED,
|
node::Features::SEED,
|
||||||
Alias::new("carol"),
|
Alias::new("carol"),
|
||||||
0,
|
0,
|
||||||
localtime::LocalTime::now().as_secs(),
|
localtime::LocalTime::now().into(),
|
||||||
[node::KnownAddress::new(
|
[node::KnownAddress::new(
|
||||||
// Eve will fail to connect to this address.
|
// Eve will fail to connect to this address.
|
||||||
node::Address::from(net::SocketAddr::from(([0, 0, 0, 0], 19873))),
|
node::Address::from(net::SocketAddr::from(([0, 0, 0, 0], 19873))),
|
||||||
|
|
@ -1231,7 +1231,7 @@ fn rad_clone_partial_fail() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
eve.db
|
eve.db
|
||||||
.routing_mut()
|
.routing_mut()
|
||||||
.insert([&acme], carol, localtime::LocalTime::now().as_secs())
|
.insert([&acme], carol, localtime::LocalTime::now().into())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
eve.config.peers = node::config::PeerConfig::Static;
|
eve.config.peers = node::config::PeerConfig::Static;
|
||||||
|
|
||||||
|
|
@ -1263,7 +1263,7 @@ fn rad_clone_connect() {
|
||||||
let bob = environment.node(Config::test(Alias::new("bob")));
|
let bob = environment.node(Config::test(Alias::new("bob")));
|
||||||
let mut eve = environment.node(Config::test(Alias::new("eve")));
|
let mut eve = environment.node(Config::test(Alias::new("eve")));
|
||||||
let acme = RepoId::from_str("z42hL2jL4XNk6K8oHQaSWfMgCL7ji").unwrap();
|
let acme = RepoId::from_str("z42hL2jL4XNk6K8oHQaSWfMgCL7ji").unwrap();
|
||||||
let now = localtime::LocalTime::now().as_secs();
|
let now = localtime::LocalTime::now().into();
|
||||||
|
|
||||||
fixtures::repository(working.join("acme"));
|
fixtures::repository(working.join("acme"));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -176,9 +176,7 @@ impl Runtime {
|
||||||
// If our announcement was made some time ago, the timestamp on it will be old,
|
// If our announcement was made some time ago, the timestamp on it will be old,
|
||||||
// and it might not get gossiped to new nodes since it will be purged from caches.
|
// and it might not get gossiped to new nodes since it will be purged from caches.
|
||||||
// Therefore, we make sure it's never too old.
|
// Therefore, we make sure it's never too old.
|
||||||
if clock.as_millis() - ann.timestamp
|
if clock - ann.timestamp.to_local_time() <= config.limits.gossip_max_age {
|
||||||
<= config.limits.gossip_max_age.as_millis() as u64
|
|
||||||
{
|
|
||||||
Some(ann)
|
Some(ann)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -202,7 +200,7 @@ impl Runtime {
|
||||||
);
|
);
|
||||||
ann
|
ann
|
||||||
} else {
|
} else {
|
||||||
service::gossip::node(&config, clock.as_millis())
|
service::gossip::node(&config, clock.into())
|
||||||
.solve(Default::default())
|
.solve(Default::default())
|
||||||
.expect("Runtime::init: unable to solve proof-of-work puzzle")
|
.expect("Runtime::init: unable to solve proof-of-work puzzle")
|
||||||
};
|
};
|
||||||
|
|
@ -218,7 +216,7 @@ impl Runtime {
|
||||||
radicle::node::Features::SEED,
|
radicle::node::Features::SEED,
|
||||||
alias,
|
alias,
|
||||||
0,
|
0,
|
||||||
clock.as_millis(),
|
clock.into(),
|
||||||
[node::KnownAddress::new(addr, address::Source::Bootstrap)],
|
[node::KnownAddress::new(addr, address::Source::Bootstrap)],
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -393,10 +393,10 @@ pub struct Service<D, S, G> {
|
||||||
last_sync: LocalTime,
|
last_sync: LocalTime,
|
||||||
/// Last time the service routing table was pruned.
|
/// Last time the service routing table was pruned.
|
||||||
last_prune: LocalTime,
|
last_prune: LocalTime,
|
||||||
/// Last time the service announced something.
|
|
||||||
last_timestamp: LocalTime,
|
|
||||||
/// Last time the inventory was announced.
|
/// Last time the inventory was announced.
|
||||||
last_announce: LocalTime,
|
last_announce: LocalTime,
|
||||||
|
/// Last timestamp used for announcements.
|
||||||
|
last_timestamp: Timestamp,
|
||||||
/// Time when the service was initialized, or `None` if it wasn't initialized.
|
/// Time when the service was initialized, or `None` if it wasn't initialized.
|
||||||
started_at: Option<LocalTime>,
|
started_at: Option<LocalTime>,
|
||||||
/// Publishes events to subscribers.
|
/// Publishes events to subscribers.
|
||||||
|
|
@ -457,7 +457,7 @@ where
|
||||||
last_idle: LocalTime::default(),
|
last_idle: LocalTime::default(),
|
||||||
last_sync: LocalTime::default(),
|
last_sync: LocalTime::default(),
|
||||||
last_prune: LocalTime::default(),
|
last_prune: LocalTime::default(),
|
||||||
last_timestamp: LocalTime::default(),
|
last_timestamp: Timestamp::MIN,
|
||||||
last_announce: LocalTime::default(),
|
last_announce: LocalTime::default(),
|
||||||
started_at: None,
|
started_at: None,
|
||||||
emitter,
|
emitter,
|
||||||
|
|
@ -601,7 +601,7 @@ where
|
||||||
// all of it. It can happen that inventory is not properly seeded if for eg. the
|
// all of it. It can happen that inventory is not properly seeded if for eg. the
|
||||||
// user creates a new repository while the node is stopped.
|
// user creates a new repository while the node is stopped.
|
||||||
let rids = self.storage.inventory()?;
|
let rids = self.storage.inventory()?;
|
||||||
self.db.routing_mut().insert(&rids, nid, time.as_millis())?;
|
self.db.routing_mut().insert(&rids, nid, time.into())?;
|
||||||
|
|
||||||
let announced = self
|
let announced = self
|
||||||
.db
|
.db
|
||||||
|
|
@ -632,7 +632,7 @@ where
|
||||||
&rid,
|
&rid,
|
||||||
&nid,
|
&nid,
|
||||||
updated_at.oid,
|
updated_at.oid,
|
||||||
updated_at.timestamp.as_millis(),
|
updated_at.timestamp.into(),
|
||||||
)? {
|
)? {
|
||||||
debug!(target: "service", "Saved local sync status for {rid}..");
|
debug!(target: "service", "Saved local sync status for {rid}..");
|
||||||
}
|
}
|
||||||
|
|
@ -722,7 +722,7 @@ where
|
||||||
if let Err(err) = self
|
if let Err(err) = self
|
||||||
.db
|
.db
|
||||||
.gossip_mut()
|
.gossip_mut()
|
||||||
.prune((now - self.config.limits.gossip_max_age).as_millis())
|
.prune((now - self.config.limits.gossip_max_age).into())
|
||||||
{
|
{
|
||||||
error!(target: "service", "Error pruning gossip entries: {err}");
|
error!(target: "service", "Error pruning gossip entries: {err}");
|
||||||
}
|
}
|
||||||
|
|
@ -782,7 +782,7 @@ where
|
||||||
|
|
||||||
// Let all our peers know that we're interested in this repo from now on.
|
// Let all our peers know that we're interested in this repo from now on.
|
||||||
self.outbox.broadcast(
|
self.outbox.broadcast(
|
||||||
Message::subscribe(self.filter(), self.clock.as_millis(), Timestamp::MAX),
|
Message::subscribe(self.filter(), self.clock.into(), Timestamp::MAX),
|
||||||
self.sessions.connected().map(|(_, s)| s),
|
self.sessions.connected().map(|(_, s)| s),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -1047,7 +1047,7 @@ where
|
||||||
info!(target: "service", "Fetched {rid} from {remote} successfully");
|
info!(target: "service", "Fetched {rid} from {remote} successfully");
|
||||||
// Update our routing table in case this fetch was user-initiated and doesn't
|
// Update our routing table in case this fetch was user-initiated and doesn't
|
||||||
// come from an announcement.
|
// come from an announcement.
|
||||||
self.seed_discovered(rid, remote, self.clock.as_millis());
|
self.seed_discovered(rid, remote, self.clock.into());
|
||||||
|
|
||||||
for update in &updated {
|
for update in &updated {
|
||||||
if update.is_skipped() {
|
if update.is_skipped() {
|
||||||
|
|
@ -1181,7 +1181,7 @@ where
|
||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
self.db
|
self.db
|
||||||
.addresses_mut()
|
.addresses_mut()
|
||||||
.connected(&remote, &peer.addr, self.clock.as_millis())
|
.connected(&remote, &peer.addr, self.clock.into())
|
||||||
{
|
{
|
||||||
error!(target: "service", "Error updating address book with connection: {e}");
|
error!(target: "service", "Error updating address book with connection: {e}");
|
||||||
}
|
}
|
||||||
|
|
@ -1782,8 +1782,8 @@ where
|
||||||
// If this is our first connection to the network, we just ask for a fixed backlog
|
// If this is our first connection to the network, we just ask for a fixed backlog
|
||||||
// of messages to get us started.
|
// of messages to get us started.
|
||||||
let since = match self.db.gossip().last() {
|
let since = match self.db.gossip().last() {
|
||||||
Ok(Some(last)) => last - MAX_TIME_DELTA.as_millis() as Timestamp,
|
Ok(Some(last)) => Timestamp::from(last.to_local_time() - MAX_TIME_DELTA),
|
||||||
Ok(None) => (*now - INITIAL_SUBSCRIBE_BACKLOG_DELTA).as_millis() as Timestamp,
|
Ok(None) => (*now - INITIAL_SUBSCRIBE_BACKLOG_DELTA).into(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(target: "service", "Error getting the lastest gossip message from storage: {e}");
|
error!(target: "service", "Error getting the lastest gossip message from storage: {e}");
|
||||||
return vec![];
|
return vec![];
|
||||||
|
|
@ -1811,7 +1811,7 @@ where
|
||||||
/// Update our routing table with our local node's inventory.
|
/// Update our routing table with our local node's inventory.
|
||||||
fn sync_inventory(&mut self) -> Result<SyncedRouting, Error> {
|
fn sync_inventory(&mut self) -> Result<SyncedRouting, Error> {
|
||||||
let inventory = self.storage.inventory()?;
|
let inventory = self.storage.inventory()?;
|
||||||
let result = self.sync_routing(inventory, self.node_id(), self.clock.as_millis())?;
|
let result = self.sync_routing(inventory, self.node_id(), self.clock.into())?;
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
@ -1914,7 +1914,7 @@ where
|
||||||
&r.remote,
|
&r.remote,
|
||||||
&SIGREFS_BRANCH,
|
&SIGREFS_BRANCH,
|
||||||
r.at,
|
r.at,
|
||||||
LocalTime::from_millis(timestamp as u128),
|
timestamp.to_local_time(),
|
||||||
) {
|
) {
|
||||||
error!(
|
error!(
|
||||||
target: "service",
|
target: "service",
|
||||||
|
|
@ -2013,7 +2013,7 @@ where
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let persistent = self.config.is_persistent(&nid);
|
let persistent = self.config.is_persistent(&nid);
|
||||||
let timestamp = self.clock().as_millis();
|
let timestamp: Timestamp = self.clock.into();
|
||||||
|
|
||||||
if let Err(e) = self.db.addresses_mut().attempted(&nid, &addr, timestamp) {
|
if let Err(e) = self.db.addresses_mut().attempted(&nid, &addr, timestamp) {
|
||||||
error!(target: "service", "Error updating address book with connection attempt: {e}");
|
error!(target: "service", "Error updating address book with connection attempt: {e}");
|
||||||
|
|
@ -2091,12 +2091,13 @@ where
|
||||||
/// Get a timestamp for using in announcements.
|
/// Get a timestamp for using in announcements.
|
||||||
/// Never returns the same timestamp twice.
|
/// Never returns the same timestamp twice.
|
||||||
fn timestamp(&mut self) -> Timestamp {
|
fn timestamp(&mut self) -> Timestamp {
|
||||||
if self.clock > self.last_timestamp {
|
let now = Timestamp::from(self.clock);
|
||||||
self.last_timestamp = self.clock;
|
if *now > *self.last_timestamp {
|
||||||
|
self.last_timestamp = now;
|
||||||
} else {
|
} else {
|
||||||
self.last_timestamp = self.last_timestamp + LocalDuration::from_millis(1);
|
self.last_timestamp = self.last_timestamp + 1;
|
||||||
}
|
}
|
||||||
self.last_timestamp.as_millis()
|
self.last_timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
@ -2113,7 +2114,7 @@ where
|
||||||
self.sessions.connected().map(|(_, p)| p),
|
self.sessions.connected().map(|(_, p)| p),
|
||||||
self.db.gossip_mut(),
|
self.db.gossip_mut(),
|
||||||
);
|
);
|
||||||
self.last_announce = LocalTime::from_millis(time as u128);
|
self.last_announce = time.to_local_time();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -2126,7 +2127,7 @@ where
|
||||||
|
|
||||||
let delta = count - self.config.limits.routing_max_size;
|
let delta = count - self.config.limits.routing_max_size;
|
||||||
self.db.routing_mut().prune(
|
self.db.routing_mut().prune(
|
||||||
(*now - self.config.limits.routing_max_age).as_millis(),
|
(*now - self.config.limits.routing_max_age).into(),
|
||||||
Some(delta),
|
Some(delta),
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use std::num::TryFromIntError;
|
||||||
use std::{fmt, io};
|
use std::{fmt, io};
|
||||||
|
|
||||||
use radicle::crypto::Signature;
|
use radicle::crypto::Signature;
|
||||||
|
|
@ -17,6 +18,9 @@ pub enum Error {
|
||||||
/// An Internal error.
|
/// An Internal error.
|
||||||
#[error("internal error: {0}")]
|
#[error("internal error: {0}")]
|
||||||
Internal(#[from] sql::Error),
|
Internal(#[from] sql::Error),
|
||||||
|
/// Unit overflow.
|
||||||
|
#[error("unit overflow:: {0}")]
|
||||||
|
UnitOverflow(#[from] TryFromIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A database that has access to historical gossip messages.
|
/// A database that has access to historical gossip messages.
|
||||||
|
|
@ -54,7 +58,7 @@ impl Store for Database {
|
||||||
.db
|
.db
|
||||||
.prepare("DELETE FROM `announcements` WHERE timestamp < ?1")?;
|
.prepare("DELETE FROM `announcements` WHERE timestamp < ?1")?;
|
||||||
|
|
||||||
stmt.bind((1, cutoff.try_into().unwrap_or(i64::MAX)))?;
|
stmt.bind((1, &cutoff))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
Ok(self.db.change_count())
|
Ok(self.db.change_count())
|
||||||
|
|
@ -66,9 +70,10 @@ impl Store for Database {
|
||||||
.prepare("SELECT MAX(timestamp) AS latest FROM `announcements`")?;
|
.prepare("SELECT MAX(timestamp) AS latest FROM `announcements`")?;
|
||||||
|
|
||||||
if let Some(Ok(row)) = stmt.into_iter().next() {
|
if let Some(Ok(row)) = stmt.into_iter().next() {
|
||||||
let latest = row.try_read::<Option<i64>, _>(0)?;
|
return match row.try_read::<Option<i64>, _>(0)? {
|
||||||
|
Some(i) => Ok(Some(Timestamp::from(u64::try_from(i)?))),
|
||||||
return Ok(latest.map(|l| l as Timestamp));
|
None => Ok(None),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
@ -101,7 +106,7 @@ impl Store for Database {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stmt.bind((5, &ann.signature))?;
|
stmt.bind((5, &ann.signature))?;
|
||||||
stmt.bind((6, ann.message.timestamp().try_into().unwrap_or(i64::MAX)))?;
|
stmt.bind((6, &ann.message.timestamp()))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
Ok(self.db.change_count() > 0)
|
Ok(self.db.change_count() > 0)
|
||||||
|
|
@ -119,10 +124,10 @@ impl Store for Database {
|
||||||
WHERE timestamp >= ?1 and timestamp < ?2
|
WHERE timestamp >= ?1 and timestamp < ?2
|
||||||
ORDER BY timestamp, node, type",
|
ORDER BY timestamp, node, type",
|
||||||
)?;
|
)?;
|
||||||
assert!(from <= to);
|
assert!(*from <= *to);
|
||||||
|
|
||||||
stmt.bind((1, i64::try_from(from).unwrap_or(i64::MAX)))?;
|
stmt.bind((1, &from))?;
|
||||||
stmt.bind((2, i64::try_from(to).unwrap_or(i64::MAX)))?;
|
stmt.bind((2, &to))?;
|
||||||
|
|
||||||
Ok(Box::new(
|
Ok(Box::new(
|
||||||
stmt.into_iter()
|
stmt.into_iter()
|
||||||
|
|
@ -145,9 +150,9 @@ impl Store for Database {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let signature = row.read::<Signature, _>("signature");
|
let signature = row.read::<Signature, _>("signature");
|
||||||
let timestamp = row.read::<i64, _>("timestamp");
|
let timestamp = row.read::<Timestamp, _>("timestamp");
|
||||||
|
|
||||||
debug_assert_eq!(timestamp, message.timestamp() as i64);
|
debug_assert_eq!(timestamp, message.timestamp());
|
||||||
|
|
||||||
Ok(Announcement {
|
Ok(Announcement {
|
||||||
node,
|
node,
|
||||||
|
|
|
||||||
|
|
@ -601,7 +601,7 @@ mod tests {
|
||||||
let msg: Message = AnnouncementMessage::from(RefsAnnouncement {
|
let msg: Message = AnnouncementMessage::from(RefsAnnouncement {
|
||||||
rid: arbitrary::gen(1),
|
rid: arbitrary::gen(1),
|
||||||
refs,
|
refs,
|
||||||
timestamp: LocalTime::now().as_millis(),
|
timestamp: LocalTime::now().into(),
|
||||||
})
|
})
|
||||||
.signed(&MockSigner::default())
|
.signed(&MockSigner::default())
|
||||||
.into();
|
.into();
|
||||||
|
|
@ -621,7 +621,7 @@ mod tests {
|
||||||
inventory: arbitrary::vec(INVENTORY_LIMIT)
|
inventory: arbitrary::vec(INVENTORY_LIMIT)
|
||||||
.try_into()
|
.try_into()
|
||||||
.expect("size within bounds limit"),
|
.expect("size within bounds limit"),
|
||||||
timestamp: LocalTime::now().as_millis(),
|
timestamp: LocalTime::now().into(),
|
||||||
},
|
},
|
||||||
&MockSigner::default(),
|
&MockSigner::default(),
|
||||||
);
|
);
|
||||||
|
|
@ -646,7 +646,7 @@ mod tests {
|
||||||
#[quickcheck]
|
#[quickcheck]
|
||||||
fn prop_refs_announcement_signing(rid: RepoId) {
|
fn prop_refs_announcement_signing(rid: RepoId) {
|
||||||
let signer = MockSigner::new(&mut fastrand::Rng::new());
|
let signer = MockSigner::new(&mut fastrand::Rng::new());
|
||||||
let timestamp = 0;
|
let timestamp = Timestamp::EPOCH;
|
||||||
let at = raw::Oid::zero().into();
|
let at = raw::Oid::zero().into();
|
||||||
let refs = BoundedVec::collect_from(
|
let refs = BoundedVec::collect_from(
|
||||||
&mut [RefsAt {
|
&mut [RefsAt {
|
||||||
|
|
@ -669,7 +669,7 @@ mod tests {
|
||||||
fn test_node_announcement_validate() {
|
fn test_node_announcement_validate() {
|
||||||
let ann = NodeAnnouncement {
|
let ann = NodeAnnouncement {
|
||||||
features: node::Features::SEED,
|
features: node::Features::SEED,
|
||||||
timestamp: 42491841,
|
timestamp: Timestamp::from(42491841),
|
||||||
alias: Alias::new("alice"),
|
alias: Alias::new("alice"),
|
||||||
addresses: BoundedVec::new(),
|
addresses: BoundedVec::new(),
|
||||||
nonce: 0,
|
nonce: 0,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use crate::node::Severity;
|
||||||
use crate::service::message;
|
use crate::service::message;
|
||||||
use crate::service::message::Message;
|
use crate::service::message::Message;
|
||||||
use crate::service::{Address, LocalTime, NodeId, Outbox, RepoId, Rng};
|
use crate::service::{Address, LocalTime, NodeId, Outbox, RepoId, Rng};
|
||||||
use crate::Link;
|
use crate::{Link, Timestamp};
|
||||||
|
|
||||||
pub use crate::node::{PingState, State};
|
pub use crate::node::{PingState, State};
|
||||||
|
|
||||||
|
|
@ -15,7 +15,7 @@ pub enum Error {
|
||||||
/// The remote peer sent an invalid announcement timestamp,
|
/// The remote peer sent an invalid announcement timestamp,
|
||||||
/// for eg. a timestamp far in the future.
|
/// for eg. a timestamp far in the future.
|
||||||
#[error("invalid announcement timestamp: {0}")]
|
#[error("invalid announcement timestamp: {0}")]
|
||||||
InvalidTimestamp(u64),
|
InvalidTimestamp(Timestamp),
|
||||||
/// The remote peer sent git protocol messages while we were expecting
|
/// The remote peer sent git protocol messages while we were expecting
|
||||||
/// gossip messages. Or vice-versa.
|
/// gossip messages. Or vice-versa.
|
||||||
#[error("protocol mismatch")]
|
#[error("protocol mismatch")]
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ pub fn messages(count: usize, now: LocalTime, delta: LocalDuration) -> Vec<Messa
|
||||||
msgs.push(Message::node(
|
msgs.push(Message::node(
|
||||||
NodeAnnouncement {
|
NodeAnnouncement {
|
||||||
features: node::Features::SEED,
|
features: node::Features::SEED,
|
||||||
timestamp: time.as_millis(),
|
timestamp: time.into(),
|
||||||
alias: node::Alias::new(gen::string(5)),
|
alias: node::Alias::new(gen::string(5)),
|
||||||
addresses: None.into(),
|
addresses: None.into(),
|
||||||
nonce: 0,
|
nonce: 0,
|
||||||
|
|
@ -41,7 +41,7 @@ pub fn messages(count: usize, now: LocalTime, delta: LocalDuration) -> Vec<Messa
|
||||||
msgs.push(Message::inventory(
|
msgs.push(Message::inventory(
|
||||||
InventoryAnnouncement {
|
InventoryAnnouncement {
|
||||||
inventory: arbitrary::vec(3).try_into().unwrap(),
|
inventory: arbitrary::vec(3).try_into().unwrap(),
|
||||||
timestamp: time.as_millis(),
|
timestamp: time.into(),
|
||||||
},
|
},
|
||||||
&signer,
|
&signer,
|
||||||
));
|
));
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ where
|
||||||
for rid in storage.inventory().unwrap() {
|
for rid in storage.inventory().unwrap() {
|
||||||
policies.seed(&rid, Scope::Followed).unwrap();
|
policies.seed(&rid, Scope::Followed).unwrap();
|
||||||
}
|
}
|
||||||
let announcement = service::gossip::node(&config.config, config.local_time.as_secs());
|
let announcement = service::gossip::node(&config.config, config.local_time.into());
|
||||||
let emitter: Emitter<Event> = Default::default();
|
let emitter: Emitter<Event> = Default::default();
|
||||||
let service = Service::new(
|
let service = Service::new(
|
||||||
config.config,
|
config.config,
|
||||||
|
|
@ -249,7 +249,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn timestamp(&self) -> Timestamp {
|
pub fn timestamp(&self) -> Timestamp {
|
||||||
self.clock().as_millis()
|
(*self.clock()).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inventory(&self) -> Inventory {
|
pub fn inventory(&self) -> Inventory {
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ use crate::{git, identity, rad, runtime, service, test};
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inventory_decode() {
|
fn test_inventory_decode() {
|
||||||
let inventory: Vec<RepoId> = arbitrary::gen(300);
|
let inventory: Vec<RepoId> = arbitrary::gen(300);
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp: Timestamp = LocalTime::now().into();
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
inventory.as_slice().encode(&mut buf).unwrap();
|
inventory.as_slice().encode(&mut buf).unwrap();
|
||||||
|
|
@ -260,7 +260,7 @@ fn test_inventory_sync() {
|
||||||
let bob_signer = MockSigner::default();
|
let bob_signer = MockSigner::default();
|
||||||
let bob_storage = fixtures::storage(tmp.path().join("bob"), &bob_signer).unwrap();
|
let bob_storage = fixtures::storage(tmp.path().join("bob"), &bob_signer).unwrap();
|
||||||
let bob = Peer::config("bob", [8, 8, 8, 8], bob_storage, peer::Config::default());
|
let bob = Peer::config("bob", [8, 8, 8, 8], bob_storage, peer::Config::default());
|
||||||
let now = LocalTime::now().as_millis();
|
let now = LocalTime::now().into();
|
||||||
let projs = bob.storage().inventory().unwrap();
|
let projs = bob.storage().inventory().unwrap();
|
||||||
|
|
||||||
alice.connect_to(&bob);
|
alice.connect_to(&bob);
|
||||||
|
|
@ -374,7 +374,7 @@ fn test_inventory_pruning() {
|
||||||
inventory: test::arbitrary::vec::<RepoId>(num_projs)
|
inventory: test::arbitrary::vec::<RepoId>(num_projs)
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
timestamp: bob.local_time().as_millis(),
|
timestamp: bob.local_time().into(),
|
||||||
},
|
},
|
||||||
peer.signer(),
|
peer.signer(),
|
||||||
),
|
),
|
||||||
|
|
@ -567,8 +567,8 @@ fn test_announcement_rebroadcast_timestamp_filtered() {
|
||||||
eve.id(),
|
eve.id(),
|
||||||
Message::Subscribe(Subscribe {
|
Message::Subscribe(Subscribe {
|
||||||
filter: Filter::default(),
|
filter: Filter::default(),
|
||||||
since: alice.local_time().as_millis(),
|
since: alice.local_time().into(),
|
||||||
until: (alice.local_time() + delta).as_millis(),
|
until: (alice.local_time() + delta).into(),
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -946,7 +946,7 @@ fn test_inventory_relay() {
|
||||||
let bob = Peer::new("bob", [8, 8, 8, 8]);
|
let bob = Peer::new("bob", [8, 8, 8, 8]);
|
||||||
let eve = Peer::new("eve", [9, 9, 9, 9]);
|
let eve = Peer::new("eve", [9, 9, 9, 9]);
|
||||||
let inv = BoundedVec::try_from(arbitrary::vec(1)).unwrap();
|
let inv = BoundedVec::try_from(arbitrary::vec(1)).unwrap();
|
||||||
let now = LocalTime::now().as_millis();
|
let now = LocalTime::now().into();
|
||||||
|
|
||||||
// Inventory from Bob relayed to Eve.
|
// Inventory from Bob relayed to Eve.
|
||||||
alice.init();
|
alice.init();
|
||||||
|
|
@ -1274,7 +1274,7 @@ fn test_seed_repo_subscribe() {
|
||||||
filter,
|
filter,
|
||||||
since,
|
since,
|
||||||
..
|
..
|
||||||
})) if since == alice.clock().as_millis() && filter.contains(&rid)
|
})) if since == alice.timestamp() && filter.contains(&rid)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1292,7 +1292,7 @@ fn test_fetch_missing_inventory_on_gossip() {
|
||||||
Message::inventory(
|
Message::inventory(
|
||||||
InventoryAnnouncement {
|
InventoryAnnouncement {
|
||||||
inventory: vec![rid].try_into().unwrap(),
|
inventory: vec![rid].try_into().unwrap(),
|
||||||
timestamp: now.as_millis(),
|
timestamp: now.into(),
|
||||||
},
|
},
|
||||||
bob.signer(),
|
bob.signer(),
|
||||||
),
|
),
|
||||||
|
|
@ -1317,7 +1317,7 @@ fn test_fetch_missing_inventory_on_schedule() {
|
||||||
Message::inventory(
|
Message::inventory(
|
||||||
InventoryAnnouncement {
|
InventoryAnnouncement {
|
||||||
inventory: vec![rid].try_into().unwrap(),
|
inventory: vec![rid].try_into().unwrap(),
|
||||||
timestamp: now.as_millis(),
|
timestamp: now.into(),
|
||||||
},
|
},
|
||||||
bob.signer(),
|
bob.signer(),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ use crate::service::filter;
|
||||||
use crate::storage::refs::Refs;
|
use crate::storage::refs::Refs;
|
||||||
use crate::storage::refs::RefsAt;
|
use crate::storage::refs::RefsAt;
|
||||||
use crate::storage::refs::SignedRefs;
|
use crate::storage::refs::SignedRefs;
|
||||||
|
use crate::Timestamp;
|
||||||
|
|
||||||
/// The default type we use to represent sizes on the wire.
|
/// The default type we use to represent sizes on the wire.
|
||||||
///
|
///
|
||||||
|
|
@ -527,6 +528,20 @@ impl Decode for tor::OnionAddrV3 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encode for Timestamp {
|
||||||
|
fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
|
||||||
|
self.deref().encode(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decode for Timestamp {
|
||||||
|
fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
|
||||||
|
let millis = u64::decode(reader)?;
|
||||||
|
|
||||||
|
Ok(Timestamp::from(millis))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ pub mod policy;
|
||||||
pub mod refs;
|
pub mod refs;
|
||||||
pub mod routing;
|
pub mod routing;
|
||||||
pub mod seed;
|
pub mod seed;
|
||||||
|
pub mod timestamp;
|
||||||
|
|
||||||
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
|
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
@ -41,6 +42,7 @@ pub use db::Database;
|
||||||
pub use events::{Event, Events};
|
pub use events::{Event, Events};
|
||||||
pub use features::Features;
|
pub use features::Features;
|
||||||
pub use seed::SyncedAt;
|
pub use seed::SyncedAt;
|
||||||
|
pub use timestamp::Timestamp;
|
||||||
|
|
||||||
/// Default name for control socket file.
|
/// Default name for control socket file.
|
||||||
pub const DEFAULT_SOCKET_NAME: &str = "control.sock";
|
pub const DEFAULT_SOCKET_NAME: &str = "control.sock";
|
||||||
|
|
@ -65,9 +67,6 @@ pub const NODE_ANNOUNCEMENT_FILE: &str = "announcement.wire.debug";
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
pub const NODE_ANNOUNCEMENT_FILE: &str = "announcement.wire";
|
pub const NODE_ANNOUNCEMENT_FILE: &str = "announcement.wire";
|
||||||
|
|
||||||
/// Milliseconds since epoch.
|
|
||||||
pub type Timestamp = u64;
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
|
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
|
||||||
pub enum PingState {
|
pub enum PingState {
|
||||||
#[default]
|
#[default]
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ impl Store for Database {
|
||||||
if let Some(Ok(row)) = stmt.into_iter().next() {
|
if let Some(Ok(row)) = stmt.into_iter().next() {
|
||||||
let features = row.read::<node::Features, _>("features");
|
let features = row.read::<node::Features, _>("features");
|
||||||
let alias = Alias::from_str(row.read::<&str, _>("alias"))?;
|
let alias = Alias::from_str(row.read::<&str, _>("alias"))?;
|
||||||
let timestamp = row.read::<i64, _>("timestamp") as Timestamp;
|
let timestamp = row.read::<Timestamp, _>("timestamp");
|
||||||
let pow = row.read::<i64, _>("pow") as u32;
|
let pow = row.read::<i64, _>("pow") as u32;
|
||||||
let penalty = row.read::<i64, _>("penalty").min(u8::MAX as i64);
|
let penalty = row.read::<i64, _>("penalty").min(u8::MAX as i64);
|
||||||
let penalty = Penalty(penalty as u8);
|
let penalty = Penalty(penalty as u8);
|
||||||
|
|
@ -185,7 +185,7 @@ impl Store for Database {
|
||||||
stmt.bind((2, features))?;
|
stmt.bind((2, features))?;
|
||||||
stmt.bind((3, sql::Value::String(alias.into())))?;
|
stmt.bind((3, sql::Value::String(alias.into())))?;
|
||||||
stmt.bind((4, pow as i64))?;
|
stmt.bind((4, pow as i64))?;
|
||||||
stmt.bind((5, timestamp as i64))?;
|
stmt.bind((5, ×tamp))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
for addr in addrs {
|
for addr in addrs {
|
||||||
|
|
@ -200,7 +200,7 @@ impl Store for Database {
|
||||||
stmt.bind((2, AddressType::from(&addr.addr)))?;
|
stmt.bind((2, AddressType::from(&addr.addr)))?;
|
||||||
stmt.bind((3, &addr.addr))?;
|
stmt.bind((3, &addr.addr))?;
|
||||||
stmt.bind((4, addr.source))?;
|
stmt.bind((4, addr.source))?;
|
||||||
stmt.bind((5, timestamp as i64))?;
|
stmt.bind((5, ×tamp))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
}
|
}
|
||||||
Ok::<_, Error>(db.change_count() > 0)
|
Ok::<_, Error>(db.change_count() > 0)
|
||||||
|
|
@ -265,7 +265,7 @@ impl Store for Database {
|
||||||
AND value = ?4",
|
AND value = ?4",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
stmt.bind((1, time as i64))?;
|
stmt.bind((1, &time))?;
|
||||||
stmt.bind((2, nid))?;
|
stmt.bind((2, nid))?;
|
||||||
stmt.bind((3, AddressType::from(addr)))?;
|
stmt.bind((3, AddressType::from(addr)))?;
|
||||||
stmt.bind((4, addr))?;
|
stmt.bind((4, addr))?;
|
||||||
|
|
@ -284,7 +284,7 @@ impl Store for Database {
|
||||||
AND value = ?4",
|
AND value = ?4",
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
stmt.bind((1, time as i64))?;
|
stmt.bind((1, &time))?;
|
||||||
stmt.bind((2, nid))?;
|
stmt.bind((2, nid))?;
|
||||||
stmt.bind((3, AddressType::from(addr)))?;
|
stmt.bind((3, AddressType::from(addr)))?;
|
||||||
stmt.bind((4, addr))?;
|
stmt.bind((4, addr))?;
|
||||||
|
|
@ -435,7 +435,7 @@ mod test {
|
||||||
let alice = arbitrary::gen::<NodeId>(1);
|
let alice = arbitrary::gen::<NodeId>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = Timestamp::from(LocalTime::now());
|
||||||
|
|
||||||
cache
|
cache
|
||||||
.insert(&alice, features, Alias::new("alice"), 16, timestamp, [])
|
.insert(&alice, features, Alias::new("alice"), 16, timestamp, [])
|
||||||
|
|
@ -455,7 +455,7 @@ mod test {
|
||||||
let alice = arbitrary::gen::<NodeId>(1);
|
let alice = arbitrary::gen::<NodeId>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = LocalTime::now().into();
|
||||||
|
|
||||||
let ka = KnownAddress {
|
let ka = KnownAddress {
|
||||||
addr: net::SocketAddr::from(([4, 4, 4, 4], 8776)).into(),
|
addr: net::SocketAddr::from(([4, 4, 4, 4], 8776)).into(),
|
||||||
|
|
@ -490,7 +490,7 @@ mod test {
|
||||||
let alice = arbitrary::gen::<NodeId>(1);
|
let alice = arbitrary::gen::<NodeId>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = LocalTime::now().into();
|
||||||
let alias = Alias::new("alice");
|
let alias = Alias::new("alice");
|
||||||
|
|
||||||
let ka = KnownAddress {
|
let ka = KnownAddress {
|
||||||
|
|
@ -517,7 +517,7 @@ mod test {
|
||||||
fn test_insert_and_update() {
|
fn test_insert_and_update() {
|
||||||
let alice = arbitrary::gen::<NodeId>(1);
|
let alice = arbitrary::gen::<NodeId>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = LocalTime::now().into();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let alias1 = Alias::new("alice");
|
let alias1 = Alias::new("alice");
|
||||||
let alias2 = Alias::new("~alice~");
|
let alias2 = Alias::new("~alice~");
|
||||||
|
|
@ -572,7 +572,7 @@ mod test {
|
||||||
let alice = arbitrary::gen::<NodeId>(1);
|
let alice = arbitrary::gen::<NodeId>(1);
|
||||||
let bob = arbitrary::gen::<NodeId>(1);
|
let bob = arbitrary::gen::<NodeId>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = LocalTime::now().into();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let alice_alias = Alias::new("alice");
|
let alice_alias = Alias::new("alice");
|
||||||
let bob_alias = Alias::new("bob");
|
let bob_alias = Alias::new("bob");
|
||||||
|
|
@ -620,7 +620,7 @@ mod test {
|
||||||
let mut rng = fastrand::Rng::new();
|
let mut rng = fastrand::Rng::new();
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let mut expected = Vec::new();
|
let mut expected = Vec::new();
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = LocalTime::now().into();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let alias = Alias::new("alice");
|
let alias = Alias::new("alice");
|
||||||
|
|
||||||
|
|
@ -660,7 +660,7 @@ mod test {
|
||||||
let addr = arbitrary::gen::<Address>(1);
|
let addr = arbitrary::gen::<Address>(1);
|
||||||
let mut cache = Database::memory().unwrap();
|
let mut cache = Database::memory().unwrap();
|
||||||
let features = node::Features::SEED;
|
let features = node::Features::SEED;
|
||||||
let timestamp = LocalTime::now().as_millis();
|
let timestamp = Timestamp::from(LocalTime::now());
|
||||||
|
|
||||||
cache
|
cache
|
||||||
.insert(&alice, features, Alias::new("alice"), 16, timestamp, [])
|
.insert(&alice, features, Alias::new("alice"), 16, timestamp, [])
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ impl Store for Database {
|
||||||
stmt.bind((2, node))?;
|
stmt.bind((2, node))?;
|
||||||
|
|
||||||
if let Some(Ok(row)) = stmt.into_iter().next() {
|
if let Some(Ok(row)) = stmt.into_iter().next() {
|
||||||
return Ok(Some(row.read::<i64, _>("timestamp") as Timestamp));
|
return Ok(Some(row.read::<Timestamp, _>("timestamp")));
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
@ -108,7 +108,6 @@ impl Store for Database {
|
||||||
node: NodeId,
|
node: NodeId,
|
||||||
time: Timestamp,
|
time: Timestamp,
|
||||||
) -> Result<Vec<(RepoId, InsertResult)>, Error> {
|
) -> Result<Vec<(RepoId, InsertResult)>, Error> {
|
||||||
let time: i64 = time.try_into().map_err(|_| Error::UnitOverflow)?;
|
|
||||||
let mut results = Vec::new();
|
let mut results = Vec::new();
|
||||||
|
|
||||||
transaction(&self.db, |db| {
|
transaction(&self.db, |db| {
|
||||||
|
|
@ -130,7 +129,7 @@ impl Store for Database {
|
||||||
|
|
||||||
stmt.bind((1, id))?;
|
stmt.bind((1, id))?;
|
||||||
stmt.bind((2, &node))?;
|
stmt.bind((2, &node))?;
|
||||||
stmt.bind((3, time))?;
|
stmt.bind((3, &time))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
let result = match (self.db.change_count() > 0, existed) {
|
let result = match (self.db.change_count() > 0, existed) {
|
||||||
|
|
@ -184,7 +183,6 @@ impl Store for Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prune(&mut self, oldest: Timestamp, limit: Option<usize>) -> Result<usize, Error> {
|
fn prune(&mut self, oldest: Timestamp, limit: Option<usize>) -> Result<usize, Error> {
|
||||||
let oldest: i64 = oldest.try_into().map_err(|_| Error::UnitOverflow)?;
|
|
||||||
let limit: i64 = limit
|
let limit: i64 = limit
|
||||||
.unwrap_or(i64::MAX as usize)
|
.unwrap_or(i64::MAX as usize)
|
||||||
.try_into()
|
.try_into()
|
||||||
|
|
@ -194,7 +192,7 @@ impl Store for Database {
|
||||||
"DELETE FROM routing WHERE rowid IN
|
"DELETE FROM routing WHERE rowid IN
|
||||||
(SELECT rowid FROM routing WHERE timestamp < ? LIMIT ?)",
|
(SELECT rowid FROM routing WHERE timestamp < ? LIMIT ?)",
|
||||||
)?;
|
)?;
|
||||||
stmt.bind((1, oldest))?;
|
stmt.bind((1, &oldest))?;
|
||||||
stmt.bind((2, limit))?;
|
stmt.bind((2, limit))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
|
|
@ -243,7 +241,7 @@ mod test {
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert(&ids, *node, 0).unwrap(),
|
db.insert(&ids, *node, Timestamp::EPOCH).unwrap(),
|
||||||
ids.iter()
|
ids.iter()
|
||||||
.map(|id| (*id, InsertResult::SeedAdded))
|
.map(|id| (*id, InsertResult::SeedAdded))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
|
@ -265,7 +263,7 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
db.insert(&ids, *node, 0).unwrap();
|
db.insert(&ids, *node, Timestamp::EPOCH).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
|
|
@ -284,7 +282,7 @@ mod test {
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
assert!(db
|
assert!(db
|
||||||
.insert(&ids, *node, 0)
|
.insert(&ids, *node, Timestamp::EPOCH)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.all(|(_, r)| *r == InsertResult::SeedAdded));
|
.all(|(_, r)| *r == InsertResult::SeedAdded));
|
||||||
|
|
@ -306,7 +304,7 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
db.insert(&ids, *node, 0).unwrap();
|
db.insert(&ids, *node, Timestamp::EPOCH).unwrap();
|
||||||
}
|
}
|
||||||
for id in &ids {
|
for id in &ids {
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
|
|
@ -325,15 +323,15 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 0).unwrap(),
|
db.insert([&id], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id, InsertResult::SeedAdded)]
|
vec![(id, InsertResult::SeedAdded)]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 0).unwrap(),
|
db.insert([&id], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id, InsertResult::NotUpdated)]
|
vec![(id, InsertResult::NotUpdated)]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 0).unwrap(),
|
db.insert([&id], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id, InsertResult::NotUpdated)]
|
vec![(id, InsertResult::NotUpdated)]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -345,14 +343,14 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 0).unwrap(),
|
db.insert([&id], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id, InsertResult::SeedAdded)]
|
vec![(id, InsertResult::SeedAdded)]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 1).unwrap(),
|
db.insert([&id], node, Timestamp::from(1)).unwrap(),
|
||||||
vec![(id, InsertResult::TimeUpdated)]
|
vec![(id, InsertResult::TimeUpdated)]
|
||||||
);
|
);
|
||||||
assert_eq!(db.entry(&id, &node).unwrap(), Some(1));
|
assert_eq!(db.entry(&id, &node).unwrap(), Some(Timestamp::from(1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -363,18 +361,18 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id1], node, 0).unwrap(),
|
db.insert([&id1], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id1, InsertResult::SeedAdded)]
|
vec![(id1, InsertResult::SeedAdded)]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id1, &id2], node, 0).unwrap(),
|
db.insert([&id1, &id2], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![
|
vec![
|
||||||
(id1, InsertResult::NotUpdated),
|
(id1, InsertResult::NotUpdated),
|
||||||
(id2, InsertResult::SeedAdded)
|
(id2, InsertResult::SeedAdded)
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id1, &id2], node, 1).unwrap(),
|
db.insert([&id1, &id2], node, Timestamp::from(1)).unwrap(),
|
||||||
vec![
|
vec![
|
||||||
(id1, InsertResult::TimeUpdated),
|
(id1, InsertResult::TimeUpdated),
|
||||||
(id2, InsertResult::TimeUpdated)
|
(id2, InsertResult::TimeUpdated)
|
||||||
|
|
@ -389,7 +387,7 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.insert([&id], node, 0).unwrap(),
|
db.insert([&id], node, Timestamp::EPOCH).unwrap(),
|
||||||
vec![(id, InsertResult::SeedAdded)]
|
vec![(id, InsertResult::SeedAdded)]
|
||||||
);
|
);
|
||||||
assert!(db.remove(&id, &node).unwrap());
|
assert!(db.remove(&id, &node).unwrap());
|
||||||
|
|
@ -402,7 +400,7 @@ mod test {
|
||||||
let ids = arbitrary::vec::<RepoId>(10);
|
let ids = arbitrary::vec::<RepoId>(10);
|
||||||
let node = arbitrary::gen(1);
|
let node = arbitrary::gen(1);
|
||||||
|
|
||||||
db.insert(&ids, node, LocalTime::now().as_millis()).unwrap();
|
db.insert(&ids, node, LocalTime::now().into()).unwrap();
|
||||||
|
|
||||||
assert_eq!(10, db.len().unwrap(), "correct number of rows in table");
|
assert_eq!(10, db.len().unwrap(), "correct number of rows in table");
|
||||||
}
|
}
|
||||||
|
|
@ -417,7 +415,7 @@ mod test {
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
let time = rng.u64(..now.as_millis());
|
let time = rng.u64(..now.as_millis());
|
||||||
db.insert(&ids, *node, time).unwrap();
|
db.insert(&ids, *node, Timestamp::from(time)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let ids = arbitrary::vec::<RepoId>(10);
|
let ids = arbitrary::vec::<RepoId>(10);
|
||||||
|
|
@ -425,16 +423,16 @@ mod test {
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
let time = rng.u64(now.as_millis()..i64::MAX as u64);
|
let time = rng.u64(now.as_millis()..i64::MAX as u64);
|
||||||
db.insert(&ids, *node, time).unwrap();
|
db.insert(&ids, *node, Timestamp::from(time)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let pruned = db.prune(now.as_millis(), None).unwrap();
|
let pruned = db.prune(now.into(), None).unwrap();
|
||||||
assert_eq!(pruned, ids.len() * nodes.len());
|
assert_eq!(pruned, ids.len() * nodes.len());
|
||||||
|
|
||||||
for id in &ids {
|
for id in &ids {
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
let t = db.entry(id, node).unwrap().unwrap();
|
let t = db.entry(id, node).unwrap().unwrap();
|
||||||
assert!(t >= now.as_millis());
|
assert!(*t >= *Timestamp::from(now));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -446,7 +444,7 @@ mod test {
|
||||||
let mut db = database(":memory:");
|
let mut db = database(":memory:");
|
||||||
|
|
||||||
for node in &nodes {
|
for node in &nodes {
|
||||||
db.insert([&id], *node, 0).unwrap();
|
db.insert([&id], *node, Timestamp::EPOCH).unwrap();
|
||||||
}
|
}
|
||||||
assert_eq!(db.count(&id).unwrap(), nodes.len());
|
assert_eq!(db.count(&id).unwrap(), nodes.len());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ impl Store for Database {
|
||||||
stmt.bind((1, rid))?;
|
stmt.bind((1, rid))?;
|
||||||
stmt.bind((2, nid))?;
|
stmt.bind((2, nid))?;
|
||||||
stmt.bind((3, at.to_string().as_str()))?;
|
stmt.bind((3, at.to_string().as_str()))?;
|
||||||
stmt.bind((4, timestamp as i64))?;
|
stmt.bind((4, ×tamp))?;
|
||||||
stmt.next()?;
|
stmt.next()?;
|
||||||
|
|
||||||
Ok(self.db.change_count() > 0)
|
Ok(self.db.change_count() > 0)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
use std::{
|
||||||
|
fmt,
|
||||||
|
ops::{Add, Deref, Sub},
|
||||||
|
};
|
||||||
|
|
||||||
|
use localtime::LocalTime;
|
||||||
|
use sqlite as sql;
|
||||||
|
|
||||||
|
/// Milliseconds since epoch.
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
pub struct Timestamp(u64);
|
||||||
|
|
||||||
|
impl Add<u64> for Timestamp {
|
||||||
|
type Output = Timestamp;
|
||||||
|
|
||||||
|
fn add(self, millis: u64) -> Self::Output {
|
||||||
|
Self(self.0 + millis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub<u64> for Timestamp {
|
||||||
|
type Output = Timestamp;
|
||||||
|
|
||||||
|
fn sub(self, millis: u64) -> Self::Output {
|
||||||
|
Self(self.0 - millis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Timestamp {
|
||||||
|
/// UNIX epoch.
|
||||||
|
pub const EPOCH: Self = Self(0);
|
||||||
|
/// Minimum value.
|
||||||
|
pub const MIN: Self = Self(0);
|
||||||
|
/// Maximum value.
|
||||||
|
// Nb. This is the maximum value that can fit in a signed 64-bit integer (`i64`).
|
||||||
|
// This makes it possible to store timestamps in sqlite.
|
||||||
|
pub const MAX: Self = Self(9223372036854775807);
|
||||||
|
|
||||||
|
/// Convert to local time.
|
||||||
|
pub fn to_local_time(&self) -> LocalTime {
|
||||||
|
(*self).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Timestamp {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
self.0.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for Timestamp {
|
||||||
|
type Target = u64;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<LocalTime> for Timestamp {
|
||||||
|
fn from(t: LocalTime) -> Self {
|
||||||
|
Self(t.as_millis())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Timestamp> for LocalTime {
|
||||||
|
fn from(t: Timestamp) -> Self {
|
||||||
|
LocalTime::from_millis(t.0 as u128)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u64> for Timestamp {
|
||||||
|
fn from(u: u64) -> Self {
|
||||||
|
Self(u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&sql::Value> for Timestamp {
|
||||||
|
type Error = sql::Error;
|
||||||
|
|
||||||
|
fn try_from(value: &sql::Value) -> Result<Self, Self::Error> {
|
||||||
|
match value {
|
||||||
|
sql::Value::Integer(i) => match (*i).try_into() {
|
||||||
|
Ok(u) => Ok(Timestamp(u)),
|
||||||
|
Err(e) => Err(sql::Error {
|
||||||
|
code: None,
|
||||||
|
message: Some(format!("sql: invalid integer for timestamp: {e}")),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
_ => Err(sql::Error {
|
||||||
|
code: None,
|
||||||
|
message: Some("sql: invalid type for timestamp".to_owned()),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl sql::BindableWithIndex for &Timestamp {
|
||||||
|
fn bind<I: sql::ParameterIndex>(self, stmt: &mut sql::Statement<'_>, i: I) -> sql::Result<()> {
|
||||||
|
match i64::try_from(*self.deref()) {
|
||||||
|
Ok(integer) => integer.bind(stmt, i),
|
||||||
|
Err(e) => Err(sql::Error {
|
||||||
|
code: None,
|
||||||
|
message: Some(format!("sql: invalid timestamp: {e}")),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ use crate::identity::{
|
||||||
Did,
|
Did,
|
||||||
};
|
};
|
||||||
use crate::node::address::AddressType;
|
use crate::node::address::AddressType;
|
||||||
use crate::node::{Address, Alias};
|
use crate::node::{Address, Alias, Timestamp};
|
||||||
use crate::storage;
|
use crate::storage;
|
||||||
use crate::storage::refs::{Refs, RefsAt, SignedRefs};
|
use crate::storage::refs::{Refs, RefsAt, SignedRefs};
|
||||||
use crate::test::storage::{MockRepository, MockStorage};
|
use crate::test::storage::{MockRepository, MockStorage};
|
||||||
|
|
@ -312,3 +312,9 @@ impl Arbitrary for Alias {
|
||||||
Alias::from_str(s).unwrap()
|
Alias::from_str(s).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Arbitrary for Timestamp {
|
||||||
|
fn arbitrary(g: &mut qcheck::Gen) -> Self {
|
||||||
|
Self::from(u64::arbitrary(g))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue