A library to build custom networking layers for decentralized and distributed applications
SwarmNL is a library designed for P2P networking in distributed systems. It's lightweight, scalable, and easy to configure, making it perfect for decentralized applications. Powered by libp2p, SwarmNL simplifies networking so developers can focus on building.
Visit the deployed Rust docs here.
SwarmNL makes buiding a peer-to-peer decentralized and distributed networking stack for your application a breeze. With SwarmNL, you can effortlessly configure nodes, tailor network conditions, and fine-tune behaviors specific to your project's needs, allowing you to dive into networking without any hassle.
Say goodbye to the complexities of networking and hello to simplicity. With SwarmNL, all the hard work is done for you, leaving you to focus on simple configurations and your application logic.
Have a look at these step-by-step examples that demonstrate the use of SwarmNL in various contexts:
Visit the examples folder here to gain a fuller understanding on ways to use the library, including how to integrate SwarmNL with IPFS and HTTP servers.
Have a look at this document for a technical overview of SwarmNL and it's design choices.
SwarmNL provides a simple interface to configure a node and specify parameters to dictate its behaviour. This includes:
#![cfg_attr(not(doctest))]
//! Using the default node setup configuration
// Default config
let config = BootstrapConfig::default();
// Build node or network core
let node = CoreBuilder::with_config(config)
.build()
.await
.unwrap();
//! Using a custom node setup configuration
// Custom configuration
// a. Using config from an `.ini` file
let config = BootstrapConfig::from_file("bootstrap_config.ini");
// b. Using config methods
let mut bootnode = HashMap::new(); // Bootnodes
let ports = (1509, 2710); // TCP, UDP ports
bootnode.insert(
PeerId::random(),
"/ip4/x.x.x.x/tcp/1509".to_string()
);
let config = BootstrapConfig::new()
.with_bootnodes(bootnode)
.with_tcp(ports.0)
.with_udp(ports.1);
// Build node or network core
let node = CoreBuilder::with_config(config)
.build()
.await
.unwrap();
Please look at a template .ini file here for configuring a node in the network.
During network operations, various events are generated. These events help us track the activities in the network layer. When generated, they are stored in an internal buffer until they are explicitly polled and consumed, or until the queue is full. It is important to consume critical events promptly to prevent loss if the buffer becomes full.
#![cfg_attr(not(doctest))]
//! Consuming the events by retrieving it as a iterator
// Default config
let config = BootstrapConfig::default();
// Build node or network core
let node = CoreBuilder::with_config(config)
.build()
.await
.unwrap();
// Read all currently buffered network events
let events = node.events().await;
let _ = events
.map(|e| {
match e {
NetworkEvent::NewListenAddr {
local_peer_id,
listener_id: _,
address,
} => {
// Announce interfaces we're listening on
println!("Peer id: {}", local_peer_id);
println!("We're listening on the {}", address);
},
NetworkEvent::ConnectionEstablished {
peer_id,
connection_id: _,
endpoint: _,
num_established: _,
established_in: _,
} => {
println!("Connection established with peer: {:?}", peer_id);
},
_ => {},
}
})
.collect::<Vec<_>>();
//! Consume the immediate next events in the internal event buffer
// Read events generated at setup
while let Some(event) = node.next_event().await {
match event {
NetworkEvent::NewListenAddr {
local_peer_id,
listener_id: _,
address,
} => {
// announce interfaces we're listening on
println!("Peer id: {}", local_peer_id);
println!("We're listening on the {}", address);
},
NetworkEvent::ConnectionEstablished {
peer_id,
connection_id: _,
endpoint: _,
num_established: _,
established_in: _,
} => {
println!("Connection established with peer: {:?}", peer_id);
},
_ => {},
}
}
For communication, SwarmNL leverages the powerful capabilities of libp2p. These includes:
#![cfg_attr(not(doctest))]
//! Communicate with remote nodes using the simple and familiar async-await paradigm.
// Build node or network core
let node = CoreBuilder::with_config(config, state)
.build()
.await
.unwrap();
// Communication interfaces
// a. Kademlia DHT e.g
// Prepare an kademlia `store_record` request to send to the network layer
let (key, value, expiration_time, explicit_peers) = (
KADEMLIA_TEST_KEY.as_bytes().to_vec(),
KADEMLIA_TEST_VALUE.as_bytes().to_vec(),
None,
None,
);
let kad_request = AppData::KademliaStoreRecord {
key: key.clone(),
value,
expiration_time,
explicit_peers,
};
// Send request
if let Ok(result) = node.query_network(kad_request).await {
assert_eq!(KademliaStoreRecordSuccess,result);
}
// b. RPC (request-response) e.g
// Prepare a RPC fetch request
let fetch_key = vec!["SomeFetchKey".as_bytes().to_vec()];
let fetch_request = AppData::SendRpc {
keys: fetch_key.clone(),
peer: node4_peer_id,
};
// Get a stream id to track the request
let stream_id = node.send_to_network(fetch_request).await.unwrap();
// Poll for the result
if let Ok(result) = node.recv_from_network(stream_id).await {
// Here, the request data was simply echoed by the remote peer
assert_eq!(AppResponse::SendRpc(fetch_key), result);
}
// c. Gossiping e.g
// Prepare gossip request
let gossip_request = AppData::GossipsubBroadcastMessage {
topic: GOSSIP_NETWORK.to_string(),
message: vec!["Daniel".to_string(), "Deborah".to_string()],
};
if let Ok(result) = node.query_network(gossip_request).await {
assert_eq!(AppResponse::GossipsubBroadcastSuccess, result);
}
SwarmNL makes fault tolerance through redundancy simple and easy to integrate into your application. With replication built into SwarmNL, you can achieve robust and scalable systems effortlessly.
Here’s how you can set up and use SwarmNL's replication capabilities:
#![cfg_attr(not(doctest))]
//! Configure the node for replication with a strong consistency model
// Define the replica network ID
const REPL_NETWORK_ID: &str = "replica_xx";
// Configure replication settings
let repl_config = ReplNetworkConfig::Custom {
queue_length: 150,
expiry_time: Some(10),
sync_wait_time: 5,
consistency_model: ConsistencyModel::Strong(ConsensusModel::All),
data_aging_period: 2,
};
// Build the node with replication enabled
let node = builder.with_replication(repl_config).build().await.unwrap();
// Join a replica network
node.join_repl_network(REPL_NETWORK_ID.into()).await;
// Replicate data across the network
node.replicate(payload, REPL_NETWORK_ID).await;
SwarmNL exposes network events to your application, allowing you to process incoming replica data effectively.
//! Listen for replication events
loop {
// Check for incoming data events
if let Some(event) = node.next_event().await {
if let NetworkEvent::ReplicaDataIncoming { source, .. } = event {
println!("Received incoming replica data from {}", source.to_base58());
}
}
// Try to consume data from the replication buffer
if let Some(repl_data) = node.consume_repl_data(REPL_NETWORK_ID).await {
println!(
"Data received from replica: {} ({} confirmations)",
repl_data.data[0],
repl_data.confirmations.unwrap()
);
}
}
Sharding is a capability in distributed systems that enables networks to scale efficiently. SwarmNL provides a generic sharding functionality, allowing applications to easily partition their network and configure it for sharding.
trap into the application layer when handling data requests. This ensures practicality and usability in real-world scenarios.Here’s how you can set up and use SwarmNL's sharding capabilities:
```rust #![cfg_
$ claude mcp add SwarmNL \
-- python -m otcore.mcp_server <graph>