Ant Node API Reference
The Ant Node provides a comprehensive API for running and managing nodes in the Autonomi network. This documentation covers both the Python bindings and the Rust implementation.
Installation
Basic Usage
from antnode import AntNode
# Create and start a node
node = AntNode()
node.run(
rewards_address="0x1234567890123456789012345678901234567890", # Your EVM wallet address
evm_network="arbitrum_sepolia", # or "arbitrum_one" for mainnet
ip="0.0.0.0",
port=12000,
initial_peers=[
"/ip4/142.93.37.4/udp/40184/quic-v1/p2p/12D3KooWPC8q7QGZsmuTtCYxZ2s3FPXPZcS8LVKkayXkVFkqDEQB",
],
local=False,
root_dir=None, # Uses default directory
home_network=False
)
use ant_node::{NodeBuilder, NodeEvent};
use ant_evm::RewardsAddress;
use libp2p::Multiaddr;
// Create and start a node
let node = NodeBuilder::new()
.rewards_address(rewards_address)
.evm_network(evm_network)
.ip(ip)
.port(port)
.initial_peers(initial_peers)
.local(false)
.root_dir(None)
.home_network(false)
.build()?;
Core Features
Node Information
# Get node's peer ID
peer_id = node.peer_id()
# Get current rewards address
address = node.get_rewards_address()
# Get routing table information
kbuckets = node.get_kbuckets()
for distance, peers in kbuckets:
print(f"Distance {distance}: {len(peers)} peers")
# Get all stored record addresses
records = node.get_all_record_addresses()
// Get node's peer ID
let peer_id = node.peer_id();
// Get current rewards address
let address = node.rewards_address();
// Get routing table information
let kbuckets = node.get_kbuckets()?;
for (distance, peers) in kbuckets {
println!("Distance {}: {} peers", distance, peers.len());
}
// Get all stored record addresses
let records = node.get_all_record_addresses()?;
Storage Operations
use ant_protocol::storage::ValidationType;
// Store data
let key = "0123456789abcdef"; // Hex string
let value = b"Hello, World!";
node.store_record(key, value, ValidationType::Chunk)?;
// Retrieve data
let data = node.get_record(key)?;
// Delete data
let success = node.delete_record(key)?;
// Get total storage size
let size = node.get_stored_records_size()?;
Directory Management
Event Handling
use ant_node::{NodeEvent, NodeEventsReceiver};
// Get event receiver
let mut events: NodeEventsReceiver = node.event_receiver();
// Handle events
while let Ok(event) = events.recv().await {
match event {
NodeEvent::ConnectedToNetwork => println!("Connected to network"),
NodeEvent::ChunkStored(addr) => println!("Chunk stored: {}", addr),
NodeEvent::RegisterCreated(addr) => println!("Register created: {}", addr),
NodeEvent::RegisterEdited(addr) => println!("Register edited: {}", addr),
NodeEvent::RewardReceived(amount, addr) => {
println!("Reward received: {} at {}", amount, addr)
}
NodeEvent::ChannelClosed => break,
NodeEvent::TerminateNode(reason) => {
println!("Node terminated: {}", reason);
break;
}
}
}
Configuration Options
Node Configuration
rewards_address
: EVM wallet address for receiving rewardsevm_network
: Network to use ("arbitrum_sepolia" or "arbitrum_one")ip
: IP address to listen onport
: Port to listen oninitial_peers
: List of initial peers to connect tolocal
: Whether to run in local moderoot_dir
: Custom root directory pathhome_network
: Whether the node is behind NAT
Network Types
arbitrum_sepolia
: Test networkarbitrum_one
: Main network
Error Handling
Best Practices
- Error Handling
- Always handle potential errors appropriately
- Implement retry logic for network operations
-
Log errors for debugging
-
Resource Management
- Monitor storage usage
- Clean up unused records
-
Handle events promptly
-
Network Operations
- Use appropriate timeouts
- Handle network disconnections
-
Maintain peer connections
-
Security
- Validate input data
- Secure storage of keys
- Regular backups of important data