Add getBlockTime rpc api (#7130) (#7140)

automerge
This commit is contained in:
mergify[bot]
2019-11-26 00:10:59 -08:00
committed by Grimes
parent 640c2f88bd
commit 96df4c772f
12 changed files with 336 additions and 47 deletions

View File

@ -85,6 +85,10 @@ pub type Segment = u64;
/// some number of Slots.
pub type Epoch = u64;
/// UnixTimestamp is an approximate measure of real-world time,
/// expressed as Unix time (ie. seconds since the Unix epoch)
pub type UnixTimestamp = i64;
/// Clock represents network time. Members of Clock start from 0 upon
/// network boot. The best way to map Clock to wallclock time is to use
/// current Slot, as Epochs vary in duration (they start short and grow

View File

@ -28,6 +28,8 @@ impl PohConfig {
impl Default for PohConfig {
fn default() -> Self {
Self::new_sleep(Duration::from_millis(1000 / DEFAULT_TICKS_PER_SECOND))
Self::new_sleep(Duration::from_micros(
1000 * 1000 / DEFAULT_TICKS_PER_SECOND,
))
}
}

View File

@ -38,27 +38,58 @@ pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64)
/ ticks_per_slot as f64
}
/// From slots per year to tick_duration
pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
// Regarding division by zero potential below: for some reason, if Rust stores an `inf` f64 and
// then converts it to a u64 on use, it always returns 0, as opposed to std::u64::MAX or any
// other huge value
let slot_in_ns = (SECONDS_PER_YEAR * 1_000_000_000.0) / slots_per_year;
Duration::from_nanos(slot_in_ns as u64)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_years_as_slots() {
let tick_duration = Duration::from_millis(1000 / 160);
let tick_duration = Duration::from_micros(1000 * 1000 / 160);
// interestingly large numbers with 160 ticks/second
assert_eq!(years_as_slots(0.0, &tick_duration, 4) as u64, 0);
assert_eq!(
years_as_slots(1.0 / 12f64, &tick_duration, 4) as u64,
109_572_659
105_189_753
);
assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_314_871_916);
assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
let tick_duration = Duration::from_millis(1000);
let tick_duration = Duration::from_micros(1000 * 1000);
// one second in years with one tick per second + one tick per slot
assert_eq!(
years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
1.0
);
}
#[test]
fn test_slot_duration_from_slots_per_year() {
let slots_per_year = 1_262_277_039.0;
let ticks_per_slot = 4;
assert_eq!(
slot_duration_from_slots_per_year(slots_per_year),
Duration::from_micros(1000 * 1000 / 160) * ticks_per_slot
);
assert_eq!(
slot_duration_from_slots_per_year(0.0),
Duration::from_micros(0) * ticks_per_slot
);
let slots_per_year = SECONDS_PER_YEAR;
let ticks_per_slot = 1;
assert_eq!(
slot_duration_from_slots_per_year(slots_per_year),
Duration::from_millis(1000) * ticks_per_slot
);
}
}