cuprate_wire/p2p/
common.rs1use bitflags::bitflags;
19
20use cuprate_epee_encoding::epee_object;
21use cuprate_helper::map::split_u128_into_low_high_bits;
22pub use cuprate_types::{BlockCompleteEntry, PrunedTxBlobEntry, TransactionBlobs};
23
24use crate::NetworkAddress;
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub struct PeerSupportFlags(u32);
28
29bitflags! {
30 impl PeerSupportFlags: u32 {
31 const FLUFFY_BLOCKS = 0b0000_0001;
32 const _ = !0;
33 }
34}
35
36impl From<u32> for PeerSupportFlags {
37 fn from(value: u32) -> Self {
38 Self(value)
39 }
40}
41
42impl From<PeerSupportFlags> for u32 {
43 fn from(value: PeerSupportFlags) -> Self {
44 value.0
45 }
46}
47
48impl<'a> From<&'a PeerSupportFlags> for &'a u32 {
49 fn from(value: &'a PeerSupportFlags) -> Self {
50 &value.0
51 }
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
56pub struct BasicNodeData {
57 pub my_port: u32,
59 pub network_id: [u8; 16],
62 pub peer_id: u64,
64 pub support_flags: PeerSupportFlags,
67 pub rpc_port: u16,
70 pub rpc_credits_per_hash: u32,
73}
74
75epee_object! {
76 BasicNodeData,
77 my_port: u32,
78 network_id: [u8; 16],
79 peer_id: u64,
80 support_flags: PeerSupportFlags as u32 = 0_u32,
81 rpc_port: u16 = 0_u16,
82 rpc_credits_per_hash: u32 = 0_u32,
83}
84
85#[derive(Debug, Clone, PartialEq, Eq)]
87pub struct CoreSyncData {
88 pub cumulative_difficulty: u64,
91 pub cumulative_difficulty_top64: u64,
94 pub current_height: u64,
96 pub pruning_seed: u32,
99 pub top_id: [u8; 32],
102 pub top_version: u8,
104}
105
106epee_object! {
107 CoreSyncData,
108 cumulative_difficulty: u64,
109 cumulative_difficulty_top64: u64 = 0_u64,
110 current_height: u64,
111 pruning_seed: u32 = 0_u32,
112 top_id: [u8; 32],
113 top_version: u8 = 0_u8,
114}
115
116impl CoreSyncData {
117 pub const fn new(
118 cumulative_difficulty_128: u128,
119 current_height: u64,
120 pruning_seed: u32,
121 top_id: [u8; 32],
122 top_version: u8,
123 ) -> Self {
124 let (cumulative_difficulty, cumulative_difficulty_top64) =
125 split_u128_into_low_high_bits(cumulative_difficulty_128);
126
127 Self {
128 cumulative_difficulty,
129 cumulative_difficulty_top64,
130 current_height,
131 pruning_seed,
132 top_id,
133 top_version,
134 }
135 }
136 pub fn cumulative_difficulty(&self) -> u128 {
138 let mut ret: u128 = self.cumulative_difficulty_top64.into();
139 ret <<= 64;
140 ret | (Into::<u128>::into(self.cumulative_difficulty))
141 }
142}
143
144#[derive(Clone, Copy, Debug, Eq, PartialEq)]
147pub struct PeerListEntryBase {
148 pub adr: NetworkAddress,
150 pub id: u64,
152 pub last_seen: i64,
154 pub pruning_seed: u32,
156 pub rpc_port: u16,
158 pub rpc_credits_per_hash: u32,
160}
161
162epee_object! {
163 PeerListEntryBase,
164 adr: NetworkAddress,
165 id: u64,
166 last_seen: i64 = 0_i64,
167 pruning_seed: u32 = 0_u32,
168 rpc_port: u16 = 0_u16,
169 rpc_credits_per_hash: u32 = 0_u32,
170}
171
172#[cfg(test)]
173mod tests {
174
175 use super::CoreSyncData;
176
177 #[test]
178 fn core_sync_cumulative_difficulty() {
179 let core_sync = CoreSyncData::new(u128::MAX, 80085, 200, [0; 32], 21);
180 assert_eq!(core_sync.cumulative_difficulty(), u128::MAX);
181 let core_sync = CoreSyncData::new(21, 80085, 200, [0; 32], 21);
182 assert_eq!(core_sync.cumulative_difficulty(), 21);
183 }
184}