Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4fd4da9
Initial commit for bitswap
sumanjeet0012 Oct 6, 2025
d0f0fb6
Merge branch 'libp2p:main' into feature/file_sharing
sumanjeet0012 Oct 6, 2025
8410c9d
Add integration tests for MerkleDag and DAG-PB functionality
sumanjeet0012 Oct 9, 2025
a502c4a
Merge branch 'feature/file_sharing' of https://github.com/sumanjeet00…
sumanjeet0012 Oct 9, 2025
97ee19b
removed extra files
sumanjeet0012 Oct 9, 2025
2989172
used bitswap compute cid function
sumanjeet0012 Oct 11, 2025
3400608
reverted deleted files
sumanjeet0012 Oct 11, 2025
44a55f8
Merge branch 'libp2p:main' into feature/file_sharing
sumanjeet0012 Oct 11, 2025
75f2e7e
checkpoint sharing data with root CID is working.
sumanjeet0012 Oct 12, 2025
46a245b
Merge branch 'main' into feature/file_sharing
sumanjeet0012 Oct 12, 2025
ff1a37f
Refactor Bitswap client and provider nodes for improved usability and…
sumanjeet0012 Oct 12, 2025
60c22f9
Enhance Bitswap client and provider nodes to get real IP address.
sumanjeet0012 Oct 13, 2025
1776668
Created a unified example for bitswap
sumanjeet0012 Oct 13, 2025
a0a37c5
Enhance Bitswap provider and client to dynamically find free ports an…
sumanjeet0012 Oct 13, 2025
7ba25ea
Add port parameter to run_client and handle StreamEOF exception in Bi…
sumanjeet0012 Oct 21, 2025
8fdd3d1
added newsfragments
sumanjeet0012 Oct 21, 2025
4906e6a
fixed some type errors
sumanjeet0012 Oct 21, 2025
d3300ed
regenerated protobuf files
sumanjeet0012 Oct 21, 2025
5b57836
Add Bitswap protobuf files to Makefile
sumanjeet0012 Oct 21, 2025
0adc731
added docs to doctree
sumanjeet0012 Oct 21, 2025
8012c05
Merge branch 'main' into feature/file_sharing
sumanjeet0012 Oct 21, 2025
0865abc
Add integration and unit tests for Bitswap protocol.
sumanjeet0012 Oct 21, 2025
371716f
removed extra docs
sumanjeet0012 Oct 21, 2025
0305260
Refactor Bitswap client and configuration for improved protocol handl…
sumanjeet0012 Oct 21, 2025
ca5b5f8
Refactor chunking logic to use a consistent default chunk size and re…
sumanjeet0012 Oct 23, 2025
c5f421c
Merge branch 'main' into feature/file_sharing
sumanjeet0012 Oct 23, 2025
dc3ba5d
Enhance Bitswap client to track DontHave responses and optimize block…
sumanjeet0012 Oct 25, 2025
8c9a83a
moved tests to tests/core/bitswap and fixed failing tests
sumanjeet0012 Oct 25, 2025
3db6927
fixed lint issues
sumanjeet0012 Oct 25, 2025
ad6cb48
Merge branch 'main' into feature/file_sharing
sumanjeet0012 Oct 25, 2025
9e5655e
Merge branch 'main' into feature/file_sharing
seetadev Oct 27, 2025
1c7c87e
Merge branch 'main' into feature/file_sharing
seetadev Nov 4, 2025
5c8cf1b
Merge branch 'main' into feature/file_sharing
seetadev Nov 4, 2025
30e0a58
Merge branch 'main' into feature/file_sharing
seetadev Nov 4, 2025
fb4c6f5
Add BlockUnavailableError to the Bitswap error handling
sumanjeet0012 Nov 4, 2025
e4477b1
set max block size to 63 kb
sumanjeet0012 Nov 4, 2025
fd3c220
Refactor logging in CID verification and DAG handling to use debug level
sumanjeet0012 Nov 4, 2025
50b1fe4
fixed lint issues using make pr
sumanjeet0012 Nov 4, 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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ PB = libp2p/crypto/pb/crypto.proto \
libp2p/relay/circuit_v2/pb/circuit.proto \
libp2p/relay/circuit_v2/pb/dcutr.proto \
libp2p/kad_dht/pb/kademlia.proto \
libp2p/discovery/rendezvous/pb/rendezvous.proto
libp2p/discovery/rendezvous/pb/rendezvous.proto \
libp2p/bitswap/pb/bitswap.proto \
libp2p/bitswap/pb/dag_pb.proto \
libp2p/bitswap/pb/unixfs.proto

PY = $(PB:.proto=_pb2.py)
PYI = $(PB:.proto=_pb2.pyi)
Expand Down
174 changes: 174 additions & 0 deletions docs/examples.bitswap.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
Bitswap File Sharing
====================

This example demonstrates peer-to-peer file sharing using the Bitswap protocol with Merkle DAG structure.

What is Bitswap?
----------------

Bitswap is a data exchange protocol that enables peers to request and share content-addressed blocks.
Files are split into blocks, organized in a Merkle DAG (Directed Acyclic Graph), and transferred efficiently between peers.

Quick Start
-----------

**Start a provider to share a file:**

