Search code examples
rustipfslibp2p

Put record into DHT


I'm trying to insert a simple /pk record to the IFPS DHT using Rust and rust-libp2p

Here's my code (Rust 1.55, libp2p-rust 0.39.1):

let mut key = "/pk/".bytes().collect::<Vec<_>>();
key.append(&mut local_key.public().into_peer_id().to_bytes());
let key = Key::from(key);
let mut value = local_key.public().into_protobuf_encoding();

kademlia_swarm.behaviour_mut().put_record(Record{
    key,
    value,
    publisher: None,
    expires: None
}, Quorum::One).expect("call put_record");

This code successfully finds 20+ peers to insert the record to, but the insertions always fail. When I run my code with RUST_LOG=debug, I see that every peer terminates the connection with EOF during the insertion. Calling kademlia::bootstrap() doesn't help.

FYI, when I run something very similar using Go and go-libp2p, it works as expected:

publicKeyBytes, _ := localKey.GetPublic().Bytes()
pid, _ := peer.IDFromPublicKey(localKey.GetPublic())
key := "/pk/" + string(pid)
err = kademlia.PutValue(ctx, key, publicKeyBytes)

Edit: a bounty is awarded for a simple demonstration of how to add a record to the IPFS DHT using Rust and rust-libp2p


Solution

  • Unfunny story - my example presented in the question is correct - but there is a bug in rust-libp2p which breaks the interoperability with go-libp2p. See https://github.com/libp2p/rust-libp2p/pull/2309 for details