1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};
pub const DEFAULT_TICKS_PER_SECOND: u64 = 10;
pub const DEFAULT_TICKS_PER_SLOT: u64 = 4;
pub const DEFAULT_SLOTS_PER_EPOCH: u64 = 8192;
pub const DEFAULT_SLOTS_PER_SEGMENT: u64 = 1024;
pub const DEFAULT_SLOTS_PER_TURN: u64 = 32 * 4;
pub const NUM_CONSECUTIVE_LEADER_SLOTS: u64 = 4;
pub const MAX_HASH_AGE_IN_SECONDS: usize = 120;
pub const MAX_RECENT_BLOCKHASHES: usize = MAX_HASH_AGE_IN_SECONDS;
pub const MAX_PROCESSING_AGE: usize = MAX_RECENT_BLOCKHASHES / 2;
#[cfg(feature = "cuda")]
pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 2;
#[cfg(not(feature = "cuda"))]
pub const MAX_TRANSACTION_FORWARDING_DELAY: usize = 6;
pub fn duration_as_ns(d: &Duration) -> u64 {
d.as_secs() * 1_000_000_000 + u64::from(d.subsec_nanos())
}
pub fn duration_as_us(d: &Duration) -> u64 {
(d.as_secs() * 1000 * 1000) + (u64::from(d.subsec_nanos()) / 1_000)
}
pub fn duration_as_ms(d: &Duration) -> u64 {
(d.as_secs() * 1000) + (u64::from(d.subsec_nanos()) / 1_000_000)
}
pub fn duration_as_s(d: &Duration) -> f32 {
d.as_secs() as f32 + (d.subsec_nanos() as f32 / 1_000_000_000.0)
}
pub fn timestamp() -> u64 {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("create timestamp in timing");
duration_as_ms(&now)
}
pub fn get_segment_from_slot(rooted_slot: Slot, slots_per_segment: u64) -> Segment {
((rooted_slot + (slots_per_segment - 1)) / slots_per_segment)
}
pub fn get_complete_segment_from_slot(
rooted_slot: Slot,
slots_per_segment: u64,
) -> Option<Segment> {
let completed_segment = rooted_slot / slots_per_segment;
if rooted_slot < slots_per_segment {
None
} else {
Some(completed_segment)
}
}
pub type Slot = u64;
pub type Segment = u64;
pub type Epoch = u64;
#[cfg(test)]
mod tests {
use super::*;
fn get_segments(slot: Slot, slots_per_segment: u64) -> (Segment, Segment) {
(
get_segment_from_slot(slot, slots_per_segment),
get_complete_segment_from_slot(slot, slots_per_segment).unwrap(),
)
}
#[test]
fn test_complete_segment_impossible() {
assert_eq!(get_complete_segment_from_slot(5, 10), None);
}
#[test]
fn test_segment_conversion() {
let (current, complete) = get_segments(2048, 1024);
assert_eq!(current, complete);
let (current, complete) = get_segments(2049, 1024);
assert!(complete < current);
}
}