ant-node 0.10.0

Pure quantum-proof network node for the Autonomi decentralized network
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
//! Test harness that orchestrates the test network and EVM testnet.
//!
//! The `TestHarness` provides a unified interface for E2E tests, managing
//! both the ant node network and optional Anvil EVM testnet.

use super::anvil::TestAnvil;
use super::testnet::{TestNetwork, TestNetworkConfig, TestNode};
use ant_node::client::XorName;
use evmlib::common::TxHash;
use saorsa_core::P2PNode;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tracing::info;

/// Error type for test harness operations.
#[derive(Debug, thiserror::Error)]
pub enum HarnessError {
    /// Testnet error
    #[error("Testnet error: {0}")]
    Testnet(#[from] super::testnet::TestnetError),

    /// Anvil error
    #[error("Anvil error: {0}")]
    Anvil(String),

    /// Node not found
    #[error("Node not found: index {0}")]
    NodeNotFound(usize),
}

/// Result type for harness operations.
pub type Result<T> = std::result::Result<T, HarnessError>;

/// Payment tracking record for a chunk.
#[derive(Debug, Clone)]
pub struct PaymentRecord {
    /// The chunk address that was paid for.
    pub chunk_address: XorName,
    /// Transaction hashes for this payment (typically 1 for `SingleNode` strategy).
    pub tx_hashes: Vec<TxHash>,
    /// Timestamp when the payment was recorded.
    pub timestamp: std::time::SystemTime,
}

/// Tracks on-chain payments made during tests.
///
/// This allows tests to verify that payment caching works correctly
/// and that duplicate payments are not made for the same chunk.
#[derive(Debug, Clone, Default)]
pub struct PaymentTracker {
    /// Map from chunk address to payment records.
    /// Multiple payments for the same chunk indicate a bug.
    payments: Arc<Mutex<HashMap<XorName, Vec<PaymentRecord>>>>,
}

impl PaymentTracker {
    /// Create a new payment tracker.
    #[must_use]
    pub fn new() -> Self {
        Self {
            payments: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Record a payment for a chunk.
    ///
    /// This should be called after a successful `wallet.pay_for_quotes()` call.
    pub fn record_payment(&self, chunk_address: XorName, tx_hashes: Vec<TxHash>) {
        let record = PaymentRecord {
            chunk_address,
            tx_hashes,
            timestamp: std::time::SystemTime::now(),
        };

        let mut payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.entry(chunk_address).or_default().push(record);
    }

    /// Get the number of payments made for a specific chunk.
    ///
    /// # Returns
    ///
    /// - `0` if no payments were made
    /// - `1` if one payment was made (expected)
    /// - `>1` if duplicate payments were made (bug - cache failed)
    #[must_use]
    pub fn payment_count(&self, chunk_address: &XorName) -> usize {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.get(chunk_address).map_or(0, Vec::len)
    }

    /// Get all payment records for a specific chunk.
    #[must_use]
    pub fn get_payments(&self, chunk_address: &XorName) -> Vec<PaymentRecord> {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.get(chunk_address).cloned().unwrap_or_default()
    }

    /// Get the total number of unique chunks that have been paid for.
    #[must_use]
    pub fn unique_chunk_count(&self) -> usize {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.len()
    }

    /// Get the total number of payment transactions (across all chunks).
    #[must_use]
    pub fn total_payment_count(&self) -> usize {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.values().map(Vec::len).sum()
    }

    /// Check if any chunk has duplicate payments (indicates cache failure).
    #[must_use]
    pub fn has_duplicate_payments(&self) -> bool {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments.values().any(|records| records.len() > 1)
    }

    /// Get all chunks with duplicate payments.
    #[must_use]
    pub fn chunks_with_duplicates(&self) -> Vec<XorName> {
        let payments = self
            .payments
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        payments
            .iter()
            .filter(|(_, records)| records.len() > 1)
            .map(|(addr, _)| *addr)
            .collect()
    }
}

/// Test harness that manages the complete test environment.
///
/// The harness coordinates:
/// - A network of 25 ant nodes
/// - Optional Anvil EVM testnet for payment verification
/// - Payment tracking for verifying cache behavior
/// - Helper methods for common test operations
pub struct TestHarness {
    /// The test network.
    network: TestNetwork,

    /// Optional Anvil EVM testnet.
    anvil: Option<TestAnvil>,

    /// Payment tracker for monitoring on-chain payments.
    payment_tracker: PaymentTracker,
}

impl TestHarness {
    /// Create and start a test network with default configuration (25 nodes).
    ///
    /// This is the standard setup for most E2E tests.
    ///
    /// # Errors
    ///
    /// Returns an error if the network fails to start.
    pub async fn setup() -> Result<Self> {
        Self::setup_with_config(TestNetworkConfig::default()).await
    }

    /// Create and start a test network with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The network configuration to use
    ///
    /// # Errors
    ///
    /// Returns an error if the network fails to start.
    pub async fn setup_with_config(config: TestNetworkConfig) -> Result<Self> {
        info!("Setting up test harness with {} nodes", config.node_count);

        let mut network = TestNetwork::new(config).await?;
        network.start().await?;

        Ok(Self {
            network,
            anvil: None,
            payment_tracker: PaymentTracker::new(),
        })
    }

    /// Create and start a minimal test network (5 nodes) for quick tests.
    ///
    /// # Errors
    ///
    /// Returns an error if the network fails to start.
    pub async fn setup_minimal() -> Result<Self> {
        Self::setup_with_config(TestNetworkConfig::minimal()).await
    }

    /// Create and start a small test network (10 nodes).
    ///
    /// # Errors
    ///
    /// Returns an error if the network fails to start.
    pub async fn setup_small() -> Result<Self> {
        Self::setup_with_config(TestNetworkConfig::small()).await
    }

    /// Create and start a test network with Anvil EVM testnet.
    ///
    /// Use this for tests that require payment verification.
    ///
    /// # Errors
    ///
    /// Returns an error if the network or Anvil fails to start.
    pub async fn setup_with_evm() -> Result<Self> {
        Self::setup_with_evm_and_config(TestNetworkConfig::default()).await
    }

    /// Create and start a test network with Anvil EVM testnet (alias for `setup_with_evm`).
    ///
    /// Use this for tests that require payment verification.
    ///
    /// # Errors
    ///
    /// Returns an error if the network or Anvil fails to start.
    pub async fn setup_with_payments() -> Result<Self> {
        Self::setup_with_evm().await
    }

    /// Create and start a test network with Anvil EVM testnet and custom config.
    ///
    /// # Arguments
    ///
    /// * `config` - The network configuration to use
    ///
    /// # Errors
    ///
    /// Returns an error if the network or Anvil fails to start.
    pub async fn setup_with_evm_and_config(config: TestNetworkConfig) -> Result<Self> {
        info!(
            "Setting up test harness with {} nodes and Anvil EVM",
            config.node_count
        );

        let mut network = TestNetwork::new(config).await?;
        network.start().await?;

        // Warm up DHT routing tables (essential for quote collection)
        info!("Warming up DHT routing tables...");
        network.warmup_dht().await?;

        let anvil = TestAnvil::new()
            .await
            .map_err(|e| HarnessError::Anvil(format!("Failed to start Anvil: {e}")))?;

        Ok(Self {
            network,
            anvil: Some(anvil),
            payment_tracker: PaymentTracker::new(),
        })
    }

    /// Access the payment tracker for verifying on-chain payment behavior.
    ///
    /// This allows tests to verify that:
    /// - Payments are actually made
    /// - Payment caching prevents duplicate payments
    /// - Multiple stores of the same chunk only pay once
    #[must_use]
    pub fn payment_tracker(&self) -> &PaymentTracker {
        &self.payment_tracker
    }

    /// Access the test network.
    #[must_use]
    pub fn network(&self) -> &TestNetwork {
        &self.network
    }

    /// Access the test network mutably.
    #[must_use]
    pub fn network_mut(&mut self) -> &mut TestNetwork {
        &mut self.network
    }

    /// Access the Anvil EVM testnet.
    #[must_use]
    pub fn anvil(&self) -> Option<&TestAnvil> {
        self.anvil.as_ref()
    }

    /// Check if EVM testnet is available.
    #[must_use]
    pub fn has_evm(&self) -> bool {
        self.anvil.is_some()
    }

    /// Access a specific node's P2P interface.
    ///
    /// # Arguments
    ///
    /// * `index` - The node index (0-based)
    ///
    /// # Returns
    ///
    /// The P2P node if found and running, None otherwise.
    #[must_use]
    pub fn node(&self, index: usize) -> Option<Arc<P2PNode>> {
        self.network.node(index)?.p2p_node.clone()
    }

    /// Access a specific test node.
    ///
    /// # Arguments
    ///
    /// * `index` - The node index (0-based)
    #[must_use]
    pub fn test_node(&self, index: usize) -> Option<&TestNode> {
        self.network.node(index)
    }

    /// Access a specific test node mutably.
    ///
    /// # Arguments
    ///
    /// * `index` - The node index (0-based)
    #[must_use]
    pub fn test_node_mut(&mut self, index: usize) -> Option<&mut TestNode> {
        self.network.node_mut(index)
    }

    /// Pre-populate the payment cache on the node matching `peer_id`.
    ///
    /// Inserts `address` into the target node's payment verifier cache so
    /// P2P store requests are accepted without an on-chain proof. This
    /// simulates the address having been previously paid for.
    /// # Panics
    ///
    /// Panics if no running node matches `peer_id` — this indicates a test
    /// configuration error (e.g. wrong peer ID or the target node was shut down).
    #[allow(clippy::panic)]
    pub fn prepopulate_payment_cache_for_peer(
        &self,
        peer_id: &saorsa_core::identity::PeerId,
        address: &XorName,
    ) {
        for node in self.network.nodes() {
            if let Some(ref p2p) = node.p2p_node {
                if p2p.peer_id() == peer_id {
                    if let Some(ref protocol) = node.ant_protocol {
                        protocol.payment_verifier().cache_insert(*address);
                    }
                    return;
                }
            }
        }
        panic!("prepopulate_payment_cache_for_peer: no running node with peer_id {peer_id}");
    }

    /// Get a random non-bootstrap node.
    ///
    /// Useful for tests that need to pick an arbitrary regular node.
    #[must_use]
    pub fn random_node(&self) -> Option<Arc<P2PNode>> {
        use rand::seq::SliceRandom;

        let regular_nodes: Vec<_> = self
            .network
            .regular_nodes()
            .iter()
            .filter(|n| n.p2p_node.is_some())
            .collect();

        regular_nodes
            .choose(&mut rand::thread_rng())
            .and_then(|n| n.p2p_node.clone())
    }

    /// Get a random bootstrap node.
    #[must_use]
    pub fn random_bootstrap_node(&self) -> Option<Arc<P2PNode>> {
        use rand::seq::SliceRandom;

        let bootstrap_nodes: Vec<_> = self
            .network
            .bootstrap_nodes()
            .iter()
            .filter(|n| n.p2p_node.is_some())
            .collect();

        bootstrap_nodes
            .choose(&mut rand::thread_rng())
            .and_then(|n| n.p2p_node.clone())
    }

    /// Get all P2P nodes.
    #[must_use]
    pub fn all_nodes(&self) -> Vec<Arc<P2PNode>> {
        self.network
            .nodes()
            .iter()
            .filter_map(|n| n.p2p_node.clone())
            .collect()
    }

    /// Get the total number of nodes.
    #[must_use]
    pub fn node_count(&self) -> usize {
        self.network.node_count()
    }

    /// Check if the network is ready.
    pub async fn is_ready(&self) -> bool {
        self.network.is_ready().await
    }

    /// Get total connections across all nodes.
    pub async fn total_connections(&self) -> usize {
        self.network.total_connections().await
    }

    /// Shutdown a specific node by index.
    ///
    /// This simulates a node failure during testing. The node is gracefully shut down
    /// and removed from the network. The remaining nodes continue to operate.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the node to shutdown (0-based)
    ///
    /// # Errors
    ///
    /// Returns an error if the node index is invalid or shutdown fails.
    pub async fn shutdown_node(&mut self, index: usize) -> Result<()> {
        self.network.shutdown_node(index).await?;
        Ok(())
    }

    /// Shutdown multiple nodes by their indices.
    ///
    /// This is a convenience method for simulating multiple node failures at once.
    ///
    /// # Arguments
    ///
    /// * `indices` - Slice of node indices to shutdown
    ///
    /// # Errors
    ///
    /// Returns an error if any node index is invalid or shutdown fails.
    pub async fn shutdown_nodes(&mut self, indices: &[usize]) -> Result<()> {
        self.network.shutdown_nodes(indices).await?;
        Ok(())
    }

    /// Get the number of currently running nodes.
    pub async fn running_node_count(&self) -> usize {
        self.network.running_node_count().await
    }

    /// Warm up DHT routing tables for quote collection.
    ///
    /// This method populates DHT routing tables by performing random lookups,
    /// which is necessary before using `get_quotes_from_dht()`.
    ///
    /// # Errors
    ///
    /// Returns an error if DHT warmup fails.
    pub async fn warmup_dht(&self) -> Result<()> {
        self.network.warmup_dht().await?;
        Ok(())
    }

    /// Add a new node to the running network.
    ///
    /// The node is fully bootstrapped (DHT warmed, replication bootstrap
    /// complete) before this method returns. Returns the new node's index.
    ///
    /// # Errors
    ///
    /// Returns an error if the node fails to start or bootstrap.
    pub async fn add_node(&mut self) -> Result<usize> {
        Ok(self.network.add_node().await?)
    }

    /// Teardown the test harness.
    ///
    /// This shuts down all nodes and the Anvil testnet if running.
    ///
    /// # Errors
    ///
    /// Returns an error if shutdown fails.
    pub async fn teardown(mut self) -> Result<()> {
        info!("Tearing down test harness");

        // Shutdown network first
        self.network.shutdown().await?;

        // Shutdown Anvil if running
        if let Some(mut anvil) = self.anvil.take() {
            anvil.shutdown().await;
        }

        info!("Test harness teardown complete");
        Ok(())
    }
}

/// Macro for setting up and tearing down test networks.
///
/// This macro handles the boilerplate of creating a test harness,
/// running the test body, and ensuring cleanup happens.
///
/// # Example
///
/// ```rust,ignore
/// with_test_network!(harness, {
///     let node = harness.node(0).unwrap();
///     // Run test assertions...
///     Ok(())
/// });
/// ```
#[macro_export]
macro_rules! with_test_network {
    ($harness:ident, $body:block) => {{
        let $harness = $crate::tests::e2e::TestHarness::setup().await?;
        let result: Result<(), Box<dyn std::error::Error>> = async { $body }.await;
        $harness.teardown().await?;
        result
    }};
    ($harness:ident, $config:expr, $body:block) => {{
        let $harness = $crate::tests::e2e::TestHarness::setup_with_config($config).await?;
        let result: Result<(), Box<dyn std::error::Error>> = async { $body }.await;
        $harness.teardown().await?;
        result
    }};
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_harness_error_display() {
        let err = HarnessError::NodeNotFound(5);
        assert!(err.to_string().contains('5'));
    }
}