Node API

The antnode crate provides an API for programmatically running a node.

Installation

cargo add ant-node

Start a Single Node

use NodeSpawner to programmatically spawn a single node and connect it to the live network.

use ant_node::spawn::node_spawner::NodeSpawner;
use autonomi::Multiaddr;
use std::str::FromStr;

#[tokio::main]
async fn main() {
    // Using the genesis node as a bootstrap node for example
    let bootstrap_peers = vec![
        Multiaddr::from_str("/ip4/209.97.181.193/udp/57402/quic-v1/p2p/12D3KooWHygG9a7inESky2KpvHQmbX5o2UC8D29B5njdshAcv1p6")
            .expect("Invalid multiaddr")
    ];

    let running_node = NodeSpawner::new()
        .with_initial_peers(bootstrap_peers)
        .spawn()
        .await
        .expect("Failed to spawn node");

    let listen_addrs = running_node
        .get_listen_addrs_with_peer_id()
        .await
        .expect("Failed to get listen addrs with peer id");

    println!("Node started with listen addrs: {listen_addrs:?}");
}

Start a Network

Use NetworkSpawner to programmatically start a node network (very useful for automated testing).

Last updated