Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic clock interface #27

Merged
merged 8 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2018"

[dependencies]
clap = "2.33.0"
chrono = "0.4.9"
57 changes: 57 additions & 0 deletions node/src/clock/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![allow(dead_code)]

#[cfg(test)]
mod test;

extern crate chrono;

use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};

GregTheGreek marked this conversation as resolved.
Show resolved Hide resolved
const ISO_FORMAT: &str = "%FT%X.%.9F";
const EPOCH_DURATION: i8 = 15;

/// ChainEpochClock is used by the system node to assume weak clock synchrony amongst the other
/// systems.
pub struct ChainEpochClock {
// Chain start time in ISO nano timestamp
genesis_time: DateTime<Utc>,
}

impl ChainEpochClock {
/// Returns a ChainEpochClock based on the given genesis_time
///
/// # Arguments
///
/// * `genesis_time` - An i64 representing a unix timestamp
fn new(genesis_time: i64) -> ChainEpochClock {
// Convert unix timestamp
let native_date_time = NaiveDateTime::from_timestamp(genesis_time, 0);

// Convert to DateTime
let date_time = DateTime::<Utc>::from_utc(native_date_time, Utc);

// Use nanoseconds
date_time.to_rfc3339_opts(SecondsFormat::Nanos, true);

ChainEpochClock {
genesis_time: date_time,
}
}

/// Returns the genesis time as a DateTime<Utc>
fn get_time(&self) -> DateTime<Utc> {
self.genesis_time
}

/// Returns the epoch at a given time
///
/// # Arguments
///
/// * `time` - A DateTime<Utx> representing the chosen time.
///
/// TODO: Should time be a unix time stamp?
fn epoch_at_time(&self, time: DateTime<Utc>) {
let _difference = time.signed_duration_since(self.genesis_time);
// TODO Finish this based on spec
}
}
9 changes: 9 additions & 0 deletions node/src/clock/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![cfg(all(test))]
use crate::clock::ChainEpochClock;

#[test]
fn create_chain_epoch_clock() {
let utc_timestamp = 1574286946904;
let clock = ChainEpochClock::new(utc_timestamp);
assert_eq!(clock.get_time().timestamp(), utc_timestamp);
}
2 changes: 1 addition & 1 deletion node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

pub mod clock;