libp2p-rskadの使用法とデバッグ方法

libp2p-rs v0.2.0はすでにKad-DHTをサポートしています。これは、DHTネットワークを介してノードとコンテンツを検出するノードをサポートします。
この記事では、主にlibp2p-rsでDHT使用する方法を示し、swarmとkadのいくつかのAPIをリストします。

kadを使用する

ステップ1:群れを作成する

    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();

ステップ2: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();

ステップ3:kadを登録する

ルーティング機能を持つように群れをアップグレードします。

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

ステップ4:kadとswarmを開始します

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

ステップ5:kadを使用する

最初にブートストラップノードをピアストアとルーティングテーブルに追加してから、ブートストラップを開始します。

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

ステップ6:cliを有効にする

統合されたcliは、swarmとkadをデバッグできます。

    let mut app = App::new("xCLI").version("v0.1").author("[email protected]");

    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デバッグ

以下に、一般的に使用されるデバッグコマンドの一部のみを示します。その他の再生方法については、自分でロックを解除してください。

群れの接続

connectionコマンドを使用して、現在のすべての接続とそのサブストリームに関する情報を取得したり、特定のピアとの接続情報を取得したりできます。

# 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の状態

状態は、反復クエリの実行ステータスをカウントするために使用されます。また、現在のノードが受信した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コマンドは、ルーティングテーブル情報をダンプし、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については、ソースコードを直接お読みください。

群れ

ピアストア

    /// 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)

接続

    /// 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<()>

ストリーム

    /// 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> 

いつ

    /// 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を開始した後、コントローラーを介してAPIを呼び出すことができます。現在の
swarmAPIは比較的完全であり、Kad-DHTなどのより複雑なプロトコルをサポートできるようになりました。これにより、新しいプロトコルを追加するための条件も作成されます。


Netwarpsは、中国のシニアクラウドコンピューティングおよび分散技術開発チームで構成されています。このチームは、金融、電力、通信、およびインターネット業界で非常に豊富な経験を持っています。Netwarpsは現在、深センと北京にR&Dセンターを持ち、チームサイズは30以上で、そのほとんどはインターネット、金融、クラウドコンピューティング、ブロックチェーン、科学研究機関などの専門分野から10年以上の開発経験を持つ技術者です。
Netwarpsは、セキュアストレージテクノロジー製品の開発とアプリケーションに重点を置いています。主な製品には、分散ファイルシステム(DFS)と分散コンピューティングプラットフォーム(DCP)があり、分散ストレージの提供と分散ネットワークテクノロジーに基づく分散に取り組んでいます。コンピューティングプラットフォームは、高可用性、低消費電力、低ネットワークという技術的特性を備えており、モノのインターネットや産業用インターネットなどのシナリオに適しています。
公式アカウント:Netwarps

おすすめ

転載: blog.51cto.com/14915984/2596842