From f12a839d6e6bdd29a86ac52d5de836519a41aaf0 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 26 Oct 2026 05:38:41 +0100 Subject: wrap unix timestamps in newtype --- src/remake/vote.rs | 64 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'src/remake/vote.rs') diff --git a/src/remake/vote.rs b/src/remake/vote.rs index 40ec6bf..cc805e3 100644 --- a/src/remake/vote.rs +++ b/src/remake/vote.rs @@ -29,6 +29,7 @@ use tracing::{info, warn}; use super::Host; use super::content; use super::records::{self, Decision, EpochRecord, Phase, Record, Register, Vote}; +use crate::clock::UnixMillis; use crate::document::store::StoreError; /// A majority of `electorate` members. @@ -162,7 +163,7 @@ pub(crate) fn freedom( epoch: &EpochRecord, all: &[(String, Record)], register: &Register, - started: u64, + started: UnixMillis, ) -> Freedom { match held_by(me, register, all) { Held::Free => {} @@ -200,8 +201,8 @@ pub(crate) fn choice( epoch: &EpochRecord, all: &[(String, Record)], register: &Register, - started: u64, - now: u64, + started: UnixMillis, + now: UnixMillis, grace: u64, ) -> Option { let only = match freedom(me, epoch, all, register, started) { @@ -231,7 +232,7 @@ pub(super) async fn cast(host: &H) { let me = host.node_id(); let epoch = EpochRecord::from(host.current_epoch()); let grace = host.dormant_after().as_millis() as u64; - let started = host.remake().started_at() + grace; + let started = host.remake().started_at().plus_ms(grace); // Usually there is nothing to vote on: look first, without writing. let (voter, wanted) = (me.clone(), epoch.clone()); @@ -294,6 +295,7 @@ mod tests { use serde_json::Value as JsonValue; fn record(proposed_at: u64, electorate: &[&str]) -> Record { + let proposed_at = UnixMillis(proposed_at); Record { proposal: Proposal { coordinator: electorate[0].to_owned(), @@ -305,7 +307,7 @@ mod tests { }, settle_ms: 1_000, proposed_at, - settle_until: proposed_at + 1_000, + settle_until: proposed_at.plus_ms(1_000), switch_delay_ms: None, electorate: electorate.iter().map(|m| (*m).to_owned()).collect(), quorum: quorum(electorate.len()), @@ -329,7 +331,7 @@ mod tests { .iter() .map(|remake| Vote { remake: (*remake).to_owned(), - at: 0, + at: UnixMillis(0), }) .collect(); ((*voter).to_owned(), votes) @@ -340,14 +342,14 @@ mod tests { fn aborted(mut record: Record) -> Record { record.decision = Some(Decision::Aborted { reason: "test".into(), - decided_at: 150, + decided_at: UnixMillis(150), }); record } const ALL: [&str; 5] = ["a", "b", "c", "d", "e"]; /// Long before any proposal: no restart concerns. - const STARTED: u64 = 0; + const STARTED: UnixMillis = UnixMillis(0); fn genesis() -> EpochRecord { Epoch::GENESIS.into() @@ -395,7 +397,7 @@ mod tests { assert_eq!((y.yes, y.elsewhere, y.pending), (1, 0, 4)); // c may vote again. assert_eq!( - choice("c", &genesis(), &all, &votes, STARTED, 250, 0).as_deref(), + choice("c", &genesis(), &all, &votes, STARTED, UnixMillis(250), 0).as_deref(), Some("y") ); } @@ -408,9 +410,9 @@ mod tests { ("y".to_owned(), record(5_000, &ALL)), ]; let votes = register(&[("a", &["x"]), ("b", &["x"]), ("c", &["x"])]); - assert_eq!(all[0].1.phase(5_000, 0), Phase::Expired); + assert_eq!(all[0].1.phase(UnixMillis(5_000), 0), Phase::Expired); assert_eq!( - choice("a", &genesis(), &all, &votes, STARTED, 5_000, 0), + choice("a", &genesis(), &all, &votes, STARTED, UnixMillis(5_000), 0), None ); let y = tally("y", &all[1].1, &all, &votes); @@ -452,21 +454,32 @@ mod tests { ]; let none = register(&[]); assert_eq!( - choice("a", &genesis(), &all, &none, STARTED, 150, 0).as_deref(), + choice("a", &genesis(), &all, &none, STARTED, UnixMillis(150), 0).as_deref(), Some("x") ); assert_eq!( - choice("outsider", &genesis(), &all, &none, STARTED, 150, 0), + choice( + "outsider", + &genesis(), + &all, + &none, + STARTED, + UnixMillis(150), + 0 + ), None ); let voted = register(&[("a", &["y"])]); - assert_eq!(choice("a", &genesis(), &all, &voted, STARTED, 150, 0), None); + assert_eq!( + choice("a", &genesis(), &all, &voted, STARTED, UnixMillis(150), 0), + None + ); } #[test] fn after_a_start_only_the_remake_voted_for_before() { - let started = 150; + let started = UnixMillis(150); // Proposed before the start: a may have voted for x then. let all = vec![ ("x".to_owned(), record(100, &ALL)), @@ -478,7 +491,16 @@ mod tests { Freedom::Only("x".to_owned()) ); assert_eq!( - choice("a", &genesis(), &all, &none, started, 250, 1_000).as_deref(), + choice( + "a", + &genesis(), + &all, + &none, + started, + UnixMillis(250), + 1_000 + ) + .as_deref(), Some("x") ); @@ -493,7 +515,15 @@ mod tests { Freedom::Held(_) )); assert_eq!( - choice("a", &genesis(), &all, &none, started, 250, 1_000), + choice( + "a", + &genesis(), + &all, + &none, + started, + UnixMillis(250), + 1_000 + ), None ); -- cgit v1.3.1