例如使用let transport = libp2p::development_transport(local_keys).await?;调用libp2p提供的高级API函数development_transport来创建传输。这个函数封装了传输的创建过程,通常会使用一组默认的配置参数和最佳实践,以便快速启动和测试libp2p应用程序。在这种情况下,local_keys是本地密钥,用于加密和身份验证。有关这个API的未来发展可以看这个ISSUE。
//Creates transport which is a feature of the lib p2p framework
let transport = TokioTcpConfig::new()
//Upgrades version of the transport once connection is established as Version 1 of the multi-stream-select protocol is the version that interacts with the noise protocol
//in short handles protocol negotiation
.upgrade(upgrade::Version::V1)
//Authenticates that channel is secure with the noise XX handshake
.authenticate(libp2p::noise::NoiseConfig::xx(auth_keys).into_authenticated())
//multiplex transport negotiates multiple sub-streams and/or connections on the authenticated transport
.multiplex(mplex::MplexConfig::new())
//boxed allows only output and error types to be captured
.boxed();
local_peer_id is PeerId("12D3KooWD4BMTXhmTV46fSbdRv238dRudXcXq9VPSF1PH4N4nWVk")
Listening on local address "/ip4/192.168.0.102/tcp/51516"
Listening on local address "/ip4/127.0.0.1/tcp/51516"
local_peer_id is PeerId("12D3KooWJRoPgqPPC8adBwrFHfx4XtCpCu1BprzBofgVirxoceJB")
Listening on local address "/ip4/192.168.0.102/tcp/51519"
Listening on local address "/ip4/127.0.0.1/tcp/51519"
在 peer 节点之间交换 ping 命令
use libp2p::futures::StreamExt; // 异步流有关
use libp2p::ping::{Ping, PingConfig};// ping 命令相关依赖
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::{identity, Multiaddr, PeerId};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let new_key = identity::Keypair::generate_ed25519();
let new_peer_id = PeerId::from(new_key.public());
println!("New Peer ID is: {:?}", new_peer_id);
let transport = libp2p::development_transport(new_key).await?;
let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
let mut swarm = Swarm::new(transport, behaviour, new_peer_id);
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
// 本地节点向远程节点发出连接 从命令行输入的参数取出
if let Some(remote_peer) = std::env::args().nth(1) {
let remote_peer_multiaddr: Multiaddr = remote_peer.parse()?;
swarm.dial(remote_peer_multiaddr)?;// 需要libp2p ,version = "0.46"
println!("Dialed remote peer: {:?}", remote_peer); // 打印远程地址
}
loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on local Address {:?}", address)
}
SwarmEvent::Behaviour(event) => println!("Event received from peer is {:?}", event),
_ => {}
}
}
}
运行效果
PS C:\Users\kingchuxing\Documents\CODE\P2PRecipeApp> cargo run
New Peer ID is: PeerId("12D3KooWFR97CdrL7jvboqU6MxyWjRzfHsR5V2sgs23yqg4xetnV")
Listening on local Address "/ip4/192.168.0.102/tcp/50997"
Listening on local Address "/ip4/127.0.0.1/tcp/50997"
PS C:\Users\kingchuxing\Documents\CODE\P2PRecipeApp> cargo run /ip4/192.168.0.102/tcp/50997
New Peer ID is: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG")
Dialed remote peer: "/ip4/192.168.0.102/tcp/50997" //对远程地址进行了拨号
Listening on local Address "/ip4/192.168.0.102/tcp/51014"
Listening on local Address "/ip4/127.0.0.1/tcp/51014"
Event received from peer is Event { peer: PeerId("12D3KooWFR97CdrL7jvboqU6MxyWjRzfHsR5V2sgs23yqg4xetnV"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWFR97CdrL7jvboqU6MxyWjRzfHsR5V2sgs23yqg4xetnV"), result: Ok(Ping { rtt: 314.3µs }) }
后续两个peer之间会不断的ping和pang
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 250.7µs }) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 319.7µs }) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 479.6µs }) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 217.8µs }) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 266.7µs }) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Pong) }
Event received from peer is Event { peer: PeerId("12D3KooWMGLjRBpysfHqjYWcYPf89bDNWH4S9Ao8RXj5tBehksCG"), result: Ok(Ping { rtt: 1.0212ms }) }
uselibp2p::{futures::StreamExt,
identity,mdns::{Mdns,MdnsConfig,MdnsEvent},swarm::{Swarm,SwarmEvent},PeerId,};usestd::error::Error;#[tokio::main]asyncfnmain()->Result<(),Box<dynError>>{let new_key =identity::Keypair::generate_ed25519();let new_peer_id =PeerId::from(new_key.public());println!("New Peer ID is: {:?}", new_peer_id);let transport =libp2p::development_transport(new_key).await?;// 使用密钥对创建传输let behaviour =Mdns::new(MdnsConfig::default()).await?;// 创建网络行为letmut swarm =Swarm::new(transport, behaviour, new_peer_id);// 创建Swarm
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;loop{match swarm.select_next_some().await{SwarmEvent::NewListenAddr{ address,..}=>{println!("Listening on local Address {:?}", address)}SwarmEvent::Behaviour(MdnsEvent::Discovered(peers))=>{for(peer, addr)in peers {println!("discovered {} {}", peer, addr);}}SwarmEvent::Behaviour(MdnsEvent::Expired(expired))=>{for(peer, addr)in expired {println!("expired {} {}", peer, addr);}}
_ =>{}}}}
运行效果
PS C:\Users\kingchuxing\Documents\CODE\P2PRecipeApp> cargo run
New Peer ID is: PeerId("12D3KooWMU94yU8ZAFnPdRwLyQ9FuR47sVQfMG9VJUiiykCvmYdt")
Listening on local Address "/ip4/192.168.0.102/tcp/51361"
Listening on local Address "/ip4/127.0.0.1/tcp/51361"
discovered 12D3KooWNMofXEV4fp5NCEBF3m7km3keRnAuScrhoKqvc5Jiacy9 /ip4/192.168.0.102/tcp/51360
discovered 12D3KooWNMofXEV4fp5NCEBF3m7km3keRnAuScrhoKqvc5Jiacy9 /ip4/127.0.0.1/tcp/51360
PS C:\Users\kingchuxing\Documents\CODE\P2PRecipeApp> cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.28s
Running `target\debug\P2PRecipeApp.exe`
New Peer ID is: PeerId("12D3KooWNMofXEV4fp5NCEBF3m7km3keRnAuScrhoKqvc5Jiacy9")
Listening on local Address "/ip4/192.168.0.102/tcp/51360"
Listening on local Address "/ip4/127.0.0.1/tcp/51360"
discovered 12D3KooWMU94yU8ZAFnPdRwLyQ9FuR47sVQfMG9VJUiiykCvmYdt /ip4/192.168.0.102/tcp/51361
discovered 12D3KooWMU94yU8ZAFnPdRwLyQ9FuR47sVQfMG9VJUiiykCvmYdt /ip4/127.0.0.1/tcp/51361