.. code-block:: bash

python examples/bitswap/bitswap.py --mode provider --file myfile.pdf

**Download the file from another machine:**

.. code-block:: bash

python examples/bitswap/bitswap.py --mode client \
--provider "/ip4/192.168.1.10/tcp/8000/p2p/QmXXX..." \
--cid "017012207b..."

Usage
-----

Provider Mode
~~~~~~~~~~~~~

Share a file with other peers:

.. code-block:: bash

# Share a file (auto port)
python bitswap.py --mode provider --file document.pdf

# Share on specific port
python bitswap.py --mode provider --file photo.jpg --port 8000

The provider will:

1. Create a Merkle DAG from the file
2. Store all blocks in memory
3. Display the root CID and connection command
4. Wait for client connections

Client Mode
~~~~~~~~~~~

Download a file from a provider:

.. code-block:: bash

python bitswap.py --mode client \
--provider "/ip4/192.168.1.10/tcp/8000/p2p/QmProviderID..." \
--cid "01701220abc..."

# Specify output directory
python bitswap.py --mode client \
--provider "<multiaddr>" \
--cid "<root_cid>" \
--output ~/Downloads

The client will:

1. Connect to the provider
2. Request blocks by CID
3. Reconstruct the file from blocks
4. Save to disk with original filename

How It Works
------------

The implementation uses a Merkle DAG structure for all files:

1. **File Chunking**: Files are split into 256KB blocks
2. **DAG Creation**: Blocks are organized in a tree structure
3. **Root CID**: A single identifier for the entire file
4. **Block Exchange**: Client requests blocks, provider sends them
5. **Reconstruction**: Client rebuilds file from blocks

Example Session
---------------

Provider Output
~~~~~~~~~~~~~~~

.. code-block:: text

======================================================================
PROVIDER NODE STARTING
======================================================================
File: document.pdf
Size: 2.5 MB
Port: auto
======================================================================
Peer ID: QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow
Listening on 2 address(es):
/ip4/192.168.1.101/tcp/50182/p2p/QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow
/ip4/127.0.0.1/tcp/50182/p2p/QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow
✓ Bitswap started

Adding file to DAG...
📤 completed: 100.0% (2.5 MB/2.5 MB)

======================================================================
FILE READY TO SHARE!
======================================================================
Root CID: 01701220336d0f55eac9b5536e1d5f4a5429bbc9a7343f1e1d19b7757baf76b61f4f4731

📋 COPY THIS COMMAND TO RUN CLIENT:
======================================================================
python bitswap.py --mode client --provider "..." --cid "..."
======================================================================

Provider is running. Press Ctrl+C to stop...

Client Output
~~~~~~~~~~~~~

.. code-block:: text

======================================================================
CLIENT NODE STARTING
======================================================================
Provider: /ip4/192.168.1.101/tcp/50182/p2p/QmSK4b...
Root CID: 01701220336d0f55eac9b5536e1d5f4a5429bbc9a7343f1e...
Output dir: /tmp
======================================================================
Client Peer ID: QmTaLxNyPszMamvE7X8oYaso1eFceB8Dqjqo3v157kfioY
✓ Bitswap started

Connecting to provider...
✓ Connected

Fetching file...

======================================================================
FETCH STATISTICS:
======================================================================
Total blocks fetched: 12
✓ 1. 01701220... (256.0 KB)
✓ 2. 01551220... (256.0 KB)
...

======================================================================
FILE DOWNLOADED!
======================================================================
Size: 2.5 MB
Filename: document.pdf (from metadata)
✓ Saved to: /tmp/document.pdf
======================================================================

Features
--------

* **Content Addressing**: Files identified by cryptographic hash (CID)
* **Merkle DAG Structure**: Efficient handling of files of any size
* **Block-level Transfer**: Parallel block fetching for speed
* **File Metadata**: Original filename preserved in DAG
* **Resume Support**: Can request specific missing blocks
* **Integrity Verification**: All blocks verified by CID

See Also
--------

* :doc:`libp2p.bitswap` - Bitswap API documentation
* `Bitswap Protocol Specification <https://specs.ipfs.tech/bitswap-protocol/>`_
* :doc:`examples` - Other py-libp2p examples
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.bitswap
examples.circuit_relay
examples.kademlia
examples.mDNS
Expand Down
37 changes: 37 additions & 0 deletions docs/libp2p.bitswap.pb.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
libp2p.bitswap.pb package
=========================

Submodules
----------

libp2p.bitswap.pb.bitswap\_pb2 module
-------------------------------------

.. automodule:: libp2p.bitswap.pb.bitswap_pb2
:members:
:show-inheritance:
:undoc-members:

libp2p.bitswap.pb.dag\_pb\_pb2 module
-------------------------------------

.. automodule:: libp2p.bitswap.pb.dag_pb_pb2
:members:
:show-inheritance:
:undoc-members:

libp2p.bitswap.pb.unixfs\_pb2 module
------------------------------------

.. automodule:: libp2p.bitswap.pb.unixfs_pb2
:members:
:show-inheritance:
:undoc-members:

Module contents
---------------

.. automodule:: libp2p.bitswap.pb
:members:
:show-inheritance:
:undoc-members:
Loading
Loading