Add time since genesis to sysvar::clock (#7289)

* genesis timestamp

* remove clock::create_account()

* ..

* add configure-able creation time

* dividing by 1T, should be dividing by 1B
This commit is contained in:
Rob Walker
2019-12-12 14:03:43 -08:00
committed by GitHub
parent 1b2a9270e8
commit 777ae3c215
10 changed files with 87 additions and 51 deletions

View File

@ -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)]

View File

@ -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},
@ -21,6 +21,7 @@ use std::{
fs::{File, OpenOptions},
io::Write,
path::{Path, PathBuf},
time::{SystemTime, UNIX_EPOCH},
};
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
@ -31,16 +32,27 @@ pub enum OperatingMode {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GenesisConfig {
/// when the network (bootstrap leader) was started relative to the UNIX Epoch
pub creation_time: UnixTimestamp,
/// initial accounts
pub accounts: BTreeMap<Pubkey, Account>,
/// built-in programs
pub native_instruction_processors: Vec<(String, Pubkey)>,
/// accounts for network rewards, these do not count towards capitalization
pub rewards_pools: BTreeMap<Pubkey, Account>,
pub ticks_per_slot: u64,
pub slots_per_segment: u64,
/// network speed configuration
pub poh_config: PohConfig,
/// transaction fee config
pub fee_calculator: FeeCalculator,
/// rent config
pub rent: Rent,
/// inflation config
pub inflation: Inflation,
/// how slots map to epochs
pub epoch_schedule: EpochSchedule,
/// network runlevel
pub operating_mode: OperatingMode,
}
@ -62,6 +74,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(),

View File

@ -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());
}
}