Skip to content

Commit

Permalink
genesis timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-solana committed Dec 5, 2019
1 parent 8a28734 commit fb2349a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
35 changes: 27 additions & 8 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use solana_metrics::{
};
use solana_sdk::{
account::Account,
clock::{get_segment_from_slot, Epoch, Slot, MAX_RECENT_BLOCKHASHES},
clock::{get_segment_from_slot, Epoch, Slot, UnixTimestamp, MAX_RECENT_BLOCKHASHES},
epoch_schedule::EpochSchedule,
fee_calculator::FeeCalculator,
genesis_config::GenesisConfig,
Expand Down Expand Up @@ -180,6 +180,7 @@ pub struct Bank {
/// Hash of this Bank's parent's state
parent_hash: Hash,

/// parent's slot
parent_slot: Slot,

/// The number of transactions processed without error
Expand Down Expand Up @@ -211,6 +212,12 @@ pub struct Bank {
/// The number of ticks in each slot.
ticks_per_slot: u64,

/// length of a slot in ns
ns_per_slot: u128,

/// genesis time, used for computed clock
genesis_creation_time: UnixTimestamp,

/// The number of slots per year, used for inflation
slots_per_year: f64,

Expand Down Expand Up @@ -342,6 +349,8 @@ impl Bank {
// TODO: clean this up, soo much special-case copying...
hashes_per_tick: parent.hashes_per_tick,
ticks_per_slot: parent.ticks_per_slot,
ns_per_slot: parent.ns_per_slot,
genesis_creation_time: parent.genesis_creation_time,
slots_per_segment: parent.slots_per_segment,
slots_per_year: parent.slots_per_year,
epoch_schedule,
Expand Down Expand Up @@ -464,16 +473,23 @@ impl Bank {
ancestors
}

/// computed unix_timestamp at this slot height
pub fn unix_timestamp(&self) -> i64 {
self.genesis_creation_time
+ ((self.slot as u128 * self.ns_per_slot) / 1_000_000_000_000) as i64
}

fn update_clock(&self) {
self.store_account(
&sysvar::clock::id(),
&sysvar::clock::create_account(
1,
self.slot,
get_segment_from_slot(self.slot, self.slots_per_segment),
self.epoch_schedule.get_epoch(self.slot),
self.epoch_schedule.get_leader_schedule_epoch(self.slot),
),
&sysvar::clock::Clock {
slot: self.slot,
segment: get_segment_from_slot(self.slot, self.slots_per_segment),
epoch: self.epoch_schedule.get_epoch(self.slot),
leader_schedule_epoch: self.epoch_schedule.get_leader_schedule_epoch(self.slot),
unix_timestamp: self.unix_timestamp(),
}
.create_account(1),
);
}

Expand Down Expand Up @@ -704,6 +720,9 @@ impl Bank {

self.hashes_per_tick = genesis_config.poh_config.hashes_per_tick;
self.ticks_per_slot = genesis_config.ticks_per_slot;
self.ns_per_slot = genesis_config.poh_config.target_tick_duration.as_nanos()
* genesis_config.ticks_per_slot as u128;
self.genesis_creation_time = genesis_config.creation_time;
self.slots_per_segment = genesis_config.slots_per_segment;
self.max_tick_height = (self.slot + 1) * self.ticks_per_slot;
self.slots_per_year = years_as_slots(
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub struct Clock {
/// the future Epoch for which the leader schedule has
/// most recently been calculated
pub leader_schedule_epoch: Epoch,
/// computed from genesis creation time and network time
/// in slots, drifts!
pub unix_timestamp: UnixTimestamp,
}

#[cfg(test)]
Expand Down
8 changes: 7 additions & 1 deletion sdk/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
account::Account,
clock::{DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT},
clock::{UnixTimestamp, DEFAULT_SLOTS_PER_SEGMENT, DEFAULT_TICKS_PER_SLOT},
epoch_schedule::EpochSchedule,
fee_calculator::FeeCalculator,
hash::{hash, Hash},
Expand All @@ -20,6 +20,7 @@ use std::{
fs::{File, OpenOptions},
io::Write,
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
};

#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
Expand All @@ -30,6 +31,7 @@ pub enum OperatingMode {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GenesisConfig {
pub creation_time: UnixTimestamp,
pub accounts: BTreeMap<Pubkey, Account>,
pub native_instruction_processors: Vec<(String, Pubkey)>,
pub rewards_pools: BTreeMap<Pubkey, Account>,
Expand Down Expand Up @@ -61,6 +63,10 @@ pub fn create_genesis_config(lamports: u64) -> (GenesisConfig, Keypair) {
impl Default for GenesisConfig {
fn default() -> Self {
Self {
creation_time: SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as UnixTimestamp,
accounts: BTreeMap::default(),
native_instruction_processors: Vec::default(),
rewards_pools: BTreeMap::default(),
Expand Down
34 changes: 1 addition & 33 deletions sdk/src/sysvar/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,8 @@
//!
pub use crate::clock::Clock;

use crate::{
account::Account,
clock::{Epoch, Segment, Slot},
sysvar::Sysvar,
};
use crate::sysvar::Sysvar;

crate::declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);

impl Sysvar for Clock {}

pub fn create_account(
lamports: u64,
slot: Slot,
segment: Segment,
epoch: Epoch,
leader_schedule_epoch: Epoch,
) -> Account {
Clock {
slot,
segment,
epoch,
leader_schedule_epoch,
}
.create_account(lamports)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_new() {
let account = create_account(1, 0, 0, 0, 0);
let clock = Clock::from_account(&account).unwrap();
assert_eq!(clock, Clock::default());
}
}

0 comments on commit fb2349a

Please sign in to comment.