1use std::fmt;
2use std::path::Path;
3
4use anyhow::Result;
5use chrono::Utc;
6use second_brain_core::keys::Epoch;
7use second_brain_core::sqlite_store::{ClockFloorRejected, ClockFloors, SqliteStore};
8use second_brain_core::store::StoreAdmin;
9use second_brain_sync::keyring::Keyring;
10use second_brain_sync::protocol::Hello;
11
12use crate::clock_gate::ClockGate;
13use crate::device_access::DeviceAccess;
14use crate::health::Health;
15use crate::identity_report;
16
17#[derive(Debug)]
18pub struct HelloRejected(pub String);
19
20impl fmt::Display for HelloRejected {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 write!(f, "sync hello rejected: {}", self.0)
23 }
24}
25
26impl std::error::Error for HelloRejected {}
27
28pub fn hello_for(
29 sqlite: &SqliteStore,
30 clock: &ClockGate,
31 data_dir: &Path,
32 peer_id: &str,
33 declared: Option<&[Epoch]>,
34) -> Result<Hello> {
35 let cursor = sqlite.get_sync_state(peer_id)?.map(|state| state.last_seq);
36 Ok(Hello {
37 machine_id: sqlite.machine_id().to_string(),
38 wall_micros: clock.wall_micros(),
39 synced: clock.synced(),
40 cursor,
41 applied: cursor
42 .map(|cursor| sqlite.applied_through(peer_id, cursor))
43 .transpose()?,
44 merging: crate::snapshot_merge::merging(data_dir)?,
45 held_origin_seq: sqlite.highest_origin_seq(peer_id)?,
46 keys: Some(Keyring::load(sqlite, data_dir)?.handshake_for(peer_id, declared)?),
47 peer_in: sqlite.peer_in(peer_id)?,
48 log_floor: sqlite.log_floor()?,
49 head: sqlite.log_head()?,
50 })
51}
52
53pub fn check_identity(sqlite: &SqliteStore, data_dir: &Path, hello: &Hello) -> Result<()> {
56 Keyring::load(sqlite, data_dir)?.check_peer(hello.keys()?, &hello.machine_id)
57}
58
59pub fn accept_handshake(
65 sqlite: &SqliteStore,
66 clock: &ClockGate,
67 health: &Health,
68 access: &DeviceAccess,
69 data_dir: &Path,
70 hello: &Hello,
71) -> Result<()> {
72 check_identity(sqlite, data_dir, hello)?;
73 let keyring = Keyring::load(sqlite, data_dir)?;
74 let accepted = keyring.accept(&hello.machine_id, hello.keys()?, clock.wall_micros());
75 access.sweep(sqlite, data_dir);
76 identity_report::report_keyring(&keyring, health);
77 accepted?;
78 accept_hello(sqlite, clock, health, hello)
79}
80
81pub fn accept_hello(
86 sqlite: &SqliteStore,
87 clock: &ClockGate,
88 health: &Health,
89 hello: &Hello,
90) -> Result<()> {
91 let raised =
92 peer_floors(sqlite, clock, hello).and_then(|floors| sqlite.raise_clock_floors(&floors));
93 match raised {
94 Err(e) if e.downcast_ref::<ClockFloorRejected>().is_some() => {
95 let reason = format!("from {}: {e:#}", hello.machine_id);
96 health.clock_handshake_rejected(&hello.machine_id, reason.clone(), Utc::now());
97 Err(HelloRejected(reason).into())
98 }
99 Err(e) => Err(e),
100 Ok(()) => {
101 if hello.synced {
102 clock.open_by_peer();
103 }
104 health.clock_handshake_accepted(&hello.machine_id, Utc::now());
105 Ok(())
106 }
107 }
108}
109
110fn peer_floors(sqlite: &SqliteStore, clock: &ClockGate, hello: &Hello) -> Result<ClockFloors> {
113 let rejected = |what: &str, e: String| ClockFloorRejected(format!("{what}: {e}"));
114 let basis_micros = if hello.synced {
115 clock
116 .check_peer_time(hello.wall_micros)
117 .map_err(|e| rejected("wall clock", e))?;
118 Some(hello.wall_micros)
119 } else {
120 None
121 };
122 let last_seq = match hello.applied_of_receiver() {
123 Some(cursor) => {
124 let cursor = i64::try_from(cursor)
125 .map_err(|_| ClockFloorRejected(format!("cursor {cursor} overflows")))?;
126 if cursor <= sqlite.clock_state()?.last {
127 None
128 } else {
129 clock
130 .check_peer_time(cursor)
131 .map_err(|e| rejected("cursor", e))?;
132 Some(cursor)
133 }
134 }
135 None => None,
136 };
137 Ok(ClockFloors {
138 basis_micros,
139 last_seq,
140 held_origin_seq: hello.held_origin_seq,
141 })
142}
143
144#[cfg(test)]
145mod tests {
146 use std::sync::Arc;
147
148 use second_brain_core::sqlite_store::{ClockFloors, SqliteStore};
149
150 use super::{HelloRejected, accept_hello};
151 use crate::clock_gate::{ClockGate, TimeSignals};
152 use crate::health::Health;
153 use second_brain_sync::protocol::Hello;
154
155 const DAY: i64 = 86_400_000_000;
156
157 struct Synced(i64);
158
159 impl TimeSignals for Synced {
160 fn now_micros(&self) -> i64 {
161 self.0
162 }
163
164 fn ntp_synced(&self) -> bool {
165 true
166 }
167
168 fn rtc_secs(&self) -> Option<i64> {
169 None
170 }
171 }
172
173 fn synced_gate(dir: &std::path::Path, now: i64) -> Arc<ClockGate> {
174 let gate = ClockGate::new(dir, Arc::new(Synced(now)), 0);
175 assert!(gate.is_open());
176 gate
177 }
178
179 fn hello(cursor: Option<i64>, held_origin_seq: Option<i64>) -> Hello {
180 Hello {
181 machine_id: "peer".to_string(),
182 wall_micros: 0,
183 synced: false,
184 cursor: cursor.map(|c| c as u64),
185 held_origin_seq,
186 keys: None,
187 peer_in: None,
188 log_floor: 0,
189 head: 0,
190 applied: None,
191 merging: None,
192 }
193 }
194
195 fn rejection_shown(health: &Health) -> bool {
196 health
197 .report()
198 .reason
199 .is_some_and(|r| r.contains("handshake was refused"))
200 }
201
202 #[test]
203 fn a_store_failure_while_raising_floors_is_an_error_not_a_rejection() {
204 let dir = tempfile::tempdir().unwrap();
205 let now = 1_790_000_000 * 1_000_000;
206 let gate = synced_gate(dir.path(), now);
207 let db = dir.path().join("brain.db");
208 let sqlite = SqliteStore::open(&db, "machine-a".to_string()).unwrap();
209 rusqlite::Connection::open(&db)
210 .unwrap()
211 .execute_batch(
212 "CREATE TRIGGER fail_meta BEFORE INSERT ON index_meta \
213 BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END;",
214 )
215 .unwrap();
216 let health = Health::new(chrono::Utc::now(), None);
217
218 let err =
219 accept_hello(&sqlite, &gate, &health, &hello(None, Some(5 << 32 | 1))).unwrap_err();
220
221 assert!(err.downcast_ref::<HelloRejected>().is_none(), "{err:#}");
222 assert!(!rejection_shown(&health), "{:?}", health.report().reason);
223 }
224
225 #[test]
226 fn a_cursor_at_this_stores_own_last_passes_however_far_ahead_of_the_clock() {
227 let dir = tempfile::tempdir().unwrap();
228 let now = 1_790_000_000 * 1_000_000;
229 let gate = synced_gate(dir.path(), now);
230 let sqlite = SqliteStore::in_memory("machine-a".to_string()).unwrap();
231 let last = now + 3 * DAY;
232 sqlite
233 .raise_clock_floors(&ClockFloors {
234 last_seq: Some(last),
235 ..Default::default()
236 })
237 .unwrap();
238 let health = Health::new(chrono::Utc::now(), None);
239
240 accept_hello(&sqlite, &gate, &health, &hello(Some(last), None)).unwrap();
241 assert!(!rejection_shown(&health));
242
243 let err = accept_hello(&sqlite, &gate, &health, &hello(Some(last + 1), None)).unwrap_err();
244 assert!(err.downcast_ref::<HelloRejected>().is_some(), "{err:#}");
245 assert_eq!(sqlite.clock_state().unwrap().last, last);
246 }
247
248 #[test]
249 fn a_receivers_peer_in_raises_last_like_a_cursor() {
250 let dir = tempfile::tempdir().unwrap();
251 let now = 1_790_000_000 * 1_000_000;
252 let gate = synced_gate(dir.path(), now);
253 let sqlite = SqliteStore::in_memory("machine-a".to_string()).unwrap();
254 let health = Health::new(chrono::Utc::now(), None);
255 let held = (now - 1_000) as u64;
256 let pushed_into = Hello {
257 peer_in: Some(held),
258 ..hello(None, None)
259 };
260
261 accept_hello(&sqlite, &gate, &health, &pushed_into).unwrap();
262
263 assert_eq!(sqlite.clock_state().unwrap().last, held as i64);
264 }
265
266 #[test]
267 fn an_accepted_hello_clears_only_a_refusal_of_the_same_peer() {
268 let dir = tempfile::tempdir().unwrap();
269 let now = 1_790_000_000 * 1_000_000;
270 let gate = synced_gate(dir.path(), now);
271 let sqlite = SqliteStore::in_memory("machine-a".to_string()).unwrap();
272 let health = Health::new(chrono::Utc::now(), None);
273 let from = |machine: &str, cursor: i64| Hello {
274 machine_id: machine.to_string(),
275 ..hello(Some(cursor), None)
276 };
277
278 accept_hello(&sqlite, &gate, &health, &from("peer-p", now + 3 * DAY)).unwrap_err();
279 assert!(rejection_shown(&health));
280 accept_hello(&sqlite, &gate, &health, &from("peer-q", now)).unwrap();
281 assert!(
282 rejection_shown(&health),
283 "another peer's handshake says nothing about peer-p"
284 );
285 accept_hello(&sqlite, &gate, &health, &from("peer-p", now)).unwrap();
286 assert!(!rejection_shown(&health), "{:?}", health.report().reason);
287 assert_eq!(health.report().state, crate::health::State::Ok);
288 }
289}