Skip to main content

nimiq_network_interface/
peer_info.rs

1use bitflags::bitflags;
2use multiaddr::Multiaddr;
3use nimiq_serde::{Deserialize, Serialize};
4
5bitflags! {
6    /// Bitmask of services
7    ///
8    ///
9    ///  - This just serializes to its numeric value for serde, but a list of strings would be nicer.
10    ///
11    #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
12    pub struct Services: u32 {
13        /// The node provides at least the latest [`nimiq_primitives::policy::NUM_BLOCKS_VERIFICATION`] as full blocks.
14        const FULL_BLOCKS = 1 << 0;
15
16        /// The node provides the full transaction history.
17        const HISTORY = 1 << 1;
18
19        /// The node provides inclusion and exclusion proofs for accounts that are necessary to verify active accounts as
20        /// well as accounts in all transactions it provided from its mempool.
21        ///
22        /// However, if [`Services::ACCOUNTS_CHUNKS`] is not set, the node may occasionally not provide a proof if it
23        /// decided to prune the account from local storage.
24        const ACCOUNTS_PROOF = 1 << 3;
25
26        /// The node provides the full accounts tree in form of chunks.
27        /// This implies that the client stores the full accounts tree.
28        const ACCOUNTS_CHUNKS = 1 << 4;
29
30        /// The node tries to stay on sync with the network wide mempool and will provide access to it.
31        ///
32        /// Nodes that do not have this flag set may occasionally announce transactions from their mempool and/or reply to
33        /// mempool requests to announce locally crafted transactions.
34        const MEMPOOL = 1 << 5;
35
36        /// The node provides an index of transactions allowing it to find historic transactions by address or by hash.
37        /// Only history nodes will have this flag set.
38        /// Nodes that have this flag set may prune any part of their transaction index at their discretion, they do not
39        /// claim completeness of their results either.
40        const TRANSACTION_INDEX = 1 << 6;
41
42        /// This node is configured as a validator, so it is interested for other validator nodes.
43        const VALIDATOR = 1 << 7;
44
45        /// This node provides pre-genesis information.
46        const PRE_GENESIS_TRANSACTIONS = 1 << 8;
47    }
48}
49
50/// Enumeration for the different node types
51pub enum NodeType {
52    /// History node type
53    History,
54    /// Light node type
55    Light,
56    /// Full node type
57    Full,
58    /// Pico node type
59    Pico,
60}
61
62impl Services {
63    /// Common provided service flags for a node
64    pub fn provided(node_type: NodeType) -> Self {
65        match node_type {
66            NodeType::History => {
67                Services::HISTORY
68                    | Services::FULL_BLOCKS
69                    | Services::ACCOUNTS_PROOF
70                    | Services::ACCOUNTS_CHUNKS
71            }
72            NodeType::Light => Services::empty(),
73            NodeType::Full => {
74                Services::ACCOUNTS_PROOF | Services::FULL_BLOCKS | Services::ACCOUNTS_CHUNKS
75            }
76            NodeType::Pico => Services::empty(),
77        }
78    }
79
80    /// Common required service flags for a node
81    pub fn required(node_type: NodeType) -> Self {
82        match node_type {
83            NodeType::History => Services::HISTORY | Services::FULL_BLOCKS,
84            NodeType::Light => Services::ACCOUNTS_PROOF,
85            NodeType::Full => Services::FULL_BLOCKS | Services::HISTORY | Services::ACCOUNTS_CHUNKS,
86            NodeType::Pico => Services::ACCOUNTS_PROOF,
87        }
88    }
89}
90
91/// Peer information. This struct contains:
92///
93///  - The connection address of the peer.
94///  - A bitmask of the services supported by this peer.
95///
96#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
97pub struct PeerInfo {
98    /// Connection address of this peer.
99    address: Multiaddr,
100
101    /// Services supported by this peer.
102    services: Services,
103}
104
105impl PeerInfo {
106    pub fn new(address: Multiaddr, services: Services) -> Self {
107        Self { address, services }
108    }
109
110    /// Gets the peer connection address
111    pub fn get_address(&self) -> Multiaddr {
112        self.address.clone()
113    }
114
115    /// Gets the peer provided services
116    pub fn get_services(&self) -> Services {
117        self.services
118    }
119}