hypercore 10.37.17 → 10.37.19

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 +69 -3
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -358,6 +358,18 @@ class Peer {
358
358
  this.wireRange = this.channel.messages[8]
359
359
  this.wireExtension = this.channel.messages[9]
360
360
 
361
+ // Same stats as replicator, but for this specific peer
362
+ this.stats = {
363
+ wireSync: { tx: 0, rx: 0 },
364
+ wireRequest: { tx: 0, rx: 0 },
365
+ wireCancel: { tx: 0, rx: 0 },
366
+ wireData: { tx: 0, rx: 0 },
367
+ wireWant: { tx: 0, rx: 0 },
368
+ wireBitfield: { tx: 0, rx: 0 },
369
+ wireRange: { tx: 0, rx: 0 },
370
+ wireExtension: { tx: 0, rx: 0 }
371
+ }
372
+
361
373
  this.receiverQueue = new ReceiverQueue()
362
374
  this.receiverBusy = false
363
375
 
@@ -446,10 +458,12 @@ class Peer {
446
458
  start,
447
459
  length
448
460
  })
461
+ incrementTx(this.stats.wireRange, this.replicator.stats.wireRange)
449
462
  }
450
463
 
451
464
  extension (name, message) {
452
465
  this.wireExtension.send({ name: name === this.lastExtensionSent ? '' : name, message })
466
+ incrementTx(this.stats.wireExtension, this.replicator.stats.wireExtension)
453
467
  this.lastExtensionSent = name
454
468
  }
455
469
 
@@ -481,6 +495,7 @@ class Peer {
481
495
  downloading: this.replicator.isDownloading(),
482
496
  hasManifest: !!this.core.header.manifest && this.core.compat === false
483
497
  })
498
+ incrementTx(this.stats.wireSync, this.replicator.stats.wireSync)
484
499
  }
485
500
 
486
501
  onopen ({ seeks, capability }) {
@@ -730,6 +745,7 @@ class Peer {
730
745
  if (msg.manifest && this.core.header.manifest) {
731
746
  const manifest = this.core.header.manifest
732
747
  this.wireData.send({ request: msg.id, fork: this.core.tree.fork, block: null, hash: null, seek: null, upgrade: null, manifest })
748
+ incrementTx(this.stats.wireData, this.replicator.stats.wireData)
733
749
  return
734
750
  }
735
751
 
@@ -750,6 +766,7 @@ class Peer {
750
766
  upgrade: proof.upgrade,
751
767
  manifest: proof.manifest
752
768
  })
769
+ incrementTx(this.stats.wireData, this.replicator.stats.wireData)
753
770
  }
754
771
 
755
772
  _cancelRequest (req) {
@@ -767,6 +784,7 @@ class Peer {
767
784
  if (this.roundtripQueue === null) this.roundtripQueue = new RoundtripQueue()
768
785
  this.roundtripQueue.add(req.id)
769
786
  this.wireCancel.send({ request: req.id })
787
+ incrementTx(this.stats.wireCancel, this.replicator.stats.wireCancel)
770
788
  }
771
789
 
772
790
  _checkIfConflict () {
@@ -786,6 +804,8 @@ class Peer {
786
804
  length
787
805
  }
788
806
  })
807
+
808
+ incrementTx(this.stats.wireRequest, this.replicator.stats.wireRequest)
789
809
  }
790
810
 
791
811
  async ondata (data) {
@@ -972,8 +992,8 @@ class Peer {
972
992
  this._remoteContiguousLength = start
973
993
  }
974
994
 
975
- if (start === 0 && drop === false && length > this._remoteContiguousLength) {
976
- this._remoteContiguousLength = length
995
+ if (start === 0 && drop === false) {
996
+ if (length > this._remoteContiguousLength) this._remoteContiguousLength = length
977
997
  } else if (length === 1) {
978
998
  const bitfield = this.core.skipBitfield === null ? this.core.bitfield : this.core.skipBitfield
979
999
  this.remoteBitfield.set(start, has)
@@ -1095,7 +1115,7 @@ class Peer {
1095
1115
  let index = off + i
1096
1116
  if (index > s.seeker.end) index -= len
1097
1117
 
1098
- if (this.remoteBitfield.get(index) === false) continue
1118
+ if (this._remoteHasBlock(index) === false) continue
1099
1119
  if (this.core.bitfield.get(index) === true) continue
1100
1120
  if (!this._hasTreeParent(index)) continue
1101
1121
 
@@ -1328,6 +1348,7 @@ class Peer {
1328
1348
  start: i * DEFAULT_SEGMENT_SIZE,
1329
1349
  length: DEFAULT_SEGMENT_SIZE
1330
1350
  })
1351
+ incrementTx(this.stats.wireWant, this.replicator.stats.wireWant)
1331
1352
  }
1332
1353
  }
1333
1354
 
@@ -1371,6 +1392,7 @@ class Peer {
1371
1392
  this.tracer.trace('send', req)
1372
1393
 
1373
1394
  this.wireRequest.send(req)
1395
+ incrementTx(this.stats.wireRequest, this.replicator.stats.wireRequest)
1374
1396
  }
1375
1397
  }
