-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change representation of PeerId to CIDv1 #2322
Conversation
To encapsulate both errors from Multihash and CID, we change the interface of `from_bytes` to return a `PeerIdError` in the case of `Err`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First off, very solid work! Thanks @JerryHue.
I have a couple of comments below:
core/src/peer_id.rs
Outdated
/// | ||
/// If the CID's multihash does not fit the specifications: | ||
/// | ||
/// * use a valid hashing algorithm for peer IDs, or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// * use a valid hashing algorithm for peer IDs, or | |
/// * use an invalid hashing algorithm for peer IDs, or |
Correct?
core/src/peer_id.rs
Outdated
/// * is a CIDv0 with a multihash that fits the specifications | ||
/// for multihash, or | ||
/// * is a CIDv1 with the proper codec (LIBP2P_KEY_CODEC), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused by this. Shouldn't the opposite be written, along the lines of: "If XXX does not fit the specification, ..."?
core/src/peer_id.rs
Outdated
/// Returns a raw bytes representation of the `PeerId`. | ||
pub fn to_cidv1_bytes(&self) -> Vec<u8> { | ||
self.cid.to_bytes() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed anywhere? If not, I would prefer removing it to ease future breaking changes. If I am not mistaken test logic should be able to call self.cid.to_bytes
directly.
self.cid.to_bytes() | ||
} | ||
|
||
/// Returns a base-58 raw encoded string of this `PeerId`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns a base-58 raw encoded string of this `PeerId`. | |
/// Returns a base-58 raw (non-multibase) encoded string of this `PeerId`. |
core/src/peer_id.rs
Outdated
pub fn to_base58(&self) -> String { | ||
bs58::encode(self.to_bytes()).into_string() | ||
} | ||
|
||
/// Returns a `base32` encoded string of this `PeerId`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Returns a `base32` encoded string of this `PeerId`. | |
/// Returns a `base32` encoded string of this `PeerId`. | |
/// | |
/// This corresponds to the new peer ID [representation]. | |
/// | |
/// [representation]: https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md |
self.cid.to_bytes() | ||
} | ||
|
||
/// Returns a base-58 raw encoded string of this `PeerId`. | ||
pub fn to_base58(&self) -> String { | ||
bs58::encode(self.to_bytes()).into_string() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following would fail given that <PeerId as FromStr>
expects the string to start with Qm
or 1
, right?
assert!(cidv1_peer_id == PeerId::from_str(cidv1_peer_id.to_base58()));
I am not quite sure how to proceed in this case. Maybe convert the CIDv1 peer ID to the old format first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe convert the CIDv1 peer ID to the old format first?
What do you think @JerryHue?
CidError(CidError), | ||
} | ||
|
||
impl fmt::Display for PeerIdError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libp2p-core
already imports thiserror
. What do you think of using it instead of this manual implementation?
Co-authored-by: Max Inden <[email protected]>
Co-authored-by: Max Inden <[email protected]>
We check whether the old representation of a PeerId and the new representation of a PeerId produce the same PeerId.
Friendly ping @JerryHue. Are you still interested in taking this fix to the finish line? :) |
@mxinden I believe this is waiting for your input. |
This PR would break compatibility with Substrate/Polkadot and is a no-no for us. |
Thanks for mentioning this. This definitely needs an upgrade path where we first support reading in a CIDv1 |
For what it's worth, I've taken the decision for Polkadot a while ago to always use CIDv0. The spec precisely mentions how to encode it: https://spec.polkadot.network/#defn-peer-id. Being able to easily encode/decode PeerIds, and ensuring uniqueness of PeerIds, are more important than conforming to some libp2p spec outside of our control and that brings zero benefits for us. It's not a deal breaker for adding support for CIDv1 in rust-libp2p, as long as there's always an option to force CIDv0. |
As far as I can tell blocked on addressing comments above.
I suggest we support reading CIDv1 as a first iteration, though always writing and handling CIDv0s. |
This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏 |
This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏 |
This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏 |
Addresses #2259.
To encapsulate both errors from Multihash and CID, we change the interface of
from_bytes
to return aPeerIdError
in the case ofErr
.