ant-node 0.4.1

The Autonomi node binary
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use crate::{
    spawn::{
        network_spawner::{NetworkSpawner, RunningNetwork},
        node_spawner::NodeSpawner,
    },
    NodeBuilder, RunningNode,
};
use ant_evm::{EvmNetwork, RewardsAddress};
use ant_protocol::{node::get_antnode_root_dir, storage::ChunkAddress, NetworkAddress};
use const_hex::FromHex;
use libp2p::{
    identity::{Keypair, PeerId},
    Multiaddr,
};
use pyo3::{exceptions::PyRuntimeError, exceptions::PyValueError, prelude::*, types::PyModule};
use pyo3_async_runtimes::tokio::future_into_py;
use std::{
    net::{IpAddr, SocketAddr},
    path::PathBuf,
    sync::Arc,
};
use tokio::sync::{Mutex, RwLock};
use xor_name::XorName;

/// Python wrapper for the Autonomi Node
#[pyclass(name = "AntNode")]
pub struct PyAntNode {
    node: Arc<RwLock<RunningNode>>,
}

#[pymethods]
impl PyAntNode {
    /// Initialize and start a new node with the given configuration
    #[staticmethod]
    #[pyo3(signature = (
        rewards_address,
        evm_network,
        ip = "0.0.0.0",
        port = 0,
        initial_peers = vec![],
        local = false,
        root_dir = None,
        relay = false,
    ))]
    #[allow(clippy::too_many_arguments)]
    fn init<'p>(
        py: Python<'p>,
        rewards_address: String,
        evm_network: PyNetwork,
        ip: &str,
        port: u16,
        initial_peers: Vec<String>,
        local: bool,
        root_dir: Option<String>,
        relay: bool,
    ) -> PyResult<Bound<'p, PyAny>> {
        let rewards_address = RewardsAddress::from_hex(&rewards_address)
            .map_err(|e| PyValueError::new_err(format!("Invalid rewards address: {e}")))?;

        let ip: IpAddr = ip
            .parse()
            .map_err(|e| PyValueError::new_err(format!("Invalid IP address: {e}")))?;

        let node_socket_addr = SocketAddr::new(ip, port);

        let initial_peers: Vec<Multiaddr> = initial_peers
            .into_iter()
            .map(|addr| addr.parse())
            .collect::<Result<_, _>>()
            .map_err(|e| PyValueError::new_err(format!("Invalid peer address: {e}")))?;

        let root_dir = root_dir.map(PathBuf::from);
        let keypair = Keypair::generate_ed25519();

        future_into_py(py, async move {
            let mut node_builder = NodeBuilder::new(
                keypair,
                initial_peers,
                rewards_address,
                evm_network.0,
                node_socket_addr,
                root_dir.unwrap_or_else(|| PathBuf::from(".")),
            );
            node_builder.local(local);
            node_builder.no_upnp(false);
            node_builder.relay_client(relay);

            let running_node = node_builder
                .build_and_run()
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to start node: {e}")))?;

            Ok(PyAntNode {
                node: Arc::new(RwLock::new(running_node)),
            })
        })
    }

    /// Get the node's PeerId as a string
    fn peer_id(self_: PyRef<Self>) -> PyResult<String> {
        let node_guard = self_
            .node
            .try_read()
            .map_err(|_| PyRuntimeError::new_err("Failed to acquire node lock"))?;

        Ok(node_guard.peer_id().to_string())
    }

    /// Get all record addresses stored by the node
    fn get_all_record_addresses<'p>(
        self_: PyRef<'p, Self>,
        py: Python<'p>,
    ) -> PyResult<Bound<'p, PyAny>> {
        let node = Arc::clone(&self_.node);

        future_into_py(py, async move {
            let node_guard = node.read().await;

            let addresses = node_guard
                .get_all_record_addresses()
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to get addresses: {e}")))?;

            Ok(addresses
                .into_iter()
                .map(|addr| addr.to_string())
                .collect::<Vec<_>>())
        })
    }

    /// Get the node's kbuckets information
    fn get_kbuckets<'p>(self_: PyRef<'p, Self>, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> {
        let node = Arc::clone(&self_.node);

        future_into_py(py, async move {
            let node_guard = node.read().await;

            let kbuckets = node_guard
                .get_kbuckets()
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to get kbuckets: {e}")))?;

            Ok(kbuckets
                .into_iter()
                .map(|(distance, peers)| {
                    (
                        distance,
                        peers.into_iter().map(|p| p.to_string()).collect::<Vec<_>>(),
                    )
                })
                .collect::<Vec<_>>())
        })
    }

    /// Get the node's rewards/wallet address as a hex string
    fn get_rewards_address(self_: PyRef<Self>) -> PyResult<String> {
        let node_guard = self_
            .node
            .try_read()
            .map_err(|_| PyRuntimeError::new_err("Failed to acquire node lock"))?;

        Ok(format!("0x{}", hex::encode(node_guard.reward_address())))
    }

    /// Get a record from the node's storage
    fn get_record<'p>(
        self_: PyRef<'p, Self>,
        py: Python<'p>,
        key: String,
    ) -> PyResult<Bound<'p, PyAny>> {
        let node = Arc::clone(&self_.node);

        future_into_py(py, async move {
            let node_guard = node.read().await;

            let xorname = XorName::from_content(
                &hex::decode(key)
                    .map_err(|e| PyValueError::new_err(format!("Invalid key format: {e}")))?,
            );
            let chunk_address = ChunkAddress::new(xorname);
            let network_address = NetworkAddress::from(chunk_address);
            let record_key = network_address.to_record_key();

            let record = node_guard
                .network
                .get_local_record(&record_key)
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to get record: {e}")))?;

            Ok(record.map(|r| r.value.to_vec()))
        })
    }

    /// Get the total size of stored records
    fn get_stored_records_size<'p>(
        self_: PyRef<'p, Self>,
        py: Python<'p>,
    ) -> PyResult<Bound<'p, PyAny>> {
        let node = Arc::clone(&self_.node);

        future_into_py(py, async move {
            let node_guard = node.read().await;

            let records = node_guard
                .network
                .get_all_local_record_addresses()
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to get records: {e}")))?;

            let mut total_size = 0u64;
            for (key, _) in records {
                if let Ok(Some(record)) = node_guard
                    .network
                    .get_local_record(&key.to_record_key())
                    .await
                {
                    total_size += record.value.len() as u64;
                }
            }
            Ok(total_size)
        })
    }

    /// Get the current root directory path for node data
    fn get_root_dir(self_: PyRef<Self>) -> PyResult<String> {
        let node_guard = self_
            .node
            .try_read()
            .map_err(|_| PyRuntimeError::new_err("Failed to acquire node lock"))?;

        Ok(node_guard
            .root_dir_path()
            .to_str()
            .ok_or_else(|| PyValueError::new_err("Invalid path encoding"))?
            .to_string())
    }

    /// Get the default root directory path for the given peer ID
    /// This is platform specific:
    ///  - Linux: $HOME/.local/share/autonomi/node/<peer-id>
    ///  - macOS: $HOME/Library/Application Support/autonomi/node/<peer-id>
    ///  - Windows: C:\Users\<username>\AppData\Roaming\autonomi\node\<peer-id>
    #[allow(clippy::redundant_closure)]
    #[staticmethod]
    #[pyo3(signature = (peer_id=None))]
    fn get_default_root_dir(peer_id: Option<String>) -> PyResult<String> {
        let peer_id = peer_id
            .map(|id| id.parse::<PeerId>()) // Parse peer ID string
            .transpose() // Convert `Option<Result>` to `Result<Option>`.
            .map_err(|e| PyValueError::new_err(format!("Invalid peer ID: {e}")))? // Throw parse error if applicable.
            .unwrap_or_else(|| PeerId::random());

        let path = get_antnode_root_dir(peer_id)
            .map_err(|e| PyRuntimeError::new_err(format!("Failed to get default root dir: {e}")))?;

        Ok(path
            .to_str()
            .ok_or_else(|| PyValueError::new_err("Invalid path encoding"))?
            .to_string())
    }

    /// Get the logs directory path
    fn get_logs_dir(self_: PyRef<Self>) -> PyResult<String> {
        let node_guard = self_
            .node
            .try_read()
            .map_err(|_| PyRuntimeError::new_err("Failed to acquire node lock"))?;

        let logs_path = node_guard.root_dir_path().join("logs");
        Ok(logs_path
            .to_str()
            .ok_or_else(|| PyValueError::new_err("Invalid path encoding"))?
            .to_string())
    }

    /// Get the data directory path where records are stored
    fn get_data_dir(self_: PyRef<Self>) -> PyResult<String> {
        let node_guard = self_
            .node
            .try_read()
            .map_err(|_| PyRuntimeError::new_err("Failed to acquire node lock"))?;

        let data_path = node_guard.root_dir_path().join("data");
        Ok(data_path
            .to_str()
            .ok_or_else(|| PyValueError::new_err("Invalid path encoding"))?
            .to_string())
    }
}

