/ node / sync / src / helpers / mod.rs
mod.rs
 1  // Copyright (c) 2025 ADnet Contributors
 2  // This file is part of the AlphaOS library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  use alphavm::prelude::Network;
17  
18  use core::hash::Hash;
19  use indexmap::IndexSet;
20  use std::net::SocketAddr;
21  
22  /// A tuple of the block hash (optional), previous block hash (optional), and the number of sync IPS to request from.
23  pub type PrepareSyncRequest<N> = (Option<<N as Network>::BlockHash>, Option<<N as Network>::BlockHash>, usize);
24  
25  /// A tuple of the block hash (optional), previous block hash (optional), and sync IPs.
26  pub type SyncRequest<N> = (Option<<N as Network>::BlockHash>, Option<<N as Network>::BlockHash>, IndexSet<SocketAddr>);
27  
28  #[derive(Copy, Clone, Debug)]
29  pub(crate) struct PeerPair(pub SocketAddr, pub SocketAddr);
30  
31  impl PeerPair {
32      pub fn contains(&self, peer_ip: &SocketAddr) -> bool {
33          peer_ip == &self.0 || peer_ip == &self.1
34      }
35  }
36  
37  impl Eq for PeerPair {}
38  
39  impl PartialEq for PeerPair {
40      fn eq(&self, other: &Self) -> bool {
41          (self.0 == other.0 && self.1 == other.1) || (self.0 == other.1 && self.1 == other.0)
42      }
43  }
44  
45  impl Hash for PeerPair {
46      fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
47          let (a, b) = if self.0 < self.1 { (self.0, self.1) } else { (self.1, self.0) };
48          a.hash(state);
49          b.hash(state);
50      }
51  }