hypercore 11.21.2 → 11.21.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 +21 -10
  2. package/package.json +1 -1
package/lib/replicator.js CHANGED
@@ -26,6 +26,7 @@ const b4a = require('b4a')
26
26
  const safetyCatch = require('safety-catch')
27
27
  const RandomIterator = require('random-array-iterator')
28
28
  const flatTree = require('flat-tree')
29
+ const Mutex = require('./mutex')
29
30
  const ReceiverQueue = require('./receiver-queue')
30
31
  const HotswapQueue = require('./hotswap-queue')
31
32
  const RemoteBitfield = require('./remote-bitfield')
@@ -461,7 +462,7 @@ class Peer {
461
462
  this.remoteBitfield = new RemoteBitfield()
462
463
  this.missingBlocks = new RemoteBitfield()
463
464
 
464
- this.pushing = false
465
+ this.pushedLength = 0
465
466
 
466
467
  this.remoteFork = 0
467
468
  this.remoteLength = 0
@@ -909,15 +910,17 @@ class Peer {
909
910
  priority: 0
910
911
  }
911
912
 
913
+ const remoteLength = Math.max(this.remoteLength, this.pushedLength)
914
+
912
915
  msg.block = {
913
916
  index,
914
- nodes: MerkleTree.maxMissingNodes(2 * index, this.remoteLength)
917
+ nodes: MerkleTree.maxMissingNodes(2 * index, remoteLength)
915
918
  }
916
919
 
917
920
  if (index >= this.remoteLength) {
918
921
  msg.upgrade = {
919
- start: this.remoteLength,
920
- length: this.core.state.length - this.remoteLength
922
+ start: remoteLength,
923
+ length: this.core.state.length - remoteLength
921
924
  }
922
925
  }
923
926
 
@@ -998,13 +1001,9 @@ class Peer {
998
1001
  this.replicator._onupload(proof.block.index, proof.block.value.byteLength, this)
999
1002
  }
1000
1003
 
1001
- // TODO: we should prob move to a sep length prop for this. This is just quick and dirty
1002
- // to produce better push upgrade proofs as we can better predict what length the remote
1003
- // is going to be at.
1004
- if ((pushing || this.pushing) && proof.upgrade) {
1005
- this.pushing = true
1004
+ if (proof.upgrade) {
1006
1005
  const remoteLength = proof.upgrade.start + proof.upgrade.length
1007
- if (remoteLength > this.remoteLength) this.remoteLength = remoteLength
1006
+ if (remoteLength > this.pushedLength) this.pushedLength = remoteLength
1008
1007
  }
1009
1008
 
1010
1009
  this.wireData.send({
@@ -1064,6 +1063,17 @@ class Peer {
1064
1063
  }
1065
1064
 
1066
1065
  async ondata(data) {
1066
+ if (data.request !== 0) return this._handleData(data)
1067
+
1068
+ await this.replicator._pushLock.lock()
1069
+ try {
1070
+ await this._handleData(data)
1071
+ } finally {
1072
+ this.replicator._pushLock.unlock()
1073
+ }
1074
+ }
1075
+
1076
+ async _handleData(data) {
1067
1077
  // always allow a fork conflict proof to be sent
1068
1078
  if (data.request === 0 && data.upgrade && data.upgrade.start === 0) {
1069
1079
  if (await this.core.checkConflict(data, this)) return
@@ -1771,6 +1781,7 @@ module.exports = class Replicator {
1771
1781
  this._reorgs = []
1772
1782
  this._ranges = []
1773
1783
 
1784
+ this._pushLock = new Mutex()
1774
1785
  this._hadPeers = false
1775
1786
  this._active = 0
1776
1787
  this._ifAvailable = 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.21.2",
3
+ "version": "11.21.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {