|
| 1 | +Bitswap File Sharing |
| 2 | +==================== |
| 3 | + |
| 4 | +This example demonstrates peer-to-peer file sharing using the Bitswap protocol with Merkle DAG structure. |
| 5 | + |
| 6 | +What is Bitswap? |
| 7 | +---------------- |
| 8 | + |
| 9 | +Bitswap is a data exchange protocol that enables peers to request and share content-addressed blocks. |
| 10 | +Files are split into blocks, organized in a Merkle DAG (Directed Acyclic Graph), and transferred efficiently between peers. |
| 11 | + |
| 12 | +Quick Start |
| 13 | +----------- |
| 14 | + |
| 15 | +**Start a provider to share a file:** |
| 16 | + |
| 17 | +.. code-block:: bash |
| 18 | +
|
| 19 | + python examples/bitswap/bitswap.py --mode provider --file myfile.pdf |
| 20 | +
|
| 21 | +**Download the file from another machine:** |
| 22 | + |
| 23 | +.. code-block:: bash |
| 24 | +
|
| 25 | + python examples/bitswap/bitswap.py --mode client \ |
| 26 | + --provider "/ip4/192.168.1.10/tcp/8000/p2p/QmXXX..." \ |
| 27 | + --cid "017012207b..." |
| 28 | +
|
| 29 | +Usage |
| 30 | +----- |
| 31 | + |
| 32 | +Provider Mode |
| 33 | +~~~~~~~~~~~~~ |
| 34 | + |
| 35 | +Share a file with other peers: |
| 36 | + |
| 37 | +.. code-block:: bash |
| 38 | +
|
| 39 | + # Share a file (auto port) |
| 40 | + python bitswap.py --mode provider --file document.pdf |
| 41 | +
|
| 42 | + # Share on specific port |
| 43 | + python bitswap.py --mode provider --file photo.jpg --port 8000 |
| 44 | +
|
| 45 | +The provider will: |
| 46 | + |
| 47 | +1. Create a Merkle DAG from the file |
| 48 | +2. Store all blocks in memory |
| 49 | +3. Display the root CID and connection command |
| 50 | +4. Wait for client connections |
| 51 | + |
| 52 | +Client Mode |
| 53 | +~~~~~~~~~~~ |
| 54 | + |
| 55 | +Download a file from a provider: |
| 56 | + |
| 57 | +.. code-block:: bash |
| 58 | +
|
| 59 | + python bitswap.py --mode client \ |
| 60 | + --provider "/ip4/192.168.1.10/tcp/8000/p2p/QmProviderID..." \ |
| 61 | + --cid "01701220abc..." |
| 62 | +
|
| 63 | + # Specify output directory |
| 64 | + python bitswap.py --mode client \ |
| 65 | + --provider "<multiaddr>" \ |
| 66 | + --cid "<root_cid>" \ |
| 67 | + --output ~/Downloads |
| 68 | +
|
| 69 | +The client will: |
| 70 | + |
| 71 | +1. Connect to the provider |
| 72 | +2. Request blocks by CID |
| 73 | +3. Reconstruct the file from blocks |
| 74 | +4. Save to disk with original filename |
| 75 | + |
| 76 | +How It Works |
| 77 | +------------ |
| 78 | + |
| 79 | +The implementation uses a Merkle DAG structure for all files: |
| 80 | + |
| 81 | +1. **File Chunking**: Files are split into 256KB blocks |
| 82 | +2. **DAG Creation**: Blocks are organized in a tree structure |
| 83 | +3. **Root CID**: A single identifier for the entire file |
| 84 | +4. **Block Exchange**: Client requests blocks, provider sends them |
| 85 | +5. **Reconstruction**: Client rebuilds file from blocks |
| 86 | + |
| 87 | +Example Session |
| 88 | +--------------- |
| 89 | + |
| 90 | +Provider Output |
| 91 | +~~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +.. code-block:: text |
| 94 | +
|
| 95 | + ====================================================================== |
| 96 | + PROVIDER NODE STARTING |
| 97 | + ====================================================================== |
| 98 | + File: document.pdf |
| 99 | + Size: 2.5 MB |
| 100 | + Port: auto |
| 101 | + ====================================================================== |
| 102 | + Peer ID: QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow |
| 103 | + Listening on 2 address(es): |
| 104 | + /ip4/192.168.1.101/tcp/50182/p2p/QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow |
| 105 | + /ip4/127.0.0.1/tcp/50182/p2p/QmSK4bN4fDCxwvSVYvxxgHex2wob6VwzpEfpw8hc2Xxbow |
| 106 | + ✓ Bitswap started |
| 107 | +
|
| 108 | + Adding file to DAG... |
| 109 | + 📤 completed: 100.0% (2.5 MB/2.5 MB) |
| 110 | +
|
| 111 | + ====================================================================== |
| 112 | + FILE READY TO SHARE! |
| 113 | + ====================================================================== |
| 114 | + Root CID: 01701220336d0f55eac9b5536e1d5f4a5429bbc9a7343f1e1d19b7757baf76b61f4f4731 |
| 115 | +
|
| 116 | + 📋 COPY THIS COMMAND TO RUN CLIENT: |
| 117 | + ====================================================================== |
| 118 | + python bitswap.py --mode client --provider "..." --cid "..." |
| 119 | + ====================================================================== |
| 120 | +
|
| 121 | + Provider is running. Press Ctrl+C to stop... |
| 122 | +
|
| 123 | +Client Output |
| 124 | +~~~~~~~~~~~~~ |
| 125 | + |
| 126 | +.. code-block:: text |
| 127 | +
|
| 128 | + ====================================================================== |
| 129 | + CLIENT NODE STARTING |
| 130 | + ====================================================================== |
| 131 | + Provider: /ip4/192.168.1.101/tcp/50182/p2p/QmSK4b... |
| 132 | + Root CID: 01701220336d0f55eac9b5536e1d5f4a5429bbc9a7343f1e... |
| 133 | + Output dir: /tmp |
| 134 | + ====================================================================== |
| 135 | + Client Peer ID: QmTaLxNyPszMamvE7X8oYaso1eFceB8Dqjqo3v157kfioY |
| 136 | + ✓ Bitswap started |
| 137 | +
|
| 138 | + Connecting to provider... |
| 139 | + ✓ Connected |
| 140 | +
|
| 141 | + Fetching file... |
| 142 | +
|
| 143 | + ====================================================================== |
| 144 | + FETCH STATISTICS: |
| 145 | + ====================================================================== |
| 146 | + Total blocks fetched: 12 |
| 147 | + ✓ 1. 01701220... (256.0 KB) |
| 148 | + ✓ 2. 01551220... (256.0 KB) |
| 149 | + ... |
| 150 | +
|
| 151 | + ====================================================================== |
| 152 | + FILE DOWNLOADED! |
| 153 | + ====================================================================== |
| 154 | + Size: 2.5 MB |
| 155 | + Filename: document.pdf (from metadata) |
| 156 | + ✓ Saved to: /tmp/document.pdf |
| 157 | + ====================================================================== |
| 158 | +
|
| 159 | +Features |
| 160 | +-------- |
| 161 | + |
| 162 | +* **Content Addressing**: Files identified by cryptographic hash (CID) |
| 163 | +* **Merkle DAG Structure**: Efficient handling of files of any size |
| 164 | +* **Block-level Transfer**: Parallel block fetching for speed |
| 165 | +* **File Metadata**: Original filename preserved in DAG |
| 166 | +* **Resume Support**: Can request specific missing blocks |
| 167 | +* **Integrity Verification**: All blocks verified by CID |
| 168 | + |
| 169 | +See Also |
| 170 | +-------- |
| 171 | + |
| 172 | +* :doc:`libp2p.bitswap` - Bitswap API documentation |
| 173 | +* `Bitswap Protocol Specification <https://specs.ipfs.tech/bitswap-protocol/>`_ |
| 174 | +* :doc:`examples` - Other py-libp2p examples |
0 commit comments