hypercore 11.40.3 → 11.40.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.
@@ -22,13 +22,10 @@ class SlimSession {
22
22
  }
23
23
  }
24
24
 
25
- module.exports = { verify, proof }
25
+ module.exports = { verify, proof, encode: encodeProof, decode: decodeProof }
26
26
 
27
27
  async function verify(storage, buffer, { referrer = null } = {}) {
28
- const state = { buffer, start: 0, end: buffer.byteLength }
29
-
30
- const discoveryKey = c.fixed32.decode(state)
31
- const proof = messages.wire.data.decode(state)
28
+ const { discoveryKey, proof } = decodeProof(buffer)
32
29
 
33
30
  const result = {
34
31
  key: null,
@@ -109,29 +106,46 @@ async function verify(storage, buffer, { referrer = null } = {}) {
109
106
  return result
110
107
  }
111
108
 
112
- async function proof(sender, { index, block = null, upgrade = null } = {}) {
113
- const proof = await sender.proof({
114
- block: block ? { index, nodes: 0 } : null,
115
- upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
116
- })
109
+ function decodeProof(buffer) {
110
+ const state = { buffer, start: 0, end: buffer.byteLength }
117
111
 
118
- if (block) proof.block.value = block
119
- proof.manifest = sender.core.header.manifest
112
+ const discoveryKey = c.fixed32.decode(state)
113
+ const proof = messages.wire.data.decode(state)
114
+
115
+ return { discoveryKey, proof }
116
+ }
120
117
 
118
+ function encodeProof({ discoveryKey, proof }) {
121
119
  const state = { buffer: null, start: 0, end: 0 }
122
- const data = { request: 0, ...proof }
123
120
 
124
- c.fixed32.preencode(state, sender.discoveryKey)
125
- messages.wire.data.preencode(state, data)
121
+ c.fixed32.preencode(state, discoveryKey)
122
+ messages.wire.data.preencode(state, proof)
126
123
 
127
124
  state.buffer = b4a.allocUnsafe(state.end)
128
125
 
129
- c.fixed32.encode(state, sender.discoveryKey)
130
- messages.wire.data.encode(state, data)
126
+ c.fixed32.encode(state, discoveryKey)
127
+ messages.wire.data.encode(state, proof)
131
128
 
132
129
  return state.buffer
133
130
  }
134
131
 
132
+ async function proof(sender, { index, block = null, upgrade = null, encode = true } = {}) {
133
+ const proof = await sender.proof({
134
+ block: block ? { index, nodes: 0 } : null,
135
+ upgrade: upgrade ? upgrade : { start: 0, length: sender.length }
136
+ })
137
+
138
+ if (block) proof.block.value = block
139
+ proof.manifest = sender.core.header.manifest
140
+
141
+ const result = {
142
+ discoveryKey: sender.discoveryKey,
143
+ proof: { request: 0, ...proof }
144
+ }
145
+
146
+ return encode ? encodeProof(result) : result
147
+ }
148
+
135
149
  function isNull(x) {
136
150
  return x === null
137
151
  }
package/lib/replicator.js CHANGED
@@ -611,7 +611,7 @@ class Peer {
611
611
  }
612
612
 
613
613
  signalUpgrade() {
614
- if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync()
614
+ if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync(false)
615
615
  else this.sendSync()
616
616
  }
617
617
 
@@ -856,31 +856,35 @@ class Peer {
856
856
  )
857
857
  }
858
858
 
859
- async _updateCanUpgradeAndSync() {
859
+ async _updateCanUpgradeAndSync(onlyIfChanged) {
860
860
  const { length, fork } = this.core.state
861
861
 
862
862
  const remoteLength = this.remoteLength
863
863
  const remoteFork = this.remoteFork
864
864
  const canUpgrade = await this._canUpgrade(this.remoteLength, this.remoteFork)
865
865
 
866
- if (
867
- this.syncsProcessing > 0 ||
868
- length !== this.core.state.length ||
869
- fork !== this.core.state.fork
870
- ) {
866
+ if (length !== this.core.state.length || fork !== this.core.state.fork) {
871
867
  return
872
868
  }
873
- if (remoteLength !== this.remoteLength || remoteFork !== this.remoteFork) {
869
+ if (this.syncsProcessing > 0) {
870
+ this.needsSync = true // the in-flight onsync sends when it finishes
874
871
  return
875
872
  }
876
- if (canUpgrade === this.canUpgrade) {
873
+ if (remoteLength !== this.remoteLength || remoteFork !== this.remoteFork) {
874
+ // if we are still longer, the onsync that changed it already signalled
875
+ if (fork === this.remoteFork && length > this.remoteLength) return
876
+ this.signalUpgrade() // remote changed while we checked, re-evaluate
877
877
  return
878
878
  }
879
879
 
880
- this.canUpgrade = canUpgrade
881
- if (canUpgrade) {
882
- this.lastUpgradableLength = this.remoteLength
883
- this.lastUpgradableFork = this.remoteFork
880
+ if (canUpgrade !== this.canUpgrade) {
881
+ this.canUpgrade = canUpgrade
882
+ if (canUpgrade) {
883
+ this.lastUpgradableLength = this.remoteLength
884
+ this.lastUpgradableFork = this.remoteFork
885
+ }
886
+ } else if (onlyIfChanged === true) {
887
+ return
884
888
  }
885
889
 
886
890
  this.sendSync()
@@ -1295,7 +1299,7 @@ class Peer {
1295
1299
  this.replicator._ondata(this, req, data)
1296
1300
 
1297
1301
  if (this._shouldUpdateCanUpgrade() === true) {
1298
- this._updateCanUpgradeAndSync()
1302
+ this._updateCanUpgradeAndSync(true)
1299
1303
  }
1300
1304
  }
1301
1305
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.40.3",
3
+ "version": "11.40.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {