-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.js
More file actions
384 lines (327 loc) · 9.53 KB
/
index.js
File metadata and controls
384 lines (327 loc) · 9.53 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
'use strict'
const WantManager = require('./want-manager')
const Network = require('./network')
const DecisionEngine = require('./decision-engine')
const Notifications = require('./notifications')
const logger = require('./utils').logger
const Stats = require('./stats')
const AbortController = require('abort-controller')
const anySignal = require('any-signal')
const defaultOptions = {
statsEnabled: false,
statsComputeThrottleTimeout: 1000,
statsComputeThrottleMaxQueueSize: 1000
}
const statsKeys = [
'blocksReceived',
'dataReceived',
'dupBlksReceived',
'dupDataReceived',
'blocksSent',
'dataSent',
'providesBufferLength',
'wantListLength',
'peerCount'
]
/**
* JavaScript implementation of the Bitswap 'data exchange' protocol
* used by IPFS.
*
* @param {Libp2p} libp2p
* @param {Blockstore} blockstore
* @param {Object} options
*/
class Bitswap {
constructor (libp2p, blockstore, options) {
this._libp2p = libp2p
this._log = logger(this.peerId)
this._options = Object.assign({}, defaultOptions, options)
// stats
this._stats = new Stats(statsKeys, {
enabled: this._options.statsEnabled,
computeThrottleTimeout: this._options.statsComputeThrottleTimeout,
computeThrottleMaxQueueSize: this._options.statsComputeThrottleMaxQueueSize
})
// the network delivers messages
this.network = new Network(libp2p, this, {}, this._stats)
// local database
this.blockstore = blockstore
this.engine = new DecisionEngine(this.peerId, blockstore, this.network, this._stats)
// handle message sending
this.wm = new WantManager(this.peerId, this.network, this._stats)
this.notifications = new Notifications(this.peerId)
}
get peerId () {
return this._libp2p.peerId
}
// handle messages received through the network
async _receiveMessage (peerId, incoming) {
try {
// Note: this allows the engine to respond to any wants in the message.
// Processing of the blocks in the message happens below, after the
// blocks have been added to the blockstore.
await this.engine.messageReceived(peerId, incoming)
} catch (err) {
// Log instead of throwing an error so as to process as much as
// possible of the message. Currently `messageReceived` does not
// throw any errors, but this could change in the future.
this._log('failed to receive message', incoming)
}
if (incoming.blocks.size === 0) {
return
}
const blocks = Array.from(incoming.blocks.values())
// quickly send out cancels, reduces chances of duplicate block receives
const wanted = blocks
.filter((b) => this.wm.wantlist.contains(b.cid))
.map((b) => b.cid)
this.wm.cancelWants(wanted)
await Promise.all(blocks.map(async (b) => {
const wasWanted = wanted.includes(b.cid)
await this._handleReceivedBlock(peerId, b, wasWanted)
}))
}
async _handleReceivedBlock (peerId, block, wasWanted) {
this._log('received block')
const has = await this.blockstore.has(block.cid)
this._updateReceiveCounters(peerId.toB58String(), block, has)
if (!wasWanted) {
return
}
await this.put(block)
}
_updateReceiveCounters (peerId, block, exists) {
this._stats.push(peerId, 'blocksReceived', 1)
this._stats.push(peerId, 'dataReceived', block.data.length)
if (exists) {
this._stats.push(peerId, 'dupBlksReceived', 1)
this._stats.push(peerId, 'dupDataReceived', block.data.length)
}
}
// handle errors on the receiving channel
_receiveError (err) {
this._log.error('ReceiveError: %s', err.message)
}
// handle new peers
_onPeerConnected (peerId) {
this.wm.connected(peerId)
}
// handle peers being disconnected
_onPeerDisconnected (peerId) {
this.wm.disconnected(peerId)
this.engine.peerDisconnected(peerId)
this._stats.disconnected(peerId)
}
/**
* @returns {void}
*/
enableStats () {
this._stats.enable()
}
/**
* @returns {void}
*/
disableStats () {
this._stats.disable()
}
/**
* Return the current wantlist for a given `peerId`
*
* @param {PeerId} peerId
* @returns {Map}
*/
wantlistForPeer (peerId) {
return this.engine.wantlistForPeer(peerId)
}
/**
* Return ledger information for a given `peerId`
*
* @param {PeerId} peerId
* @returns {Object}
*/
ledgerForPeer (peerId) {
return this.engine.ledgerForPeer(peerId)
}
/**
* Fetch a given block by cid. If the block is in the local
* blockstore it is returned, otherwise the block is added to the wantlist and returned once another node sends it to us.
*
* @param {CID} cid
* @param {Object} options
* @param {AbortSignal} options.abortSignal
* @returns {Promise<Block>}
*/
async get (cid, options = {}) {
const fetchFromNetwork = (cid, options) => {
// add it to the want list - n.b. later we will abort the AbortSignal
// so no need to remove the blocks from the wantlist after we have it
this.wm.wantBlocks([cid], options)
return this.notifications.wantBlock(cid, options)
}
let promptedNetwork = false
const loadOrFetchFromNetwork = async (cid, options) => {
try {
// have to await here as we want to handle ERR_NOT_FOUND
const block = await this.blockstore.get(cid, options)
return block
} catch (err) {
if (err.code !== 'ERR_NOT_FOUND') {
throw err
}
if (!promptedNetwork) {
promptedNetwork = true
this.network.findAndConnect(cid)
.catch((err) => this._log.error(err))
}
// we don't have the block locally so fetch it from the network
return fetchFromNetwork(cid, options)
}
}
// depending on implementation it's possible for blocks to come in while
// we do the async operations to get them from the blockstore leading to
// a race condition, so register for incoming block notifications as well
// as trying to get it from the datastore
const controller = new AbortController()
const signal = anySignal([options.signal, controller.signal])
const block = await Promise.race([
this.notifications.wantBlock(cid, {
signal
}),
loadOrFetchFromNetwork(cid, {
signal
})
])
// since we have the block we can now remove our listener
controller.abort()
return block
}
/**
* Fetch a a list of blocks by cid. If the blocks are in the local
* blockstore they are returned, otherwise the blocks are added to the wantlist and returned once another node sends them to us.
*
* @param {AsyncIterator<CID>} cids
* @param {Object} options
* @param {AbortSignal} options.abortSignal
* @returns {Promise<AsyncIterator<Block>>}
*/
async * getMany (cids, options = {}) {
for await (const cid of cids) {
yield this.get(cid, options)
}
}
/**
* Removes the given CIDs from the wantlist independent of any ref counts.
*
* This will cause all outstanding promises for a given block to reject.
*
* If you want to cancel the want for a block without doing that, pass an
* AbortSignal in to `.get` or `.getMany` and abort it.
*
* @param {Iterable<CID>} cids
* @returns {void}
*/
unwant (cids) {
if (!Array.isArray(cids)) {
cids = [cids]
}
this.wm.unwantBlocks(cids)
cids.forEach((cid) => this.notifications.unwantBlock(cid))
}
/**
* Removes the given keys from the want list. This may cause pending promises
* for blocks to never resolve. If you wish these promises to abort instead
* call `unwant(cids)` instead.
*
* @param {Iterable<CID>} cids
* @returns {void}
*/
cancelWants (cids) {
if (!Array.isArray(cids)) {
cids = [cids]
}
this.wm.cancelWants(cids)
}
/**
* Put the given block to the underlying blockstore and
* send it to nodes that have it in their wantlist.
*
* @param {Block} block
* @returns {Promise<void>}
*/
async put (block) {
await this.blockstore.put(block)
this._sendHaveBlockNotifications(block)
}
/**
* Put the given blocks to the underlying blockstore and
* send it to nodes that have it them their wantlist.
*
* @param {AsyncIterable<Block>} blocks
* @returns {AsyncIterable<Block>}
*/
async * putMany (blocks) {
for await (const block of this.blockstore.putMany(blocks)) {
this._sendHaveBlockNotifications(block)
yield block
}
}
/**
* Sends notifications about the arrival of a block
*
* @param {Block} block
*/
_sendHaveBlockNotifications (block) {
this.notifications.hasBlock(block)
this.engine.receivedBlocks([block])
// Note: Don't wait for provide to finish before returning
this.network.provide(block.cid).catch((err) => {
this._log.error('Failed to provide: %s', err.message)
})
}
/**
* Get the current list of wants.
*
* @returns {Iterator<WantlistEntry>}
*/
getWantlist () {
return this.wm.wantlist.entries()
}
/**
* Get the current list of partners.
*
* @returns {Iterator<PeerId>}
*/
peers () {
return this.engine.peers()
}
/**
* Get stats about the bitswap node.
*
* @returns {Object}
*/
stat () {
return this._stats
}
/**
* Start the bitswap node.
*
* @returns {void}
*/
start () {
this.wm.start()
this.network.start()
this.engine.start()
}
/**
* Stop the bitswap node.
*
* @returns {void}
*/
stop () {
this._stats.stop()
this.wm.stop()
this.network.stop()
this.engine.stop()
}
}
module.exports = Bitswap