ant-node 0.20.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
//! ant-devnet CLI entry point.
//!
//! Runs a local devnet for testing. By default all nodes bind loopback
//! (`127.0.0.1`), so only the host machine can reach them.
//!
//! Three opt-in flags extend this to a **LAN / external-devnet** scenario for
//! testing from another device (a phone, a simulator, a second machine). All
//! default off — omitting them reproduces the original loopback behavior:
//!
//! - `--host <ipv4>`: bind `0.0.0.0` and advertise this LAN IP in the manifest's
//!   bootstrap addresses, so other devices on the LAN can connect.
//! - `--evm-network arbitrum-sepolia`: verify payments against the real deployed
//!   Arbitrum Sepolia contracts (no local Anvil, empty wallet key) — exercises
//!   the external-signer payment flow.
//! - `--serve-port <port>`: expose the manifest over a small read-only HTTP API
//!   (`GET /api/devnet-manifest.json` + `/api/info`) so devices fetch it instead
//!   of copying files.
//!
//! ```text
//! # single-machine, local Anvil (unchanged default behavior)
//! ant-devnet --preset small --enable-evm
//!
//! # LAN devnet backed by Arbitrum Sepolia, manifest served over HTTP
//! ant-devnet --preset small --host 192.168.1.100 \
//!     --evm-network arbitrum-sepolia --serve-port 25000
//! ```

#![cfg_attr(not(feature = "logging"), allow(unused_variables))]

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

mod cli;

use ant_node::devnet::{Devnet, DevnetConfig, DevnetEvmInfo, DevnetManifest};
use ant_node::BrowserDevnetManifest;
use clap::Parser;
use cli::Cli;

