snarkos_node_sync/helpers/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkOS 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
16use snarkvm::prelude::Network;
17
18use core::hash::Hash;
19use indexmap::IndexSet;
20use 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.
23pub 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.
26pub type SyncRequest<N> = (Option<<N as Network>::BlockHash>, Option<<N as Network>::BlockHash>, IndexSet<SocketAddr>);
27
28#[derive(Copy, Clone, Debug)]
29pub(crate) struct PeerPair(pub SocketAddr, pub SocketAddr);
30
31impl PeerPair {
32    pub fn contains(&self, peer_ip: &SocketAddr) -> bool {
33        peer_ip == &self.0 || peer_ip == &self.1
34    }
35}
36
37impl Eq for PeerPair {}
38
39impl 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
45impl 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}