-
Notifications
You must be signed in to change notification settings - Fork 5
/
message.js
266 lines (232 loc) · 6.91 KB
/
message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { CID } from 'multiformats/cid'
import * as Prefix from './prefix.js'
import * as gen from './gen/message.js'
const MAX_PRIORITY = Math.pow(2, 31) - 1
// https://github.com/ipfs/go-bitswap/blob/81393bcd77fb6ea8470057adb5f7acc52b195b5f/internal/defaults/defaults.go#L21
const MAX_MESSAGE_SIZE = 16 * 1024
/**
* Overhead added to the message when either block and blockPresence are non
* empty.
* - 2 is the size of the varint used to declare the new embedded messages
* - 8 (4 x 2) is the size of the varint used to declare embedded messages
* payload when the total message size is 4 MB
*/
const NON_EMPTY_OVERHEAD = 2 + 8
/**
* Overhead added by a new wantlist entry (without considering the cid field size)
* - 1 is the varint which declare the new embedded message
* - 1 is the varint which declare the block field (a CID)
* - 1 is the varint which declare the priority field
* - 1 is the varint of the priority field value
* - 1 is the varint which declare the cancel field
* - 1 is the varint of the cancel field value
* - 1 is the varint which declare the wantType field
* - 1 is the varint of the wantType field value
* - 1 is the varint which declare the sendDontHave field
* - 1 is the varint of the sendDontHave field value
*/
const NEW_ENTRY_OVERHEAD = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
/**
* An arbitrary percentage added to minimize the probability of false negatives
* since this is an estimated algorithm.
*/
const ADDED_ESTIMATION_PERCENTAGE = 0.1
export class Entry {
/**
* @param {import('multiformats').UnknownLink} cid
* @param {Object} [options]
* @param {number} [options.priority]
* @param {boolean} [options.cancel]
* @param {gen.Message.Wantlist.WantType} [options.wantType]
* @param {boolean} [options.sendDontHave]
*/
constructor (cid, options = {}) {
const { priority, cancel, wantType, sendDontHave } = options
this.cid = cid
this.priority = priority
this.cancel = Boolean(cancel)
this.wantType = wantType || 0
this.sendDontHave = Boolean(sendDontHave)
if (this.priority == null || this.priority < 0) {
this.priority = 1
} else if (this.priority > MAX_PRIORITY) {
this.priority = MAX_PRIORITY
}
if (!gen.Message.Wantlist.WantType[this.wantType]) {
this.wantType = 0
}
}
/**
* @param {gen.Message.Wantlist.Entry} raw
*/
static fromRaw (raw) {
const wantType = raw.wantType
const sendDontHave = raw.sendDontHave
const cid = CID.decode(raw.block)
return new Entry(cid, { priority: raw.priority, cancel: raw.cancel, wantType, sendDontHave })
}
serialize () {
const { cid, priority, cancel, wantType, sendDontHave } = this
return {
block: cid.bytes,
priority,
cancel,
wantType,
sendDontHave
}
}
encode () {
return gen.Message.Wantlist.Entry.encode(this.serialize()).finish()
}
}
export class Wantlist {
/**
* @param {Object} [options]
* @param {Entry[]} [options.entries]
* @param {boolean} [options.full]
*/
constructor (options = {}) {
const { entries, full } = options
this.entries = entries || []
this.full = Boolean(full)
}
/**
* @param {gen.Message.Wantlist} raw
*/
static fromRaw (raw) {
return new Wantlist({
// @ts-ignore
entries: raw.entries.map(e => Entry.fromRaw(e)),
full: raw.full
})
}
serialize () {
return {
entries: this.entries.map(e => e.serialize()),
full: this.full
}
}
encode () {
return gen.Message.Wantlist.encode(this.serialize()).finish()
}
}
export class Block {
/**
* @param {Uint8Array|import('multiformats').UnknownLink} prefixOrCid
* @param {Uint8Array} data
*/
constructor (prefixOrCid, data) {
this.prefix = prefixOrCid instanceof Uint8Array
? prefixOrCid
: Prefix.encode(prefixOrCid)
this.data = data
}
/**
* @param {gen.Message.Block} raw
*/
static fromRaw (raw) {
return new Block(raw.prefix, raw.data)
}
serialize () {
return { prefix: this.prefix, data: this.data }
}
encode () {
return gen.Message.Block.encode(this.serialize()).finish()
}
}
export class BlockPresence {
/**
* @param {import('multiformats').UnknownLink} cid
* @param {gen.Message.BlockPresenceType} type
*/
constructor (cid, type) {
this.cid = cid
this.type = type
if (!gen.Message.BlockPresenceType[this.type]) {
this.type = 0
}
}
/**
* @param {gen.Message.BlockPresence} raw
*/
static fromRaw (raw) {
return new BlockPresence(CID.decode(raw.cid), raw.type)
}
serialize () {
return { cid: this.cid.bytes, type: this.type }
}
encode () {
return gen.Message.BlockPresence.encode(this.serialize()).finish()
}
}
/**
* As specified in the constants above, each Message can be 4MB maximum (after
* serialization).
* Each block can be at most 2 MB.
* Each CID is roughly 40 byte.
*/
export class Message {
/**
* @param {Object} [options]
* @param {Wantlist} [options.wantlist]
* @param {Block[]} [options.blocks]
* @param {BlockPresence[]} [options.blockPresences]
* @param {number} [options.pendingBytes]
*/
constructor (options = {}) {
const { wantlist, blocks, blockPresences, pendingBytes } = options
this.wantlist = wantlist || new Wantlist()
this.blocks = blocks || []
this.blockPresences = blockPresences || []
this.pendingBytes = pendingBytes || 0
// Validate pendingBytes
if (this.pendingBytes < 0) {
this.pendingBytes = 0
}
this.estimatedLength = this.encode().length + NON_EMPTY_OVERHEAD
}
/**
* @param {Uint8Array} encoded
*/
static decode (encoded) {
const decoded = gen.Message.decode(encoded)
return new Message({
// @ts-ignore
wantlist: Wantlist.fromRaw(decoded.wantlist),
// @ts-ignore
blocks: decoded.payload.map(b => Block.fromRaw(b)),
// @ts-ignore
blockPresences: decoded.blockPresences.map(b => BlockPresence.fromRaw(b)),
pendingBytes: decoded.pendingBytes
})
}
serialize () {
const { wantlist, blocks, blockPresences } = this
return {
wantlist: wantlist.serialize(),
payload: blocks.map(b => b.serialize()),
blockPresences: blockPresences.map(b => b.serialize()),
pendingBytes: this.pendingBytes * Number.MAX_SAFE_INTEGER
}
}
encode () {
return gen.Message.encode(this.serialize()).finish()
}
/**
* @param {Entry} entry
*/
addWantlistEntry (entry) {
const newSize = NEW_ENTRY_OVERHEAD + entry.cid.bytes.byteLength
if (this.estimateNewSizeAfter(newSize) > MAX_MESSAGE_SIZE) {
return false
}
this.wantlist.entries.push(entry)
this.estimatedLength += newSize
return true
}
estimateNewSizeAfter (newElement) {
return (this.estimatedLength + newElement) * (1 + ADDED_ESTIMATION_PERCENTAGE)
}
}
export const { WantType } = gen.Message.Wantlist
export const { BlockPresenceType } = gen.Message