Skip to content

Conversation

sumanjeet0012
Copy link
Contributor

@sumanjeet0012 sumanjeet0012 commented Sep 8, 2025

What was wrong?

Fixes #898

The py-libp2p library lacked a Rendezvous protocol implementation for peer discovery. This missing functionality created a gap between py-libp2p and other libp2p implementations (Go, Rust) that already support rendezvous-based peer discovery through namespaces.

How was it fixed?

1. Core Module Implementation

  • libp2p/discovery/rendezvous/ directory with complete protocol implementation:
    • service.py: Rendezvous service handling peer registrations and discovery
    • client.py: Low-level protocol client with trio structured concurrency
    • discovery.py: High-level discovery interface with caching and auto-refresh
    • config.py: Configuration constants and defaults
    • errors.py: Protocol-specific exception classes
    • messages.py: Protocol buffer message utilities
    • pb/rendezvous_pb2.py: Protocol buffer definitions

2. Key Features

  • Peer Registration: Advertise peers under namespaces with configurable TTL
  • Peer Discovery: Find peers by namespace with pagination support
  • Auto-refresh: Trio-based automatic re-registration before TTL expiry using structured concurrency
  • Caching: Discovery result caching with TTL management to reduce repeated queries
  • Resource Management: Clean startup/shutdown with proper trio nursery lifecycle
  • Error Handling: Comprehensive error types with proper null response handling
  • Type Safety: Full mypy compatibility with proper Optional/Union type annotations

3. Protocol Compliance

  • Full libp2p rendezvous protocol specification compliance
  • Protocol buffer message serialization/deserialization
  • Pagination support with limit and cookie-based navigation
  • Proper error status handling and propagation
  • Trio structured concurrency for reliable background operations

4. Examples and Integration

  • Complete example demonstrating server and client usage in examples/rendezvous/
  • Command-line interface for testing (--mode server/client/discover)
  • Optional refresh functionality showcase with trio nursery management
  • Seamless integration with existing py-libp2p host implementation
  • Proper logging integration with configurable debug output

5. Documentation and Testing

  • Comprehensive Documentation:
    • docs/examples.rendezvous.rst: Complete user guide with tutorials and real log outputs
    • docs/libp2p.discovery.rendezvous.rst: Full API documentation with cross-references
    • Sphinx build integration with proper toctree structure
  • Type Safety: All mypy type checking errors resolved
  • Logging Integration: Proper propagation configuration for debug visibility
  • Release Notes: Added newsfragment documenting the new feature

Benefits

  • Feature Parity: Brings py-libp2p closer to other libp2p implementations
  • Flexible Discovery: Application-specific peer discovery patterns with namespace organization
  • Bootstrap Alternative: Lightweight alternative to DHT-based discovery
  • Namespace Organization: Logical grouping of peers by service type

To-Do

  • Clean up commit history
  • Add documentation updates
  • Add entry to release notes

@sumanjeet0012 sumanjeet0012 marked this pull request as ready for review September 21, 2025 10:13
@sumanjeet0012
Copy link
Contributor Author

@seetadev @pacrob Rendezvous is working as expected, This PR is now ready for review.

@seetadev
Copy link
Contributor

Great work @sumanjeet0012 🎉

This is a very important addition to py-libp2p, and I really appreciate the level of depth and completeness you’ve brought into the Rendezvous implementation.

Thank you for keeping a note of each of these important aspects/ features:

  • Protocol compliance (peer registration, discovery, pagination, error handling)

  • Trio-based structured concurrency for auto-refresh and lifecycle management

  • Type safety and mypy compatibility

  • Documentation + examples that make it much easier for new contributors to try it out.

  • Logging integration and user-friendly CLI for testing

Wish if you could add a small document/discussion page along with a screencast recording/demo for the implementation. We will play it during the upcoming maintainer's meeting.

This closes a long-standing gap between py-libp2p and other implementations like Go/Rust, and brings us much closer to feature parity in peer discovery. The namespace-based discovery patterns also give developers a lot of flexibility beyond just bootstrapping or DHT-based methods.

Thanks again for pushing this forward — it’s a big step for the project.

CCing @pacrob, @lla-dane and @acul71 for their feedback and review. We plan to scale this module in dapps built using universal connectivity dapp.

Reviewing it in detail. Looking forward to getting this merged soon.

seetadev and others added 2 commits September 22, 2025 02:04
- Add rendezvous.proto to Makefile PB variable for protobuf generation
- Fix type annotations in errors.py and messages.py for mypy compatibility
- Changes ensure proper protobuf file generation and type checking
@acul71
Copy link
Contributor

acul71 commented Sep 22, 2025

@sumanjeet0012
While reviewing this PR I had this type errors:

(venv) luca@r17:~/PNL_Launchpad_Curriculum/Libp2p/py-libp2p$ make typecheck
pre-commit run mypy-local --all-files && pre-commit run pyrefly-local --all-files
run mypy with all dev dependencies present...............................Failed
- hook id: mypy-local
- exit code: 1

