1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use std::collections::HashMap;

use super::{
    errors::ListenAddrErrors,
    identifier::derive::{digest::DigestDerivator, KeyDerivator},
};
use config::Value;
use serde::Deserialize;

/// Configuration parameters of a TAPLE node divided into categories.
#[derive(Debug, Deserialize, Clone)]
pub struct TapleSettings {
    pub network: NetworkSettings,
    pub node: NodeSettings,
}

/// P2P network configuration parameters of a TAPLE node.
#[derive(Debug, Deserialize, Clone)]
pub struct NetworkSettings {
    /// [Multiaddr](https://github.com/multiformats/multiaddr) to consider by the node.
    pub listen_addr: Vec<ListenAddr>,
    #[serde(rename = "knownnodes")]
    /// List of bootstrap nodes to connect to.
    pub known_nodes: Vec<String>,
    #[serde(rename = "externaladdress")]
    /// List of bootstrap nodes to connect to.
    pub external_address: Vec<String>,
}

const DEFAULT_PORT: u32 = 40040;

/// Represents a valid listening address for TAPLE. Internally, they are constituted as a MultiAddr.
#[derive(Debug, Deserialize, Clone)]
pub enum ListenAddr {
    /// Represents in-memory addressing.
    Memory {
        port: Option<u32>,
    },
    /// Represents an ip4 address
    IP4 {
        addr: Option<std::net::Ipv4Addr>,
        port: Option<u32>,
    },
    /// Represents an ip6 address
    IP6 {
        addr: Option<std::net::Ipv6Addr>,
        port: Option<u32>,
    },
}

impl Default for ListenAddr {
    fn default() -> Self {
        Self::IP4 {
            addr: Some(std::net::Ipv4Addr::new(0, 0, 0, 0)),
            port: Some(DEFAULT_PORT),
        }
    }
}

impl ListenAddr {
    /// Allows to obtain the port of the listening address
    pub fn get_port(&self) -> Option<u32> {
        match self {
            Self::IP4 { port, .. } => port.clone(),
            Self::IP6 { port, .. } => port.clone(),
            Self::Memory { port } => port.clone(),
        }
    }

    /// Allows to increment the port of the listening address by a specified value.
    pub fn increment_port(&mut self, offset: u32) {
        match self {
            Self::IP4 { port, .. } => port.as_mut().map(|p| *p += offset),
            Self::IP6 { port, .. } => port.as_mut().map(|p| *p += offset),
            Self::Memory { port } => port.as_mut().map(|p| *p += offset),
        };
    }

    /// Allows to obtain, as a string, the listening address in MultiAddr format.
    pub fn to_string(&self) -> Result<String, ListenAddrErrors> {
        let result = match self {
            ListenAddr::Memory { port } => {
                let mut result = format!("/memory");
                if let Some(port) = port {
                    result.push_str(&format!("/{}", port));
                }
                result
            }
            ListenAddr::IP4 { addr, port } => {
                let mut result = format!("/ip4");
                if let Some(ip) = addr {
                    result.push_str(&format!(
                        "/{}/tcp/{}",
                        ip.to_string(),
                        port.ok_or(ListenAddrErrors::InvalidCombination)?
                    ));
                }
                result
            }
            ListenAddr::IP6 { addr, port } => {
                let mut result = format!("/ip6");
                if let Some(ip) = addr {
                    result.push_str(&format!(
                        "/{}/tcp/{}",
                        ip.to_string(),
                        port.ok_or(ListenAddrErrors::InvalidCombination)?
                    ));
                }
                result
            }
        };
        Ok(result)
    }
}

