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

feat: add identify protocol example #537

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

acul71
Copy link
Contributor

@acul71 acul71 commented Mar 13, 2025

The Identify Protocol example was missing in py-libp2p, making it difficult for users to test and understand how the identify protocol works in a real peer-to-peer scenario.

How was it fixed?

Added a standalone identify.py example demonstrating the Identify Protocol in action. The example sets up two hosts:

  • Host A (Listener): Waits for an incoming identify request.
  • Host B (Dialer): Connects to Host A and initiates the identify protocol exchange.

The output includes details such as:

  • The remote peer's observed address
  • Supported protocols
  • Listen addresses
  • Public key information
python identify.py 
First host listening. Run this from another console:

python identify.py -p 8889 -d /ip4/0.0.0.0/tcp/8888/p2p/QmPF97e2nktakZVBzmn1uDGGZTaDyDLDtVR8MLLrH6UmYz

Waiting for incoming identify request...
python identify.py -p 8889 -d /ip4/0.0.0.0/tcp/8888/p2p/QmPF97e2nktakZVBzmn1uDGGZTaDyDLDtVR8MLLrH6UmYz
dialer (host_b) listening on /ip4/0.0.0.0/tcp/8889
Second host connecting to peer: QmPF97e2nktakZVBzmn1uDGGZTaDyDLDtVR8MLLrH6UmYz
Starting identify protocol...
Identify response:
  Public Key (Base64): CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCSPdishWS5vDq5H1x/T2Za8Sp6pQ10bwlyfpe0BdoNBtCAkVvVtR2EiSa7bQYVAMK6QH7Hzxdxs/LR4rwaK20fS9PLQ0I7v4Pkk+wCAne2EGSDrrXGRRC9tNaWWNujaRlNSmWlQH3V/W8Y2MGJIl6uezwKt3EUZQ12SMDgeTFGogvNnf5KYwlD3GczpButKgBkeeerscBZzJvm+JGIDqWBGZF4BPye691EGH8Nj2XMKm4Adj5Yew9UnSxI1mNIFksNEzhuN6j1NrzWjnL3cgIfUaYBS7Or/zatcayY+WiwQSZzcpVB0lIp06NLrbIx3GWdV6Y9LfZ9AlKfMMYG4ZTRAgMBAAE=
  Listen Addresses: ['/ip4/0.0.0.0/tcp/8888/p2p/QmPF97e2nktakZVBzmn1uDGGZTaDyDLDtVR8MLLrH6UmYz']
  Protocols: ['/ipfs/id/1.0.0', '/ipfs/ping/1.0.0']
  Observed Address: ['/ip4/127.0.0.1/tcp/37498']
  Protocol Version: ipfs/0.1.0
  Agent Version: py-libp2p/0.2.0

To-Do

  • Clean up commit history
  • Add or update documentation related to these changes
  • Add entry to the release notes

Cute Animal Picture

Cute Animal

@pacrob
Copy link
Member

pacrob commented Mar 15, 2025

Looking great, @acul71!

  • You've added the get_remote_address function across a number of libp2p modules, but I don't see it being tested anywhere. Ideally we'd have at least a basic test for each.
  • You can add the demo to the list in setup.py, down at the bottom within console_scripts.

I've left a few notes throughout the code. Let me know what you think.

@acul71
Copy link
Contributor Author

acul71 commented Mar 15, 2025

Looking great, @acul71!

  • You've added the get_remote_address function across a number of libp2p modules, but I don't see it being tested anywhere. Ideally we'd have at least a basic test for each.

TODO: Planning to add more tests

  • You can add the demo to the list in setup.py, down at the bottom within console_scripts.

DONE

I've left a few notes throughout the code. Let me know what you think.

Changed name form protocol.py to identify.py as you suggested, removed comments.

@seetadev
Copy link

Looking great, @acul71!

  • You've added the get_remote_address function across a number of libp2p modules, but I don't see it being tested anywhere. Ideally we'd have at least a basic test for each.

TODO: Planning to add more tests

  • You can add the demo to the list in setup.py, down at the bottom within console_scripts.

DONE

I've left a few notes throughout the code. Let me know what you think.

Changed name form protocol.py to identify.py as you suggested, removed comments.

@acul71 @pacrob : This looks great. We will need to add the identify example along with the other 3 examples of py-libp2p.

@pacrob : Thank you so much for your valuable guidance and helping us arrive at a good conclusion on Identify protocol example. The feedback shared is very helpful and will enable us to bootstrap other examples.

Our longer term goal is to have atleast all the features and examples that we see at rust-libp2p(please visit https://github.com/libp2p/rust-libp2p/tree/master/examples ) and js-libp2p (please visit https://github.com/libp2p/js-libp2p-examples/tree/main/examples ).

The feedback shared in this issue will be used in developing the other py-libp2p features and examples.

@acul71 : Wish if you could also share a document adding @pacrob 's feedback and pointers (discussed in this pull request) for new devs interested in developing key examples in py-libp2p as shared above.

@acul71
Copy link
Contributor Author

acul71 commented Mar 17, 2025

@acul71 : Wish if you could also share a document adding @pacrob 's feedback and pointers (discussed in this pull request) for new devs interested in developing key examples in py-libp2p as shared above.
@pacrob @seetadev
Here it is:

@acul71
Copy link
Contributor Author

acul71 commented Mar 17, 2025

py-libp2p Developer Guide: Feedback & Best Practices for Examples

This document compiles key feedback and pointers from recent code reviews and discussions. It is intended to serve as a reference for new contributors working on examples and core features for py-libp2p. Our goal is to align with the quality and functionality seen in rust-libp2p and js-libp2p examples.

1. Overview of py-libp2p Examples

py-libp2p aims to provide a Python implementation of libp2p protocols and features. Our longer-term goal is to match the breadth of features found in rust-libp2p examples and js-libp2p examples. The examples are not only demos but also act as living documentation for developers exploring protocol interactions.

2. Key Feedback from Recent Reviews

a. Testing and Quality Assurance

  • get_remote_address Function:
    • The get_remote_address function has been added across multiple modules. However, it currently lacks dedicated tests.
    • Action Item: Add at least basic tests for every module that uses this function. This will help ensure that any future changes do not inadvertently break its functionality.

b. Code Organization and Naming Conventions

  • File Renaming:
    • The file previously named protocol.py has been renamed to identify.py to better reflect its purpose.
  • Comments and Clean Code:
    • Unnecessary comments have been removed to clean up the codebase.
    • Action Item: When making further changes, ensure that any remaining inline comments are clear and add value without cluttering the code.

c. Examples and Demo Integration

  • Adding the Identify Protocol Example:
    • In addition to the three existing examples, there is now a need to include a dedicated Identify protocol example.
    • Action Item: Develop a comprehensive example for the Identify protocol and add it alongside the other demos.
  • Demo Registration in setup.py:
    • The demo has been added to the list in setup.py under console_scripts.
    • Action Item: Verify that the demo registration works correctly when installed via pip.

3. Best Practices for Developing Examples

a. Consistency with Rust and JS Examples

  • Reference Implementations:
    • Use rust-libp2p and js-libp2p examples as references for structure, feature set, and usability.
    • Ensure that the examples in py-libp2p are self-contained, well-documented, and serve as learning tools for protocol interactions.

b. Testing and Documentation

  • Write Tests:
    • Every new feature or protocol example should be accompanied by tests. Start with basic tests to validate the core functionality and then expand to cover edge cases.
  • Document the Code:
    • Maintain clear documentation within the code and in supplementary developer guides.
    • Use docstrings and inline comments sparingly, ensuring they add clarity.

c. Feedback Incorporation Process

  • Iterative Improvements:
    • Use feedback from code reviews (such as those provided by @pacrob) to guide further improvements.
    • Encourage discussion within the community for any proposed changes, ensuring that solutions are robust and future-proof.
  • Sharing Knowledge:
    • This document itself is a living reference. Update it as new feedback or guidelines emerge to help onboard future contributors.

4. Next Steps for Contributors

  1. Implement Testing:
    • Prioritize writing tests for the get_remote_address function across all relevant modules.
  2. Develop and Add Examples:
    • Complete the Identify protocol example and ensure it is consistent with other demos.
  3. Update Documentation:
    • Use this guide to inform any new documentation you produce. If you have further insights or improvements, consider updating this document.
  4. Continuous Feedback:
    • Engage with the team (including reviewing PRs and providing feedback like @pacrob) to keep improving the codebase and examples.

5. Additional Resources


This guide should help streamline contributions and ensure consistency across the project. Please refer to it whenever you work on new examples or core features in py-libp2p. Let’s continue to build a robust and developer-friendly libp2p ecosystem in Python!

@acul71
Copy link
Contributor Author

acul71 commented Mar 17, 2025

I will add a video in the next days

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.

3 participants