hypercore 10.0.0-alpha.4 → 10.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/replicator.js +22 -8
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -25,11 +25,12 @@ class InvertedPromise {
25
25
  }
26
26
 
27
27
  class Request {
28
- constructor (index, seek) {
28
+ constructor (index, seek, nodes) {
29
29
  this.peer = null
30
30
  this.index = index
31
31
  this.seek = seek
32
32
  this.value = seek === 0
33
+ this.nodes = nodes
33
34
  this.promises = []
34
35
  }
35
36
 
@@ -278,7 +279,9 @@ class RequestPool {
278
279
 
279
280
  _updateSeek (peer, seek) {
280
281
  if (seek.request) return false
281
- seek.request = this._requestRange(peer, seek.seeker.start, seek.seeker.end, seek.seeker.bytes)
282
+ // We have to snapshot the nodes here now, due to the caching of the request...
283
+ const nodes = log2(seek.seeker.end - seek.seeker.start)
284
+ seek.request = this._requestRange(peer, seek.seeker.start, seek.seeker.end, seek.seeker.bytes, nodes)
282
285
  return seek.request !== null
283
286
  }
284
287
 
@@ -296,10 +299,10 @@ class RequestPool {
296
299
  const end = range.end === -1 ? peer.state.length : range.end
297
300
  if (end <= range._start) return false
298
301
 
299
- if (range.linear) return !!this._requestRange(peer, range._start, end, 0)
302
+ if (range.linear) return !!this._requestRange(peer, range._start, end, 0, 0)
300
303
 
301
304
  const r = range._start + Math.floor(Math.random() * (end - range._start))
302
- return !!(this._requestRange(peer, r, end, 0) || this._requestRange(peer, range._start, r, 0))
305
+ return !!(this._requestRange(peer, r, end, 0, 0) || this._requestRange(peer, range._start, r, 0, 0))
303
306
  }
304
307
 
305
308
  _updateUpgrade (peer) {
@@ -328,7 +331,7 @@ class RequestPool {
328
331
  this.pendingUpgrade = null
329
332
  }
330
333
 
331
- _requestRange (peer, start, end, seek) {
334
+ _requestRange (peer, start, end, seek, nodes) {
332
335
  const remote = peer.state.bitfield
333
336
  const local = this.core.bitfield
334
337
 
@@ -341,7 +344,7 @@ class RequestPool {
341
344
  if (this.requests.has(i)) continue
342
345
 
343
346
  // TODO: if seeking and i >= core.length, let that takes precendance in the upgrade req
344
- const req = new Request(i, i < this.core.tree.length ? seek : 0)
347
+ const req = new Request(i, i < this.core.tree.length ? seek : 0, nodes)
345
348
  this.requests.set(i, req)
346
349
  this.send(peer, req)
347
350
  return req
@@ -390,7 +393,7 @@ class RequestPool {
390
393
 
391
394
  if (data.block.index < this.core.tree.length || this.core.truncating > 0) {
392
395
  try {
393
- data.block.nodes = await this.core.tree.nodes(data.block.index * 2)
396
+ data.block.nodes = Math.max(req.nodes, await this.core.tree.nodes(data.block.index * 2))
394
397
  } catch (err) {
395
398
  console.error('TODO handle me:', err.stack)
396
399
  }
@@ -606,7 +609,7 @@ class RequestPool {
606
609
  return e.createPromise()
607
610
  }
608
611
 
609
- const r = new Request(index, 0)
612
+ const r = new Request(index, 0, 0)
610
613
 
611
614
  this.requests.set(index, r)
612
615
  this.pending.push(r)
@@ -822,3 +825,14 @@ function pages (core) {
822
825
  }
823
826
 
824
827
  function noop () {}
828
+
829
+ function log2 (n) {
830
+ let res = 1
831
+
832
+ while (n > 2) {
833
+ n /= 2
834
+ res++
835
+ }
836
+
837
+ return res
838
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.4",
3
+ "version": "10.0.0-alpha.5",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {