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.
- package/lib/fully-remote-proof.js +31 -17
- package/lib/replicator.js +18 -14
- package/package.json +1 -1
|
@@ -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
|
|
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
|
-
|
|
113
|
-
const
|
|
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
|
-
|
|
119
|
-
proof
|
|
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,
|
|
125
|
-
messages.wire.data.preencode(state,
|
|
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,
|
|
130
|
-
messages.wire.data.encode(state,
|
|
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 (
|
|
869
|
+
if (this.syncsProcessing > 0) {
|
|
870
|
+
this.needsSync = true // the in-flight onsync sends when it finishes
|
|
874
871
|
return
|
|
875
872
|
}
|
|
876
|
-
if (
|
|
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
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
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
|
|