peer_status.rs (1987B)
1 #[cfg(test)] 2 use crate::app::now_ms; 3 4 #[derive(Clone, Debug, Eq, PartialEq)] 5 pub(super) struct PeerStatus { 6 pub(super) height: u64, 7 pub(super) tip_hash: String, 8 pub(super) time_ms: u64, 9 pub(super) request_bootstrap: bool, 10 pub(super) reject_session: bool, 11 pub(super) capabilities: Vec<String>, 12 } 13 14 impl PeerStatus { 15 #[cfg(test)] 16 pub(super) fn new(height: u64, tip_hash: String) -> Self { 17 Self::with_time(height, tip_hash, now_ms()) 18 } 19 20 pub(super) fn with_time(height: u64, tip_hash: String, time_ms: u64) -> Self { 21 Self { 22 height, 23 tip_hash, 24 time_ms, 25 request_bootstrap: false, 26 reject_session: false, 27 capabilities: Vec::new(), 28 } 29 } 30 31 pub(super) fn from_envelope(height: u64, tip_hash: String, time_ms: u64) -> Self { 32 Self { 33 height, 34 tip_hash, 35 time_ms, 36 request_bootstrap: false, 37 reject_session: false, 38 capabilities: Vec::new(), 39 } 40 } 41 42 pub(super) fn with_bootstrap_request(height: u64, tip_hash: String, time_ms: u64) -> Self { 43 Self { 44 height, 45 tip_hash, 46 time_ms, 47 request_bootstrap: true, 48 reject_session: false, 49 capabilities: Vec::new(), 50 } 51 } 52 53 pub(super) fn rejected(height: u64, tip_hash: String, time_ms: u64) -> Self { 54 Self { 55 height, 56 tip_hash, 57 time_ms, 58 request_bootstrap: false, 59 reject_session: true, 60 capabilities: Vec::new(), 61 } 62 } 63 64 pub(super) fn with_capabilities(mut self, capabilities: Vec<String>) -> Self { 65 self.capabilities = capabilities; 66 self 67 } 68 69 pub(super) fn supports(&self, capability: &str) -> bool { 70 self.capabilities 71 .binary_search_by(|candidate| candidate.as_str().cmp(capability)) 72 .is_ok() 73 } 74 }