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
a7542b1
feat: Added flood publishing
Sep 17, 2025
2d25030
basic flow created
Sep 19, 2025
91bbca5
documentation added
Sep 19, 2025
0013612
Merge branch 'main' into added-flooding-publishing
SuchitraSwain Sep 22, 2025
c87b5e0
Merge branch 'main' into added-flooding-publishing
seetadev Sep 22, 2025
d0f4232
Merge branch 'main' into added-flooding-publishing
seetadev Sep 22, 2025
2df06f1
Address ci fail
bomanaps Sep 22, 2025
04b113a
Merge branch 'main' into added-flooding-publishing
SuchitraSwain Sep 23, 2025
60a129f
test fixed
Sep 23, 2025
3b926e5
fix: add missing newline at end of file in conftest.py
yashksaini-coder Sep 23, 2025
8ffaa24
refactor: update FloodSub examples to use Multiaddr and PeerInfo for …
yashksaini-coder Sep 23, 2025
4f3ef2b
refactor: streamline peer connection handling in FloodSub interoperab…
yashksaini-coder Sep 23, 2025
7e6ec8c
refactor: remove redundant module references from FloodSub examples d…
yashksaini-coder Sep 23, 2025
97bdfb4
Merge branch 'main' into added-flooding-publishing
yashksaini-coder Sep 24, 2025
621324a
Merge branch 'main' into added-flooding-publishing
seetadev Sep 24, 2025
1db6930
Merge branch 'main' into added-flooding-publishing
SuchitraSwain Sep 25, 2025
8e6ba8e
Merge branch 'main' into added-flooding-publishing
yashksaini-coder Sep 30, 2025
e329499
Merge branch 'main' into added-flooding-publishing
acul71 Oct 4, 2025
822469e
Merge branch 'main' into added-flooding-publishing
yashksaini-coder Oct 5, 2025
c051e54
Linting fix applied
yashksaini-coder Oct 5, 2025
d7c86c5
Merge branch 'libp2p:main' into added-flooding-publishing
yashksaini-coder Oct 6, 2025
98f992f
fix: ensure newline at end of file in 908.bugfix.rst
yashksaini-coder Oct 8, 2025
ea64779
chore: remove outdated FloodSub implementation documentation and PR d…
yashksaini-coder Oct 8, 2025
8e12685
refactor: enhance FloodSub behavior in GossipSub for improved message…
yashksaini-coder Oct 8, 2025
624d63f
test: enhance flood publish tests with timeouts and logging for bette…
yashksaini-coder Oct 8, 2025
d9396ed
comment resolved
Oct 9, 2025
cd372dd
ruff check
Oct 9, 2025
2c5590f
Merge branch 'main' into added-flooding-publishing
yashksaini-coder Oct 10, 2025
9f698d3
Merge branch 'main' into added-flooding-publishing
SuchitraSwain Oct 11, 2025
571b00e
Merge branch 'libp2p:main' into added-flooding-publishing
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
25 changes: 25 additions & 0 deletions docs/examples.floodsub.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FloodSub Examples
=================

This section contains examples demonstrating the FloodSub pubsub implementation.

.. toctree::
:maxdepth: 1

Simple PubSub Example
---------------------

.. automodule:: examples.floodsub.simple_pubsub
:members:

Basic Example
-------------

.. automodule:: examples.floodsub.basic_example
:members:

Multi-Node PubSub Example
-------------------------

.. automodule:: examples.floodsub.multi_node_pubsub
:members:
1 change: 1 addition & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Examples
examples.echo_quic
examples.ping
examples.pubsub
examples.floodsub
examples.circuit_relay
examples.kademlia
examples.mDNS
Expand Down
174 changes: 174 additions & 0 deletions docs/floodsub_discussion.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
FloodSub Implementation Discussion
==================================

This document provides a comprehensive discussion of the FloodSub implementation in py-libp2p, including the new flood_publish feature for GossipSub.

Overview
--------

FloodSub is a simple pubsub protocol that implements a flooding-based message distribution mechanism. Unlike more sophisticated protocols like GossipSub, FloodSub forwards every message to all connected peers, ensuring maximum message propagation at the cost of bandwidth efficiency.

Key Features
------------

1. **Simple Flooding Algorithm**: Messages are forwarded to all peers subscribed to a topic
2. **No Message Caching**: Each message is processed and forwarded immediately
3. **Guaranteed Delivery**: High probability of message delivery due to flooding behavior
4. **Bandwidth Intensive**: Uses more bandwidth compared to selective forwarding protocols

Implementation Details
----------------------

The FloodSub implementation in py-libp2p follows the libp2p specification and provides:

