-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathblock-router.js
408 lines (388 loc) · 11.9 KB
/
block-router.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'use strict'
const EventEmitter = require('events').EventEmitter
const config = require('./config')
const BlockCache = require('./block-cache')
const Block = require('./block')
const ReadableOffStream = require('./readable-off-stream')
const WritableOffStream = require('./writable-off-stream')
const Recycler = require('./recycler')
const URL = require('./off-url')
const bs58 = require('bs58')
const RPC = require('./rpc')
const Peer = require('./peer')
const Bucket = require('./bucket')
const cbor = require('cbor-js')
const toAb = require('to-array-buffer')
const abToB = require('arraybuffer-to-buffer')
const CuckooFilter = require('cuckoo-filter').CuckooFilter
const Scheduler = require('./scheduler')
const pth = require('path')
const fs = require('fs')
let _bc = new WeakMap()
let _mc = new WeakMap()
let _nc = new WeakMap()
let _rpc = new WeakMap()
let _scheduler = new WeakMap()
let _bucket = new WeakMap()
let _path = new WeakMap()
let _timeout = new WeakMap()
let _bucketPath = new WeakMap()
module.exports = class BlockRouter extends EventEmitter {
constructor (path, bucketPath) {
if (typeof path !== 'string') {
throw new TypeError('Invalid Path')
}
super()
let bucket = new Bucket(Peer.self.id, config.bucketSize)
_bucket.set(this, bucket)
_bucketPath.set(this, bucketPath)
let rpc = new RPC(bucket, this.rpcInterface())
_rpc.set(this, rpc)
_path.set(this, path)
let bc = new BlockCache(pth.join(path, config.blockPath), config.blockSize, config.blockCacheSize, this.cacheInterface(config.block))
let mc = new BlockCache(pth.join(path, config.miniPath), config.miniBlockSize, config.miniBlockCacheSize, this.cacheInterface(config.mini))
let nc = new BlockCache(pth.join(path, config.nanoPath), config.nanoBlockSize, config.nanoBlockCacheSize, this.cacheInterface(config.nano))
let scheduler = new Scheduler(rpc, bucket, bc, mc, nc)
_bc.set(this, bc)
_mc.set(this, mc)
_nc.set(this, nc)
_scheduler.set(this, scheduler)
scheduler.on('error', (err) => this.emit('error', err))
scheduler.on('locator', () => this.emit('locator'))
rpc.on('error', (err) => this.emit('error', err))
bc.on('block', (block)=> {
rpc.store(block.hash, config.block, block.data, () => {})
})
mc.on('block', (block)=> {
rpc.store(block.hash, config.mini, block.data, () => {})
})
nc.on('block', (block)=> {
rpc.store(block.hash, config.nano, block.data, () => {})
})
bc.on('full', () => {
this.emit('full', config.block)
})
mc.on('full', () => {
this.emit('full', config.mini)
})
nc.on('full', () => {
this.emit('full', config.nano)
})
bc.on('capacity', (capacity) => {
this.emit('capacity', config.block, capacity)
})
mc.on('capacity', (capacity) => {
this.emit('capacity', config.mini, capacity)
})
nc.on('capacity', (capacity) => {
this.emit('capacity', config.nano, capacity)
})
bucket.on('removed', () => {
this.emit('connection', bucket.count)
this.savePeers()
})
bucket.on('added', () => {
this.emit('connection', bucket.count)
this.savePeers()
})
bucket.on('updated', () => {
this.savePeers()
})
}
createWriteStreamWithRecycler (url, urls, temporary) {
if (!(url instanceof URL)) {
throw new TypeError('Invalid URL')
}
if (!url.streamLength) {
throw new TypeError('URL must have a stream length')
}
if (url.streamLength >= config.blockSize) {
let bc = _bc.get(this)
let recycler = new Recycler(config.blockSize, urls, bc, this)
return new WritableOffStream(config.blockSize, { bc, url, recycler, temporary })
} else if (url.streamLength >= config.miniBlockSize) {
let mc = _mc.get(this)
let recycler = new Recycler(config.miniBlockSize, urls, mc, this)
return new WritableOffStream(config.miniBlockSize, { bc: mc, url, recycler, temporary })
} else {
let nc = _nc.get(this)
let recycler = new Recycler(config.nanoBlockSize, urls, nc, this)
return new WritableOffStream(config.nanoBlockSize, { bc: nc, url, recycler, temporary })
}
}
createReadStream (url) {
if (!(url instanceof URL)) {
throw new TypeError('Invalid URL')
}
if (!url.streamLength) {
throw new TypeError('URL must have a stream length')
}
if (url.streamLength >= config.blockSize) {
let bc = _bc.get(this)
return new ReadableOffStream(url, config.blockSize, bc)
} else if (url.streamLength >= config.miniBlockSize) {
let mc = _mc.get(this)
return new ReadableOffStream(url, config.miniBlockSize, mc)
} else {
let nc = _nc.get(this)
return new ReadableOffStream(url, config.nanoBlockSize, nc)
}
}
createWriteStream (url, temporary) {
if (!(url instanceof URL)) {
throw new TypeError('Invalid URL')
}
if (!url.streamLength) {
throw new TypeError('URL must have a stream length')
}
if (url.streamLength >= config.blockSize) {
let bc = _bc.get(this)
return new WritableOffStream(config.blockSize, { bc: bc, url: url, temporary })
} else if (url.streamLength >= config.miniBlockSize) {
let mc = _mc.get(this)
return new WritableOffStream(config.miniBlockSize, { bc: mc, url: url, temporary })
} else {
let nc = _nc.get(this)
return new WritableOffStream(config.nanoBlockSize, { bc: nc, url: url, temporary })
}
}
releaseTemporaries (url) {
if (!(url instanceof URL)) {
throw new TypeError('Invalid URL')
}
if (!url.streamLength) {
throw new TypeError('URL must have a stream length')
}
if (url.streamLength >= config.blockSize) {
let bc = _bc.get(this)
bc.releaseTemporaries(url.fileHash + url.descriptorHash)
} else if (url.streamLength >= config.miniBlockSize) {
let mc = _mc.get(this)
mc.releaseTemporaries(url.fileHash + url.descriptorHash)
} else {
let nc = _nc.get(this)
nc.releaseTemporaries(url.fileHash + url.descriptorHash)
}
}
removeTemporaries (url, cb) {
if (!(url instanceof URL)) {
return cb(new TypeError('Invalid URL'))
}
if (!url.streamLength) {
return cb(new TypeError('URL must have a stream length'))
}
if (url.streamLength >= config.blockSize) {
let bc = _bc.get(this)
bc.releaseTemporaries(url.fileHash + url.descriptorHash, cb)
} else if (url.streamLength >= config.miniBlockSize) {
let mc = _mc.get(this)
mc.releaseTemporaries(url.fileHash + url.descriptorHash, cb)
} else {
let nc = _nc.get(this)
nc.releaseTemporaries(url.fileHash + url.descriptorHash, cb)
}
}
rpcInterface () {
let rpc = {}
rpc.storeValue = (value, type, hash, cb)=> {
if (typeof hash === 'function') {
cb = hash
hash = undefined
}
let bc
let block
switch (type) {
case 1:
bc = _bc.get(this)
block = new Block(value, config.blockSize)
break;
case 2:
bc = _mc.get(this)
block = new Block(value, config.miniBlockSize)
break;
case 3:
bc = _nc.get(this)
block = new Block(value, config.nanoBlockSize)
break;
}
if (hash && hash.compare(block.hash) !== 0) {
return cb(new Error('Hash does not match data'))
}
bc.put(block, (err) => cb(err, block))
}
rpc.getValue = (hash, type, cb) => {
let bc
switch (type) {
case 1:
bc = _bc.get(this)
break;
case 2:
bc = _mc.get(this)
break;
case 3:
bc = _nc.get(this)
break;
}
let key = bs58.encode(hash)
bc.get(key, (err, block) => {
if (err) {
return cb(err)
}
return cb(err, block.data)
})
}
rpc.closestBlock = (hash, filter, type, cb) => {
let bc
switch (type) {
case 1:
bc = _bc.get(this)
break;
case 2:
bc = _mc.get(this)
break;
case 3:
bc = _nc.get(this)
break;
}
let key = bs58.encode(hash)
bc.closestBlock(key, filter, cb)
}
rpc.containsValue = (hash, type, cb)=> {
let bc
switch (type) {
case 1:
bc = _bc.get(this)
break;
case 2:
bc = _mc.get(this)
break;
case 3:
bc = _nc.get(this)
break;
}
let key = bs58.encode(hash)
return bc.get(key, (err) => cb(!err))
}
rpc.storageCapacity = (type) => {
let bc
switch (type) {
case 1:
bc = _bc.get(this)
break;
case 2:
bc = _mc.get(this)
break;
case 3:
bc = _nc.get(this)
break;
}
return bc.capacity
}
return rpc
}
cacheInterface (type) {
let rpc = _rpc.get(this)
let cache = {}
cache.load = (key, cb) => {
rpc.findValue(bs58.decode(key), type, (err, block) => {
if (err) {
return cb(err)
}
return cb(null, block)
})
}
return cache
}
connect (peer, cb) {
let rpc = _rpc.get(this)
rpc.connect(peer, cb)
}
bootstrap (cb) {
try {
let bootstrap = config.bootstrap.map((peer) => Peer.fromLocator(peer)).filter((peer) => !peer.isEqual(Peer.self))
let connect = () => {
let i = -1
let next = (err) => {
if (err) {
this.emit('error', err)
}
i++
if (i < bootstrap.length) {
let peer = bootstrap[ i ]
this.connect(peer, next)
} else { //Fill routing table with the closet nodes to themselves
let rpc = _rpc.get(this)
rpc.findNode(Peer.self.id, cb)
}
}
next()
}
// If this option is selected then Bootstrap to whomever we were last online with
if (config.lastKnownPeers) {
let path = _path.get(this)
let fd = pth.join(path, '.bucket')
fs.readFile(fd, (err, bucketFile) => {
if (err) {
this.emit(err)
return connect()
}
if (bucketFile) {
let peers = cbor.decode(toAb(bucketFile))
for (let pier of peers) {
let peer = Peer.fromLocator(pier)
let found = bootstrap.find((boot) => peer.isEqual(boot))
if (!found) {
bootstrap.push(peer)
}
}
}
return connect()
})
} else {
return connect()
}
} catch(err) {
return cb(err)
}
}
savePeers () {
let timeout = _timeout.get(this)
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
let bucket = _bucket.get(this)
let bucketPath = _bucketPath.get(this)
let peers = bucket.toArray()
peers = peers.map((peer) => peer.toLocator())
let buf = abToB(cbor.encode(peers))
let fd = pth.join(bucketPath , '.bucket')
fs.writeFile(fd, buf, (err) => {
if (err) {
return this.emit('error', err)
}
})
}, config.peerTimeout)
_timeout.set(this, timeout)
}
listen () {
let rpc = _rpc.get(this)
rpc.listen()
}
get blockCapacity () {
let bc = _bc.get(this)
return bc.capacity
}
get miniCapacity () {
let mc = _mc.get(this)
return mc.capacity
}
get nanoCapacity () {
let nc = _nc.get(this)
return nc.capacity
}
get connections () {
let bucket = _bucket.get(this)
return bucket.count
}
}