Giter Site home page Giter Site logo

Comments (6)

thomaseizinger avatar thomaseizinger commented on May 30, 2024 1

We tend to be very conservative in what functionality we add to libp2p-identity because it is a dependency of the entire libp2p ecosystem.
Given that PeerId offers, from_bytes, am I right to assume that you can implement this on top of the APIs that are currently offered and it doesn't necessarily have to live in libp2p-identity?
Before we add an API like this, I'd like to first see it pulling its weight in where we use it.

Yes, it doesn't need to reside in libp2p-identity. This is mostly useful inside Kademlia.

Cool, thanks for the confirmation! Optimising DHT queries is definitely something we are very open to :)

from rust-libp2p.

vbhattaccmu avatar vbhattaccmu commented on May 30, 2024 1

I have been able to successfully generate peer ids from the existing APIs provided using to_bytes() and from_multihash(). I will reach out with a separate pull request on an added method to optimize DHT queries in a while. Would like to close this for now as my issue has been resolved. Thanks!!

from rust-libp2p.

mxinden avatar mxinden commented on May 30, 2024 1

@vbhattaccmu the two snippets below might give some additional inspiration:

.map(|b| {
// Try to find a key that falls into the bucket. While such keys can
// be generated fully deterministically, the current libp2p kademlia
// wire protocol requires transmission of the preimages of the actual
// keys in the DHT keyspace, hence for now this is just a "best effort"
// to find a key that hashes into a specific bucket. The probabilities
// of finding a key in the bucket `b` with as most 16 trials are as
// follows:
//
// Pr(bucket-255) = 1 - (1/2)^16 ~= 1
// Pr(bucket-254) = 1 - (3/4)^16 ~= 1
// Pr(bucket-253) = 1 - (7/8)^16 ~= 0.88
// Pr(bucket-252) = 1 - (15/16)^16 ~= 0.64
// ...
let mut target = kbucket::Key::from(PeerId::random());
for _ in 0..16 {
let d = local_key.distance(&target);
if b.contains(&d) {
break;
}
target = kbucket::Key::from(PeerId::random());
}
target
})

// Generate a random keypair, optionally with a prefix
Command::Rand { prefix } => {
if let Some(prefix) = prefix {
if prefix.as_bytes().iter().any(|c| !ALPHABET.contains(c)) {
eprintln!("Prefix {prefix} is not valid base58");
std::process::exit(1);
}
// Checking conformity to ALLOWED_FIRST_BYTE.
if !prefix.is_empty() && !ALLOWED_FIRST_BYTE.contains(&prefix.as_bytes()[0]) {
eprintln!("Prefix {prefix} is not reachable");
eprintln!(
"Only the following bytes are possible as first byte: {}",
str::from_utf8(ALLOWED_FIRST_BYTE).unwrap()
);
std::process::exit(1);
}
let (tx, rx) = mpsc::channel::<(PeerId, identity::Keypair)>();
// Find peer IDs in a multithreaded fashion.
for _ in 0..thread::available_parallelism()?.get() {
let prefix = prefix.clone();
let tx = tx.clone();
thread::spawn(move || loop {
let keypair = identity::Keypair::generate_ed25519();
let peer_id = keypair.public().to_peer_id();
let base58 = peer_id.to_base58();
if base58[8..].starts_with(&prefix) {
tx.send((peer_id, keypair)).expect("to send");
}
});
}
rx.recv().expect("to recv")
} else {
let keypair = identity::Keypair::generate_ed25519();
(keypair.public().into(), keypair)
}
}

from rust-libp2p.

thomaseizinger avatar thomaseizinger commented on May 30, 2024

We tend to be very conservative in what functionality we add to libp2p-identity because it is a dependency of the entire libp2p ecosystem.

Given that PeerId offers, from_bytes, am I right to assume that you can implement this on top of the APIs that are currently offered and it doesn't necessarily have to live in libp2p-identity?

Before we add an API like this, I'd like to first see it pulling its weight in where we use it.

from rust-libp2p.

thomaseizinger avatar thomaseizinger commented on May 30, 2024

Before we add an API like this, I'd like to first see it pulling its weight in where we use it.

For example, it appears that this is mostly useful for Kademlia so it could also just be a private function in kademlia somewhere and doesn't have to go into the public API of PeerId.

from rust-libp2p.

vbhattaccmu avatar vbhattaccmu commented on May 30, 2024

We tend to be very conservative in what functionality we add to libp2p-identity because it is a dependency of the entire libp2p ecosystem.

Given that PeerId offers, from_bytes, am I right to assume that you can implement this on top of the APIs that are currently offered and it doesn't necessarily have to live in libp2p-identity?

Before we add an API like this, I'd like to first see it pulling its weight in where we use it.

Yes, it doesn't need to reside in libp2p-identity. This is mostly useful inside Kademlia.

from rust-libp2p.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.