libp2p-rs v0.2.0已经支持Kad-DHT,支持节点通过DHT网络发现节点和内容。
本文主要展示如何在libp2p-rs使用DHT,以及罗列出swarm和kad的部分API。

使用kad

step1: 创建swarm

    let sec = secio::Config::new(keys.clone());
    let mux = yamux::Config::new();
    let tu = TransportUpgrade::new(TcpConfig::default(), mux, sec);

    let mut swarm = Swarm::new(keys.public())
        .with_transport(Box::new(tu))
        .with_identify(IdentifyConfig::new(false));

    swarm.listen_on(vec![listen_addr]).expect("listen on");
    let mut swarm_control = swarm.control();

step2: 创建kad

    let store = MemoryStore::new(swarm.local_peer_id().clone());
    let kad = Kademlia::new(swarm.local_peer_id().clone(), store);
    let kad_handler = kad.handler();
    let mut kad_ctrl = kad.control();

step3: 注册kad

将swarm升级,使其具备routing功能。

    // register handler to swarm
    swarm = swarm.with_protocol(Box::new(kad_handler)).with_routing(Box::new(kad_ctrl.clone()));

step4: 启动kad和swarm

    kad.start(swarm_control.clone());
    swarm.start();

step5: 使用kad

先将bootstrap节点添加到peerstore和路由表,再启动bootstrap。

    kad_control.add_node(bootstrap_peer, vec![bootstrap_addr]).await;
    kad_control.bootstrap().await;

step6: 启用cli

集成cli,可调试swarm和kad。

    let mut app = App::new("xCLI").version("v0.1").author("kingwel.xie@139.com");

    app.add_subcommand_with_userdata(swarm_cli_commands(), Box::new(swarm_control.clone()));
    app.add_subcommand_with_userdata(dht_cli_commands(), Box::new(kad_control.clone()));

    app.run();

cli调试

以下只列出部分常用的调试命令,更多玩法请亲自下场解锁。

swarm connection

connection命令可以用于获取当前所有连接及其子流的信息,也可以获取和某个peer的连接信息。

# s con
CID   DIR Remote-Peer-Id                                       I/O  Remote-Multiaddr
231   In  Qmf3ZX3yHnmzaXFGH5G149HyrAeKRFantVnZ9gZdnuPv1U       2/0  /ip4/114.227.83.230/tcp/24792
      (231   Sid(7) In  /ipfs/kad/1.0.0)
      (231   Sid(9) In  /ipfs/kad/1.0.0)
161   In  QmZC9dZPyJWXSB2Ao2ChGJMjfuFiT7TyKdsGsFEKVSqnnf       0/0  /ip4/212.102.37.201/tcp/4001
2185  In  QmTmnqSEarcSLJxhehJRKX64pxSkeKn7jS2fDEZFjjt9Bn       1/0  /ip4/114.226.44.86/tcp/3109
      (2185  Sid(7) In  /ipfs/kad/1.0.0)
2069  In  12D3KooWPtfLkqAVMPP6FNufHvqxPYe55XuAdEZWUn2cPbLLAwuT 1/0  /ip4/111.16.39.80/tcp/17881
      (2069  Sid(1) In  /ipfs/kad/1.0.0)
2349  In  QmVcXP4bnoCJkUUJinduuM68n5jfSjDj6sKaTjRhHoecpt       1/0  /ip4/83.248.150.24/tcp/42761
      (2349  Sid(3) In  /ipfs/kad/1.0.0)
492   In  12D3KooWKBkFNUCvyP5PbV2mAhrzcvPi4EPL4vD3CjieGGU9ZcQr 1/0  /ip4/203.145.95.60/tcp/64417
      (492   Sid(1) In  /ipfs/kad/1.0.0)
18    In  QmP6waLA8S6M8WPoQ5tWPE6xpgtsJ44LGQcq7vDTUAmyob       1/0  /ip4/188.127.190.220/tcp/4001
      (18    Sid(7) In  /ipfs/kad/1.0.0)

# s con Qmf3ZX3yHnmzaXFGH5G149HyrAeKRFantVnZ9gZdnuPv1U
CID   DIR Remote-Peer-Id                                       I/O  Remote-Multiaddr
231   In  Qmf3ZX3yHnmzaXFGH5G149HyrAeKRFantVnZ9gZdnuPv1U       2/0  /ip4/114.227.83.230/tcp/24792
      (231   Sid(7) In  /ipfs/kad/1.0.0)
      (231   Sid(9) In  /ipfs/kad/1.0.0)

dht states

states用于统计迭代查询运行状况,也可以观察当前节点接收到的Kad请求的次数。

# d st
Total refreshes : 1
Successful queries   : 4
Timeout queries   : 0
Query details   : QueryStats { requests: 59, success: 41, failure: 10, duration: 18.198653932s }
Kad rx messages : MessageStats { ping: 0, find_node: 17216, get_provider: 559, add_provider: 3667, get_value: 1, put_value: 27 }

dht dump

dump命令用于dump出路由表的信息,使用verbose可以打印详细信息。

# d dp
Index Entries Active
244   1       1
246   1       1
247   2       2
248   7       7
249   10      8
250   20      20
251   20      20
252   20      20
253   20      20
254   20      19
255   20      20

# d dp 1
Index Entries Active
244   1       1
      Qme9PR5oDcSSGoS2He53RqaML4vinDD5CNgxxmV2qPefFP       Conn(false) Some(52292.68894773s) Addrs([])
246   1       1
      QmboRZYso6VdQ5yfXe1DAj9u8EqouZGUsf2inoqYDtzdf8       Conn(true) Some(4330.367016609s) Addrs([])
247   2       2
      QmZsbivLpaVpWQ4Mum2nzbEcoXbH7QbftRkQCmmJiTqcUp       Conn(false) Some(77688.875853187s) Addrs([])
      QmZaCQ6anyaPuebhLeomzpyKRAY6GnNS5NCU8h7kSjwFKN       Conn(false) Some(43775.12096365s) Addrs(["/ip4/127.0.0.1/tcp/4001", "/ip4/138.68.29.104/tcp/4001", "/ip4/10.46.0.6/tcp/4001", "/ip4/10.138.16.85/tcp/4001", "/ip6/::1/tcp/4001"])

API介绍

目前swarm和kad的API已经比较完善,以下只列出部分常用的API,想了解更多API请直接阅读源码。

swarm

peerstore

    /// Gets the public key by peer_id.
    pub fn get_key(&self, peer_id: &PeerId) -> Option<PublicKey>

    /// Gets all multiaddr of a peer.
    pub fn get_addrs(&self, peer_id: &PeerId) -> Option<Vec<Multiaddr>>

    /// Adds a address to address_book by peer_id, if exists, update rtt.
    pub fn add_addr(&self, peer_id: &PeerId, addr: Multiaddr, ttl: Duration)

    /// Adds many new addresses if they're not already in the peer store.
    pub fn add_addrs(&self, peer_id: &PeerId, addrs: Vec<Multiaddr>, ttl: Duration)

    /// Clears all multiaddr of a peer from the peer store.
    pub fn clear_addrs(&self, peer_id: &PeerId)

connection

    /// Make a new connection towards the remote peer with addresses specified.
    pub async fn connect_with_addrs(&mut self, peer_id: PeerId, addrs: Vec<Multiaddr>) -> Result<()>

    /// Make a new connection towards the remote peer.
    ///
    /// It will lookup the peer store for address of the peer, otherwise
    /// initiate the routing interface for querying the addresses, if routing
    /// is available.
    pub async fn new_connection(&mut self, peer_id: PeerId) -> Result<()>

    /// Make a new connection towards the remote peer, without using routing(Kad-DHT).
    pub async fn new_connection_no_routing(&mut self, peer_id: PeerId) -> Result<()>

    /// Close connection towards the remote peer.
    pub async fn disconnect(&mut self, peer_id: PeerId) -> Result<()>

stream

    /// Open a new outbound stream towards the remote peer.
    ///
    /// It will lookup the peer store for address of the peer,
    /// otherwise initiate the routing interface for address querying,
    /// when routing is enabled. In the end, it will open an outgoing
    /// sub-stream when the connection is eventually established.
    pub async fn new_stream(&mut self, peer_id: PeerId, pids: Vec<ProtocolId>) -> Result<Substream>

    /// Open a new outbound stream towards the remote peer, without routing.
    pub async fn new_stream_no_routing(&mut self, peer_id: PeerId, pids: Vec<ProtocolId>) -> Result<Substream> 

    /// Open a new outbound stream towards the remote peer, without routing.
    pub async fn new_stream_no_routing(&mut self, peer_id: PeerId, pids: Vec<ProtocolId>) -> Result<Substream> 

kad

    /// Add a node and its listening addresses to KBuckets.
    pub async fn add_node(&mut self, peer_id: PeerId, addrs: Vec<Multiaddr>)

    /// Add a node and its listening addresses to KBuckets.
    pub async fn remove_node(&mut self, peer_id: PeerId)

    /// Initiate bootstrapping.
    ///
    /// In general it should be done only once upon Kad startup.
    pub async fn bootstrap(&mut self)

    /// Lookup the closer peers with the given key.
    pub async fn lookup(&mut self, key: record::Key) -> Result<Vec<KadPeer>>

    /// Lookup the given peer.
    pub async fn find_peer(&mut self, peer_id: &PeerId) -> Result<KadPeer>

    /// Put value in local and other peers which closest to the given key.
    pub async fn put_value(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<()>

    /// Get value from local and other peers  which closest to the given key.
    pub async fn get_value(&mut self, key: Vec<u8>) -> Result<Vec<u8>>

    /// Announce to peers which closer to the given key that self provide content.
    pub async fn provide(&mut self, key: Vec<u8>) -> Result<()>

    /// Find peers who provide content.
    pub async fn find_providers(&mut self, key: Vec<u8>, count: usize) -> Option<Vec<KadPeer>>

总结

在启动kad和swarm后,就可以通过句柄(controller),调用API。当前
swarm的API已经比较完善,目前已经可以支持Kad-DHT这种比较复杂的协议,这也为新协议的添加创造了条件。


Netwarps 由国内资深的云计算和分布式技术开发团队组成,该团队在金融、电力、通信及互联网行业有非常丰富的落地经验。Netwarps 目前在深圳、北京均设立了研发中心,团队规模30+,其中大部分为具备十年以上开发经验的技术人员,分别来自互联网、金融、云计算、区块链以及科研机构等专业领域。
Netwarps 专注于安全存储技术产品的研发与应用,主要产品有去中心化文件系统(DFS)、去中心化计算平台(DCP),致力于提供基于去中心化网络技术实现的分布式存储和分布式计算平台,具有高可用、低功耗和低网络的技术特点,适用于物联网、工业互联网等场景。
公众号:Netwarps