clockwork_crank/state/
clock.rs

1use {
2    anchor_lang::{
3        prelude::borsh::BorshSchema,
4        prelude::*,
5        solana_program::{clock::UnixTimestamp, slot_history::Slot, stake_history::Epoch},
6        AnchorDeserialize,
7    },
8    std::convert::TryFrom,
9};
10
11#[derive(AnchorDeserialize, AnchorSerialize, BorshSchema, Clone, Debug, PartialEq)]
12pub struct ClockData {
13    pub slot: Slot,
14    /// the timestamp of the first Slot in this Epoch
15    pub epoch_start_timestamp: UnixTimestamp,
16    /// the bank Epoch
17    pub epoch: Epoch,
18    /// the future Epoch for which the leader schedule has
19    ///  most recently been calculated
20    pub leader_schedule_epoch: Epoch,
21    /// originally computed from genesis creation time and network time
22    /// in slots (drifty); corrected using validator timestamp oracle as of
23    /// timestamp_correction and timestamp_bounding features
24    pub unix_timestamp: UnixTimestamp,
25}
26
27impl From<Clock> for ClockData {
28    fn from(clock: Clock) -> Self {
29        ClockData {
30            slot: clock.slot,
31            epoch_start_timestamp: clock.epoch_start_timestamp,
32            epoch: clock.epoch,
33            leader_schedule_epoch: clock.leader_schedule_epoch,
34            unix_timestamp: clock.unix_timestamp,
35        }
36    }
37}
38
39impl From<&ClockData> for Clock {
40    fn from(clock: &ClockData) -> Self {
41        Clock {
42            slot: clock.slot,
43            epoch_start_timestamp: clock.epoch_start_timestamp,
44            epoch: clock.epoch,
45            leader_schedule_epoch: clock.leader_schedule_epoch,
46            unix_timestamp: clock.unix_timestamp,
47        }
48    }
49}
50
51impl TryFrom<Vec<u8>> for ClockData {
52    type Error = Error;
53    fn try_from(data: Vec<u8>) -> std::result::Result<Self, Self::Error> {
54        Ok(
55            borsh::try_from_slice_with_schema::<ClockData>(data.as_slice())
56                .map_err(|_err| ErrorCode::AccountDidNotDeserialize)?,
57        )
58    }
59}