#[pyclass(name = "RunningNetwork")]
pub struct PyRunningNetwork(Arc<Mutex<Option<RunningNetwork>>>);

#[pymethods]
impl PyRunningNetwork {
    fn bootstrap_peer<'a>(&mut self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
        let self_ = Arc::clone(&self.0);
        future_into_py(py, async move {
            let mut self_ = self_.lock().await;
            let running_network = self_.as_mut().ok_or_else(|| {
                PyRuntimeError::new_err("RunningNetwork probably already shutdown")
            })?;

            let peer = running_network.bootstrap_peer().await;

            Ok(peer.to_string())
        })
    }

    fn shutdown<'a>(&mut self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
        let self_ = Arc::clone(&self.0);
        future_into_py(py, async move {
            let mut self_ = self_.lock().await;
            let running_network = self_.take().ok_or_else(|| {
                PyRuntimeError::new_err("RunningNetwork probably already shutdown")
            })?;

            running_network.shutdown();

            Ok(())
        })
    }
}

#[pyclass(name = "NodeSpawner")]
pub struct PyNodeSpawner(Option<NodeSpawner>);

#[pymethods]
impl PyNodeSpawner {
    /// Create a new instance of `NodeSpawner` with default values.
    #[new]
    fn new() -> Self {
        Self(Some(NodeSpawner::new()))
    }

