gemachain_program/clock.rs
1//! Provides information about the network's clock which is made up of ticks, slots, etc...
2
3// The default tick rate that the cluster attempts to achieve. Note that the actual tick
4// rate at any given time should be expected to drift
5pub const DEFAULT_TICKS_PER_SECOND: u64 = 160;
6
7#[cfg(test)]
8static_assertions::const_assert_eq!(MS_PER_TICK, 6);
9pub const MS_PER_TICK: u64 = 1000 / DEFAULT_TICKS_PER_SECOND;
10
11#[cfg(test)]
12static_assertions::const_assert_eq!(SLOT_MS, 400);
13pub const SLOT_MS: u64 = (DEFAULT_TICKS_PER_SLOT * 1000) / DEFAULT_TICKS_PER_SECOND;
14
15// At 160 ticks/s, 64 ticks per slot implies that leader rotation and voting will happen
16// every 400 ms. A fast voting cadence ensures faster finality and convergence
17pub const DEFAULT_TICKS_PER_SLOT: u64 = 64;
18
19// GCP n1-standard hardware and also a xeon e5-2520 v4 are about this rate of hashes/s
20pub const DEFAULT_HASHES_PER_SECOND: u64 = 2_000_000;
21
22#[cfg(test)]
23static_assertions::const_assert_eq!(DEFAULT_HASHES_PER_TICK, 12_500);
24pub const DEFAULT_HASHES_PER_TICK: u64 = DEFAULT_HASHES_PER_SECOND / DEFAULT_TICKS_PER_SECOND;
25
26// 1 Dev Epoch = 400 ms * 8192 ~= 55 minutes
27pub const DEFAULT_DEV_SLOTS_PER_EPOCH: u64 = 8192;
28
29#[cfg(test)]
30static_assertions::const_assert_eq!(SECONDS_PER_DAY, 86_400);
31pub const SECONDS_PER_DAY: u64 = 24 * 60 * 60;
32
33#[cfg(test)]
34static_assertions::const_assert_eq!(TICKS_PER_DAY, 13_824_000);
35pub const TICKS_PER_DAY: u64 = DEFAULT_TICKS_PER_SECOND * SECONDS_PER_DAY;
36
37#[cfg(test)]
38static_assertions::const_assert_eq!(DEFAULT_SLOTS_PER_EPOCH, 432_000);
39// 1 Epoch ~= 2 days
40pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 2 * TICKS_PER_DAY / DEFAULT_TICKS_PER_SLOT;
41
42// leader schedule is governed by this
43pub const NUM_CONSECUTIVE_LEADER_SLOTS: u64 = 4;
44
45#[cfg(test)]
46static_assertions::const_assert_eq!(DEFAULT_MS_PER_SLOT, 400);
47pub const DEFAULT_MS_PER_SLOT: u64 = 1_000 * DEFAULT_TICKS_PER_SLOT / DEFAULT_TICKS_PER_SECOND;
48pub const DEFAULT_S_PER_SLOT: f64 = DEFAULT_TICKS_PER_SLOT as f64 / DEFAULT_TICKS_PER_SECOND as f64;
49
50/// The time window of recent block hash values that the bank will track the signatures
51/// of over. Once the bank discards a block hash, it will reject any transactions that use
52/// that `recent_blockhash` in a transaction. Lowering this value reduces memory consumption,
53/// but requires clients to update its `recent_blockhash` more frequently. Raising the value
54/// lengthens the time a client must wait to be certain a missing transaction will
55/// not be processed by the network.
56pub const MAX_HASH_AGE_IN_SECONDS: usize = 120;
57
58#[cfg(test)]
59static_assertions::const_assert_eq!(MAX_RECENT_BLOCKHASHES, 300);
60// Number of maximum recent blockhashes (one blockhash per slot)
61pub const MAX_RECENT_BLOCKHASHES: usize =
62 MAX_HASH_AGE_IN_SECONDS * DEFAULT_TICKS_PER_SECOND as usize / DEFAULT_TICKS_PER_SLOT as usize;
63
64#[cfg(test)]
65static_assertions::const_assert_eq!(MAX_PROCESSING_AGE, 150);
66// The maximum age of a blockhash that will be accepted by the leader
67pub const MAX_PROCESSING_AGE: usize = MAX_RECENT_BLOCKHASHES / 2;
68
69/// This is maximum time consumed in forwarding a transaction from one node to next, before
70/// it can be processed in the target node
71pub const MAX_TRANSACTION_FORWARDING_DELAY_GPU: usize = 2;
72
73/// More delay is expected if CUDA is not enabled (as signature verification takes longer)
74pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 6;
75
76/// Slot is a unit of time given to a leader for encoding,
77/// is some some number of Ticks long.
78pub type Slot = u64;
79
80/// Uniquely distinguishes every version of a slot, even if the
81/// slot number is the same, i.e. duplicate slots
82pub type BankId = u64;
83
84/// Epoch is a unit of time a given leader schedule is honored,
85/// some number of Slots.
86pub type Epoch = u64;
87
88pub const GENESIS_EPOCH: Epoch = 0;
89// must be sync with Account::rent_epoch::default()
90pub const INITIAL_RENT_EPOCH: Epoch = 0;
91
92/// SlotIndex is an index to the slots of a epoch
93pub type SlotIndex = u64;
94
95/// SlotCount is the number of slots in a epoch
96pub type SlotCount = u64;
97
98/// UnixTimestamp is an approximate measure of real-world time,
99/// expressed as Unix time (ie. seconds since the Unix epoch)
100pub type UnixTimestamp = i64;
101
102/// Clock represents network time. Members of Clock start from 0 upon
103/// network boot. The best way to map Clock to wallclock time is to use
104/// current Slot, as Epochs vary in duration (they start short and grow
105/// as the network progresses).
106///
107#[repr(C)]
108#[derive(Serialize, Clone, Deserialize, Debug, Default, PartialEq)]
109pub struct Clock {
110 /// the current network/bank Slot
111 pub slot: Slot,
112 /// the timestamp of the first Slot in this Epoch
113 pub epoch_start_timestamp: UnixTimestamp,
114 /// the bank Epoch
115 pub epoch: Epoch,
116 /// the future Epoch for which the leader schedule has
117 /// most recently been calculated
118 pub leader_schedule_epoch: Epoch,
119 /// originally computed from genesis creation time and network time
120 /// in slots (drifty); corrected using validator timestamp oracle as of
121 /// timestamp_correction and timestamp_bounding features
122 pub unix_timestamp: UnixTimestamp,
123}