prns-runtime-tokio 0.3.4

Tokio host runtime for Personal Reticulum
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
use core::time::Duration;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

use tokio::sync::mpsc;

use crate::identity::vault::FileVault;
use crate::persistence::FileStore;
use crate::storage::StorageLayout;
use crate::wire::DestinationHash;

use crate::engine::InstantMillis;
use crate::manifold::driver::SelfRatchetSnapshot;

use prns_runtime::runtime::{Diagnostic, NoPersistence};

use crate::engine::PersistenceFlushCause;

use super::{
    boot_timeline_origin, DestinationIdentitySeedReport, FlushError, FlushMark, FlushReport,
    PrepareFlushError, PrnsEvent, PrnsNode, PrnsNodeHandle, RatchetSeedReport, RequestEndpointSet,
    RouteSeedProgress, RouteSeedReport, TunnelSeedReport,
};

const WRITE_PROBE: &str = ".write-probe";
const DEFAULT_FLUSH_INTERVAL: Duration = Duration::from_secs(5 * 60);
const SAVE_ON_LEARN_DEBOUNCE: Duration = Duration::from_secs(2);

pub struct NodePersistence {
    store: FileStore,
    vault: FileVault,
}

#[derive(Debug)]
pub enum DefaultLocationError {
    HomeDirectoryUnavailable,
    Io(std::io::Error),
}

impl core::fmt::Display for DefaultLocationError {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::HomeDirectoryUnavailable => formatter.write_str(
                "could not determine the conventional Reticulum directory; no home directory is available",
            ),
            Self::Io(error) => {
                write!(formatter, "could not open the default persistence directory: {error}")
            }
        }
    }
}

impl std::error::Error for DefaultLocationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::HomeDirectoryUnavailable => None,
            Self::Io(error) => Some(error),
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct PersistenceRestoreReport {
    pub routes: RouteSeedReport,
    pub destination_identities: DestinationIdentitySeedReport,
    pub tunnels: TunnelSeedReport,
    pub ratchets: RatchetSeedReport,
}

impl PersistenceRestoreReport {
    #[must_use]
    pub const fn refused_total(&self) -> u32 {
        self.routes
            .refused_count
            .saturating_add(self.destination_identities.refused_count)
            .saturating_add(self.tunnels.refused_count)
            .saturating_add(self.ratchets.refused_count)
    }

    #[must_use]
    pub const fn dropped_total(&self) -> u32 {
        self.routes
            .dropped_count
            .saturating_add(self.destination_identities.dropped_count)
            .saturating_add(self.tunnels.dropped_count)
            .saturating_add(self.ratchets.dropped_count)
    }
}

impl NodePersistence {
    /// Resolves the conventional Reticulum directory (`/etc/reticulum` holding a config, else `~/.config/reticulum` holding one, else `~/.reticulum`) and opens the snapshot store beneath it.
    pub fn at_default_location() -> Result<Self, DefaultLocationError> {
        let reticulum_dir = crate::persistence::reticulum_directory::resolve()
            .ok_or(DefaultLocationError::HomeDirectoryUnavailable)?;
        Self::in_reticulum_dir(reticulum_dir).map_err(DefaultLocationError::Io)
    }

    /// Snapshots live in a `prns` subdir of the RNS storage dir: a config dir shared with stock RNS keeps its own msgpack `storage/tunnels`, and our sealed region of the same name must never clobber it.
    pub fn in_reticulum_dir(directory: impl Into<PathBuf>) -> Result<Self, std::io::Error> {
        Self::custom_dir(directory.into().join("storage").join("prns"))
    }

    pub fn custom_dir(directory: impl Into<PathBuf>) -> Result<Self, std::io::Error> {
        let directory = directory.into();
        std::fs::create_dir_all(&directory)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700))?;
        }
        verify_writable(&directory)?;
        Ok(Self {
            store: FileStore::new(&directory),
            vault: FileVault::new(directory),
        })
    }

    #[must_use]
    pub fn timeline_origin(&self) -> InstantMillis {
        boot_timeline_origin(&self.store)
    }

    #[must_use]
    pub const fn store(&self) -> &FileStore {
        &self.store
    }

    #[must_use]
    pub const fn vault(&self) -> &FileVault {
        &self.vault
    }

    pub fn restore<St, R, F, S>(&self, node: &mut PrnsNode<St, R, F, S>) -> PersistenceRestoreReport
    where
        R: RequestEndpointSet<St>,
        F: FnMut(PrnsEvent<'_>, &St),
        S: StorageLayout,
    {
        self.restore_reporting(node, |_| {})
    }

    pub fn restore_reporting<St, R, F, S>(
        &self,
        node: &mut PrnsNode<St, R, F, S>,
        progress: impl FnMut(RouteSeedProgress),
    ) -> PersistenceRestoreReport
    where
        R: RequestEndpointSet<St>,
        F: FnMut(PrnsEvent<'_>, &St),
        S: StorageLayout,
    {
        PersistenceRestoreReport {
            routes: node.seed_routes_from_store_reporting(&self.store, progress),
            destination_identities: node.seed_destination_identities_from_store(&self.store),
            tunnels: node.seed_tunnels_from_store(&self.store),
            ratchets: node.seed_self_ratchets_from_vault(&self.vault),
        }
    }

    #[must_use]
    pub fn worker(self, handle: PrnsNodeHandle) -> PersistenceWorker {
        PersistenceWorker {
            handle,
            storage: Arc::new(Mutex::new(WorkerStorage {
                store: self.store,
                vault: self.vault,
                mark: FlushMark::default(),
            })),
            flush_interval: DEFAULT_FLUSH_INTERVAL,
            rotations: None,
            changes: None,
            change_debounce: Duration::ZERO,
            failure_policy: FlushFailurePolicy::Tolerate,
        }
    }
}

pub struct SaveOnLearn {
    route_changes: mpsc::UnboundedSender<()>,
    ratchet_rotations: mpsc::UnboundedSender<DestinationHash>,
}

pub struct SaveOnLearnWiring {
    route_changes: mpsc::UnboundedReceiver<()>,
    ratchet_rotations: mpsc::UnboundedReceiver<DestinationHash>,
}

impl SaveOnLearn {
    #[must_use]
    pub fn channel() -> (Self, SaveOnLearnWiring) {
        let (route_changes_sender, route_changes) = mpsc::unbounded_channel();
        let (ratchet_rotations_sender, ratchet_rotations) = mpsc::unbounded_channel();
        (
            Self {
                route_changes: route_changes_sender,
                ratchet_rotations: ratchet_rotations_sender,
            },
            SaveOnLearnWiring {
                route_changes,
                ratchet_rotations,
            },
        )
    }

    pub fn observe(&self, event: &PrnsEvent<'_>) {
        match event {
            PrnsEvent::Diagnostic(
                Diagnostic::AnnounceHeard { .. } | Diagnostic::RouteRemoved { .. },
            ) => {
                let _ignored = self.route_changes.send(());
            }
            PrnsEvent::Diagnostic(Diagnostic::SelfRatchetRotated { destination }) => {
                let _ignored = self.ratchet_rotations.send(*destination);
            }
            _ => {}
        }
    }
}

/// The recipe's `persistence` field vocabulary: `NoPersistence` or a [`NodePersistence`], resolved by the host at construction.
pub trait PersistenceIntent {
    fn into_node_persistence(self) -> Option<NodePersistence>;
}

impl PersistenceIntent for NoPersistence {
    fn into_node_persistence(self) -> Option<NodePersistence> {
        None
    }
}

impl PersistenceIntent for NodePersistence {
    fn into_node_persistence(self) -> Option<NodePersistence> {
        Some(self)
    }
}

impl From<PersistenceTrigger> for PersistenceFlushCause {
    fn from(trigger: PersistenceTrigger) -> Self {
        match trigger {
            PersistenceTrigger::Startup => Self::Startup,
            PersistenceTrigger::Interval => Self::Interval,
            PersistenceTrigger::RouteChange => Self::RouteChange,
            PersistenceTrigger::RatchetRotation => Self::RatchetRotation,
            PersistenceTrigger::Shutdown => Self::Shutdown,
        }
    }
}

fn verify_writable(directory: &std::path::Path) -> Result<(), std::io::Error> {
    use std::io::Write as _;
    let probe = directory.join(WRITE_PROBE);
    let tested = (|| {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&probe)?;
        file.write_all(b"prns")?;
        file.sync_all()
    })();
    let removed = std::fs::remove_file(&probe);
    tested.and(removed)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PersistenceTrigger {
    Startup,
    Interval,
    RouteChange,
    RatchetRotation,
    Shutdown,
}

impl PersistenceTrigger {
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Startup => "startup",
            Self::Interval => "interval",
            Self::RouteChange => "route_change",
            Self::RatchetRotation => "ratchet_rotation",
            Self::Shutdown => "shutdown",
        }
    }
}