#[tokio::main]
#[allow(clippy::too_many_lines)]
async fn main() -> color_eyre::Result<()> {
    color_eyre::install()?;

    let cli = Cli::parse();

    #[cfg(feature = "logging")]
    if cli.enable_logging {
        use tracing_subscriber::{fmt, prelude::*, EnvFilter};

        let filter =
            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&cli.log_level));

        tracing_subscriber::registry()
            .with(fmt::layer())
            .with(filter)
            .init();
    }

    ant_node::logging::info!("ant-devnet v{}", env!("CARGO_PKG_VERSION"));

    let mut config =
        cli.preset
            .as_deref()
            .map_or_else(DevnetConfig::default, |preset| match preset {
                "minimal" => DevnetConfig::minimal(),
                "small" => DevnetConfig::small(),
                _ => DevnetConfig::default(),
            });

    if let Some(count) = cli.nodes {
        config.node_count = count;
    }
    if let Some(bootstrap) = cli.bootstrap_count {
        config.bootstrap_count = bootstrap;
    }
    if let Some(base_port) = cli.base_port {
        config.base_port = base_port;
    }
    if let Some(dir) = cli.data_dir {
        config.data_dir = dir;
    }
    config.cleanup_data_dir = !cli.no_cleanup;
    if let Some(delay_ms) = cli.spawn_delay_ms {
        config.spawn_delay = std::time::Duration::from_millis(delay_ms);
    }
    if let Some(timeout_secs) = cli.stabilization_timeout_secs {
        config.stabilization_timeout = std::time::Duration::from_secs(timeout_secs);
    }

    #[cfg(not(feature = "webrtc-direct"))]
    if cli.webrtc_direct {
        return Err(color_eyre::eyre::eyre!(
            "--webrtc-direct requires a binary built with --features webrtc-direct"
        ));
    }

    #[cfg(not(feature = "test-utils"))]
    if cli.public_file.is_some() {
        return Err(color_eyre::eyre::eyre!(
            "prepaid public-file seeding requires --features test-utils"
        ));
    }

    // A non-unicast --host would stamp unreachable bootstrap addresses into the
    // manifest (LAN mode would fail non-obviously), so reject it early.
    if let Some(host) = cli
        .host
        .filter(|h| h.is_loopback() || h.is_unspecified() || h.is_multicast() || h.is_broadcast())
    {
        return Err(color_eyre::eyre::eyre!(
            "--host must be a routable unicast LAN IPv4 (got {host}); \
             loopback/unspecified/multicast/broadcast are not reachable bootstrap addresses"
        ));
    }
    config.advertise_ip = cli.host;
    config.webrtc_direct = cli.webrtc_direct;
    if let Some(base_port) = cli.webrtc_direct_base_port {
        config.webrtc_direct_base_port = base_port;
    }
    let ResolvedEvm {
        manifest: evm_info,
        local_testnet: _local_evm_testnet,
    } = resolve_evm_info(
        cli.evm_network.as_deref(),
        cli.enable_evm,
        cli.host,
        &mut config,
    )
    .await?;

    let mut devnet = Devnet::new(config).await?;
    devnet.start().await?;

    let created_at = chrono::Utc::now().to_rfc3339();

    #[cfg(feature = "webrtc-direct")]
    let browser_manifest = if cli.webrtc_direct {
        #[cfg(feature = "test-utils")]
        let files = {
            let (name, content_type, content) =
                load_public_file(cli.public_file.as_deref()).await?;
            vec![
                devnet
                    .publish_public_file(name, content_type, &content)
                    .await?,
            ]
        };
        #[cfg(not(feature = "test-utils"))]
        let files = Vec::new();
        let network_id = format!("local-devnet-{}-{}", devnet.config().base_port, created_at);
        Some(BrowserDevnetManifest::new(
            network_id,
            created_at.clone(),
            devnet.browser_endpoints(),
            devnet.browser_payment_network().await?,
            files,
        ))
    } else {
        None
    };

    #[cfg(not(feature = "webrtc-direct"))]
    let browser_manifest: Option<BrowserDevnetManifest> = None;

    let manifest = DevnetManifest {
        base_port: devnet.config().base_port,
        node_count: devnet.config().node_count,
        bootstrap: devnet.bootstrap_addrs(),
        data_dir: devnet.config().data_dir.clone(),
        created_at,
        evm: evm_info,
    };

    let mut manifest_value = serde_json::to_value(&manifest)?;
    if let Some(browser) = &browser_manifest {
        let bootstrap_webrtc: Vec<&str> = browser
            .endpoints
            .iter()
            .map(|node| node.endpoint.multiaddr.as_str())
            .collect();
        manifest_value["bootstrap_webrtc"] = serde_json::to_value(bootstrap_webrtc)?;
    }
    let json = serde_json::to_string_pretty(&manifest_value)?;
    let browser_json = browser_manifest
        .as_ref()
        .map(serde_json::to_string_pretty)
        .transpose()?;
    if let Some(path) = cli.manifest {
        tokio::fs::write(&path, &json).await?;
        ant_node::logging::info!("Wrote manifest to {}", path.display());
    } else {
        println!("{json}");
    }

    // Optional read-only HTTP API so LAN devices fetch the manifest instead of
    // copying files (GET /api/devnet-manifest.json + /api/info).
    let serve_port = cli
        .serve_port
        .or_else(|| cli.webrtc_direct.then_some(25_000));
    if let Some(port) = serve_port {
        serve_manifest_api(
            port,
            cli.host,
            &manifest,
            json.clone(),
            browser_manifest.as_ref(),
            browser_json,
        )?;
    }

    ant_node::logging::info!("Devnet running. Press Ctrl+C to stop.");
    tokio::signal::ctrl_c().await?;

    devnet.shutdown().await?;
    Ok(())
}

#[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
async fn load_public_file(
    path: Option<&std::path::Path>,
) -> color_eyre::Result<(String, String, Vec<u8>)> {
    const DEFAULT_NAME: &str = "autonomi-browser-testnet.txt";
    const DEFAULT_SEED: &[u8] = include_bytes!("../../../assets/browser-devnet-public.txt");
    const DEFAULT_SIZE: usize = 5 * 1024 * 1024;
    const MAX_FILE_SIZE: u64 = 1_000_000_000;

    let Some(path) = path else {
        let mut content = Vec::with_capacity(DEFAULT_SIZE);
        while content.len() < DEFAULT_SIZE {
            content.extend_from_slice(DEFAULT_SEED);
        }
        content.truncate(DEFAULT_SIZE);
        return Ok((
            DEFAULT_NAME.to_string(),
            "text/plain; charset=utf-8".to_string(),
            content,
        ));
    };

    let name = path
        .file_name()
        .and_then(std::ffi::OsStr::to_str)
        .filter(|name| !name.is_empty())
        .ok_or_else(|| {
            color_eyre::eyre::eyre!(
                "--public-file must identify a file with a valid UTF-8 filename"
            )
        })?
        .to_string();
    let file_size = tokio::fs::metadata(path)
        .await
        .map_err(|error| {
            color_eyre::eyre::eyre!("failed to inspect public file {}: {error}", path.display())
        })?
        .len();
    if file_size > MAX_FILE_SIZE {
        return Err(color_eyre::eyre::eyre!(
            "--public-file is {file_size} bytes; the browser devnet limit is {MAX_FILE_SIZE} bytes"
        ));
    }
    let content = tokio::fs::read(path).await.map_err(|error| {
        color_eyre::eyre::eyre!("failed to read public file {}: {error}", path.display())
    })?;
    let content_type = match path
        .extension()
        .and_then(std::ffi::OsStr::to_str)
        .map(str::to_ascii_lowercase)
        .as_deref()
    {
        Some("txt" | "md" | "csv") => "text/plain; charset=utf-8",
        Some("json") => "application/json",
        Some("html" | "htm") => "text/html; charset=utf-8",
        Some("png") => "image/png",
        Some("jpg" | "jpeg") => "image/jpeg",
        Some("pdf") => "application/pdf",
        _ => "application/octet-stream",
    }
    .to_string();

    Ok((name, content_type, content))
}

/// Resolve which EVM backing the devnet uses, updating `config` accordingly:
/// an **external** network (`--evm-network`, e.g. Arbitrum Sepolia verified
/// against the real deployed contracts, no embedded wallet key); a **local
/// Anvil** chain (`--enable-evm`); or **none**. External takes precedence.
struct ResolvedEvm {
    manifest: Option<DevnetEvmInfo>,
    // Retain ownership until main exits so the Anvil child is terminated on
    // normal shutdown instead of being orphaned.
    local_testnet: Option<evmlib::testnet::Testnet>,
}

async fn resolve_evm_info(
    evm_network: Option<&str>,
    enable_evm: bool,
    host: Option<std::net::Ipv4Addr>,
    config: &mut DevnetConfig,
) -> color_eyre::Result<ResolvedEvm> {
    if let Some(net_name) = evm_network {
        let network = match net_name {
            "arbitrum-sepolia" => evmlib::Network::ArbitrumSepoliaTest,
            other => {
                return Err(color_eyre::eyre::eyre!(
                    "Unsupported --evm-network {other} (supported: arbitrum-sepolia)"
                ))
            }
        };
        let rpc_url = network.rpc_url().to_string();
        let token_addr = format!("{:?}", network.payment_token_address());
        let vault_addr = format!("{:?}", network.payment_vault_address());
        ant_node::logging::info!(
            "Using external EVM network {net_name}: rpc={rpc_url} token={token_addr} vault={vault_addr}"
        );
        config.evm_network = Some(network);
        Ok(ResolvedEvm {
            manifest: Some(DevnetEvmInfo {
                rpc_url,
                wallet_private_key: String::new(),
                payment_token_address: token_addr,
                payment_vault_address: vault_addr,
            }),
            local_testnet: None,
        })
    } else if enable_evm {
        // Anvil binds — and evmlib publishes in the manifest's `rpc_url` —
        // the address in `ANVIL_IP_ADDR`, defaulting to localhost. A LAN
        // devnet must expose the chain the way it exposes the nodes, or the
        // published `rpc_url` is unreachable from every other device —
        // exactly the audience `--host` serves (external signers can join
        // and download, but not pay). An explicit `ANVIL_IP_ADDR` wins.
        if let Some(anvil_ip) = anvil_ip_for_lan(host, std::env::var_os("ANVIL_IP_ADDR")) {
            ant_node::logging::info!(
                "Binding Anvil to {anvil_ip} (via ANVIL_IP_ADDR) so LAN clients \
                 can reach the manifest's rpc_url"
            );
            std::env::set_var("ANVIL_IP_ADDR", anvil_ip);
        }
        ant_node::logging::info!("Starting local Anvil blockchain for EVM payment enforcement...");
        let testnet = evmlib::testnet::Testnet::new()
            .await
            .map_err(|e| color_eyre::eyre::eyre!("Failed to start Anvil testnet: {e}"))?;
        let network = testnet.to_network();
        let wallet_key = testnet
            .default_wallet_private_key()
            .map_err(|e| color_eyre::eyre::eyre!("Failed to get wallet key: {e}"))?;

        let (rpc_url, token_addr, vault_addr) = match &network {
            evmlib::Network::Custom(custom) => (
                custom.rpc_url_http.to_string(),
                format!("{:?}", custom.payment_token_address),
                format!("{:?}", custom.payment_vault_address),
            ),
            _ => {
                return Err(color_eyre::eyre::eyre!(
                    "Anvil testnet returned non-Custom network"
                ))
            }
        };

        config.evm_network = Some(network);

        ant_node::logging::info!("Anvil blockchain running at {rpc_url}");
        ant_node::logging::info!("Funded wallet private key: {wallet_key}");

        Ok(ResolvedEvm {
            manifest: Some(DevnetEvmInfo {
                rpc_url,
                wallet_private_key: wallet_key,
                payment_token_address: token_addr,
                payment_vault_address: vault_addr,
            }),
            local_testnet: Some(testnet),
        })
    } else {
        Ok(ResolvedEvm {
            manifest: None,
            local_testnet: None,
        })
    }
}

/// Build the `/api/info` payload for `manifest` and start the read-only HTTP
/// server on `port`. `host` is the advertised LAN IP when set, otherwise a
/// best-effort local-IP guess is used for the URLs it reports.
fn serve_manifest_api(
    port: u16,
    host: Option<std::net::Ipv4Addr>,
    manifest: &DevnetManifest,
    manifest_json: String,
    browser_manifest: Option<&BrowserDevnetManifest>,
    browser_manifest_json: Option<String>,
) -> color_eyre::Result<()> {
    let host_ip = host.map_or_else(|| "127.0.0.1".to_string(), |i| i.to_string());
    let evm_block = manifest.evm.as_ref().map_or(serde_json::Value::Null, |e| {
        let loopback = e.rpc_url.contains("127.0.0.1") || e.rpc_url.contains("localhost");
        serde_json::json!({
            "rpc_url": e.rpc_url,
            "network": if loopback { "local-anvil" } else { "external" },
            "reachable_from_lan": !loopback,
            "note": if loopback {
                format!(
                    "Anvil binds loopback on the host — bridge it (socat \
                     TCP-LISTEN:8545,fork,bind=0.0.0.0 TCP:127.0.0.1:<anvil-port>) \
                     and use http://{host_ip}:8545/."
                )
            } else {
                "Public RPC — reachable directly from any device.".to_string()
            },
        })
    });
    let bootstrap = serde_json::to_value(&manifest.bootstrap)?;
    let browser_manifest_url =
        browser_manifest.map(|_| format!("http://{host_ip}:{port}/api/browser-manifest.json"));
    let public_files = browser_manifest.map_or_else(Vec::new, |browser| browser.files.clone());
    let info = serde_json::json!({
        "host_ip": host_ip,
        "manifest_url": format!("http://{host_ip}:{port}/api/devnet-manifest.json"),
        "browser_manifest_url": browser_manifest_url,
        "node_count": manifest.node_count as u64,
        "bootstrap": bootstrap,
        "public_files": public_files,
        "evm": evm_block,
    });
    let info_json = serde_json::to_string_pretty(&info)?;
    // Bind synchronously so a failure (e.g. the port is already in use)
    // propagates to the caller instead of the devnet silently coming up
    // without its manifest API.
    let bind_ip = host.map_or(std::net::Ipv4Addr::LOCALHOST, |_| {
        std::net::Ipv4Addr::UNSPECIFIED
    });
    let listener = std::net::TcpListener::bind((bind_ip, port)).map_err(|e| {
        color_eyre::eyre::eyre!("failed to bind manifest API on {bind_ip}:{port}: {e}")
    })?;
    ant_node::logging::info!(
        "manifest API on http://{host_ip}:{port}/api/devnet-manifest.json (+ /api/info)"
    );
    if browser_manifest.is_some() {
        ant_node::logging::info!(
            "browser app manifest: http://{host_ip}:{port}/api/browser-manifest.json"
        );
    }
    spawn_manifest_server(listener, manifest_json, info_json, browser_manifest_json);
    Ok(())
}

