solana_clock/lib.rs
1//! Information about the network's clock, ticks, slots, etc.
2//!
3//! Time in Solana is marked primarily by _slots_, which occur approximately every
4//! 400 milliseconds, and are numbered sequentially. For every slot, a leader is
5//! chosen from the validator set, and that leader is expected to produce a new
6//! block, though sometimes leaders may fail to do so. Blocks can be identified
7//! by their slot number, and some slots do not contain a block.
8//!
9//! An approximation of the passage of real-world time can be calculated by
10//! multiplying a number of slots by [`DEFAULT_MS_PER_SLOT`], which is a constant target
11//! time for the network to produce slots. Note though that this method suffers
12//! a variable amount of drift, as the network does not produce slots at exactly
13//! the target rate, and the greater number of slots being calculated for, the
14//! greater the drift. Epochs cannot be used this way as they contain variable
15//! numbers of slots.
16//!
17//! The network's current view of the real-world time can always be accessed via
18//! [`Clock::unix_timestamp`], which is produced by an [oracle derived from the
19//! validator set][oracle].
20//!
21//! [oracle]: https://docs.solanalabs.com/implemented-proposals/validator-timestamp-oracle
22#![no_std]
23#![cfg_attr(docsrs, feature(doc_auto_cfg))]
24
25#[cfg(feature = "sysvar")]
26pub mod sysvar;
27
28#[cfg(feature = "serde")]
29use serde_derive::{Deserialize, Serialize};
30use solana_sdk_macro::CloneZeroed;
31
32/// The default tick rate that the cluster attempts to achieve (160 per second).
33///
34/// Note that the actual tick rate at any given time should be expected to drift.
35pub const DEFAULT_TICKS_PER_SECOND: u64 = 160;
36
37#[cfg(test)]
38static_assertions::const_assert_eq!(MS_PER_TICK, 6);
39
40/// The number of milliseconds per tick (6).
41pub const MS_PER_TICK: u64 = 1000 / DEFAULT_TICKS_PER_SECOND;
42
43// At 160 ticks/s, 64 ticks per slot implies that leader rotation and voting will happen
44// every 400 ms. A fast voting cadence ensures faster finality and convergence
45pub const DEFAULT_TICKS_PER_SLOT: u64 = 64;
46
47pub const DEFAULT_HASHES_PER_SECOND: u64 = 10_000_000;
48
49#[cfg(test)]
50static_assertions::const_assert_eq!(DEFAULT_HASHES_PER_TICK, 62_500);
51pub const DEFAULT_HASHES_PER_TICK: u64 = DEFAULT_HASHES_PER_SECOND / DEFAULT_TICKS_PER_SECOND;
52
53// 1 Dev Epoch = 400 ms * 8192 ~= 55 minutes
54pub const DEFAULT_DEV_SLOTS_PER_EPOCH: u64 = 8192;
55
56#[cfg(test)]
57static_assertions::const_assert_eq!(SECONDS_PER_DAY, 86_400);
58pub const SECONDS_PER_DAY: u64 = 24 * 60 * 60;
59
60#[cfg(test)]
61static_assertions::const_assert_eq!(TICKS_PER_DAY, 13_824_000);
62pub const TICKS_PER_DAY: u64 = DEFAULT_TICKS_PER_SECOND * SECONDS_PER_DAY;
63
64#[cfg(test)]
65static_assertions::const_assert_eq!(DEFAULT_SLOTS_PER_EPOCH, 432_000);
66
67/// The number of slots per epoch after initial network warmup.
68///
69/// 1 Epoch ~= 2 days.
70pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 2 * TICKS_PER_DAY / DEFAULT_TICKS_PER_SLOT;
71
72// leader schedule is governed by this
73pub const NUM_CONSECUTIVE_LEADER_SLOTS: u64 = 4;
74
75#[cfg(test)]
76static_assertions::const_assert_eq!(DEFAULT_MS_PER_SLOT, 400);
77/// The expected duration of a slot (400 milliseconds).
78pub const DEFAULT_MS_PER_SLOT: u64 = 1_000 * DEFAULT_TICKS_PER_SLOT / DEFAULT_TICKS_PER_SECOND;
79pub const DEFAULT_S_PER_SLOT: f64 = DEFAULT_TICKS_PER_SLOT as f64 / DEFAULT_TICKS_PER_SECOND as f64;
80
81/// The time window of recent block hash values over which the bank will track
82/// signatures.
83///
84/// Once the bank discards a block hash, it will reject any transactions that
85/// use that `recent_blockhash` in a transaction. Lowering this value reduces
86/// memory consumption, but requires a client to update its `recent_blockhash`
87/// more frequently. Raising the value lengthens the time a client must wait to
88/// be certain a missing transaction will not be processed by the network.
89pub const MAX_HASH_AGE_IN_SECONDS: usize = 120;
90
91#[cfg(test)]
92static_assertions::const_assert_eq!(MAX_RECENT_BLOCKHASHES, 300);
93// Number of maximum recent blockhashes (one blockhash per non-skipped slot)
94pub const MAX_RECENT_BLOCKHASHES: usize =
95 MAX_HASH_AGE_IN_SECONDS * DEFAULT_TICKS_PER_SECOND as usize / DEFAULT_TICKS_PER_SLOT as usize;
96
97#[cfg(test)]
98static_assertions::const_assert_eq!(MAX_PROCESSING_AGE, 150);
99// The maximum age of a blockhash that will be accepted by the leader
100pub const MAX_PROCESSING_AGE: usize = MAX_RECENT_BLOCKHASHES / 2;
101
102/// This is maximum time consumed in forwarding a transaction from one node to next, before
103/// it can be processed in the target node
104pub const MAX_TRANSACTION_FORWARDING_DELAY_GPU: usize = 2;
105
106/// More delay is expected if CUDA is not enabled (as signature verification takes longer)
107pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 6;
108
109/// Transaction forwarding, which leader to forward to and how long to hold
110pub const FORWARD_TRANSACTIONS_TO_LEADER_AT_SLOT_OFFSET: u64 = 2;
111pub const HOLD_TRANSACTIONS_SLOT_OFFSET: u64 = 20;
112
113/// The unit of time given to a leader for encoding a block.
114///
115/// It is some some number of _ticks_ long.
116pub type Slot = u64;
117
118/// Uniquely distinguishes every version of a slot.
119///
120/// The `BankId` is unique even if the slot number of two different slots is the
121/// same. This can happen in the case of e.g. duplicate slots.
122pub type BankId = u64;
123
124/// The unit of time a given leader schedule is honored.
125///
126/// It lasts for some number of [`Slot`]s.
127pub type Epoch = u64;
128
129pub const GENESIS_EPOCH: Epoch = 0;
130// must be sync with Account::rent_epoch::default()
131pub const INITIAL_RENT_EPOCH: Epoch = 0;
132
133/// An index to the slots of a epoch.
134pub type SlotIndex = u64;
135
136/// The number of slots in a epoch.
137pub type SlotCount = u64;
138
139/// An approximate measure of real-world time.
140///
141/// Expressed as Unix time (i.e. seconds since the Unix epoch).
142pub type UnixTimestamp = i64;
143
144/// A representation of network time.
145///
146/// All members of `Clock` start from 0 upon network boot.
147#[repr(C)]
148#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
149#[derive(Debug, CloneZeroed, Default, PartialEq, Eq)]
150pub struct Clock {
151 /// The current `Slot`.
152 pub slot: Slot,
153 /// The timestamp of the first `Slot` in this `Epoch`.
154 pub epoch_start_timestamp: UnixTimestamp,
155 /// The current `Epoch`.
156 pub epoch: Epoch,
157 /// The future `Epoch` for which the leader schedule has
158 /// most recently been calculated.
159 pub leader_schedule_epoch: Epoch,
160 /// The approximate real world time of the current slot.
161 ///
162 /// This value was originally computed from genesis creation time and
163 /// network time in slots, incurring a lot of drift. Following activation of
164 /// the [`timestamp_correction` and `timestamp_bounding`][tsc] features it
165 /// is calculated using a [validator timestamp oracle][oracle].
166 ///
167 /// [tsc]: https://docs.solanalabs.com/implemented-proposals/bank-timestamp-correction
168 /// [oracle]: https://docs.solanalabs.com/implemented-proposals/validator-timestamp-oracle
169 pub unix_timestamp: UnixTimestamp,
170}
171
172#[cfg(test)]
173mod tests {
174 use super::*;
175
176 #[test]
177 fn test_clone() {
178 let clock = Clock {
179 slot: 1,
180 epoch_start_timestamp: 2,
181 epoch: 3,
182 leader_schedule_epoch: 4,
183 unix_timestamp: 5,
184 };
185 let cloned_clock = clock.clone();
186 assert_eq!(cloned_clock, clock);
187 }
188}