ant-node 0.23.1

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
//! Local devnet infrastructure for spawning and managing multiple nodes.
//!
//! This module provides a local, in-process devnet suitable for running
//! multi-node networks on a single machine.

use crate::ant_protocol::CHUNK_PROTOCOL_ID;
use crate::config::{default_root_dir, NODES_SUBDIR, NODE_IDENTITY_FILENAME};
use crate::logging::{debug, info, warn};
use crate::payment::{
    EvmVerifierConfig, PaymentVerifier, PaymentVerifierConfig, PriceFloorConfig, QuoteGenerator,
    QuotingMetricsTracker,
};
use crate::replication::config::ReplicationConfig;
use crate::storage::{AntProtocol, ChunkRequestContext, ChunkStore, ChunkStoreConfig};
use evmlib::Network as EvmNetwork;
use evmlib::RewardsAddress;
use rand::Rng;
use saorsa_core::identity::NodeIdentity;
use saorsa_core::{
    IPDiversityConfig, MultiAddr, NodeConfig as CoreNodeConfig, P2PEvent, P2PNode, PeerId,
};
use std::net::{Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::task::JoinHandle;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;

#[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
use crate::ant_protocol::{ChunkMessage, ChunkMessageBody, ChunkPutRequest, ChunkPutResponse};
#[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
use crate::browser::BrowserPublicFile;
#[cfg(feature = "webrtc-direct")]
use crate::browser::{browser_payment_network, BrowserBootstrapNode, BrowserPaymentNetwork};
#[cfg(feature = "webrtc-direct")]
use crate::config::WebRtcDirectConfig;
#[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
use bytes::Bytes;
#[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
use std::collections::HashMap;

// =============================================================================
// Devnet Constants
// =============================================================================

/// Minimum port for random devnet allocation.
pub const DEVNET_PORT_RANGE_MIN: u16 = 20_000;

/// Maximum port for random devnet allocation.
pub const DEVNET_PORT_RANGE_MAX: u16 = 60_000;

// =============================================================================
// Default Timing Constants
// =============================================================================

/// Default delay between spawning nodes (milliseconds).
const DEFAULT_SPAWN_DELAY_MS: u64 = 200;

/// Default timeout for network stabilization (seconds).
const DEFAULT_STABILIZATION_TIMEOUT_SECS: u64 = 120;

/// Default timeout for single node startup (seconds).
const DEFAULT_NODE_STARTUP_TIMEOUT_SECS: u64 = 30;

/// Stabilization timeout for minimal network (seconds).
const MINIMAL_STABILIZATION_TIMEOUT_SECS: u64 = 30;

/// Stabilization timeout for small network (seconds).
const SMALL_STABILIZATION_TIMEOUT_SECS: u64 = 60;

/// Polling interval when waiting for individual nodes to become ready (milliseconds).
const NODE_READY_POLL_INTERVAL_MS: u64 = 100;

/// Polling interval when waiting for network stabilization (seconds).
const STABILIZATION_POLL_INTERVAL_SECS: u64 = 1;

/// Maximum minimum connections required per node during stabilization.
const STABILIZATION_MIN_CONNECTIONS_CAP: usize = 3;

/// Health monitor check interval (seconds).
const HEALTH_CHECK_INTERVAL_SECS: u64 = 5;

// =============================================================================
// AntProtocol Devnet Configuration
// =============================================================================

/// Payment cache capacity for devnet nodes.
const DEVNET_PAYMENT_CACHE_CAPACITY: usize = 1000;

/// Devnet rewards address (20 bytes, all 0x01).
const DEVNET_REWARDS_ADDRESS: [u8; 20] = [0x01; 20];

/// Initial records for quoting metrics (devnet value).
const DEVNET_INITIAL_RECORDS: usize = 1000;

async fn handle_chunk_message(
    protocol: Arc<AntProtocol>,
    p2p: Arc<P2PNode>,
    source: PeerId,
    data: Vec<u8>,
    node_index: usize,
    received_at: Instant,
) {
    let handled = protocol
        .try_handle_request_with_context(
            &data,
            Some(ChunkRequestContext::new(
                source.to_string(),
                received_at.into(),
                Duration::ZERO,
            )),
        )
        .await;
    let telemetry = handled.get_telemetry;
    match handled.response {
        Ok(Some(response)) => {
            let send_started = Instant::now();
            let send_result = p2p
                .send_message(&source, CHUNK_PROTOCOL_ID, response.to_vec(), &[])
                .await;
            if let Some(telemetry) = telemetry {
                telemetry.finish_send(send_started.elapsed(), send_result.is_ok());
            }
            if let Err(e) = send_result {
                warn!("Node {node_index} failed to send response to {source}: {e}");
            }
        }
        Ok(None) => {
            if let Some(telemetry) = telemetry {
                telemetry.finish_without_send("no_response");
            }
        }
        Err(e) => {
            if let Some(telemetry) = telemetry {
                telemetry.finish_without_send("encode_error");
            }
            warn!("Node {node_index} protocol handler error: {e}");
        }
    }
}

// =============================================================================
// Default Node Counts
// =============================================================================

/// Default number of nodes in a full devnet.
pub const DEFAULT_NODE_COUNT: usize = 25;

/// Default number of bootstrap nodes.
pub const DEFAULT_BOOTSTRAP_COUNT: usize = 3;

/// Number of nodes in a minimal devnet.
pub const MINIMAL_NODE_COUNT: usize = 5;

/// Number of bootstrap nodes in a minimal network.
pub const MINIMAL_BOOTSTRAP_COUNT: usize = 2;

/// Number of nodes in a small devnet.
pub const SMALL_NODE_COUNT: usize = 10;

/// Error type for devnet operations.
#[derive(Debug, thiserror::Error)]
pub enum DevnetError {
    /// Configuration error
    #[error("Configuration error: {0}")]
    Config(String),

    /// Node startup error
    #[error("Node startup error: {0}")]
    Startup(String),

    /// Network stabilization error
    #[error("Network stabilization error: {0}")]
    Stabilization(String),

    /// IO error
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

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

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

/// Configuration for the devnet.
///
/// Each configuration is automatically isolated with unique ports and
/// data directories to prevent collisions when running multiple devnets.
#[derive(Debug, Clone)]
pub struct DevnetConfig {
    /// Number of nodes to spawn (default: 25).
    pub node_count: usize,

    /// Base port for node allocation (0 = auto).
    pub base_port: u16,

    /// Number of bootstrap nodes (first N nodes, default: 3).
    pub bootstrap_count: usize,

    /// Root directory for devnet data.
    pub data_dir: PathBuf,

    /// Delay between node spawns (default: 200ms).
    pub spawn_delay: Duration,

    /// Timeout for network stabilization (default: 120s).
    pub stabilization_timeout: Duration,

    /// Timeout for single node startup (default: 30s).
    pub node_startup_timeout: Duration,

    /// Enable verbose logging for devnet nodes.
    pub enable_node_logging: bool,

    /// Whether to remove the data directory on shutdown.
    pub cleanup_data_dir: bool,

    /// Optional EVM network for payment verification.
    /// When `Some`, nodes will use this network (e.g. Anvil testnet) for
    /// on-chain verification. Defaults to Arbitrum One when `None`.
    pub evm_network: Option<EvmNetwork>,

    /// Optional IPv4 to advertise to peers/clients (LAN devnet). When `Some`,
    /// nodes bind 0.0.0.0 and advertise this IP instead of 127.0.0.1.
    pub advertise_ip: Option<Ipv4Addr>,

    /// Run one direct-browser WebRTC Direct listener per devnet node.
    pub webrtc_direct: bool,

    /// First UDP port in the WebRTC Direct node range (0 = allocate).
    pub webrtc_direct_base_port: u16,
}

impl Default for DevnetConfig {
    fn default() -> Self {
        let mut rng = rand::thread_rng();

        #[allow(clippy::cast_possible_truncation)] // DEFAULT_NODE_COUNT is 25, always fits u16
        let max_base_port = DEVNET_PORT_RANGE_MAX.saturating_sub(DEFAULT_NODE_COUNT as u16);
        let base_port = rng.gen_range(DEVNET_PORT_RANGE_MIN..max_base_port);

        Self {
            node_count: DEFAULT_NODE_COUNT,
            base_port,
            bootstrap_count: DEFAULT_BOOTSTRAP_COUNT,
            data_dir: default_root_dir(),
            spawn_delay: Duration::from_millis(DEFAULT_SPAWN_DELAY_MS),
            stabilization_timeout: Duration::from_secs(DEFAULT_STABILIZATION_TIMEOUT_SECS),
            node_startup_timeout: Duration::from_secs(DEFAULT_NODE_STARTUP_TIMEOUT_SECS),
            enable_node_logging: false,
            cleanup_data_dir: true,
            evm_network: None,
            advertise_ip: None,
            webrtc_direct: false,
            webrtc_direct_base_port: 0,
        }
    }
}

impl DevnetConfig {
    /// Minimal devnet preset (5 nodes).
    #[must_use]
    pub fn minimal() -> Self {
        Self {
            node_count: MINIMAL_NODE_COUNT,
            bootstrap_count: MINIMAL_BOOTSTRAP_COUNT,
            stabilization_timeout: Duration::from_secs(MINIMAL_STABILIZATION_TIMEOUT_SECS),
            ..Self::default()
        }
    }

    /// Small devnet preset (10 nodes).
    #[must_use]
    pub fn small() -> Self {
        Self {
            node_count: SMALL_NODE_COUNT,
            bootstrap_count: MINIMAL_BOOTSTRAP_COUNT,
            stabilization_timeout: Duration::from_secs(SMALL_STABILIZATION_TIMEOUT_SECS),
            ..Self::default()
        }
    }
}

// The manifest types are shared with ant-client (CLI reads them, devnet
// writes them), so they live in ant-protocol. Re-exported here for
// backwards compatibility.
pub use ant_protocol::devnet_manifest::{DevnetEvmInfo, DevnetManifest};

/// Network state for devnet startup lifecycle.
#[derive(Debug, Clone)]
pub enum NetworkState {
    /// Not started.
    Uninitialized,
    /// Bootstrapping nodes are starting.
    BootstrappingPhase,
    /// Regular nodes are starting.
    NodeSpawningPhase,
    /// Waiting for stabilization.
    Stabilizing,
    /// Network is ready.
    Ready,
    /// Shutting down.
    ShuttingDown,
    /// Stopped.
    Stopped,
}

/// Node state for devnet nodes.
#[derive(Debug, Clone)]
pub enum NodeState {
    /// Not started yet.
    Pending,
    /// Starting up.
    Starting,
    /// Running.
    Running,
    /// Connected to peers.
    Connected,
    /// Stopped.
    Stopped,
    /// Failed to start.
    Failed(String),
}

/// A single devnet node instance.
#[allow(dead_code)]
pub struct DevnetNode {
    index: usize,
    label: String,
    peer_id: PeerId,
    port: u16,
    data_dir: PathBuf,
    p2p_node: Option<Arc<P2PNode>>,
    ant_protocol: Option<Arc<AntProtocol>>,
    is_bootstrap: bool,
    state: Arc<RwLock<NodeState>>,
    bootstrap_addrs: Vec<MultiAddr>,
    protocol_task: Option<JoinHandle<()>>,
    #[cfg(feature = "webrtc-direct")]
    webrtc_direct_task: Option<JoinHandle<()>>,
    #[cfg(feature = "webrtc-direct")]
    webrtc_diagnostics: Option<crate::web_rtc::WebRtcServerDiagnostics>,
    #[cfg(feature = "webrtc-direct")]
    browser_endpoint: Option<crate::browser::BrowserEndpoint>,
}

impl DevnetNode {
    /// Get the node's peer count.
    pub async fn peer_count(&self) -> usize {
        if let Some(ref node) = self.p2p_node {
            node.peer_count().await
        } else {
            0
        }
    }
}

/// A local devnet composed of multiple nodes.
pub struct Devnet {
    config: DevnetConfig,
    nodes: Vec<DevnetNode>,
    shutdown: CancellationToken,
    state: Arc<RwLock<NetworkState>>,
    health_monitor: Option<JoinHandle<()>>,
    #[cfg(feature = "webrtc-direct")]
    browser_endpoint_catalog: Arc<crate::web_rtc::BrowserEndpointCatalog>,
}

impl Devnet {
    /// Create a new devnet with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns `DevnetError::Config` if the configuration is invalid (e.g. bootstrap
    /// count exceeds node count or port range overflow).
    /// Returns `DevnetError::Io` if the data directory cannot be created.
    #[allow(clippy::too_many_lines)]
    pub async fn new(mut config: DevnetConfig) -> Result<Self> {
        if config.bootstrap_count >= config.node_count {
            return Err(DevnetError::Config(
                "Bootstrap count must be less than node count".to_string(),
            ));
        }

        if config.bootstrap_count == 0 {
            return Err(DevnetError::Config(
                "At least one bootstrap node is required".to_string(),
            ));
        }

        let node_count = config.node_count;
        let node_count_u16 = u16::try_from(node_count).map_err(|_| {
            DevnetError::Config(format!("Node count {node_count} exceeds u16::MAX"))
        })?;

        if config.base_port == 0 {
            let mut rng = rand::thread_rng();
            let max_base_port = DEVNET_PORT_RANGE_MAX.saturating_sub(node_count_u16);
            config.base_port = rng.gen_range(DEVNET_PORT_RANGE_MIN..max_base_port);
        }

        let base_port = config.base_port;
        let max_port = base_port
            .checked_add(node_count_u16)
            .ok_or_else(|| {
                DevnetError::Config(format!(
                    "Port range overflow: base_port {base_port} + node_count {node_count} exceeds u16::MAX"
                ))
            })?;
        if max_port > DEVNET_PORT_RANGE_MAX {
            return Err(DevnetError::Config(format!(
                "Port range overflow: max port {max_port} exceeds DEVNET_PORT_RANGE_MAX {DEVNET_PORT_RANGE_MAX}"
            )));
        }

        #[cfg(not(feature = "webrtc-direct"))]
        if config.webrtc_direct {
            return Err(DevnetError::Config(
                "WebRtcDirect devnet support requires the 'webrtc-direct' feature".to_string(),
            ));
        }

        #[cfg(feature = "webrtc-direct")]
        if config.webrtc_direct {
            if config.webrtc_direct_base_port == 0 {
                let adjacent = max_port;
                let adjacent_end = adjacent.checked_add(node_count_u16);
                config.webrtc_direct_base_port = if adjacent_end
                    .is_some_and(|end| end <= DEVNET_PORT_RANGE_MAX)
                {
                    adjacent
                } else if let Some(before) = base_port
                    .checked_sub(node_count_u16)
                    .filter(|before| *before >= DEVNET_PORT_RANGE_MIN)
                {
                    before
                } else {
                    let mut rng = rand::thread_rng();
                    let max_base = DEVNET_PORT_RANGE_MAX.saturating_sub(node_count_u16);
                    (0..128)
                        .map(|_| rng.gen_range(DEVNET_PORT_RANGE_MIN..max_base))
                        .find(|candidate| {
                            let end = candidate.saturating_add(node_count_u16);
                            end <= base_port || *candidate >= max_port
                        })
                        .ok_or_else(|| {
                            DevnetError::Config(
                                "Could not allocate a disjoint WebRtcDirect port range".to_string(),
                            )
                        })?
                };
            }

            let webrtc_direct_end = config
                .webrtc_direct_base_port
                .checked_add(node_count_u16)
                .ok_or_else(|| {
                    DevnetError::Config("WebRtcDirect port range overflow".to_string())
                })?;
            if config.webrtc_direct_base_port < DEVNET_PORT_RANGE_MIN
                || webrtc_direct_end > DEVNET_PORT_RANGE_MAX
            {
                return Err(DevnetError::Config(format!(
                    "WebRtcDirect ports must remain in the local test range {DEVNET_PORT_RANGE_MIN}..{DEVNET_PORT_RANGE_MAX}"
                )));
            }
            if base_port < webrtc_direct_end && config.webrtc_direct_base_port < max_port {
                return Err(DevnetError::Config(
                    "Native and WebRtcDirect devnet port ranges overlap".to_string(),
                ));
            }
        }

        tokio::fs::create_dir_all(&config.data_dir).await?;

        Ok(Self {
            config,
            nodes: Vec::new(),
            shutdown: CancellationToken::new(),
            state: Arc::new(RwLock::new(NetworkState::Uninitialized)),
            health_monitor: None,
            #[cfg(feature = "webrtc-direct")]
            browser_endpoint_catalog: Arc::new(crate::web_rtc::BrowserEndpointCatalog::default()),
        })
    }

    /// Start the devnet.
    ///
    /// # Errors
    ///
    /// Returns `DevnetError::Startup` if any node fails to start, or
    /// `DevnetError::Stabilization` if the network does not stabilize within the timeout.
    pub async fn start(&mut self) -> Result<()> {
        info!(
            "Starting devnet with {} nodes ({} bootstrap)",
            self.config.node_count, self.config.bootstrap_count
        );

        *self.state.write().await = NetworkState::BootstrappingPhase;
        self.start_bootstrap_nodes().await?;

        *self.state.write().await = NetworkState::NodeSpawningPhase;
        self.start_regular_nodes().await?;

        *self.state.write().await = NetworkState::Stabilizing;
        self.wait_for_stabilization().await?;

        self.start_health_monitor();

        *self.state.write().await = NetworkState::Ready;
        info!("Devnet is ready");
        Ok(())
    }

    /// Shutdown the devnet.
    ///
    /// # Errors
    ///
    /// Returns `DevnetError::Io` if the data directory cleanup fails.
    pub async fn shutdown(&mut self) -> Result<()> {
        info!("Shutting down devnet");
        *self.state.write().await = NetworkState::ShuttingDown;

        self.shutdown.cancel();

        if let Some(handle) = self.health_monitor.take() {
            handle.abort();
        }

        let mut shutdown_futures = Vec::with_capacity(self.nodes.len());
        for node in self.nodes.iter_mut().rev() {
            debug!("Stopping node {}", node.index);
            if let Some(handle) = node.protocol_task.take() {
                handle.abort();
            }
            #[cfg(feature = "webrtc-direct")]
            if let Some(handle) = node.webrtc_direct_task.take() {
                if let Err(error) = handle.await {
                    warn!(
                        "Error stopping node {} WebRtcDirect listener: {error}",
                        node.index
                    );
                }
            }

            let node_index = node.index;
            let node_state = Arc::clone(&node.state);
            let p2p_node = node.p2p_node.take();

            shutdown_futures.push(async move {
                if let Some(p2p) = p2p_node {
                    if let Err(e) = p2p.shutdown().await {
                        warn!("Error shutting down node {node_index}: {e}");
                    }
                }
                *node_state.write().await = NodeState::Stopped;
            });
        }
        futures::future::join_all(shutdown_futures).await;

        if self.config.cleanup_data_dir {
            if let Err(e) = tokio::fs::remove_dir_all(&self.config.data_dir).await {
                warn!("Failed to cleanup devnet data directory: {e}");
            }
        }

        *self.state.write().await = NetworkState::Stopped;
        info!("Devnet shutdown complete");
        Ok(())
    }

    /// Get devnet configuration.
    #[must_use]
    pub fn config(&self) -> &DevnetConfig {
        &self.config
    }

    /// Get bootstrap addresses.
    #[must_use]
    pub fn bootstrap_addrs(&self) -> Vec<MultiAddr> {
        self.nodes
            .iter()
            .take(self.config.bootstrap_count)
            .map(|n| {
                MultiAddr::quic(SocketAddr::from((
                    self.config.advertise_ip.unwrap_or(Ipv4Addr::LOCALHOST),
                    n.port,
                )))
            })
            .collect()
    }

    /// Snapshot every browser listener, including counters retained after shutdown.
    #[cfg(feature = "webrtc-direct")]
    #[must_use]
    pub fn browser_listener_diagnostics(&self) -> Vec<crate::web_rtc::WebRtcServerSnapshot> {
        self.nodes
            .iter()
            .filter_map(|node| {
                node.webrtc_diagnostics
                    .as_ref()
                    .map(crate::web_rtc::WebRtcServerDiagnostics::snapshot)
            })
            .collect()
    }

    /// Test-only handles for exercising browser admission during stalled storage.
    #[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
    #[must_use]
    pub fn test_browser_node(
        &self,
        index: usize,
    ) -> Option<(Arc<AntProtocol>, crate::web_rtc::WebRtcServerDiagnostics)> {
        let node = self.nodes.get(index)?;
        Some((
            Arc::clone(node.ant_protocol.as_ref()?),
            node.webrtc_diagnostics.clone()?,
        ))
    }

    /// Get every direct browser endpoint in this devnet.
    #[cfg(feature = "webrtc-direct")]
    #[must_use]
    pub fn browser_endpoints(&self) -> Vec<BrowserBootstrapNode> {
        self.nodes
            .iter()
            .filter_map(|node| {
                node.browser_endpoint
                    .clone()
                    .map(|endpoint| BrowserBootstrapNode { endpoint })
            })
            .collect()
    }

    /// Publish a complete self-encrypted file to the browser-enabled devnet.
    ///
    /// The file is split using the same `self_encryption` crate as `ant-client`.
    /// Every encrypted data chunk and the public `MessagePack` `DataMap` are then
    /// submitted through each node's ordinary chunk PUT handler. Address
    /// verification, DHT responsibility, payment-cache admission, and chunk
    /// integrity checks therefore remain active.
    ///
    /// # Errors
    ///
    /// Returns an error when WebRTC Direct is disabled, self-encryption fails,
    /// a generated chunk is too large, no node admits a required record, or
    /// protocol serialization fails.
    #[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
    pub async fn publish_public_file(
        &self,
        name: String,
        content_type: String,
        content: &[u8],
    ) -> Result<BrowserPublicFile> {
        if !self.config.webrtc_direct {
            return Err(DevnetError::Config(
                "Cannot publish a browser file when WebRtcDirect is disabled".to_string(),
            ));
        }
        if content.len() < self_encryption::MIN_ENCRYPTABLE_BYTES {
            return Err(DevnetError::Config(format!(
                "Public file is {} bytes; self-encryption requires at least {} bytes",
                content.len(),
                self_encryption::MIN_ENCRYPTABLE_BYTES
            )));
        }

        let (published_data_map, encrypted_chunks) =
            self_encryption::encrypt(Bytes::copy_from_slice(content)).map_err(|error| {
                DevnetError::Core(format!("Failed to self-encrypt browser file: {error}"))
            })?;
        let mut records = HashMap::<[u8; 32], Bytes>::new();
        for chunk in encrypted_chunks {
            if chunk.content.len() > crate::ant_protocol::MAX_CHUNK_SIZE {
                return Err(DevnetError::Core(format!(
                    "Self-encryption produced a {}-byte chunk; node maximum is {}",
                    chunk.content.len(),
                    crate::ant_protocol::MAX_CHUNK_SIZE
                )));
            }
            let address = crate::client::compute_address(&chunk.content);
            records.entry(address).or_insert(chunk.content);
        }

        let mut get_local_chunk = |address: self_encryption::XorName| {
            records.get(&address.0).cloned().ok_or_else(|| {
                self_encryption::Error::Generic(format!(
                    "Self-encryption output omitted chunk {}",
                    hex::encode(address.0)
                ))
            })
        };
        let root_data_map =
            self_encryption::get_root_data_map(published_data_map.clone(), &mut get_local_chunk)
                .map_err(|error| {
                    DevnetError::Core(format!("Failed to resolve browser file DataMap: {error}"))
                })?;
        let serialized_data_map = rmp_serde::to_vec(&published_data_map).map_err(|error| {
            DevnetError::Core(format!("Failed to serialize browser file DataMap: {error}"))
        })?;
        let data_map_size = serialized_data_map.len();
        let data_map_address = crate::client::compute_address(&serialized_data_map);
        records.insert(data_map_address, Bytes::from(serialized_data_map));

        let record_count = records.len();
        let mut replicas = usize::MAX;
        for (address, bytes) in &records {
            replicas = replicas.min(self.publish_browser_record(*address, bytes).await?);
        }

        let chunks = root_data_map
            .infos()
            .iter()
            .map(|info| crate::browser::BrowserChunkInfo {
                index: info.index,
                dst_hash: hex::encode(info.dst_hash.0),
                src_hash: hex::encode(info.src_hash.0),
                src_size: info.src_size,
            })
            .collect();
        let published = BrowserPublicFile {
            name,
            address: hex::encode(data_map_address),
            size: content.len(),
            content_type,
            blake3: hex::encode(crate::client::compute_address(content)),
            data_map_size,
            chunks,
            replicas,
        };
        info!(
            "Published browser devnet file '{}' at {} as {record_count} record(s), each on at least {} node(s)",
            published.name, published.address, published.replicas
        );
        Ok(published)
    }

    /// Public EVM chain and contracts advertised to direct browser clients.
    ///
    /// # Errors
    ///
    /// Returns `DevnetError::Config` if a custom RPC cannot report its chain ID.
    #[cfg(feature = "webrtc-direct")]
    pub async fn browser_payment_network(&self) -> Result<BrowserPaymentNetwork> {
        let network = self
            .config
            .evm_network
            .as_ref()
            .unwrap_or(&EvmNetwork::ArbitrumOne);
        browser_payment_network(network)
            .await
            .map_err(|error| DevnetError::Config(error.to_string()))
    }

    #[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
    async fn publish_browser_record(&self, address: [u8; 32], content: &Bytes) -> Result<usize> {
        let mut replicas = 0usize;
        let mut failures = Vec::new();

        for node in &self.nodes {
            let Some(protocol) = node.ant_protocol.as_ref() else {
                failures.push(format!("node {} has no protocol handler", node.index));
                continue;
            };
            protocol
                .payment_verifier_arc()
                .cache_insert_browser_devnet_seed(address);

            let request = ChunkMessage {
                request_id: u64::try_from(node.index).unwrap_or(u64::MAX),
                body: ChunkMessageBody::PutRequest(ChunkPutRequest::new(address, content.clone())),
            };
            let request_bytes = request.encode().map_err(|error| {
                DevnetError::Core(format!("Failed to encode public-file PUT: {error}"))
            })?;
            let response_bytes = protocol
                .try_handle_request(&request_bytes)
                .await
                .map_err(|error| {
                    DevnetError::Core(format!(
                        "Node {} public-file PUT failed: {error}",
                        node.index
                    ))
                })?
                .ok_or_else(|| {
                    DevnetError::Core(format!(
                        "Node {} returned no public-file PUT response",
                        node.index
                    ))
                })?;
            let response = ChunkMessage::decode(&response_bytes).map_err(|error| {
                DevnetError::Core(format!(
                    "Failed to decode node {} public-file response: {error}",
                    node.index
                ))
            })?;
            match response.body {
                ChunkMessageBody::PutResponse(
                    ChunkPutResponse::Success { .. } | ChunkPutResponse::AlreadyExists { .. },
                ) => {
                    replicas += 1;
                }
                ChunkMessageBody::PutResponse(other) => {
                    failures.push(format!("node {}: {other:?}", node.index));
                }
                other => failures.push(format!(
                    "node {} returned unexpected response {other:?}",
                    node.index
                )),
            }
        }

        if replicas == 0 {
            return Err(DevnetError::Startup(format!(
                "No devnet node admitted browser record {}: {}",
                hex::encode(address),
                failures.join("; ")
            )));
        }
        if !failures.is_empty() {
            debug!(
                "Browser record was admitted by {replicas} nodes; {} non-responsible/failed nodes: {}",
                failures.len(),
                failures.join("; ")
            );
        }
        Ok(replicas)
    }

    async fn start_bootstrap_nodes(&mut self) -> Result<()> {
        info!("Starting {} bootstrap nodes", self.config.bootstrap_count);

        for i in 0..self.config.bootstrap_count {
            let node = self.create_node(i, true, vec![]).await?;
            self.start_node(node).await?;
            tokio::time::sleep(self.config.spawn_delay).await;
        }

        self.wait_for_nodes_ready(0..self.config.bootstrap_count)
            .await?;

        info!("All bootstrap nodes are ready");
        Ok(())
    }

    async fn start_regular_nodes(&mut self) -> Result<()> {
        let regular_count = self.config.node_count - self.config.bootstrap_count;
        info!("Starting {} regular nodes", regular_count);

        let bootstrap_addrs: Vec<MultiAddr> = self
            .nodes
            .get(0..self.config.bootstrap_count)
            .ok_or_else(|| {
                DevnetError::Config(format!(
                    "Bootstrap count {} exceeds nodes length {}",
                    self.config.bootstrap_count,
                    self.nodes.len()
                ))
            })?
            .iter()
            .map(|n| {
                MultiAddr::quic(SocketAddr::from((
                    self.config.advertise_ip.unwrap_or(Ipv4Addr::LOCALHOST),
                    n.port,
                )))
            })
            .collect();

        for i in self.config.bootstrap_count..self.config.node_count {
            let node = self.create_node(i, false, bootstrap_addrs.clone()).await?;
            self.start_node(node).await?;
            tokio::time::sleep(self.config.spawn_delay).await;
        }

        info!("All regular nodes started");
        Ok(())
    }

    async fn create_node(
        &self,
        index: usize,
        is_bootstrap: bool,
        bootstrap_addrs: Vec<MultiAddr>,
    ) -> Result<DevnetNode> {
        let index_u16 = u16::try_from(index)
            .map_err(|_| DevnetError::Config(format!("Node index {index} exceeds u16::MAX")))?;
        let port = self.config.base_port + index_u16;

        // Generate identity first so we can use peer_id as the directory name
        let identity = NodeIdentity::generate()
            .map_err(|e| DevnetError::Core(format!("Failed to generate node identity: {e}")))?;
        let peer_id = *identity.peer_id();
        let label = format!("devnet_node_{index}");
        let data_dir = self
            .config
            .data_dir
            .join(NODES_SUBDIR)
            .join(peer_id.to_hex());

        tokio::fs::create_dir_all(&data_dir).await?;

        identity
            .save_to_file(&data_dir.join(NODE_IDENTITY_FILENAME))
            .await
            .map_err(|e| DevnetError::Core(format!("Failed to save node identity: {e}")))?;

        let ant_protocol = Self::create_ant_protocol(&data_dir, &identity, &self.config).await?;

        Ok(DevnetNode {
            index,
            label,
            peer_id,
            port,
            data_dir,
            p2p_node: None,
            ant_protocol: Some(Arc::new(ant_protocol)),
            is_bootstrap,
            state: Arc::new(RwLock::new(NodeState::Pending)),
            bootstrap_addrs,
            protocol_task: None,
            #[cfg(feature = "webrtc-direct")]
            webrtc_direct_task: None,
            #[cfg(feature = "webrtc-direct")]
            webrtc_diagnostics: None,
            #[cfg(feature = "webrtc-direct")]
            browser_endpoint: None,
        })
    }

    async fn create_ant_protocol(
        data_dir: &std::path::Path,
        identity: &NodeIdentity,
        config: &DevnetConfig,
    ) -> Result<AntProtocol> {
        let storage_config = ChunkStoreConfig {
            root_dir: data_dir.to_path_buf(),
            verify_on_read: true,
            ..ChunkStoreConfig::default()
        };
        let storage = ChunkStore::new(storage_config)
            .await
            .map_err(|e| DevnetError::Core(format!("Failed to create the chunk store: {e}")))?;

        let evm_config = EvmVerifierConfig {
            network: config
                .evm_network
                .clone()
                .unwrap_or(EvmNetwork::ArbitrumOne),
        };

        let rewards_address = RewardsAddress::new(DEVNET_REWARDS_ADDRESS);
        let replication_config = ReplicationConfig::default();
        let payment_config = PaymentVerifierConfig {
            evm: evm_config,
            cache_capacity: DEVNET_PAYMENT_CACHE_CAPACITY,
            close_group_size: replication_config.close_group_size,
            local_rewards_address: rewards_address,
            price_floor: PriceFloorConfig::default(),
        };
        let payment_verifier = PaymentVerifier::new(payment_config);
        let metrics_tracker = QuotingMetricsTracker::new(DEVNET_INITIAL_RECORDS);
        let mut quote_generator = QuoteGenerator::new(rewards_address, metrics_tracker);

        // Wire ML-DSA-65 signing from the devnet node's identity
        crate::payment::wire_ml_dsa_signer(&mut quote_generator, identity)
            .map_err(|e| DevnetError::Startup(format!("Failed to wire ML-DSA-65 signer: {e}")))?;

        let storage = Arc::new(storage);
        let payment_verifier = Arc::new(payment_verifier);

        // Same pointer wiring as a production node, so a devnet exercises the
        // real path rather than a node that silently refuses every pointer.
        let pointer_store = crate::pointer::PointerStore::new(storage.root_dir())
            .await
            .map_err(|e| DevnetError::Startup(format!("Failed to open pointer store: {e}")))?;
        let pointers = crate::pointer::PointerService::new(pointer_store)
            .with_chunk_store(Arc::clone(&storage))
            .with_payments(Arc::clone(&payment_verifier));

        Ok(
            AntProtocol::new(storage, payment_verifier, Arc::new(quote_generator))
                .with_pointer_service(pointers),
        )
    }

    #[allow(clippy::too_many_lines)]
    async fn start_node(&mut self, mut node: DevnetNode) -> Result<()> {
        debug!("Starting node {} on port {}", node.index, node.port);
        *node.state.write().await = NodeState::Starting;

        let mut core_config = CoreNodeConfig::builder()
            .port(node.port)
            .local(self.config.advertise_ip.is_none())
            .max_message_size(crate::ant_protocol::MAX_WIRE_MESSAGE_SIZE)
            .build()
            .map_err(|e| DevnetError::Core(format!("Failed to create core config: {e}")))?;

        // Load the node identity for app-level message signing.
        let identity = NodeIdentity::load_from_file(
            &node.data_dir.join(crate::config::NODE_IDENTITY_FILENAME),
        )
        .await
        .map_err(|e| DevnetError::Core(format!("Failed to load node identity: {e}")))?;

        core_config.node_identity = Some(Arc::new(identity));
        core_config
            .bootstrap_peers
            .clone_from(&node.bootstrap_addrs);
        core_config.diversity_config = Some(IPDiversityConfig::permissive());

        let index = node.index;
        let p2p_node = P2PNode::new(core_config)
            .await
            .map_err(|e| DevnetError::Startup(format!("Failed to create node {index}: {e}")))?;

        p2p_node
            .start()
            .await
            .map_err(|e| DevnetError::Startup(format!("Failed to start node {index}: {e}")))?;

        node.p2p_node = Some(Arc::new(p2p_node));
        *node.state.write().await = NodeState::Running;

        #[cfg(feature = "webrtc-direct")]
        if self.config.webrtc_direct {
            let index_u16 = u16::try_from(node.index).map_err(|_| {
                DevnetError::Config(format!("Node index {} exceeds u16::MAX", node.index))
            })?;
            let port = self
                .config
                .webrtc_direct_base_port
                .checked_add(index_u16)
                .ok_or_else(|| {
                    DevnetError::Config(format!(
                        "WebRtcDirect port overflow for node {}",
                        node.index
                    ))
                })?;
            let advertised_ip = self.config.advertise_ip.unwrap_or(Ipv4Addr::LOCALHOST);
            let bind_ip = self
                .config
                .advertise_ip
                .map_or(Ipv4Addr::LOCALHOST, |_| Ipv4Addr::UNSPECIFIED);
            let webrtc_direct_config = WebRtcDirectConfig {
                enabled: true,
                bind: SocketAddr::from((bind_ip, port)),
                advertised_addr: Some(SocketAddr::from((advertised_ip, port))),
                ..WebRtcDirectConfig::default()
            };

            let p2p = node.p2p_node.clone().ok_or_else(|| {
                DevnetError::Startup(format!(
                    "Node {} lost its P2P handle before WebRtcDirect startup",
                    node.index
                ))
            })?;
            let server = crate::web_rtc::spawn(
                &webrtc_direct_config,
                &node.data_dir,
                p2p,
                node.ant_protocol.clone(),
                self.config
                    .evm_network
                    .as_ref()
                    .unwrap_or(&EvmNetwork::ArbitrumOne),
                self.shutdown.clone(),
                Some(Arc::clone(&self.browser_endpoint_catalog)),
            )
            .await
            .map_err(|error| {
                DevnetError::Startup(format!(
                    "Failed to start node {} WebRtcDirect listener: {error}",
                    node.index
                ))
            })?;
            node.browser_endpoint = server.endpoint;
            node.webrtc_diagnostics = Some(server.diagnostics);
            node.webrtc_direct_task = Some(server.task);
        }

        if let (Some(ref p2p), Some(ref protocol)) = (&node.p2p_node, &node.ant_protocol) {
            // Wire P2P into AntProtocol for payment-proof closeness checks.
            protocol.attach_p2p_node(Arc::clone(p2p));

            let mut events = p2p.subscribe_events();
            let p2p_clone = Arc::clone(p2p);
            let protocol_clone = Arc::clone(protocol);
            let node_index = node.index;
            node.protocol_task = Some(tokio::spawn(async move {
                while let Ok(event) = events.recv().await {
                    if let P2PEvent::Message {
                        topic,
                        source: Some(source),
                        data,
                        ..
                    } = event
                    {
                        if topic == CHUNK_PROTOCOL_ID {
                            debug!(
                                "Node {node_index} received chunk protocol message from {source}"
                            );
                            let protocol = Arc::clone(&protocol_clone);
                            let p2p = Arc::clone(&p2p_clone);
                            let received_at = Instant::now();
                            tokio::spawn(handle_chunk_message(
                                protocol,
                                p2p,
                                source,
                                data,
                                node_index,
                                received_at,
                            ));
                        }
                    }
                }
            }));
        }

        debug!("Node {} started successfully", node.index);
        self.nodes.push(node);
        Ok(())
    }

    async fn wait_for_nodes_ready(&self, range: std::ops::Range<usize>) -> Result<()> {
        let deadline = Instant::now() + self.config.node_startup_timeout;

        for i in range {
            while Instant::now() < deadline {
                let node = self.nodes.get(i).ok_or_else(|| {
                    DevnetError::Config(format!(
                        "Node index {i} out of bounds (len: {})",
                        self.nodes.len()
                    ))
                })?;
                let state = node.state.read().await.clone();
                match state {
                    NodeState::Running | NodeState::Connected => break,
                    NodeState::Failed(ref e) => {
                        return Err(DevnetError::Startup(format!("Node {i} failed: {e}")));
                    }
                    _ => {
                        tokio::time::sleep(Duration::from_millis(NODE_READY_POLL_INTERVAL_MS))
                            .await;
                    }
                }
            }
        }
        Ok(())
    }

    async fn wait_for_stabilization(&self) -> Result<()> {
        let deadline = Instant::now() + self.config.stabilization_timeout;
        let min_connections = self
            .config
            .bootstrap_count
            .min(STABILIZATION_MIN_CONNECTIONS_CAP);

        info!(
            "Waiting for devnet stabilization (min {} connections per node)",
            min_connections
        );

        while Instant::now() < deadline {
            let mut all_connected = true;
            let mut total_connections = 0;

            for node in &self.nodes {
                let peer_count = node.peer_count().await;
                total_connections += peer_count;

                if peer_count < min_connections {
                    all_connected = false;
                }
            }

            if all_connected {
                info!("Devnet stabilized: {} total connections", total_connections);
                return Ok(());
            }

            debug!(
                "Waiting for stabilization: {} total connections",
                total_connections
            );
            tokio::time::sleep(Duration::from_secs(STABILIZATION_POLL_INTERVAL_SECS)).await;
        }

        Err(DevnetError::Stabilization(
            "Devnet failed to stabilize within timeout".to_string(),
        ))
    }

    fn start_health_monitor(&mut self) {
        let nodes: Vec<Arc<P2PNode>> = self
            .nodes
            .iter()
            .filter_map(|n| n.p2p_node.clone())
            .collect();
        let shutdown = self.shutdown.clone();

        self.health_monitor = Some(tokio::spawn(async move {
            let check_interval = Duration::from_secs(HEALTH_CHECK_INTERVAL_SECS);

            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(check_interval) => {
                        for (i, node) in nodes.iter().enumerate() {
                            if !node.is_running() {
                                warn!("Node {} appears unhealthy", i);
                            }
                        }
                    }
                }
            }
        }));
    }
}

impl Drop for Devnet {
    fn drop(&mut self) {
        self.shutdown.cancel();
        if let Some(handle) = self.health_monitor.take() {
            handle.abort();
        }
    }
}

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

    /// Backward-compatibility contract: a default config advertises no LAN IP,
    /// so nodes bind loopback (`.local(advertise_ip.is_none())` → `.local(true)`)
    /// and stamp `127.0.0.1` into the manifest bootstrap addresses — exactly as
    /// before the `--host` flag existed.
    #[test]
    fn default_config_has_no_advertise_ip() {
        assert!(DevnetConfig::default().advertise_ip.is_none());
    }
}