Skip to content

Commit b9a50f6

Browse files
authored
Merge branch 'main' into drouting-peer
2 parents 331c903 + e5bbe2a commit b9a50f6

12 files changed

+358
-230
lines changed

BITSWAP.md

Lines changed: 2 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -1,198 +1,3 @@
1-
# ![Status: WIP](https://img.shields.io/badge/status-wip-orange.svg?style=flat-square) Bitswap
1+
# Bitswap
22

3-
**Author(s)**:
4-
- Adin Schmahmann
5-
- David Dias
6-
- Jeromy Johnson
7-
- Juan Benet
8-
9-
**Maintainer(s)**:
10-
11-
* * *
12-
13-
**Abstract**
14-
15-
Bitswap is a data exchange protocol for sending and receiving content addressed blocks of data. Bitswap has two primary jobs:
16-
1. Attempt to acquire blocks from the network that have been requested by the client.
17-
2. Send blocks in its possession to other peers who want them.
18-
19-
## Organization of this document
20-
21-
- [Introduction](#introduction)
22-
- [Bitswap Protocol Versions](#bitswap-protocol-versions)
23-
- [Bitswap 1.0.0](#bitswap-100)
24-
- [Bitswap 1.1.0](#bitswap-110)
25-
- [Bitswap 1.2.0](#bitswap-120)
26-
- [Implementations](#implementations)
27-
28-
## Introduction
29-
30-
Bitswap is a message-based protocol, as opposed to request-response. All messages contain wantlists, and/or blocks.
31-
Upon receiving a wantlist, a Bitswap server should eventually process and respond to the requester with either information about the block or the block itself.
32-
Upon receiving blocks, the client should send a `Cancel` notification to peers that have asked for the data, signifying that the client no longer wants the block.
33-
34-
Bitswap aims to be a simple protocol, so that implementations can balance aspects such as throughput, latency, fairness, memory usage, etc. for their specific requirements.
35-
36-
## Bitswap Protocol Versions
37-
38-
There are multiple Bitswap versions and more may evolve over time. We give brief overviews as to the changes behind each protocol version.
39-
40-
- `/ipfs/bitswap/1.0.0` - Initial version
41-
- `/ipfs/bitswap/1.1.0` - Support CIDv1
42-
- `/ipfs/bitswap/1.2.0` - Support Wantlist Have's and Have/DontHave responses
43-
44-
## Block Sizes
45-
46-
Bitswap implementations must support sending and receiving individual blocks of sizes less than or equal to 2MiB. Handling blocks larger than 2MiB is not recommended so as to keep compatibility with implementations which only support up to 2MiB.
47-
48-
## Bitswap 1.0.0
49-
50-
### Bitswap 1.0.0: Interaction Pattern
51-
52-
Given that a client C wants to fetch data from some server S:
53-
54-
1. C sends a message to S for the blocks it wants, via a stream `s_want`
55-
1. C may either send a complete wantlist, or an update to an outstanding wantlist
56-
2. C may reuse this stream to send new wants
57-
2. S sends back blocks on a stream `s_receive`. S may reuse this stream to send back subsequent responses.
58-
1. S should respect the relative priority of wantlist requests from C, with wants that have higher `priority` values being responded to first.
59-
3. When C no longer needs a block it previously asked for, it should send a `Cancel` message for that block to all peers from which it has not received a response about that block
60-
61-
### Bitswap 1.0.0: Message
62-
63-
A single Bitswap message may contain any of the following content:
64-
65-
1. The sender’s wantlist. This wantlist may either be the sender’s complete wantlist or just the changes to the sender’s wantlist that the receiver needs to know.
66-
2. Data blocks. These are meant to be blocks that the receiver has requested (i.e., blocks that are on the receiver’s wantlist as far as the sender is aware at the time of sending).
67-
68-
#### Bitswap 1.0.0: Wire Format
69-
70-
The wire format for Bitswap is simply a stream of Bitswap messages. The following protobuf describes the form of these messages. Note: all protobufs are described using proto3 syntax.
71-
72-
```protobuf
73-
message Message {
74-
message Wantlist {
75-
message Entry {
76-
bytes block = 1; // the block key, i.e. a CIDv0
77-
int32 priority = 2; // the priority (normalized). default to 1
78-
bool cancel = 3; // whether this revokes an entry
79-
}
80-
81-
repeated Entry entries = 1; // a list of wantlist entries
82-
bool full = 2; // whether this is the full wantlist. default to false
83-
}
84-
85-
Wantlist wantlist = 1;
86-
repeated bytes blocks = 2;
87-
```
88-
89-
### Bitswap 1.0.0: Protocol Format
90-
91-
All protocol messages sent over a stream are prefixed with the message length in
92-
bytes, encoded as an unsigned variable length integer as defined by the
93-
[multiformats unsigned-varint spec](https://github.com/multiformats/unsigned-varint).
94-
95-
All protocol messages must be less than or equal to 4MiB in size
96-
97-
## Bitswap 1.1.0
98-
99-
Bitswap 1.1.0 introduces a 'payload' field to the protobuf message and deprecates the
100-
existing 'blocks' field. The 'payload' field is an array of pairs of cid
101-
prefixes and block data. The cid prefixes are used to ensure the correct
102-
codecs and hash functions are used to handle the block on the receiving
103-
end.
104-
105-
It is otherwise identical to 1.0.0
106-
107-
### Bitswap 1.1.0: Wire Format
108-
109-
```protobuf
110-
message Message {
111-
message Entry {
112-
bytes block = 1; // CID of the block
113-
int32 priority = 2; // the priority (normalized). default to 1
114-
bool cancel = 3; // whether this revokes an entry
115-
}
116-
117-
repeated Entry entries = 1; // a list of wantlist entries
118-
bool full = 2; // whether this is the full wantlist. default to false
119-
}
120-
121-
message Block {
122-
bytes prefix = 1; // CID prefix (all of the CID components except for the digest of the multihash)
123-
bytes data = 2;
124-
}
125-
126-
Wantlist wantlist = 1;
127-
repeated Block payload = 3;
128-
}
129-
```
130-
131-
## Bitswap 1.2.0
132-
133-
Bitswap 1.2.0 extends the Bitswap 1.1.0 protocol with the three changes:
134-
1. Being able to ask if a peer has the data, not just to send the data
135-
2. A peer can respond that it does not have some data rather than just not responding
136-
3. Nodes can indicate on messages how much data they have queued to send to the peer they are sending the message to
137-
138-
### Bitswap 1.2.0: Interaction Pattern
139-
140-
Given that a client C wants to fetch data from some server S:
141-
142-
1. C opens a stream `s_want` to S and sends a message for the blocks it wants
143-
1. C may either send a complete wantlist, or an update to an outstanding wantlist
144-
2. C may reuse this stream to send new wants
145-
3. For each of the items in the wantlist C may ask if S has the block (i.e. a Have request) or for S to send the block (i.e. a block request). C may also ask S to send back a DontHave message in the event it doesn't have the block
146-
2. S responds back on a stream `s_receive`. S may reuse this stream to send back subsequent responses
147-
1. If C sends S a Have request for data S has (and is willing to give to C) it should respond with a Have, although it may instead respond with the block itself (e.g. if the block is very small)
148-
2. If C sends S a Have request for data S does not have (or has but is not willing to give to C) and C has requested for DontHave responses then S should respond with DontHave
149-
3. S may choose to include the number of bytes that are pending to be sent to C in the response message
150-
4. S should respect the relative priority of wantlist requests from C, with wants that have higher `priority` values being responded to first.
151-
3. When C no longer needs a block it previously asked for it should send a Cancel message for that request to any peers that have not already responded about that particular block. It should particularly send Cancel messages for Block requests (as opposed to Have requests) that have not yet been answered.
152-
153-
### Bitswap 1.2.0: Wire Format
154-
155-
```protobuf
156-
message Message {
157-
message Wantlist {
158-
enum WantType {
159-
Block = 0;
160-
Have = 1;
161-
}
162-
163-
message Entry {
164-
bytes block = 1; // CID of the block
165-
int32 priority = 2; // the priority (normalized). default to 1
166-
bool cancel = 3; // whether this revokes an entry
167-
WantType wantType = 4; // Note: defaults to enum 0, ie Block
168-
bool sendDontHave = 5; // Note: defaults to false
169-
}
170-
171-
repeated Entry entries = 1; // a list of wantlist entries
172-
bool full = 2; // whether this is the full wantlist. default to false
173-
}
174-
message Block {
175-
bytes prefix = 1; // CID prefix (all of the CID components except for the digest of the multihash)
176-
bytes data = 2;
177-
}
178-
179-
enum BlockPresenceType {
180-
Have = 0;
181-
DontHave = 1;
182-
}
183-
message BlockPresence {
184-
bytes cid = 1;
185-
BlockPresenceType type = 2;
186-
}
187-
188-
Wantlist wantlist = 1;
189-
repeated Block payload = 3;
190-
repeated BlockPresence blockPresences = 4;
191-
int32 pendingBytes = 5;
192-
}
193-
```
194-
195-
## Implementations
196-
197-
- <https://github.com/ipfs/go-bitswap>
198-
- <https://github.com/ipfs/js-ipfs-bitswap>
3+
Moved to https://specs.ipfs.tech/bitswap-protocol/

0 commit comments

Comments
 (0)