-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpeer_id.rs
More file actions
59 lines (47 loc) · 1.94 KB
/
Copy pathpeer_id.rs
File metadata and controls
59 lines (47 loc) · 1.94 KB
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
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use torrust_peer_id::PeerId;
const DEFAULT_PRODUCTION_PEER_ID_PREFIX_BYTES: &[u8; 8] = b"-RC3000-";
/// Deterministic peer ID for tests and fixtures.
///
/// Format: `-<CC><VVVV>-<random-12-digits>`.
pub const DEFAULT_TEST_PEER_ID_BYTES: [u8; 20] = *b"-RC3000-000000000001";
pub const DEFAULT_TEST_PEER_ID: PeerId = PeerId(DEFAULT_TEST_PEER_ID_BYTES);
/// Returns the default production peer ID.
///
/// The 12-digit suffix is generated once per process and reused for the lifetime
/// of the process.
#[must_use]
pub fn default_production_peer_id() -> PeerId {
static DEFAULT_PEER_ID: OnceLock<PeerId> = OnceLock::new();
*DEFAULT_PEER_ID.get_or_init(|| PeerId(generate_default_production_peer_id_bytes()))
}
fn generate_default_production_peer_id_bytes() -> [u8; 20] {
let mut bytes = [0_u8; 20];
bytes[..8].copy_from_slice(DEFAULT_PRODUCTION_PEER_ID_PREFIX_BYTES);
bytes[8..].copy_from_slice(random_suffix_12_digits().as_bytes());
bytes
}
fn random_suffix_12_digits() -> String {
let nanos_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos();
let process_id = u128::from(std::process::id());
let mixed = nanos_since_epoch ^ (process_id << 64) ^ nanos_since_epoch.rotate_left(29);
let value = mixed % 1_000_000_000_000;
format!("{value:012}")
}
#[cfg(test)]
mod tests {
use super::{DEFAULT_TEST_PEER_ID, default_production_peer_id};
#[test]
fn default_test_peer_id_should_use_rc_prefix_and_3000_version() {
assert_eq!(DEFAULT_TEST_PEER_ID.0[..8], *b"-RC3000-");
}
#[test]
fn default_production_peer_id_should_be_stable_within_a_process() {
let first = default_production_peer_id();
let second = default_production_peer_id();
assert_eq!(first.0, second.0);
assert_eq!(first.0[..8], *b"-RC3000-");
assert!(first.0[8..].iter().all(u8::is_ascii_digit));
}
}