A library to build custom networking layers for decentralized 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.
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 some tutorials that demonstrate the use of SwarmNl in various contexts:
Visit the deployed Rust docs here.
SwarmNL provides a simple interface to configure a node and specify parameters to dictate its behaviour. This includes:
//! 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.
//! 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:
//! 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::FetchData {
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::FetchData(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);
}
In Development 👷:
In essence, SwarmNL is designed to simplify networking so you can focus on building that world-changing application of yours! Cheers! 🥂