/// Run a tiny read-only HTTP server on `listener` (its own thread) exposing the
/// manifest over the LAN. GET-only, open CORS; hand-rolled HTTP/1.1 so there's
/// no new dependency. Connections are handled **inline, one at a time** — the
/// payloads are tiny and a devnet serves a handful of LAN devices, so a single
/// thread bounds resource use (no per-connection thread to exhaust). Each
/// connection gets a read timeout so a slow/idle client can't stall the loop.
/// The thread is detached and dies when the process exits on Ctrl+C.
fn spawn_manifest_server(
    listener: std::net::TcpListener,
    manifest_json: String,
    info_json: String,
    browser_manifest_json: Option<String>,
) {
    std::thread::spawn(move || {
        use std::io::{Read, Write};
        for stream in listener.incoming() {
            let Ok(mut stream) = stream else { continue };
            let _ = stream.set_read_timeout(Some(std::time::Duration::from_secs(5)));
            let mut buf = [0u8; 2048];
            let n = stream.read(&mut buf).unwrap_or(0);
            let req = String::from_utf8_lossy(&buf[..n]);
            let mut tokens = req.split_whitespace();
            let method = tokens.next().unwrap_or("");
            let raw = tokens.next().unwrap_or("/");
            let path = raw.split('?').next().unwrap_or("/").trim_end_matches('/');
            // Read-only API — only GET is allowed; anything else is 405.
            let (status, body) = if method == "GET" {
                match path {
                    "/api/devnet-manifest.json" => ("200 OK", manifest_json.as_str()),
                    "/api/browser-manifest.json" => browser_manifest_json.as_deref().map_or(
                        (
                            "404 Not Found",
                            "{\"error\":\"browser manifest not enabled\"}",
                        ),
                        |body| ("200 OK", body),
                    ),
                    "/api/info" => ("200 OK", info_json.as_str()),
                    "" | "/api" => (
                        "200 OK",
                        "{\"service\":\"ant-devnet manifest API\",\
                         \"endpoints\":[\"/api/devnet-manifest.json\",\
                         \"/api/browser-manifest.json\",\"/api/info\"]}",
                    ),
                    _ => ("404 Not Found", "{\"error\":\"not found\"}"),
                }
            } else {
                (
                    "405 Method Not Allowed",
                    "{\"error\":\"method not allowed\"}",
                )
            };
            let resp = format!(
                "HTTP/1.1 {status}\r\nContent-Type: application/json\r\n\
                 Access-Control-Allow-Origin: *\r\nCache-Control: no-store\r\n\
                 Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
                body.len()
            );
            let _ = stream.write_all(resp.as_bytes());
        }
    });
}

/// The Anvil bind/publish address a LAN devnet should use: the `--host` IP,
/// unless the operator set `ANVIL_IP_ADDR` explicitly (their override wins),
/// or there is no `--host` (loopback devnet — keep Anvil's localhost
/// default). Returns the value to write into `ANVIL_IP_ADDR`, or `None` to
/// leave the environment untouched.
fn anvil_ip_for_lan(
    host: Option<std::net::Ipv4Addr>,
    existing_override: Option<std::ffi::OsString>,
) -> Option<String> {
    match (host, existing_override) {
        (Some(host), None) => Some(host.to_string()),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::anvil_ip_for_lan;
    use std::net::Ipv4Addr;

    #[test]
    fn lan_host_becomes_anvil_ip() {
        assert_eq!(
            anvil_ip_for_lan(Some(Ipv4Addr::new(192, 168, 0, 61)), None),
            Some("192.168.0.61".to_string())
        );
    }

    #[test]
    fn explicit_override_wins() {
        assert_eq!(
            anvil_ip_for_lan(
                Some(Ipv4Addr::new(192, 168, 0, 61)),
                Some("10.0.0.9".into())
            ),
            None
        );
    }

    #[test]
    fn loopback_devnet_keeps_anvil_default() {
        assert_eq!(anvil_ip_for_lan(None, None), None);
        assert_eq!(anvil_ip_for_lan(None, Some("10.0.0.9".into())), None);
    }
}