snarkos_node_router/helpers/
peer.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 crate::messages::{ChallengeRequest, NodeType};
17use snarkvm::prelude::{Address, Network};
18
19use std::{net::SocketAddr, time::Instant};
20
21/// The state for each connected peer.
22#[derive(Clone, Debug)]
23pub struct Peer<N: Network> {
24    /// The IP address of the peer, with the port set to the listener port.
25    peer_ip: SocketAddr,
26    /// The connected address of the peer.
27    peer_addr: SocketAddr,
28    /// The Aleo address of the peer.
29    address: Address<N>,
30    /// The node type of the peer.
31    node_type: NodeType,
32    /// The message version of the peer.
33    version: u32,
34    /// The timestamp of the first message received from the peer.
35    first_seen: Instant,
36    /// The timestamp of the last message received from this peer.
37    last_seen: Instant,
38}
39
40impl<N: Network> Peer<N> {
41    /// Initializes a new instance of `Peer`.
42    pub fn new(listening_ip: SocketAddr, connected_ip: SocketAddr, challenge_request: &ChallengeRequest<N>) -> Self {
43        Self {
44            peer_ip: listening_ip,
45            peer_addr: connected_ip,
46            address: challenge_request.address,
47            node_type: challenge_request.node_type,
48            version: challenge_request.version,
49            first_seen: Instant::now(),
50            last_seen: Instant::now(),
51        }
52    }
53
54    /// Returns the IP address of the peer, with the port set to the listener port.
55    pub const fn ip(&self) -> SocketAddr {
56        self.peer_ip
57    }
58
59    /// Returns the connected address of the peer.
60    pub const fn addr(&self) -> SocketAddr {
61        self.peer_addr
62    }
63
64    /// Returns the Aleo address of the peer.
65    pub const fn address(&self) -> Address<N> {
66        self.address
67    }
68
69    /// Returns the node type.
70    pub const fn node_type(&self) -> NodeType {
71        self.node_type
72    }
73
74    /// Returns `true` if the peer is a validator.
75    pub const fn is_validator(&self) -> bool {
76        self.node_type.is_validator()
77    }
78
79    /// Returns `true` if the peer is a prover.
80    pub const fn is_prover(&self) -> bool {
81        self.node_type.is_prover()
82    }
83
84    /// Returns `true` if the peer is a client.
85    pub const fn is_client(&self) -> bool {
86        self.node_type.is_client()
87    }
88
89    /// Returns the message version of the peer.
90    pub const fn version(&self) -> u32 {
91        self.version
92    }
93
94    /// Returns the first seen timestamp of the peer.
95    pub fn first_seen(&self) -> Instant {
96        self.first_seen
97    }
98
99    /// Returns the last seen timestamp of the peer.
100    pub fn last_seen(&self) -> Instant {
101        self.last_seen
102    }
103}
104
105impl<N: Network> Peer<N> {
106    /// Updates the node type.
107    pub fn set_node_type(&mut self, node_type: NodeType) {
108        self.node_type = node_type;
109    }
110
111    /// Updates the version.
112    pub fn set_version(&mut self, version: u32) {
113        self.version = version;
114    }
115
116    /// Updates the last seen timestamp of the peer.
117    pub fn set_last_seen(&mut self, last_seen: Instant) {
118        self.last_seen = last_seen;
119    }
120}