pub enum PersistenceEvent<'a> {
    Flushed {
        trigger: PersistenceTrigger,
        report: FlushReport,
    },
    FlushFailed {
        trigger: PersistenceTrigger,
        error: &'a dyn core::fmt::Display,
    },
    RatchetsFlushed {
        trigger: PersistenceTrigger,
        stored: u32,
    },
    RatchetFlushFailed {
        trigger: PersistenceTrigger,
        error: &'a dyn core::fmt::Display,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlushFailurePolicy {
    Tolerate,
    Exit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PersistenceFlushStatus {
    Landed,
    NodeStopped,
    Failed,
}

impl PersistenceFlushStatus {
    const fn worst(self, other: Self) -> Self {
        match (self, other) {
            (Self::NodeStopped, _) | (_, Self::NodeStopped) => Self::NodeStopped,
            (Self::Failed, _) | (_, Self::Failed) => Self::Failed,
            (Self::Landed, Self::Landed) => Self::Landed,
        }
    }
}

struct WorkerStorage {
    store: FileStore,
    vault: FileVault,
    mark: FlushMark,
}

pub struct PersistenceWorker {
    handle: PrnsNodeHandle,
    storage: Arc<Mutex<WorkerStorage>>,
    flush_interval: Duration,
    rotations: Option<mpsc::UnboundedReceiver<DestinationHash>>,
    changes: Option<mpsc::UnboundedReceiver<()>>,
    change_debounce: Duration,
    failure_policy: FlushFailurePolicy,
}

impl PersistenceWorker {
    #[must_use]
    pub fn with_flush_interval(mut self, interval: Duration) -> Self {
        self.flush_interval = interval;
        self
    }

    #[must_use]
    pub fn with_flush_failure_policy(mut self, policy: FlushFailurePolicy) -> Self {
        self.failure_policy = policy;
        self
    }

    #[must_use]
    pub fn with_ratchet_rotations(
        mut self,
        rotations: mpsc::UnboundedReceiver<DestinationHash>,
    ) -> Self {
        self.rotations = Some(rotations);
        self
    }

    #[must_use]
    pub fn with_route_changes(
        mut self,
        changes: mpsc::UnboundedReceiver<()>,
        debounce: Duration,
    ) -> Self {
        self.changes = Some(changes);
        self.change_debounce = debounce;
        self
    }

    /// A flush rewrites each changed region whole, the same granularity the RNS reference uses for its path table; tens of thousands of routes still serialize to a few megabytes, unchanged regions are fingerprint-skipped, and the debounce coalesces bursts, so this trigger governs write frequency rather than write size.
    #[must_use]
    pub fn with_save_on_learn(self, wiring: SaveOnLearnWiring) -> Self {
        self.with_ratchet_rotations(wiring.ratchet_rotations)
            .with_route_changes(wiring.route_changes, SAVE_ON_LEARN_DEBOUNCE)
    }

    pub async fn flush_now(
        &self,
        on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send),
    ) -> PersistenceFlushStatus {
        let state = flush_state(
            &self.handle,
            &self.storage,
            PersistenceTrigger::Startup,
            on_event,
        )
        .await;
        if let PersistenceFlushStatus::NodeStopped = state {
            return state;
        }
        let ratchets = flush_all_ratchets(
            &self.handle,
            &self.storage,
            PersistenceTrigger::Startup,
            on_event,
        )
        .await;
        state.worst(ratchets)
    }

    pub async fn run(
        self,
        shutdown: impl core::future::Future<Output = ()>,
        mut on_event: impl FnMut(PersistenceEvent<'_>) + Send,
    ) -> PersistenceFlushStatus {
        let Self {
            handle,
            storage,
            flush_interval,
            mut rotations,
            mut changes,
            change_debounce,
            failure_policy,
        } = self;
        let on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send) = &mut on_event;
        let mut ticker = tokio::time::interval(flush_interval);
        ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        ticker.tick().await;
        tokio::pin!(shutdown);
        let mut rotations_open = rotations.is_some();
        let mut changes_open = changes.is_some();
        loop {
            tokio::select! {
                biased;
                () = &mut shutdown => {
                    let state = flush_state(&handle, &storage, PersistenceTrigger::Shutdown, on_event).await;
                    if let PersistenceFlushStatus::NodeStopped = state {
                        return state;
                    }
                    let ratchets = flush_all_ratchets(&handle, &storage, PersistenceTrigger::Shutdown, on_event).await;
                    return state.worst(ratchets);
                }
                destination = recv_or_pending(rotations.as_mut()), if rotations_open => {
                    match destination {
                        Some(destination) => {
                            let status = flush_rotated_ratchet(&handle, &storage, destination, on_event).await;
                            if should_exit(status, failure_policy) {
                                return status;
                            }
                        }
                        None => rotations_open = false,
                    }
                }
                change = recv_or_pending(changes.as_mut()), if changes_open => {
                    match change {
                        Some(()) => {
                            tokio::time::sleep(change_debounce).await;
                            if let Some(changes) = changes.as_mut() {
                                while changes.try_recv().is_ok() {}
                            }
                            let status = flush_state(&handle, &storage, PersistenceTrigger::RouteChange, on_event).await;
                            if should_exit(status, failure_policy) {
                                return status;
                            }
                        }
                        None => changes_open = false,
                    }
                }
                _ = ticker.tick() => {
                    let status = flush_state(&handle, &storage, PersistenceTrigger::Interval, on_event).await;
                    if should_exit(status, failure_policy) {
                        return status;
                    }
                }
            }
        }
    }
}