- Protocol ID: ``/floodsub/1.0.0``
- Message validation and forwarding
- Peer management and topic subscriptions
- Integration with the libp2p pubsub framework

Flood Publish Feature for GossipSub
-----------------------------------

A significant enhancement to the pubsub system is the introduction of the ``flood_publish`` option for GossipSub. This feature provides a hybrid approach that combines the benefits of both FloodSub and GossipSub.

How It Works
~~~~~~~~~~~~

When ``flood_publish=True`` is set on a GossipSub instance:

1. **Initial Publishing**: Messages originating from the local node are sent to ALL peers subscribed to the topic (flooding behavior)
2. **Message Forwarding**: Subsequent message forwarding follows normal GossipSub mesh-based routing
3. **Selective Application**: Only affects messages published by the local node, not forwarded messages

Benefits
~~~~~~~~

- **Improved Reliability**: Ensures better initial message propagation in sparse networks
- **Hybrid Efficiency**: Combines FloodSub's reliability with GossipSub's bandwidth efficiency
- **Backward Compatibility**: Normal GossipSub behavior when ``flood_publish=False``
- **Configurable**: Can be enabled/disabled based on network requirements

Use Cases
~~~~~~~~~

The flood_publish feature is particularly useful for:

- **Sparse Networks**: Where normal GossipSub mesh formation might be insufficient
- **Critical Messages**: When maximum propagation probability is required
- **Bootstrap Scenarios**: Initial message distribution in new networks
- **Debugging**: Ensuring message delivery during development

Configuration
~~~~~~~~~~~~~

The flood_publish feature can be configured when creating a GossipSub instance:

.. code-block:: python

from libp2p.pubsub.gossipsub import GossipSub
from libp2p.tools.constants import GOSSIPSUB_PROTOCOL_ID

# Create GossipSub with flood_publish enabled
gossipsub = GossipSub(
protocols=[GOSSIPSUB_PROTOCOL_ID],
flood_publish=True # Enable flooding for initial publishes
)

Performance Considerations
--------------------------

Bandwidth Usage
~~~~~~~~~~~~~~~

- **FloodSub**: High bandwidth usage due to flooding all messages
- **GossipSub**: Lower bandwidth usage with selective forwarding
- **GossipSub + flood_publish**: Moderate bandwidth usage (flooding only for initial publishes)

Network Scalability
~~~~~~~~~~~~~~~~~~~

- **FloodSub**: Limited scalability due to O(n) message forwarding
- **GossipSub**: Better scalability with mesh-based routing
- **GossipSub + flood_publish**: Good scalability with improved reliability

Message Delivery
~~~~~~~~~~~~~~~~

- **FloodSub**: High delivery probability but with redundancy
- **GossipSub**: Good delivery probability with efficiency
- **GossipSub + flood_publish**: High delivery probability with controlled redundancy

Interoperability
----------------

The py-libp2p FloodSub implementation is designed to be interoperable with other libp2p implementations:

- **Protocol Compliance**: Follows the libp2p FloodSub specification
- **Message Format**: Uses standard protobuf message format
- **Peer Discovery**: Compatible with standard libp2p peer discovery mechanisms

Testing and Validation
----------------------

The implementation includes comprehensive testing:

- **Unit Tests**: Core functionality and edge cases
- **Integration Tests**: End-to-end message flow testing
- **Interoperability Tests**: Compatibility with other libp2p implementations
- **Performance Tests**: Bandwidth and latency measurements

Examples
--------

Basic FloodSub Usage
~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

from libp2p import new_host
from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.pubsub import Pubsub
from libp2p.tools.constants import FLOODSUB_PROTOCOL_ID

# Create host and FloodSub router
host = new_host()
floodsub = FloodSub(protocols=[FLOODSUB_PROTOCOL_ID])
pubsub = Pubsub(host=host, router=floodsub)

# Subscribe to topic
subscription = await pubsub.subscribe("my-topic")

# Publish message
await pubsub.publish("my-topic", b"Hello, FloodSub!")

GossipSub with Flood Publish
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

from libp2p.pubsub.gossipsub import GossipSub
from libp2p.tools.constants import GOSSIPSUB_PROTOCOL_ID

# Create GossipSub with flood_publish enabled
gossipsub = GossipSub(
protocols=[GOSSIPSUB_PROTOCOL_ID],
degree=10,
flood_publish=True # Enable flooding for initial publishes
)

Future Considerations
---------------------

Potential improvements and considerations for future development:

1. **Peer Scoring Integration**: When peer scoring is implemented, flood_publish should respect score thresholds
2. **Dynamic Configuration**: Runtime configuration of flood_publish based on network conditions
3. **Metrics and Monitoring**: Enhanced metrics for flood_publish usage and effectiveness
4. **Advanced Routing**: Integration with more sophisticated routing algorithms

