Crates.io | atm0s-small-p2p |
lib.rs | atm0s-small-p2p |
version | 0.2.1 |
source | src |
created_at | 2024-10-23 03:12:18.098178 |
updated_at | 2024-11-26 10:49:06.881999 |
description | A lightweight peer-to-peer network utilizing the atm0s routing mechanism, built entirely with async programming and QUIC (using the quinn library). |
homepage | |
repository | |
max_upload_size | |
id | 1419623 |
size | 261,579 |
A lightweight peer-to-peer (P2P) network utilizing the atm0s routing mechanism, built entirely with asynchronous programming and QUIC (using the quinn library). This project aims to provide a robust and efficient framework for P2P communication.
The architecture of the P2P network is designed to facilitate efficient communication between peers. Key components include:
PeerDiscovery
module manages the discovery of peers in the network, allowing nodes to find and connect to each other.RouterTable
manages the routing of messages between peers, ensuring that data is sent through the most efficient paths.SharedKeyHandshake
protocol ensures that connections between peers are secure, using cryptographic techniques to verify identities and protect data.To get started with the P2P network, you need to set up a node. Here’s a basic example of how to create a node:
let _ = rustls::crypto::ring::default_provider().install_default();
let priv_key: PrivatePkcs8KeyDer<'_> = PrivatePkcs8KeyDer::from(DEFAULT_CLUSTER_KEY.to_vec());
let cert = CertificateDer::from(DEFAULT_CLUSTER_CERT.to_vec());
let peer_id = PeerId::from("127.0.0.1:10000".parse().unwrap());
let network = P2pNetwork::new(P2pNetworkConfig {
peer_id,
listen_addr: addr,
advertise: advertise.then(|| addr.into()),
priv_key,
cert,
tick_ms: 100,
seeds,
secure: DEFAULT_SECURE_KEY.into(),
}).await;
let service = network.create_service(1.into());
We can handle event from service
while let Some(event) = service.recv() {
match event {
P2pServiceEvent::Unicast(from_peer, data) => {
// handle data from other node here
},
P2pServiceEvent::Broadcast(from_peer, data) => {
// handle broadcast data from other node here
},
P2pServiceEvent::Stream(from_peer, meta, stream) => {
// stream is AsyncRead + AsyncWrite, we can tunnel it to other by bicopydirection ...
},
}
}
To send messages, you can use the send_unicast
method for direct communication or send_broadcast
for broadcasting messages to all connected peers:
service.send_unicast(dest_peer_id, data).await.expect("should send ok");
The project includes a suite of tests to ensure the functionality of the P2P network. You can run the tests using:
cargo test