Skip to content

Conversation

sumanjeet0012
Copy link
Contributor

@sumanjeet0012 sumanjeet0012 commented Oct 6, 2025

What was wrong?

• py-libp2p lacked Bitswap protocol support for IPFS block exchange
• Could not participate in decentralized file sharing networks
• Missing feature parity with Go, Rust, and JavaScript implementations
• Limited use cases for distributed storage and content delivery

How was it fixed?

Core Module Implementation

Added new libp2p.bitswap package:

Protocol Layer:
client.py - Bitswap protocol client with block request/response
block_store.py - Abstract storage interface + in-memory implementation
bitswap_message.py - Message serialization/deserialization
exceptions.py - Custom exception types

Merkle DAG:
dag.py - File chunking and DAG construction
dag_pb.py - DAG-PB encoding/decoding
unixfs.py - UnixFS metadata support
utils.py - CID computation utilities

Protocol Buffers:
pb/bitswap.proto, dag_pb.proto, unixfs.proto - Wire format definitions
• Generated .py and .pyi files with mypy stubs

Key Features

Bitswap Protocol: Bidirectional block exchange, want-list management, priority requests, block deduplication, multistream negotiation
Merkle DAG: File chunking, DAG construction/traversal, CID computation, raw + DAG-PB encoding
UnixFS: File metadata, directory structure, IPFS UnixFS v1 compatible
Block Storage: Pluggable BlockStore interface, in-memory implementation, async/await, type-safe CIDs
CLI Tool: Provider/client modes for file sharing with examples

Protocol Compliance

Bitswap: Compatible with go-ipfs and js-ipfs
DAG-PB: IPLD Merkle DAG encoding
UnixFS v1: IPFS file system abstraction
Multicodec/Multihash: Standard content addressing
• Protobuf definitions integrated in Makefile with mypy stubs

Examples

CLI Usage:

# Provider: Share a file
python examples/bitswap/bitswap.py --mode provider --file <file_path>

# Client: Download by CID
python examples/bitswap/bitswap.py --mode client --provider <multiaddr> --cid <root_cid> --output <output_path>

Benefits

Feature Parity: Matches Go/Rust/JavaScript implementations, enables IPFS network participation
Developer Experience: Type-safe API, async/await, documented examples, modular design
Performance: Efficient chunking, minimal memory footprint, proper async cleanup
Interoperability: Compatible with go-ipfs/js-ipfs, standard multiaddr/CID addressing

To-Do

  • Clean up commit history (rebase/squash if needed)
  • Add performance benchmarks (optional)
  • Consider adding more storage backend implementations (optional)

Cute Animal Picture

Cute Otter

- Created `test_dag.py` to test the MerkleDag class, including methods for adding bytes and files, fetching files, and verifying file information.
- Implemented tests for handling small and large data, progress tracking, and error handling.
- Added `test_dag_pb.py` to test the encoding and decoding of DAG-PB structures, including file and directory nodes.
- Introduced a verification script `verify_bitswap.py` to ensure the Bitswap implementation's structure and functionality, including imports, configuration, block store operations, message creation, and file structure validation.
@sumanjeet0012 sumanjeet0012 marked this pull request as ready for review October 21, 2025 08:35
@sumanjeet0012
Copy link
Contributor Author

@seetadev @pacrob Ready for Review.

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.

1 participant