Conclusion
----------

The FloodSub implementation provides a reliable, simple pubsub solution for libp2p networks. The addition of the flood_publish feature to GossipSub offers a valuable hybrid approach that balances reliability and efficiency, making it suitable for a wide range of network scenarios and use cases.

The implementation maintains compatibility with the libp2p specification while providing the flexibility needed for different network requirements and performance characteristics.
150 changes: 150 additions & 0 deletions examples/floodsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# FloodSub Examples

This directory contains examples demonstrating FloodSub functionality in py-libp2p.

## What is FloodSub?

FloodSub is the simplest pubsub routing algorithm in libp2p. It works by flooding messages to all connected peers that are subscribed to a topic. While not as efficient as more advanced algorithms like GossipSub, FloodSub is:

- Simple to understand and implement
- Reliable for small networks
- Useful for testing and development
- A good foundation for learning pubsub concepts

## Examples

### 1. Simple PubSub (`simple_pubsub.py`)

A basic example showing:

- Creating two libp2p hosts with FloodSub
- Connecting them together
- Publishing and subscribing to messages
- Basic message flow

**Run it:**

```bash
python examples/floodsub/simple_pubsub.py
```

**What it does:**

1. Creates two libp2p nodes with FloodSub
1. Connects them
1. One node subscribes to a topic
1. The other node publishes messages
1. Shows received messages

### 2. Multi-Node PubSub (`multi_node_pubsub.py`)

A more advanced example showing:

- Multiple nodes (3) in a network
- Different nodes subscribing to different topics
- Publishing from multiple nodes
- Message flooding across the network

**Run it:**

```bash
python examples/floodsub/multi_node_pubsub.py
```

**What it does:**

1. Creates 3 libp2p nodes with FloodSub
1. Connects them in a chain: A -> B -> C
1. Each node subscribes to different topics
1. Each node publishes messages to different topics
1. Shows how messages flood through the network

## Key Concepts

### Publishing Messages

```python
# Publish a message to a topic
await pubsub.publish("my-topic", b"Hello, FloodSub!")
```

### Subscribing to Topics

```python
# Subscribe to a topic
subscription = await pubsub.subscribe("my-topic")

# Receive messages
message = await subscription.get()
print(f"Received: {message.data.decode()}")
```

### Creating FloodSub

```python
from libp2p.pubsub.floodsub import FloodSub
from libp2p.pubsub.pubsub import Pubsub
from libp2p.tools.constants import FLOODSUB_PROTOCOL_ID

# Create FloodSub router
floodsub = FloodSub(protocols=[FLOODSUB_PROTOCOL_ID])

# Create Pubsub with FloodSub
pubsub = Pubsub(
host=host,
router=floodsub,
strict_signing=False, # Disable for simplicity
)
```

## Interoperability

FloodSub in py-libp2p is designed to be compatible with other libp2p implementations:

- **go-libp2p**: Uses the same FloodSub protocol
- **js-libp2p**: Compatible with js-libp2p FloodSub
- **rust-libp2p**: Should work with rust-libp2p FloodSub

See the interoperability tests in `tests/interop/` for examples of cross-language communication.

## Protocol Details

FloodSub uses the `/floodsub/1.0.0` protocol and follows the libp2p pubsub specification:

1. **Message Format**: Uses protobuf messages as defined in the libp2p pubsub spec
1. **Flooding Algorithm**: Forwards messages to all connected peers subscribed to the topic
1. **Deduplication**: Uses message IDs to prevent duplicate message processing
1. **Subscription Management**: Handles topic subscriptions and unsubscriptions

## Performance Characteristics

- **Latency**: Low (direct flooding)
- **Bandwidth**: High (floods to all peers)
- **Scalability**: Poor (doesn't scale well with network size)
- **Reliability**: High (simple, no complex routing)

## Use Cases

FloodSub is suitable for:

- Small networks (< 100 peers)
- Testing and development
- Learning pubsub concepts
- Networks where simplicity is more important than efficiency
- Bootstrapping more complex pubsub algorithms

## Limitations

- Doesn't scale well with network size
- High bandwidth usage
- No intelligent routing or optimization
- All peers receive all messages for subscribed topics

## Next Steps

After understanding FloodSub, consider exploring:

- **GossipSub**: More efficient pubsub algorithm
- **Custom Validators**: Message validation and filtering
- **Message Signing**: Cryptographic message authentication
- **Topic Discovery**: Finding peers interested in topics
1 change: 1 addition & 0 deletions examples/floodsub/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# FloodSub examples for py-libp2p
Loading