Skip to content
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

Closed
wants to merge 5 commits into from

Conversation

JerryHue
Copy link
Contributor

@JerryHue JerryHue commented Nov 1, 2021

Addresses #2259.

To encapsulate both errors from Multihash and CID, we change the interface of from_bytes to return a PeerIdError in the case of Err.

To encapsulate both errors from Multihash and CID,
we change the interface of `from_bytes` to
return a `PeerIdError` in the case of `Err`.
Copy link
Member

@mxinden mxinden left a 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:

///
/// If the CID's multihash does not fit the specifications:
///
/// * use a valid hashing algorithm for peer IDs, or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// * use a valid hashing algorithm for peer IDs, or
/// * use an invalid hashing algorithm for peer IDs, or

Correct?

Comment on lines 124 to 126
/// * is a CIDv0 with a multihash that fits the specifications
/// for multihash, or
/// * is a CIDv1 with the proper codec (LIBP2P_KEY_CODEC),
Copy link
Member

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, ..."?

Comment on lines 161 to 164
/// Returns a raw bytes representation of the `PeerId`.
pub fn to_cidv1_bytes(&self) -> Vec<u8> {
self.cid.to_bytes()
}
Copy link
Member

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.

core/src/peer_id.rs Outdated Show resolved Hide resolved
self.cid.to_bytes()
}

/// Returns a base-58 raw encoded string of this `PeerId`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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 Show resolved Hide resolved
pub fn to_base58(&self) -> String {
bs58::encode(self.to_bytes()).into_string()
}

/// Returns a `base32` encoded string of this `PeerId`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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()
Copy link
Member

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?

Copy link
Member

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 {
Copy link
Member

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?

JerryHue and others added 4 commits November 3, 2021 17:49
We check whether the old representation of
a PeerId and the new representation of a
PeerId produce the same PeerId.
@mxinden
Copy link
Member

mxinden commented Mar 2, 2022

Friendly ping @JerryHue. Are you still interested in taking this fix to the finish line? :)

@JerryHue
Copy link
Contributor Author

JerryHue commented Mar 8, 2022

Hello again, @mxinden! I would like to try to bring this to the finish line throughout this week, sorry for the excruciatingly long wait. I wonder if I should improve this PR by taking into account @tomaka's comments on the original issue. What do you think?

@thomaseizinger
Copy link
Contributor

@mxinden I believe this is waiting for your input.

@tomaka
Copy link
Member

tomaka commented Nov 2, 2022

This PR would break compatibility with Substrate/Polkadot and is a no-no for us.

@thomaseizinger
Copy link
Contributor

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 PeerId as described in the RFC.

@tomaka
Copy link
Member

tomaka commented Nov 2, 2022

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.

@mxinden
Copy link
Member

mxinden commented Nov 14, 2022

@mxinden I believe this is waiting for your input.

As far as I can tell blocked on addressing comments above.

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.

I suggest we support reading CIDv1 as a first iteration, though always writing and handling CIDv0s.

@thomaseizinger thomaseizinger added the need/author-input Needs input from the original author label Nov 15, 2022
@mergify
Copy link
Contributor

mergify bot commented Nov 15, 2022

This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏

@github-actions github-actions bot added the Stale label Jan 14, 2023
@mergify
Copy link
Contributor

mergify bot commented Jan 14, 2023

This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏

@github-actions github-actions bot removed the Stale label Jan 15, 2023
@github-actions github-actions bot added the Stale label Mar 16, 2023
@mergify
Copy link
Contributor

mergify bot commented Mar 16, 2023

This pull request has merge conflicts. Could you please resolve them @JerryHue? 🙏

@github-actions github-actions bot removed the Stale label Mar 17, 2023
@github-actions github-actions bot added the Stale label May 17, 2023
@github-actions github-actions bot closed this May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/author-input Needs input from the original author Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants