Skip to content

chore(kad_dht): centralize shared values in common.py file #695

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

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

Conversation

LVivona
Copy link

@LVivona LVivona commented Jun 20, 2025

What was wrong?

The kad_dht module contained repeated definitions of commonly used variables, leading to redundancy and reduced maintainability.

How was it fixed?

A new common.py file was introduced to centralize shared default values and constants, promoting reuse and improving code clarity.

To-Do

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

Cute Animal Picture

Copy link
Contributor

@sumanjeet0012 sumanjeet0012 left a comment

Choose a reason for hiding this comment

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

@LVivona The code looks clean and well-structured.
I’ve shared a few suggestions which may be addressed.

@@ -243,7 +239,7 @@ async def _ping_peer(self, peer_id: ID) -> bool:
raise ValueError(f"Peer {peer_id} not in bucket")

# Default protocol ID for Kademlia DHT
protocol_id = TProtocol("/ipfs/kad/1.0.0")
protocol_id = PROTOCOL_ID
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use PROTOCOL_ID directly in the stream creation statement:
stream = await self.host.new_stream(peer_id, [PROTOCOL_ID])
This makes the assignment protocol_id = PROTOCOL_ID unnecessary and can be removed.

Copy link
Author

@LVivona LVivona Jun 21, 2025

Choose a reason for hiding this comment

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

Within peer_routing.py there exist PROTOCOL_ID as well, should I set it to just PROTOCOL_ID, or is it necessary within the state of the object PeerRouting

PeerRouting

    def __init__(self, host: IHost, routing_table: RoutingTable):
        """
        Initialize the peer routing service.

        :param host: The libp2p host
        :param routing_table: The Kademlia routing table

        """
        self.host = host
        self.routing_table = routing_table
        self.protocol_id = PROTOCOL_ID

Copy link
Contributor

Choose a reason for hiding this comment

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

@LVivona Since PROTOCOL_ID is now defined in common.py, it can be imported and used as a global constant wherever needed. Therefore, there is no longer a need to store it in the state of the PeerRouting object.

So you can remove self.protocol_id = PROTOCOL_ID and use PROTOCOL_ID directly wherever applicable.

Copy link
Author

Choose a reason for hiding this comment

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

Okay perfect

@LVivona LVivona requested a review from sumanjeet0012 June 21, 2025 19:10
@seetadev seetadev removed the request for review from sumanjeet0012 June 21, 2025 19:49
@seetadev seetadev requested a review from sumanjeet0012 June 21, 2025 19:49
)
from .pb.kademlia_pb2 import Message
Copy link
Contributor

Choose a reason for hiding this comment

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

Import this using the multiline import style to maintain consistency

Copy link
Author

Choose a reason for hiding this comment

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

My bad I'm not sure how I missed that one.

@LVivona LVivona requested a review from sumanjeet0012 June 22, 2025 04:28
@LVivona
Copy link
Author

LVivona commented Jun 22, 2025

While running the workflow locally, I noticed there was a test tests/core/kad_dht/test_unit_peer_routing.py that asserted the attribute protocol_id. Since it’s no longer used, I removed that assertion from the test.

Copy link
Contributor

@sumanjeet0012 sumanjeet0012 left a comment

Choose a reason for hiding this comment

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

@LVivona The pull request looks good to me. All requested changes have been addressed.

@sumanjeet0012
Copy link
Contributor

@seetadev All feedback has been addressed. Please run the CI/CD tests, and if all tests pass, we can proceed with merging the pull request.

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.

2 participants