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

Remove p2pservice networkcodec trait #2388

Open
wants to merge 25 commits into
base: master
Choose a base branch
from

Conversation

acerone85
Copy link
Contributor

@acerone85 acerone85 commented Oct 23, 2024

Linked Issues/PRs

Closes #2368

Description

In the P2P service, we couple together GossipSub and RequestResponse codecs in a NetworkCodec trait. In practice, this coupling is not necessary as the two codecs use different Messages and different logics for encoding/deconding messages. The only thing that they share in common is using Postcard as the data format to serialize and deserialize data, but this is not made clear in the network trait.

This PR is a rework of Codecs in the libp2p service, which deletes the NetworkCodec trait and therefore the coupling between GossipSubCodec and RequestResponseCodec. In particular:

  • A new DataFormat trait is defined for serializing deserializing any SerDe datatype, and the helper functions for postcard serialization and deserializationare moved to a structPostcardthat implements theDataFormat` trait,
  • A new BoundedCodec struct that implements the RequestResponse::Codec trait for any DataFormat supersedes the old PostcardCodec.
  • A new UnboundedCodec struct that implements the GossipsubCodec trait for any DataFormatsupersedes the oldPostcardCodec`,
  • [ ]
  • Parts specific to the Request/Response protocol have been moved to a new request_response::protocols module`,
  • The FuelBehaviour and LibP2P service have been refactored to use different codecs for Gossipsub and RequestResponse.

TODO:

  • Better Documentation
  • Better names for modules
  • Maybe move DataFormats to a different part of the codebase?

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

After merging, notify other teams

[Add or remove entries as needed]

@acerone85 acerone85 self-assigned this Oct 23, 2024
@acerone85 acerone85 linked an issue Oct 23, 2024 that may be closed by this pull request
@acerone85 acerone85 marked this pull request as draft October 23, 2024 17:01
@acerone85 acerone85 changed the title 2368 remove p2pservice networkcodec trait Remove p2pservice networkcodec trait Oct 23, 2024
@acerone85 acerone85 changed the base branch from release/v0.40.0 to master October 23, 2024 17:01
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Generally I like the decoupling you're achieving here. I think we could do some simplifications by reducing the number of traits, and for example just having one GossipSubCodec trait and oneRequestResponseReadWrite trait. Both of these could be generic over the data format if needed.

crates/services/p2p/src/codecs.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/codecs/bounded.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/codecs/bounded.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/p2p_service.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/codecs.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/codecs.rs Outdated Show resolved Hide resolved
crates/services/p2p/src/codecs.rs Outdated Show resolved Hide resolved
@acerone85 acerone85 marked this pull request as ready for review October 29, 2024 09:48
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Overall looks good to me. There's a dangling todo that needs to reference an issue, and a few potentially dangerous as conversions I'd prefer to see removed. Other than that, great stuff!

fn encode(&self, data: Self::RequestMessage) -> Result<Vec<u8>, io::Error> {
match data {
GossipsubBroadcastRequest::NewTx(tx) => {
Ok(self.codec.encode(&*tx)?.as_bytes().into_owned())
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: The dereference is redundant here.

Suggested change
Ok(self.codec.encode(&*tx)?.as_bytes().into_owned())
Ok(self.codec.encode(&tx)?.as_bytes().into_owned())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in e9f1404

crates/services/p2p/src/codecs/postcard.rs Show resolved Hide resolved
{
let mut response = Vec::new();
socket
.take(self.max_response_size as u64)
Copy link
Contributor

Choose a reason for hiding this comment

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

This as conversion can theoretically error. Personally I'd prefer to use u64::try_from explicitly instead, or as suggested before - redefine max_response_size as a u64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, see #2460

crates/services/p2p/src/codecs/request_response.rs Outdated Show resolved Hide resolved
@acerone85 acerone85 requested a review from netrome November 26, 2024 17:37
Copy link
Contributor

@netrome netrome left a comment

Choose a reason for hiding this comment

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

Thanks for updating! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove P2PService NetworkCodec trait
2 participants