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

Merged
merged 41 commits into from
Feb 20, 2025

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.

@acerone85 acerone85 marked this pull request as ready for review October 29, 2024 09:48
rafal-ch
rafal-ch previously approved these changes Feb 18, 2025
Copy link
Contributor

@rafal-ch rafal-ch left a comment

Choose a reason for hiding this comment

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

Nice one 👍
Left just some nitpics and a question

rafal-ch
rafal-ch previously approved these changes Feb 18, 2025
netrome
netrome previously approved these changes Feb 18, 2025
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.

Approved, but I'd prefer to see d4e2198 reverted.

/// Returns the serialized object as a slice.
fn as_bytes(&self) -> Cow<[u8]>;
/// Returns the serialized object as a Vector.
fn to_vec(self) -> Vec<u8>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think using Cow is strictly better than Vec here. See my comment in the other thread.

xgreenx
xgreenx previously approved these changes Feb 18, 2025
@acerone85 acerone85 dismissed stale reviews from xgreenx and netrome via 37e4e23 February 18, 2025 21:19
netrome
netrome previously approved these changes Feb 19, 2025
Rename function to higlight how it works
@acerone85 acerone85 enabled auto-merge (squash) February 20, 2025 14:41
@acerone85 acerone85 merged commit f3159db into master Feb 20, 2025
32 of 33 checks passed
@acerone85 acerone85 deleted the 2368-remove-p2pservice-networkcodec-trait branch February 20, 2025 15:03
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
4 participants