Skip to content

Latest commit

 

History

History
95 lines (76 loc) · 2.14 KB

ant-node.md

File metadata and controls

95 lines (76 loc) · 2.14 KB

Node API

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

Installation

{% tabs %} {% tab title="Rust" %}

cargo add ant-node

{% endtab %}

{% tab title="Python" %}

# Install using uv (recommended)
curl -LsSf <https://astral.sh/uv/install.sh> | sh
uv pip install maturin
uv pip install antnode

# Or using pip
pip install antnode.

{% endtab %} {% endtabs %}

Start a Single Node

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

{% tabs %} {% tab title="Rust" %}

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:?}");
}

{% endtab %} {% endtabs %}

Start a Network

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

{% tabs %} {% tab title="Rust" %}

use ant_node::spawn::network_spawner::NetworkSpawner;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let network_size = 20;

    let running_network = NetworkSpawner::new()
        .with_evm_network(Default::default())
        .with_local(true)
        .with_size(network_size)
        .spawn()
        .await
        .expect("Failed to spawn network");

    // Wait for nodes to dial each other
    sleep(Duration::from_secs(10)).await;

    for node in running_network.running_nodes() {
        println!("Node listening on: {:?}", node.get_listen_addrs().await);
    }
}

{% endtab %} {% endtabs %}