libp2p/discovery/rendezvous/pb/rendezvous_pb2.py:17: error: Call to untyped function "Default" in typed context  [no-untyped-call]
libp2p/discovery/rendezvous/messages.py:9: error: Module "libp2p.discovery.rendezvous.pb.rendezvous_pb2" has no attribute "Message"  [attr-defined]
libp2p/discovery/rendezvous/errors.py:5: error: Module "libp2p.discovery.rendezvous.pb.rendezvous_pb2" has no attribute "Message"  [attr-defined]
libp2p/discovery/rendezvous/service.py:26: error: Module "libp2p.discovery.rendezvous.pb.rendezvous_pb2" has no attribute "Message"  [attr-defined]
libp2p/discovery/rendezvous/client.py:33: error: Module "libp2p.discovery.rendezvous.pb.rendezvous_pb2" has no attribute "Message"  [attr-defined]
Found 5 errors in 5 files (checked 200 source files)

make: *** [Makefile:46: typecheck] Error 1

Fix here: sumanjeet0012#1

Fix build system integration and type annotations for rendezvous module
@sumanjeet0012
Copy link
Contributor Author

@acul71 I have merged your PR. Could you please verify if everything is functioning as expected?

@acul71
Copy link
Contributor

acul71 commented Sep 22, 2025

@acul71 I have merged your PR. Could you please verify if everything is functioning as expected?

Hi @sumanjeet0012
I had to updrade protobuf Python package from 4.25.86.32.1 to make it work
but in pyproject.toml is used

"protobuf>=4.25.0,<5.0.0",

Hi @pacrob can you help understand why this had happened ?
Is protobuf version needed to be upgraded ? ( remove constrain <5.0.0

Problem: After merging the PR, we encountered protobuf version compatibility issues causing import errors:

ImportError: cannot import name 'runtime_version' from 'google.protobuf'

Root Cause: Version mismatch between:

  • protoc compiler: 32.0 (generates files expecting newer protobuf features)
  • protobuf Python package: 4.25.8 (missing runtime_version module)

Solution: Upgraded protobuf Python package from 4.25.86.32.1

Results:

  • ✅ All type checking passes (mypy + pyrefly)
  • ✅ All tests pass (765 passed, 3 skipped)
  • ✅ Rendezvous module fully functional
  • ✅ No import errors

Note: The pyproject.toml constraint protobuf>=4.25.0,<5.0.0 should be updated to allow newer versions for better protoc compatibility.

Everything is now working as expected! 🎉

@sumanjeet0012
Copy link
Contributor Author

@seetadev A detailed discussion, along with a short demonstration video of Rendezvous Peer Discovery, is available here: GitHub Discussion #955.

@seetadev
Copy link
Contributor

@sumanjeet0012 : Thank you Sumanjeet. Appreciate your efforts. Reviewing the screencast and the discussion page too.

We will wait for feedback from @pacrob. Once the feedback points are marked resolved, we'll also do a final review together. Once I get a thumbs up from @pacrob, we will merge the PR.

Will merge this before the upcoming release of py-libp2p. Looking forward to seeing all the peer discovery methods.

@sumanjeet0012
Copy link
Contributor Author

sumanjeet0012 commented Sep 24, 2025

@pacrob I have removed the class Registration and added the peer Id validation in _handle_register.
Now I am working on adding test cases for this PR.

@sumanjeet0012
Copy link
Contributor Author

@pacrob Ready for final review.

register.ns = self.namespace
register.peer.id = self.peer_id.to_bytes()
register.peer.addrs.extend(self.addrs)
register.ttl = self.ttl
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking question: This will return the original ttl set, even if it's about to expire. Would it be helpful to peers if we proivded remaining ttl instead of the original value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, we should send the remaining TTL instead of initial TTL value.

Copy link
Member

@pacrob pacrob left a comment

Choose a reason for hiding this comment

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

Looks good! One question, but not blocking merge.

@seetadev
Copy link
Contributor

@sumanjeet0012 : Thank you for your continued efforts on implementing Rendezvous module in py-libp2p. Appreciate it.

@pacrob : +1, thank you so much for your review and guidance. Appreciate it.

I spent sometime trying to understand where Rendezvous peer discovery module would be useful. Wish to recommend @sumanjeet0012 to share some test cases around it. We are very near to merging this PR.

@sumanjeet0012 , @pacrob : Please visit #965

@sumanjeet0012
Copy link
Contributor Author

@pacrob I have changed the TTL calculation according to your suggestions.
@seetadev The mentioned test cases are already added only scaling tests are not present.

I don't know why kademlia test is failing in this PR.

@seetadev
Copy link
Contributor

@sumanjeet0012 : Great to hear. Appreciate your efforts.

In reference to kad-dht test failure, it is not just your PR but a couple of others too. Documented it at #949 . Wish if you could test locally and share review at #963.

@yashksaini-coder is making good progress on the PR. Will ask him to add you as a collaborator.

@sumanjeet0012
Copy link
Contributor Author

@seetadev Could you please confirm if there’s anything pending in this PR? If not, we can proceed with merging it.

@seetadev
Copy link
Contributor

seetadev commented Oct 4, 2025

@sumanjeet0012 : Hi Sumanjeet. Did a final review. This PR is good to merge. Appreciate your great efforts.

Let us know try developing some examples using Rendezvous module in py-libp2p.

@seetadev seetadev merged commit 847dc6d into libp2p:main Oct 4, 2025
28 checks passed
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.

Add Rendezvous Peer Discovery Module in py-libp2p
4 participants