const fn should_exit(status: PersistenceFlushStatus, policy: FlushFailurePolicy) -> bool {
    match status {
        PersistenceFlushStatus::NodeStopped => true,
        PersistenceFlushStatus::Failed => matches!(policy, FlushFailurePolicy::Exit),
        PersistenceFlushStatus::Landed => false,
    }
}

async fn recv_or_pending<T>(receiver: Option<&mut mpsc::UnboundedReceiver<T>>) -> Option<T> {
    match receiver {
        Some(receiver) => receiver.recv().await,
        None => core::future::pending().await,
    }
}

async fn flush_state(
    handle: &PrnsNodeHandle,
    storage: &Arc<Mutex<WorkerStorage>>,
    trigger: PersistenceTrigger,
    on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send),
) -> PersistenceFlushStatus {
    let prepared = match handle.prepare_flush().await {
        Ok(prepared) => prepared,
        Err(PrepareFlushError::NodeStopped) => return PersistenceFlushStatus::NodeStopped,
    };
    let storage = Arc::clone(storage);
    let committed = tokio::task::spawn_blocking(move || {
        let mut storage = match storage.lock() {
            Ok(storage) => storage,
            Err(poisoned) => poisoned.into_inner(),
        };
        let WorkerStorage { store, mark, .. } = &mut *storage;
        prepared.commit_to_store(store, mark)
    })
    .await;
    match committed {
        Ok(Ok(report)) => {
            on_event(PersistenceEvent::Flushed { trigger, report });
            PersistenceFlushStatus::Landed
        }
        Ok(Err(FlushError::NodeStopped)) => PersistenceFlushStatus::NodeStopped,
        Ok(Err(FlushError::Store(error))) => {
            on_event(PersistenceEvent::FlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
        Err(error) => {
            on_event(PersistenceEvent::FlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
    }
}

async fn flush_rotated_ratchet(
    handle: &PrnsNodeHandle,
    storage: &Arc<Mutex<WorkerStorage>>,
    destination: DestinationHash,
    on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send),
) -> PersistenceFlushStatus {
    let snapshot = match handle.snapshot_self_ratchet(destination).await {
        Ok(Some(snapshot)) => snapshot,
        Ok(None) => return PersistenceFlushStatus::Landed,
        Err(PrepareFlushError::NodeStopped) => return PersistenceFlushStatus::NodeStopped,
    };
    store_single_ratchet(
        storage,
        snapshot,
        PersistenceTrigger::RatchetRotation,
        on_event,
    )
    .await
}

async fn flush_all_ratchets(
    handle: &PrnsNodeHandle,
    storage: &Arc<Mutex<WorkerStorage>>,
    trigger: PersistenceTrigger,
    on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send),
) -> PersistenceFlushStatus {
    let Some(snapshot) = handle.snapshot_self_ratchets().await else {
        return PersistenceFlushStatus::NodeStopped;
    };
    let storage = Arc::clone(storage);
    let committed = tokio::task::spawn_blocking(move || {
        let mut storage = match storage.lock() {
            Ok(storage) => storage,
            Err(poisoned) => poisoned.into_inner(),
        };
        snapshot.store_into(&mut storage.vault)
    })
    .await;
    match committed {
        Ok(Ok(stored)) => {
            on_event(PersistenceEvent::RatchetsFlushed { trigger, stored });
            PersistenceFlushStatus::Landed
        }
        Ok(Err(error)) => {
            on_event(PersistenceEvent::RatchetFlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
        Err(error) => {
            on_event(PersistenceEvent::RatchetFlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
    }
}

async fn store_single_ratchet(
    storage: &Arc<Mutex<WorkerStorage>>,
    snapshot: SelfRatchetSnapshot,
    trigger: PersistenceTrigger,
    on_event: &mut (dyn FnMut(PersistenceEvent<'_>) + Send),
) -> PersistenceFlushStatus {
    let storage = Arc::clone(storage);
    let committed = tokio::task::spawn_blocking(move || {
        let mut storage = match storage.lock() {
            Ok(storage) => storage,
            Err(poisoned) => poisoned.into_inner(),
        };
        snapshot.store_into(&mut storage.vault)
    })
    .await;
    match committed {
        Ok(Ok(())) => {
            on_event(PersistenceEvent::RatchetsFlushed { trigger, stored: 1 });
            PersistenceFlushStatus::Landed
        }
        Ok(Err(error)) => {
            on_event(PersistenceEvent::RatchetFlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
        Err(error) => {
            on_event(PersistenceEvent::RatchetFlushFailed {
                trigger,
                error: &error,
            });
            PersistenceFlushStatus::Failed
        }
    }
}