    /// Set the socket address for the node.
    pub fn with_socket_addr(&mut self, socket_addr: &str) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            let socket_addr = socket_addr
                .parse()
                .map_err(|e| PyValueError::new_err(format!("Invalid socket address: {e}")))?;
            self.0 = Some(self_.with_socket_addr(socket_addr));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the EVM network for the node to connect to.
    pub fn with_evm_network(&mut self, network: PyNetwork) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_evm_network(network.0));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the rewards address for the node for distributing rewards.
    pub fn with_rewards_address(&mut self, rewards_address: &str) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            let rewards_address = rewards_address
                .parse()
                .map_err(|e| PyValueError::new_err(format!("Invalid rewards address: {e}")))?;
            self.0 = Some(self_.with_rewards_address(rewards_address));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the initial peers for the node.
    pub fn with_initial_peers(&mut self, initial_peers: Vec<String>) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            let initial_peers = initial_peers
                .into_iter()
                .map(|addr| addr.parse())
                .collect::<Result<_, _>>()
                .map_err(|e| PyValueError::new_err(format!("Invalid peer address: {e}")))?;
            self.0 = Some(self_.with_initial_peers(initial_peers));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the local mode flag for the node, indicating whether the node should run in local mode.
    pub fn with_local(&mut self, local: bool) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_local(local));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the flag to disable UPnP for the node.
    pub fn with_no_upnp(&mut self, no_upnp: bool) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_no_upnp(no_upnp));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }
        Ok(())
    }

    /// Set the root directory for the node.
    pub fn with_root_dir(&mut self, root_dir: PathBuf) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_root_dir(Some(root_dir)));
        } else {
            return Err(PyRuntimeError::new_err("NodeSpawner inner error"));
        }

        Ok(())
    }

    /// Spawn the node using the configured parameters.
    pub fn spawn<'a>(&mut self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
        let self_ = self
            .0
            .take()
            .ok_or_else(|| PyRuntimeError::new_err("NodeSpawner inner error"))?;

        future_into_py(py, async move {
            let running_node = self_
                .spawn()
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to spawn node: {e}")))?;

            Ok(PyAntNode {
                node: Arc::new(RwLock::new(running_node)),
            })
        })
    }
}