impl TryFrom<String> for ListenAddr {
    type Error = ListenAddrErrors;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        let mut sections = value.split("/");
        // Addr must start with "/"
        let Some(data) = sections.next() else {
            return Err(ListenAddrErrors::InvalidListenAddr);
        };
        if !data.is_empty() {
            return Err(ListenAddrErrors::InvalidListenAddr);
        }
        // The specification of the protocol
        let Some(protocol) = sections.next() else {
            return Err(ListenAddrErrors::InvalidListenAddr);
        };
        match protocol {
            "ip4" => {
                if let Some(ip) = sections.next() {
                    let ip = ip
                        .parse::<std::net::Ipv4Addr>()
                        .map_err(|_| ListenAddrErrors::InvalidIP4)?;
                    // Check TCP
                    let Some(tcp) = sections.next() else {
                        return Err(ListenAddrErrors::NoTransportProtocolSpecified);
                    };
                    if tcp != "tcp" {
                        return Err(ListenAddrErrors::NoTCP);
                    }
                    if let Some(port) = sections.next() {
                        // Port must be u32
                        let port = port
                            .parse::<u32>()
                            .map_err(|_| ListenAddrErrors::NoU32Port)?;
                        return Ok(ListenAddr::IP4 {
                            addr: Some(ip),
                            port: Some(port),
                        });
                    } else {
                        return Ok(ListenAddr::IP4 {
                            addr: Some(ip),
                            port: None,
                        });
                    }
                } else {
                    return Ok(ListenAddr::IP4 {
                        addr: None,
                        port: None,
                    });
                }
            }
            "ip6" => {
                if let Some(ip) = sections.next() {
                    let ip = ip
                        .parse::<std::net::Ipv6Addr>()
                        .map_err(|_| ListenAddrErrors::InvalidIP6)?;
                    // Check TCP
                    let Some(tcp) = sections.next() else {
                        return Err(ListenAddrErrors::NoTransportProtocolSpecified);
                    };
                    if tcp != "tcp" {
                        return Err(ListenAddrErrors::NoTCP);
                    }
                    if let Some(port) = sections.next() {
                        // Port must be u32
                        let port = port
                            .parse::<u32>()
                            .map_err(|_| ListenAddrErrors::NoU32Port)?;
                        return Ok(ListenAddr::IP6 {
                            addr: Some(ip),
                            port: Some(port),
                        });
                    } else {
                        return Ok(ListenAddr::IP6 {
                            addr: Some(ip),
                            port: None,
                        });
                    }
                } else {
                    return Ok(ListenAddr::IP6 {
                        addr: None,
                        port: None,
                    });
                }
            }
            "memory" => {
                // Check for the port
                if let Some(port) = sections.next() {
                    // Port must be u32
                    let port = port
                        .parse::<u32>()
                        .map_err(|_| ListenAddrErrors::NoU32Port)?;
                    return Ok(ListenAddr::Memory { port: Some(port) });
                } else {
                    return Ok(ListenAddr::Memory { port: None });
                }
            }
            _ => Err(ListenAddrErrors::InvalidProtocolSpecified),
        }
    }
}

#[derive(Debug, Deserialize, Clone)]
pub struct AccessPoint {
    #[serde(rename = "peer-id")]
    pub peer_id: String,
    pub addr: String,
}

/// General settings of a TAPLE node.
#[derive(Debug, Deserialize, Clone)]
pub struct NodeSettings {
    /// [KeyDerivator] to be used by the secret key.
    #[serde(rename = "keyderivator")]
    pub key_derivator: KeyDerivator,
    /// Secret key to be used by the node
    #[serde(rename = "secretkey")]
    pub secret_key: Option<String>,
    /// [DigestDerivator] to be used for future event and subject identifiers
    #[serde(rename = "digestderivator")]
    pub digest_derivator: DigestDerivator,
    /// Percentage of network nodes receiving protocol messages in one iteration
    #[serde(rename = "replicationfactor")]
    pub replication_factor: f64,
    /// Timeout to be used between protocol iterations
    pub timeout: u32,
    #[doc(hidden)]
    pub passvotation: u8,
    #[cfg(feature = "evaluation")]
    pub smartcontracts_directory: String,
}

impl From<AccessPoint> for Value {
    fn from(data: AccessPoint) -> Self {
        let mut map = HashMap::new();
        map.entry("peer_id".to_owned())
            .or_insert(Value::new(None, config::ValueKind::String(data.peer_id)));
        map.entry("addr".to_owned())
            .or_insert(Value::new(None, config::ValueKind::String(data.addr)));
        Self::new(None, config::ValueKind::Table(map))
    }
}

pub enum VotationType {
    Normal,
    AlwaysAccept,
    AlwaysReject,
}

impl From<u8> for VotationType {
    fn from(passvotation: u8) -> Self {
        match passvotation {
            2 => Self::AlwaysReject,
            1 => Self::AlwaysAccept,
            _ => Self::Normal,
        }
    }
}