oasis_core_runtime/common/
time.rs1use std::{
3    sync::Mutex,
4    time::{Duration, SystemTime, UNIX_EPOCH},
5};
6
7use lazy_static::lazy_static;
8use slog::error;
9
10use crate::common::{logger::get_logger, process};
11
12const INITIAL_MINIMUM_TIME: i64 = 1704067200; struct TimeSource {
15    inner: Mutex<Inner>,
16}
17
18struct Inner {
19    timestamp: i64,
20}
21
22pub fn insecure_posix_time() -> i64 {
29    let mut inner = TIME_SOURCE.inner.lock().unwrap();
30
31    let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
32    let now = now.as_secs() as i64;
33
34    if now < inner.timestamp {
35        error!(
36            get_logger("runtime/time"),
37            "clock appeared to have ran backwards"
38        );
39        process::abort();
40    }
41    inner.timestamp = now;
42
43    inner.timestamp
44}
45
46pub fn insecure_posix_system_time() -> SystemTime {
48    UNIX_EPOCH + Duration::from_secs(insecure_posix_time() as u64)
49}
50
51pub(crate) fn update_insecure_posix_time(timestamp: i64) {
55    let mut inner = TIME_SOURCE.inner.lock().unwrap();
56
57    if timestamp > inner.timestamp {
58        inner.timestamp = timestamp;
59    }
60
61    }
66
67lazy_static! {
68    static ref TIME_SOURCE: TimeSource = TimeSource {
69        inner: Mutex::new(Inner {
70            timestamp: INITIAL_MINIMUM_TIME,
71        })
72    };
73}