#[pyclass(name = "NetworkSpawner")]
pub struct PyNetworkSpawner(Option<NetworkSpawner>);

#[pymethods]
impl PyNetworkSpawner {
    /// Creates a new `NetworkSpawner` with default configurations.
    ///
    /// Default values:
    /// - `evm_network`: `EvmNetwork::default()`
    /// - `rewards_address`: `RewardsAddress::default()`
    /// - `local`: `false`
    /// - `no_upnp`: `false`
    /// - `root_dir`: `None`
    /// - `size`: `5`
    #[new]
    fn new() -> Self {
        Self(Some(NetworkSpawner::new()))
    }

    /// Set the EVM network for the network to use.
    pub fn with_evm_network(&mut self, network: PyNetwork) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_evm_network(network.0));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }
        Ok(())
    }

    /// Set the rewards address for the nodes for distributing rewards.
    pub fn with_rewards_address(&mut self, rewards_address: &str) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            let rewards_address = rewards_address
                .parse()
                .map_err(|e| PyValueError::new_err(format!("Invalid rewards address: {e}")))?;
            self.0 = Some(self_.with_rewards_address(rewards_address));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }
        Ok(())
    }

    /// Set the local mode flag for the node, indicating whether the node should run in local mode.
    pub fn with_local(&mut self, local: bool) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_local(local));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }
        Ok(())
    }

    /// Set to disable UPnP for the node.
    pub fn with_no_upnp(&mut self, no_upnp: bool) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_no_upnp(no_upnp));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }
        Ok(())
    }

    /// Set the root directory for the node.
    pub fn with_root_dir(&mut self, root_dir: PathBuf) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_root_dir(Some(root_dir)));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }

        Ok(())
    }

    /// Specifies the number of nodes to spawn in the network.
    pub fn with_size(&mut self, size: usize) -> PyResult<()> {
        if let Some(self_) = self.0.take() {
            self.0 = Some(self_.with_size(size));
        } else {
            return Err(PyRuntimeError::new_err("NetworkSpawner inner error"));
        }

        Ok(())
    }

    /// Spawns the network with the configured parameters.
    pub fn spawn<'a>(&mut self, py: Python<'a>) -> PyResult<Bound<'a, PyAny>> {
        let self_ = self
            .0
            .take()
            .ok_or_else(|| PyRuntimeError::new_err("NetworkSpawner inner error"))?;

        future_into_py(py, async move {
            let running_network = self_
                .spawn()
                .await
                .map_err(|e| PyRuntimeError::new_err(format!("Failed to spawn network: {e}")))?;

            Ok(PyRunningNetwork(Arc::new(Mutex::new(Some(
                running_network,
            )))))
        })
    }
}

#[pyclass(name = "Network", eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct PyNetwork(EvmNetwork);

#[pymethods]
impl PyNetwork {
    /// Creates a new network configuration.
    ///
    /// If `local` is true, configures for local network connections.
    #[new]
    fn new(local: bool) -> PyResult<Self> {
        let network =
            EvmNetwork::new(local).map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?;
        Ok(Self(network))
    }

    /// Creates a new custom network configuration.
    ///
    /// Can be one of:
    /// - "evm-arbitrum-one"
    /// - "evm-arbitrum-sepolia"
    /// - "evm-arbitrum-sepolia-test"
    #[staticmethod]
    fn from_str(s: &str) -> PyResult<Self> {
        let network = s
            .parse()
            .map_err(|_| PyValueError::new_err("Invalid network name"))?;
        Ok(Self(network))
    }
}

/// Python module initialization
#[pymodule]
#[pyo3(name = "_antnode")]
fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<PyAntNode>()?;
    m.add_class::<PyNodeSpawner>()?;
    m.add_class::<PyNetworkSpawner>()?;
    m.add_class::<PyRunningNetwork>()?;
    m.add_class::<PyNetwork>()?;
    Ok(())
}