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

PeerId::from_bytes should be const #5864

Open
tqwewe opened this issue Feb 17, 2025 · 4 comments
Open

PeerId::from_bytes should be const #5864

tqwewe opened this issue Feb 17, 2025 · 4 comments
Labels
need/author-input Needs input from the original author

Comments

@tqwewe
Copy link

tqwewe commented Feb 17, 2025

Description

There's currently no way to create a PeerId in a const context.

Motivation

I'm writing unit tests, and I need a global PeerId shared between tests. A const peer id would be great for this.

Current Implementation

My work around is just to call PeerId::from_bytes every time using the same bytes.

Are you planning to do it yourself in a pull request?

No

@jxs
Copy link
Member

jxs commented Feb 17, 2025

Hi, and thanks for the interest!
Making this const depends on multihash so I'd suggest to open an issue there and link it here for tracking purposes.

@jxs jxs added the need/author-input Needs input from the original author label Feb 17, 2025
@tqwewe
Copy link
Author

tqwewe commented Feb 17, 2025

It seems like the only method which is const in multihash is Multihash::wrap. I'm not sure if this is used in libp2p, and lets it be constructed in a const context already? Or if libp2p only uses other multihash methods under the hood which aren't const. If its the latter, then I guess this issue can be closed.

From searching on the multihash repo, I found these two discussions about const:
multiformats/rust-multihash#330
multiformats/rust-multihash#331 (comment)

@dariusc93
Copy link
Member

For the time being wouldnt it be possible for you to use LazyLock for the construction of the PeerId that you wish to use in your test?

@tqwewe
Copy link
Author

tqwewe commented Feb 18, 2025

Yes I've resolved it by just using a function returning the same peer id in my tests thanks! But if it's still possible to add const to PeerId I think it's worth keeping the issue open

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
Projects
None yet
Development

No branches or pull requests

3 participants