Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bde19ca
added flood publishing
mystical-prog Jun 29, 2025
fa99d32
Merge branch 'main' into flood-publishing
seetadev Jun 29, 2025
75a3749
added tests for flood publising
Khwahish29 Jun 30, 2025
4780904
fix lint
Khwahish29 Jun 30, 2025
9ddc245
Merge branch 'main' into flood-publishing
Khwahish29 Jul 8, 2025
ed67340
resolved merge conflicts
Khwahish29 Jul 8, 2025
ca8d494
Merge branch 'main' into flood-publishing
seetadev Jul 12, 2025
9f6b409
Merge branch 'main' into flood-publishing
seetadev Jul 15, 2025
1f70934
Merge branch 'main' into flood-publishing
seetadev Jul 19, 2025
2a3742b
Merge branch 'main' into flood-publishing
seetadev Jul 21, 2025
888cc6e
Merge branch 'main' into flood-publishing
seetadev Sep 17, 2025
70252b1
[905]: Enhancement: Fix Kademila DHT
Sep 19, 2025
405d377
Merge branch 'main' into 905-kademlia-dht
SuchitraSwain Sep 22, 2025
35562a8
Merge branch 'main' into 905-kademlia-dht
seetadev Sep 22, 2025
f6bdf36
Merge branch 'main' into 905-kademlia-dht
seetadev Sep 22, 2025
572746e
Merge branch 'main' into 905-kademlia-dht
SuchitraSwain Sep 23, 2025
6415caf
Merge branch 'main' into 905-kademlia-dht
SuchitraSwain Sep 25, 2025
579a681
Merge branch 'main' into 905-kademlia-dht
SuchitraSwain Sep 30, 2025
d13c68b
Merge branch 'libp2p:main' into 905-kademlia-dht
yashksaini-coder Oct 6, 2025
8fbd754
added the comments
Oct 7, 2025
81b8203
Refactor GossipSub to enable flood publishing option and enhance mess…
yashksaini-coder Oct 8, 2025
2287cf5
Enhance tests for peer routing and message propagation
yashksaini-coder Oct 8, 2025
42c739b
Refactor logging statements for clarity and consistency in KadDHT and…
yashksaini-coder Oct 8, 2025
2ebf603
Refactor logging statements for improved readability and consistency …
yashksaini-coder Oct 8, 2025
b4489b9
Refactor logging statements for consistency and clarity across multip…
yashksaini-coder Oct 8, 2025
3580bfc
Refactor logging statements for consistency and clarity across multip…
yashksaini-coder Oct 8, 2025
fbc39d7
Refactor indentation and logging statements for consistency in KadDHT…
yashksaini-coder Oct 9, 2025
ebc3032
Refactor logging statements for improved indentation and consistency …
yashksaini-coder Oct 9, 2025
d05a229
Refactor basic_example.py to use Multiaddr for listen addresses and i…
yashksaini-coder Oct 9, 2025
bdab6b1
Refactor KadDHT class to use instance variable for MIN_PEERS_THRESHOL…
yashksaini-coder Oct 9, 2025
b1d5fba
Refactor logging statement for improved readability in PeerRouting class
yashksaini-coder Oct 9, 2025
e2a2693
Refactor comments for improved clarity in GossipSub class
yashksaini-coder Oct 9, 2025
2106103
Refactor logging statements in YamuxStream and Yamux classes for impr…
yashksaini-coder Oct 9, 2025
cc4e984
Refactor logging statements in QUICListener and WebsocketListener cla…
yashksaini-coder Oct 9, 2025
cd325bb
Refactor logging statements for improved readability in QUICListener,…
yashksaini-coder Oct 9, 2025
202e0d4
Refactor logging statements for improved readability in WebsocketTran…
yashksaini-coder Oct 9, 2025
6f1aff7
Refactor test_temp_files_cleanup and add additional tests for sensiti…
yashksaini-coder Oct 9, 2025
db322c3
Refactor comments for clarity in KadDHT class; update type hint in QU…
yashksaini-coder Oct 9, 2025
b017e5e
Refactor return statement in KadDHT class for consistency; update QUI…
yashksaini-coder Oct 9, 2025
0abf411
Merge branch 'main' into 905-kademlia-dht
yashksaini-coder Oct 10, 2025
d59e254
Merge branch 'libp2p:main' into 905-kademlia-dht
yashksaini-coder Oct 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions examples/floodsub/basic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""
Basic FloodSub Example

This is a simple example that demonstrates FloodSub publishing and subscribing
without relying on test utilities. It shows the core functionality.

Run this example with:
python examples/floodsub/basic_example.py
"""

import logging
import sys

from multiaddr import Multiaddr
import trio

from libp2p import new_host
from libp2p.crypto.secp256k1 import create_new_key_pair
from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.pubsub import Pubsub
from libp2p.tools.async_service import background_trio_service
from libp2p.tools.constants import FLOODSUB_PROTOCOL_ID

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("floodsub_basic")


async def main() -> None:
"""Main function demonstrating basic FloodSub functionality."""
logger.info("Starting basic FloodSub example...")

# Create two hosts
key_pair1 = create_new_key_pair()
key_pair2 = create_new_key_pair()
host1 = new_host(
key_pair=key_pair1,
listen_addrs=[Multiaddr("/ip4/127.0.0.1/tcp/0")],
)

host2 = new_host(
key_pair=key_pair2,
listen_addrs=[Multiaddr("/ip4/127.0.0.1/tcp/0")],
)

# Create FloodSub routers
floodsub1 = FloodSub(protocols=[FLOODSUB_PROTOCOL_ID])
floodsub2 = FloodSub(protocols=[FLOODSUB_PROTOCOL_ID])

# Create Pubsub instances
pubsub1 = Pubsub(
host=host1,
router=floodsub1,
strict_signing=False, # Disable for simplicity
)

pubsub2 = Pubsub(
host=host2,
router=floodsub2,
strict_signing=False, # Disable for simplicity
)

# Start both pubsub services
async with background_trio_service(pubsub1):
async with background_trio_service(pubsub2):
await pubsub1.wait_until_ready()
await pubsub2.wait_until_ready()

logger.info(f"Host 1 ID: {host1.get_id()}")
logger.info(f"Host 2 ID: {host2.get_id()}")

# Start listening on both hosts
logger.info("Starting hosts...")
await host1.get_network().listen()
await host2.get_network().listen()
await trio.sleep(0.5) # Wait for hosts to start listening

# Connect the hosts
logger.info("Connecting hosts...")
from libp2p.peer.peerinfo import info_from_p2p_addr

peer_info = info_from_p2p_addr(
host2.get_addrs()[0].encapsulate(
Multiaddr(f"/p2p/{host2.get_id().pretty()}")
)
)
await host1.connect(peer_info)
await trio.sleep(1) # Wait for connection

# Subscribe to topic on host2
topic = "test-topic"
logger.info(f"Subscribing to topic: {topic}")
subscription = await pubsub2.subscribe(topic)
await trio.sleep(0.5) # Wait for subscription to propagate

# Publish messages from host1
messages = [
"Hello from FloodSub!",
"This is message number 2",
"FloodSub is working great!",
]

for i, message in enumerate(messages):
logger.info(f"Publishing message {i + 1}: {message}")
await pubsub1.publish(topic, message.encode())
await trio.sleep(0.5)

# Receive messages on host2
logger.info("Receiving messages...")
for i in range(len(messages)):
message = await subscription.get()
logger.info(f"Received message {i + 1}: {message.data.decode()}")
logger.info(f" From peer: {message.from_id.hex()}")
logger.info(f" Topics: {message.topicIDs}")

logger.info("Basic FloodSub example completed successfully!")


if __name__ == "__main__":
try:
trio.run(main)
except KeyboardInterrupt:
logger.info("Example interrupted by user")
sys.exit(0)
except Exception as e:
logger.error(f"Example failed: {e}")
sys.exit(1)
13 changes: 8 additions & 5 deletions libp2p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def new_swarm(
tls_client_config: ssl.SSLContext | None = None,
tls_server_config: ssl.SSLContext | None = None,
) -> INetworkService:
logger.debug(f"new_swarm: enable_quic={enable_quic}, listen_addrs={listen_addrs}")
logger.debug("new_swarm: enable_quic=%s, listen_addrs=%s", enable_quic, listen_addrs)
"""
Create a swarm instance based on the parameters.

Expand Down Expand Up @@ -227,7 +227,7 @@ def new_swarm(
)

addr = listen_addrs[0]
logger.debug(f"new_swarm: Creating transport for address: {addr}")
logger.debug("new_swarm: Creating transport for address: %s", addr)
transport_maybe = create_transport_for_multiaddr(
addr,
temp_upgrader,
Expand All @@ -241,14 +241,17 @@ def new_swarm(
raise ValueError(f"Unsupported transport for listen_addrs: {listen_addrs}")

transport = transport_maybe
logger.debug(f"new_swarm: Created transport: {type(transport)}")
logger.debug("new_swarm: Created transport: %s", type(transport))

# If enable_quic is True but we didn't get a QUIC transport, force QUIC
if enable_quic and not isinstance(transport, QUICTransport):
logger.debug(f"new_swarm: Forcing QUIC transport (enable_quic=True but got {type(transport)})")
logger.debug(
"new_swarm: Forcing QUIC transport (enable_quic=True but got %s)",
type(transport),
)
transport = QUICTransport(key_pair.private_key, config=quic_transport_opt)

logger.debug(f"new_swarm: Final transport type: {type(transport)}")
logger.debug("new_swarm: Final transport type: %s", type(transport))

# Generate X25519 keypair for Noise
noise_key_pair = create_new_x25519_key_pair()
Expand Down
Loading