1376
1398
 
@@ -1404,6 +1426,19 @@ module.exports = class Replicator {
1404
1426
 
1405
1427
  this.inflightRange = inflightRange || DEFAULT_MAX_INFLIGHT
1406
1428
 
1429
+ // Note: nodata and unwant not currently tracked
1430
+ // tx = transmitted, rx = received
1431
+ this.stats = {
1432
+ wireSync: { tx: 0, rx: 0 },
1433
+ wireRequest: { tx: 0, rx: 0 },
1434
+ wireCancel: { tx: 0, rx: 0 },
1435
+ wireData: { tx: 0, rx: 0 },
1436
+ wireWant: { tx: 0, rx: 0 },
1437
+ wireBitfield: { tx: 0, rx: 0 },
1438
+ wireRange: { tx: 0, rx: 0 },
1439
+ wireExtension: { tx: 0, rx: 0 }
1440
+ }
1441
+
1407
1442
  this._attached = new Set()
1408
1443
  this._inflight = new InflightTracker()
1409
1444
  this._blocks = new BlockTracker()
@@ -1997,12 +2032,25 @@ module.exports = class Replicator {
1997
2032
  }
1998
2033
 
1999
2034
  _onwant (peer, start, length) {
2035
+ const contig = Math.min(this.core.tree.length, this.core.header.hints.contiguousLength)
2036
+
2037
+ if (start + length < contig || (this.core.tree.length === contig)) {
2038
+ peer.wireRange.send({
2039
+ drop: false,
2040
+ start: 0,
2041
+ length: contig
2042
+ })
2043
+ incrementTx(peer.stats.wireRange, this.stats.wireRange)
2044
+ return
2045
+ }
2046
+
2000
2047
  length = Math.min(length, this.core.tree.length - start)
2001
2048
 
2002
2049
  peer.protomux.cork()
2003
2050
 
2004
2051
  for (const msg of this.core.bitfield.want(start, length)) {
2005
2052
  peer.wireBitfield.send(msg)
2053
+ incrementTx(peer.stats.wireBitfield, this.stats.wireBitfield)
2006
2054
  }
2007
2055
 
2008
2056
  peer.protomux.uncork()
@@ -2376,18 +2424,22 @@ function onwiredrain (c) {
2376
2424
  }
2377
2425
 
2378
2426
  function onwiresync (m, c) {
2427
+ incrementRx(c.userData.stats.wireSync, c.userData.replicator.stats.wireSync)
2379
2428
  return c.userData.onsync(m)
2380
2429
  }
2381
2430
 
2382
2431
  function onwirerequest (m, c) {
2432
+ incrementRx(c.userData.stats.wireRequest, c.userData.replicator.stats.wireRequest)
2383
2433
  return c.userData.onrequest(m)
2384
2434
  }
2385
2435
 
2386
2436
  function onwirecancel (m, c) {
2437
+ incrementRx(c.userData.stats.wireCancel, c.userData.replicator.stats.wireCancel)
2387
2438
  return c.userData.oncancel(m)
2388
2439
  }
2389
2440
 
2390
2441
  function onwiredata (m, c) {
2442
+ incrementRx(c.userData.stats.wireData, c.userData.replicator.stats.wireData)
2391
2443
  return c.userData.ondata(m)
2392
2444
  }
2393
2445
 
@@ -2396,6 +2448,7 @@ function onwirenodata (m, c) {
2396
2448
  }
2397
2449
 
2398
2450
  function onwirewant (m, c) {
2451
+ incrementRx(c.userData.stats.wireWant, c.userData.replicator.stats.wireWant)
2399
2452
  return c.userData.onwant(m)
2400
2453
  }
2401
2454
 
@@ -2404,14 +2457,17 @@ function onwireunwant (m, c) {
2404
2457
  }
2405
2458
 
2406
2459
  function onwirebitfield (m, c) {
2460
+ incrementRx(c.userData.stats.wireBitfield, c.userData.replicator.stats.wireBitfield)
2407
2461
  return c.userData.onbitfield(m)
2408
2462
  }
2409
2463
 
2410
2464
  function onwirerange (m, c) {
2465
+ incrementRx(c.userData.stats.wireRange, c.userData.replicator.stats.wireRange)
2411
2466
  return c.userData.onrange(m)
2412
2467
  }
2413
2468
 
2414
2469
  function onwireextension (m, c) {
2470
+ incrementRx(c.userData.stats.wireExtension, c.userData.replicator.stats.wireExtension)
2415
2471
  return c.userData.onextension(m)
2416
2472
  }
2417
2473
 
@@ -2426,3 +2482,13 @@ function isBlockRequest (req) {
2426
2482
  function isUpgradeRequest (req) {
2427
2483
  return req !== null && req.upgrade !== null
2428
2484
  }
2485
+
2486
+ function incrementTx (stats1, stats2) {
2487
+ stats1.tx++
2488
+ stats2.tx++
2489
+ }
2490
+
2491
+ function incrementRx (stats1, stats2) {
2492
+ stats1.rx++
2493
+ stats2.rx++
2494
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.37.17",
3
+ "version": "10.37.19",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {