Skip to main content

nym_node_tester_utils/
node.rs

1// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::NodeId;
5use nym_topology::node::RoutingNode;
6use serde::{Deserialize, Serialize};
7use std::fmt::{Display, Formatter};
8
9#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)]
10pub struct TestableNode {
11    pub encoded_identity: String,
12    pub node_id: NodeId,
13
14    #[serde(rename = "type")]
15    pub typ: NodeType,
16}
17
18impl TestableNode {
19    pub fn new(encoded_identity: String, typ: NodeType, node_id: NodeId) -> Self {
20        TestableNode {
21            encoded_identity,
22            node_id,
23            typ,
24        }
25    }
26
27    pub fn new_routing(routing_node: &RoutingNode, typ: NodeType) -> Self {
28        TestableNode::new(
29            routing_node.identity_key.to_base58_string(),
30            typ,
31            routing_node.node_id,
32        )
33    }
34
35    pub fn new_mixnode(encoded_identity: String, node_id: NodeId) -> Self {
36        TestableNode::new(encoded_identity, NodeType::Mixnode, node_id)
37    }
38
39    pub fn new_gateway(encoded_identity: String, node_id: NodeId) -> Self {
40        TestableNode::new(encoded_identity, NodeType::Gateway, node_id)
41    }
42
43    pub fn is_mixnode(&self) -> bool {
44        self.typ.is_mixnode()
45    }
46}
47
48impl Display for TestableNode {
49    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50        write!(
51            f,
52            "{}-{}: {}",
53            self.typ, self.node_id, self.encoded_identity
54        )
55    }
56}
57
58#[derive(Serialize, Deserialize, Hash, Clone, Copy, Debug, Eq, PartialEq)]
59#[serde(rename_all = "snake_case")]
60pub enum NodeType {
61    Mixnode,
62    Gateway,
63}
64
65impl NodeType {
66    pub fn is_mixnode(&self) -> bool {
67        matches!(self, NodeType::Mixnode)
68    }
69}
70
71impl Display for NodeType {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            NodeType::Mixnode => write!(f, "mixnode"),
75            NodeType::Gateway => write!(f, "gateway"),
76        }
77    }
78}