hypercore 11.39.5 → 11.40.0
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/index.js +5 -4
- package/lib/core.js +12 -0
- package/lib/merkle-tree.js +197 -146
- package/lib/replicator.js +3 -3
- package/lib/session-state.js +6 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -551,16 +551,17 @@ class Hypercore extends EventEmitter {
|
|
|
551
551
|
this.state.removeSession(this)
|
|
552
552
|
this._removeSession()
|
|
553
553
|
|
|
554
|
-
this.readable = false
|
|
555
|
-
this.writable = false
|
|
556
|
-
this.opened = false
|
|
557
|
-
|
|
558
554
|
const gc = []
|
|
559
555
|
for (const ext of this.extensions.values()) {
|
|
560
556
|
if (ext.session === this) gc.push(ext)
|
|
561
557
|
}
|
|
562
558
|
for (const ext of gc) ext.destroy()
|
|
563
559
|
|
|
560
|
+
this.readable = false
|
|
561
|
+
this.writable = false
|
|
562
|
+
// needs to be below extension gc as `.peers` gates on `.opened`
|
|
563
|
+
this.opened = false
|
|
564
|
+
|
|
564
565
|
this.core.replicator.findingPeers -= this._findingPeers
|
|
565
566
|
this.core.replicator.clearRequests(this.activeRequests, error)
|
|
566
567
|
this.core.replicator.updateActivity(this._active ? -1 : 0)
|
package/lib/core.js
CHANGED
|
@@ -795,6 +795,18 @@ module.exports = class Core {
|
|
|
795
795
|
// but this is easy atm (and the above layer will just retry)
|
|
796
796
|
if (proof.fork !== this.state.fork) return false
|
|
797
797
|
|
|
798
|
+
if (proof.upgrade && proof.upgrade.start !== this.state.length) {
|
|
799
|
+
const length = this.state.length
|
|
800
|
+
proof = await MerkleTree.refit(this.state, proof)
|
|
801
|
+
|
|
802
|
+
// the nodes it carries cannot bridge our current length. the proof is not invalid, just not applicable to us
|
|
803
|
+
if (proof === null) return false
|
|
804
|
+
|
|
805
|
+
// refitting yields, so the tree may have moved under us.
|
|
806
|
+
// note that commitable() cannot catch this as it samples the fork after the fact
|
|
807
|
+
if (this.state.fork !== proof.fork || this.state.length !== length) return false
|
|
808
|
+
}
|
|
809
|
+
|
|
798
810
|
const batch = await MerkleTree.verify(this.state, proof)
|
|
799
811
|
|
|
800
812
|
if (batch.upgraded && batch.length <= this.state.length) {
|
package/lib/merkle-tree.js
CHANGED
|
@@ -3,6 +3,7 @@ const crypto = require('hypercore-crypto')
|
|
|
3
3
|
const b4a = require('b4a')
|
|
4
4
|
const unslab = require('unslab')
|
|
5
5
|
const caps = require('./caps')
|
|
6
|
+
const safetyCatch = require('safety-catch')
|
|
6
7
|
const {
|
|
7
8
|
INVALID_PROOF,
|
|
8
9
|
INVALID_CHECKSUM,
|
|
@@ -12,20 +13,29 @@ const {
|
|
|
12
13
|
} = require('hypercore-errors')
|
|
13
14
|
|
|
14
15
|
class NodeQueue {
|
|
15
|
-
constructor(nodes,
|
|
16
|
+
constructor(nodes, root = null) {
|
|
16
17
|
this.i = 0
|
|
17
18
|
this.nodes = nodes
|
|
18
|
-
this.
|
|
19
|
-
this.length = nodes.length + (this.
|
|
19
|
+
this.root = root
|
|
20
|
+
this.length = nodes.length + (this.root === null ? 0 : 1)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
peek(index) {
|
|
24
|
+
if (this.root !== null && this.root.index === index) return this.root
|
|
25
|
+
if (this.i < this.nodes.length && this.nodes[this.i].index === index) return this.nodes[this.i]
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
shiftRoot() {
|
|
30
|
+
if (this.root === null) return null
|
|
31
|
+
const node = this.root
|
|
32
|
+
this.root = null
|
|
33
|
+
this.length--
|
|
34
|
+
return node
|
|
20
35
|
}
|
|
21
36
|
|
|
22
37
|
shift(index) {
|
|
23
|
-
if (this.
|
|
24
|
-
const node = this.extra
|
|
25
|
-
this.extra = null
|
|
26
|
-
this.length--
|
|
27
|
-
return node
|
|
28
|
-
}
|
|
38
|
+
if (this.root !== null && this.root.index === index) return this.shiftRoot()
|
|
29
39
|
|
|
30
40
|
if (this.i >= this.nodes.length) {
|
|
31
41
|
throw INVALID_OPERATION('Expected node ' + index + ', got (nil)')
|
|
@@ -167,8 +177,8 @@ class MerkleTreeBatch {
|
|
|
167
177
|
}
|
|
168
178
|
|
|
169
179
|
// deprecated, use sssion proof instead
|
|
170
|
-
proof(batch,
|
|
171
|
-
return
|
|
180
|
+
proof(batch, req) {
|
|
181
|
+
return MerkleTree.proof(this.session, batch, req)
|
|
172
182
|
}
|
|
173
183
|
|
|
174
184
|
verifyUpgrade(proof) {
|
|
@@ -490,9 +500,14 @@ class ByteSeeker {
|
|
|
490
500
|
}
|
|
491
501
|
|
|
492
502
|
class TreeProof {
|
|
493
|
-
constructor(session, block, hash, seek, upgrade) {
|
|
503
|
+
constructor(session, rx, { block = null, hash = null, seek = null, upgrade = null } = {}) {
|
|
504
|
+
this.session = session
|
|
505
|
+
this.rx = rx
|
|
506
|
+
|
|
494
507
|
this.fork = session.fork
|
|
495
508
|
this.signature = session.signature
|
|
509
|
+
this.length = session.length
|
|
510
|
+
this.prologue = session.prologue
|
|
496
511
|
|
|
497
512
|
this.block = block
|
|
498
513
|
this.hash = hash
|
|
@@ -507,9 +522,72 @@ class TreeProof {
|
|
|
507
522
|
}
|
|
508
523
|
}
|
|
509
524
|
|
|
525
|
+
getTreeNode(index) {
|
|
526
|
+
return this.rx.getTreeNode(index)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async generate() {
|
|
530
|
+
// Important that this does not throw inbetween making the promise arrays
|
|
531
|
+
// and settle being called, otherwise there will be lingering promises in the background
|
|
532
|
+
|
|
533
|
+
const { block, hash, seek, upgrade } = this
|
|
534
|
+
|
|
535
|
+
if (this.prologue && upgrade) {
|
|
536
|
+
upgrade.start = upgrade.start < this.prologue.length ? 0 : upgrade.start
|
|
537
|
+
upgrade.length = upgrade.start < this.prologue.length ? this.prologue.length : upgrade.length
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const head = 2 * this.length
|
|
541
|
+
const from = upgrade ? upgrade.start * 2 : 0
|
|
542
|
+
const to = upgrade ? from + upgrade.length * 2 : head
|
|
543
|
+
const node = normalizeIndexed(block, hash)
|
|
544
|
+
|
|
545
|
+
// can't do anything as we have no data...
|
|
546
|
+
if (head === 0) {
|
|
547
|
+
this.block = this.hash = this.seek = this.upgrade = null
|
|
548
|
+
return this
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if (from >= to || to > head) {
|
|
552
|
+
throw INVALID_OPERATION('Invalid upgrade')
|
|
553
|
+
}
|
|
554
|
+
if (seek && upgrade && node !== null && node.index >= from) {
|
|
555
|
+
throw INVALID_OPERATION('Cannot both do a seek and block/hash request when upgrading')
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
let subTree = head
|
|
559
|
+
|
|
560
|
+
if (node !== null && (!upgrade || node.lastIndex < upgrade.start)) {
|
|
561
|
+
subTree = nodesToRoot(node.index, node.nodes, to)
|
|
562
|
+
const seekRoot = seek
|
|
563
|
+
? await seekUntrustedTree(this.session, subTree, seek.bytes, seek.padding)
|
|
564
|
+
: head
|
|
565
|
+
blockAndSeekProof(this, node, seek, seekRoot, subTree)
|
|
566
|
+
} else if ((node || seek) && upgrade) {
|
|
567
|
+
subTree = seek ? await seekFromHead(this.session, to, seek.bytes, seek.padding) : node.index
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (upgrade) {
|
|
571
|
+
const pending = this.pending
|
|
572
|
+
|
|
573
|
+
pending.upgrade = []
|
|
574
|
+
upgradeProof(this, node, seek, from, to, subTree, pending.upgrade)
|
|
575
|
+
|
|
576
|
+
if (head > to) {
|
|
577
|
+
pending.additionalUpgrade = []
|
|
578
|
+
upgradeProof(this, node, seek, to, head, subTree, pending.additionalUpgrade)
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return this
|
|
583
|
+
}
|
|
584
|
+
|
|
510
585
|
async settle() {
|
|
511
586
|
const result = {
|
|
512
587
|
fork: this.fork,
|
|
588
|
+
// the tree length proof was generated at, aak the length it upgrades the receiver to
|
|
589
|
+
// local only, never sent on the wire
|
|
590
|
+
coreLength: this.length,
|
|
513
591
|
block: null,
|
|
514
592
|
hash: null,
|
|
515
593
|
seek: null,
|
|
@@ -555,6 +633,57 @@ class TreeProof {
|
|
|
555
633
|
}
|
|
556
634
|
}
|
|
557
635
|
|
|
636
|
+
// generates a proof from our current length using only the nodes carried by an existing proof
|
|
637
|
+
class RefitTreeProof extends TreeProof {
|
|
638
|
+
constructor(session, proof) {
|
|
639
|
+
super(session, null, {
|
|
640
|
+
block: proof.block ? { index: proof.block.index, nodes: 0 } : null,
|
|
641
|
+
hash: proof.hash ? { index: proof.hash.index, nodes: 0 } : null
|
|
642
|
+
})
|
|
643
|
+
|
|
644
|
+
this.insufficient = false
|
|
645
|
+
|
|
646
|
+
const nodes = []
|
|
647
|
+
verifyTree(proof, nodes)
|
|
648
|
+
nodes.push(...proof.upgrade.nodes, ...proof.upgrade.additionalNodes)
|
|
649
|
+
|
|
650
|
+
this.nodes = new Map()
|
|
651
|
+
|
|
652
|
+
let length = 0
|
|
653
|
+
|
|
654
|
+
for (const node of nodes) {
|
|
655
|
+
this.nodes.set(node.index, node)
|
|
656
|
+
length = Math.max(length, flat.rightSpan(node.index) / 2 + 1)
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
if (length > this.length) {
|
|
660
|
+
this.upgrade = { start: this.length, length: length - this.length }
|
|
661
|
+
this.length = length
|
|
662
|
+
this.signature = proof.upgrade.signature
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
getTreeNode(index) {
|
|
667
|
+
if (!this.nodes.has(index)) {
|
|
668
|
+
this.insufficient = true
|
|
669
|
+
return Promise.resolve(null)
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
return Promise.resolve(this.nodes.get(index))
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
async generate() {
|
|
676
|
+
const indexed = this.block || this.hash
|
|
677
|
+
|
|
678
|
+
if (indexed) {
|
|
679
|
+
const index = this.block ? 2 * this.block.index : this.hash.index
|
|
680
|
+
indexed.nodes = await MerkleTree.missingNodes(this.session, index, this.session.length)
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
return super.generate()
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
558
687
|
class MerkleTree {
|
|
559
688
|
static hash(s) {
|
|
560
689
|
return unslab(crypto.tree(s.roots))
|
|
@@ -711,8 +840,31 @@ class MerkleTree {
|
|
|
711
840
|
return batch
|
|
712
841
|
}
|
|
713
842
|
|
|
714
|
-
static proof(session, rx,
|
|
715
|
-
return
|
|
843
|
+
static proof(session, rx, req) {
|
|
844
|
+
return new TreeProof(session, rx, req).generate()
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// rewrite a proof as if it was requested from our current length, using only the nodes it
|
|
848
|
+
// carries. returns the original proof if no rewrite is needed, or null if the nodes it
|
|
849
|
+
// carries are not sufficient, ie the proof cannot be applied at our current length
|
|
850
|
+
static async refit(session, proof) {
|
|
851
|
+
if (!proof.upgrade || proof.seek || proof.upgrade.start === session.length) return proof
|
|
852
|
+
|
|
853
|
+
let refitted = null
|
|
854
|
+
|
|
855
|
+
const p = new RefitTreeProof(session, proof)
|
|
856
|
+
try {
|
|
857
|
+
refitted = await (await p.generate()).settle()
|
|
858
|
+
} catch (err) {
|
|
859
|
+
if (!p.insufficient) throw err
|
|
860
|
+
safetyCatch(err)
|
|
861
|
+
return null
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
if (refitted.block) refitted.block.value = proof.block.value
|
|
865
|
+
refitted.manifest = proof.manifest
|
|
866
|
+
|
|
867
|
+
return refitted
|
|
716
868
|
}
|
|
717
869
|
|
|
718
870
|
static maxMissingNodes(index, length) {
|
|
@@ -773,18 +925,6 @@ class MerkleTree {
|
|
|
773
925
|
rx.tryFlush()
|
|
774
926
|
return Promise.all([offset, size])
|
|
775
927
|
}
|
|
776
|
-
|
|
777
|
-
static upgradeLength(proof) {
|
|
778
|
-
const extra = proof.additionalNodes
|
|
779
|
-
|
|
780
|
-
if (extra.length) {
|
|
781
|
-
const last = extra[extra.length - 1]
|
|
782
|
-
const index = flat.rightSpan(last.index)
|
|
783
|
-
return index / 2 + 1
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
return proof.start + proof.length
|
|
787
|
-
}
|
|
788
928
|
}
|
|
789
929
|
|
|
790
930
|
module.exports = {
|
|
@@ -971,31 +1111,31 @@ function verifyUpgrade({ fork, upgrade }, blockRoot, batch) {
|
|
|
971
1111
|
}
|
|
972
1112
|
}
|
|
973
1113
|
|
|
974
|
-
|
|
1114
|
+
// move the root to the additionalNodes queue if the block is past the upgrade,
|
|
1115
|
+
// since its relevant for the additionalNodes queue if so
|
|
1116
|
+
const root = q.root !== null && q.root.index >= to ? q.shiftRoot() : null
|
|
1117
|
+
const additional = new NodeQueue(upgrade.additionalNodes, root)
|
|
975
1118
|
|
|
976
1119
|
ite.seek(batch.roots[batch.roots.length - 1].index)
|
|
977
|
-
i = 0
|
|
978
1120
|
|
|
979
|
-
while (
|
|
980
|
-
batch.appendRoot(
|
|
1121
|
+
while (additional.length > 0 && additional.peek(ite.sibling()) !== null) {
|
|
1122
|
+
batch.appendRoot(additional.shift(ite.index), ite)
|
|
981
1123
|
}
|
|
982
1124
|
|
|
983
|
-
while (
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
while (node.index !== ite.index) {
|
|
987
|
-
if (ite.factor === 2) throw INVALID_OPERATION('Unexpected node: ' + node.index)
|
|
1125
|
+
while (additional.length > 0) {
|
|
1126
|
+
while (additional.peek(ite.index) === null) {
|
|
1127
|
+
if (ite.factor === 2) throw INVALID_OPERATION('Unexpected additional node')
|
|
988
1128
|
ite.leftChild()
|
|
989
1129
|
}
|
|
990
1130
|
|
|
991
|
-
batch.appendRoot(
|
|
1131
|
+
batch.appendRoot(additional.shift(ite.index), ite)
|
|
992
1132
|
ite.sibling()
|
|
993
1133
|
}
|
|
994
1134
|
|
|
995
1135
|
batch.signature = unslab(upgrade.signature)
|
|
996
1136
|
batch.fork = fork
|
|
997
1137
|
|
|
998
|
-
return q.
|
|
1138
|
+
return q.root === null
|
|
999
1139
|
}
|
|
1000
1140
|
|
|
1001
1141
|
async function seekFromHead(session, head, bytes, padding) {
|
|
@@ -1068,29 +1208,31 @@ async function seekUntrustedTree(session, root, bytes, padding) {
|
|
|
1068
1208
|
// Note, that all these methods are sync as we can statically infer which nodes
|
|
1069
1209
|
// are needed for the remote to verify given they arguments they passed us
|
|
1070
1210
|
|
|
1071
|
-
function seekProof(
|
|
1211
|
+
function seekProof(proof, seekRoot, root) {
|
|
1212
|
+
const p = proof.pending
|
|
1072
1213
|
const ite = flat.iterator(seekRoot)
|
|
1073
1214
|
let cnt = 0
|
|
1074
1215
|
|
|
1075
1216
|
p.seek = []
|
|
1076
|
-
p.seek.push(getTreeNodeOrError(
|
|
1217
|
+
p.seek.push(getTreeNodeOrError(proof, ite.index))
|
|
1077
1218
|
|
|
1078
1219
|
while (ite.index !== root) {
|
|
1079
1220
|
if (++cnt >= 1024) throw ASSERTION('Bad seekProof seekRoot=' + seekRoot + ', root=' + root)
|
|
1080
1221
|
ite.sibling()
|
|
1081
|
-
p.seek.push(getTreeNodeOrError(
|
|
1222
|
+
p.seek.push(getTreeNodeOrError(proof, ite.index))
|
|
1082
1223
|
ite.parent()
|
|
1083
1224
|
}
|
|
1084
1225
|
}
|
|
1085
1226
|
|
|
1086
|
-
function blockAndSeekProof(
|
|
1087
|
-
if (!node) return seekProof(
|
|
1227
|
+
function blockAndSeekProof(proof, node, seek, seekRoot, root) {
|
|
1228
|
+
if (!node) return seekProof(proof, seekRoot, root)
|
|
1088
1229
|
|
|
1230
|
+
const p = proof.pending
|
|
1089
1231
|
const ite = flat.iterator(node.index)
|
|
1090
1232
|
let cnt = 0
|
|
1091
1233
|
|
|
1092
1234
|
p.node = []
|
|
1093
|
-
if (!node.value) p.node.push(getTreeNodeOrError(
|
|
1235
|
+
if (!node.value) p.node.push(getTreeNodeOrError(proof, ite.index))
|
|
1094
1236
|
|
|
1095
1237
|
while (ite.index !== root) {
|
|
1096
1238
|
if (++cnt >= 1024) {
|
|
@@ -1099,25 +1241,26 @@ function blockAndSeekProof(session, rx, node, seek, seekRoot, root, p) {
|
|
|
1099
1241
|
ite.sibling()
|
|
1100
1242
|
|
|
1101
1243
|
if (seek && ite.contains(seekRoot) && ite.index !== seekRoot) {
|
|
1102
|
-
seekProof(
|
|
1244
|
+
seekProof(proof, seekRoot, ite.index)
|
|
1103
1245
|
} else {
|
|
1104
|
-
p.node.push(getTreeNodeOrError(
|
|
1246
|
+
p.node.push(getTreeNodeOrError(proof, ite.index))
|
|
1105
1247
|
}
|
|
1106
1248
|
|
|
1107
1249
|
ite.parent()
|
|
1108
1250
|
}
|
|
1109
1251
|
}
|
|
1110
1252
|
|
|
1111
|
-
function upgradeProof(
|
|
1112
|
-
|
|
1253
|
+
function upgradeProof(proof, node, seek, from, to, subTree, nodes) {
|
|
1254
|
+
const p = proof.pending
|
|
1255
|
+
let connected = from === 0
|
|
1113
1256
|
|
|
1114
1257
|
for (const ite = flat.iterator(0); ite.fullRoot(to); ite.nextTree()) {
|
|
1115
1258
|
// check if they already have the node
|
|
1116
1259
|
if (ite.index + ite.factor / 2 < from) continue
|
|
1117
1260
|
|
|
1118
1261
|
// connect existing tree
|
|
1119
|
-
if (
|
|
1120
|
-
|
|
1262
|
+
if (!connected && ite.contains(from - 2)) {
|
|
1263
|
+
connected = true
|
|
1121
1264
|
|
|
1122
1265
|
const root = ite.index
|
|
1123
1266
|
const target = from - 2
|
|
@@ -1131,9 +1274,9 @@ function upgradeProof(session, rx, node, seek, from, to, subTree, p) {
|
|
|
1131
1274
|
ite.sibling()
|
|
1132
1275
|
if (ite.index > target) {
|
|
1133
1276
|
if (p.node === null && p.seek === null && ite.contains(subTree)) {
|
|
1134
|
-
blockAndSeekProof(
|
|
1277
|
+
blockAndSeekProof(proof, node, seek, subTree, ite.index)
|
|
1135
1278
|
} else {
|
|
1136
|
-
|
|
1279
|
+
nodes.push(getTreeNodeOrError(proof, ite.index))
|
|
1137
1280
|
}
|
|
1138
1281
|
}
|
|
1139
1282
|
ite.parent()
|
|
@@ -1142,61 +1285,17 @@ function upgradeProof(session, rx, node, seek, from, to, subTree, p) {
|
|
|
1142
1285
|
continue
|
|
1143
1286
|
}
|
|
1144
1287
|
|
|
1145
|
-
|
|
1146
|
-
p.upgrade = []
|
|
1147
|
-
}
|
|
1288
|
+
connected = true
|
|
1148
1289
|
|
|
1149
1290
|
// if the subtree included is a child of this tree, include that one
|
|
1150
1291
|
// instead of a dup node
|
|
1151
1292
|
if (p.node === null && p.seek === null && ite.contains(subTree)) {
|
|
1152
|
-
blockAndSeekProof(
|
|
1293
|
+
blockAndSeekProof(proof, node, seek, subTree, ite.index)
|
|
1153
1294
|
continue
|
|
1154
1295
|
}
|
|
1155
1296
|
|
|
1156
1297
|
// add root (can be optimised since the root might be in tree.roots)
|
|
1157
|
-
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1161
|
-
function additionalUpgradeProof(session, rx, from, to, p) {
|
|
1162
|
-
if (from === 0) p.additionalUpgrade = []
|
|
1163
|
-
|
|
1164
|
-
for (const ite = flat.iterator(0); ite.fullRoot(to); ite.nextTree()) {
|
|
1165
|
-
// check if they already have the node
|
|
1166
|
-
if (ite.index + ite.factor / 2 < from) continue
|
|
1167
|
-
|
|
1168
|
-
// connect existing tree
|
|
1169
|
-
if (p.additionalUpgrade === null && ite.contains(from - 2)) {
|
|
1170
|
-
p.additionalUpgrade = []
|
|
1171
|
-
|
|
1172
|
-
const root = ite.index
|
|
1173
|
-
const target = from - 2
|
|
1174
|
-
let cnt = 0
|
|
1175
|
-
|
|
1176
|
-
ite.seek(target)
|
|
1177
|
-
|
|
1178
|
-
while (ite.index !== root) {
|
|
1179
|
-
if (++cnt >= 1024) {
|
|
1180
|
-
throw ASSERTION(
|
|
1181
|
-
'Bad arguments to additionalUpgradeProof root=' + root + ' target=' + target
|
|
1182
|
-
)
|
|
1183
|
-
}
|
|
1184
|
-
ite.sibling()
|
|
1185
|
-
if (ite.index > target) {
|
|
1186
|
-
p.additionalUpgrade.push(getTreeNodeOrError(rx, ite.index))
|
|
1187
|
-
}
|
|
1188
|
-
ite.parent()
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
|
-
continue
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
if (p.additionalUpgrade === null) {
|
|
1195
|
-
p.additionalUpgrade = []
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
// add root (can be optimised since the root is in tree.roots)
|
|
1199
|
-
p.additionalUpgrade.push(getTreeNodeOrError(rx, ite.index))
|
|
1298
|
+
nodes.push(getTreeNodeOrError(proof, ite.index))
|
|
1200
1299
|
}
|
|
1201
1300
|
}
|
|
1202
1301
|
|
|
@@ -1310,54 +1409,6 @@ async function settleProof(p) {
|
|
|
1310
1409
|
}
|
|
1311
1410
|
}
|
|
1312
1411
|
|
|
1313
|
-
// tree can be either the merkle tree or a merkle tree batch
|
|
1314
|
-
async function generateProof(session, rx, block, hash, seek, upgrade) {
|
|
1315
|
-
// Important that this does not throw inbetween making the promise arrays
|
|
1316
|
-
// and finalise being called, otherwise there will be lingering promises in the background
|
|
1317
|
-
|
|
1318
|
-
if (session.prologue && upgrade) {
|
|
1319
|
-
upgrade.start = upgrade.start < session.prologue.length ? 0 : upgrade.start
|
|
1320
|
-
upgrade.length =
|
|
1321
|
-
upgrade.start < session.prologue.length ? session.prologue.length : upgrade.length
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
const head = 2 * session.length
|
|
1325
|
-
const from = upgrade ? upgrade.start * 2 : 0
|
|
1326
|
-
const to = upgrade ? from + upgrade.length * 2 : head
|
|
1327
|
-
const node = normalizeIndexed(block, hash)
|
|
1328
|
-
|
|
1329
|
-
// can't do anything as we have no data...
|
|
1330
|
-
if (head === 0) return new TreeProof(session, null, null, null, null)
|
|
1331
|
-
|
|
1332
|
-
if (from >= to || to > head) {
|
|
1333
|
-
throw INVALID_OPERATION('Invalid upgrade')
|
|
1334
|
-
}
|
|
1335
|
-
if (seek && upgrade && node !== null && node.index >= from) {
|
|
1336
|
-
throw INVALID_OPERATION('Cannot both do a seek and block/hash request when upgrading')
|
|
1337
|
-
}
|
|
1338
|
-
|
|
1339
|
-
let subTree = head
|
|
1340
|
-
|
|
1341
|
-
const p = new TreeProof(session, block, hash, seek, upgrade)
|
|
1342
|
-
|
|
1343
|
-
if (node !== null && (!upgrade || node.lastIndex < upgrade.start)) {
|
|
1344
|
-
subTree = nodesToRoot(node.index, node.nodes, to)
|
|
1345
|
-
const seekRoot = seek
|
|
1346
|
-
? await seekUntrustedTree(session, subTree, seek.bytes, seek.padding)
|
|
1347
|
-
: head
|
|
1348
|
-
blockAndSeekProof(session, rx, node, seek, seekRoot, subTree, p.pending)
|
|
1349
|
-
} else if ((node || seek) && upgrade) {
|
|
1350
|
-
subTree = seek ? await seekFromHead(session, to, seek.bytes, seek.padding) : node.index
|
|
1351
|
-
}
|
|
1352
|
-
|
|
1353
|
-
if (upgrade) {
|
|
1354
|
-
upgradeProof(session, rx, node, seek, from, to, subTree, p.pending)
|
|
1355
|
-
if (head > to) additionalUpgradeProof(session, rx, to, head, p.pending)
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
return p
|
|
1359
|
-
}
|
|
1360
|
-
|
|
1361
1412
|
function getUnpaddedSize(node, padding, ite) {
|
|
1362
1413
|
return padding === 0
|
|
1363
1414
|
? node.size
|
package/lib/replicator.js
CHANGED
|
@@ -1138,9 +1138,8 @@ class Peer {
|
|
|
1138
1138
|
this.replicator._onupload(proof.block.index, proof.block.value.byteLength, this)
|
|
1139
1139
|
}
|
|
1140
1140
|
|
|
1141
|
-
if (proof.upgrade) {
|
|
1142
|
-
|
|
1143
|
-
if (remoteLength > this.pushedLength) this.pushedLength = remoteLength
|
|
1141
|
+
if (proof.upgrade && proof.coreLength > this.pushedLength) {
|
|
1142
|
+
this.pushedLength = proof.coreLength
|
|
1144
1143
|
}
|
|
1145
1144
|
|
|
1146
1145
|
this.wireData.send({
|
|
@@ -3173,6 +3172,7 @@ module.exports = class Replicator {
|
|
|
3173
3172
|
|
|
3174
3173
|
attachTo(protomux) {
|
|
3175
3174
|
if (this.destroyed) return
|
|
3175
|
+
if (protomux.stream.destroying) return
|
|
3176
3176
|
|
|
3177
3177
|
const makePeer = this._makePeer.bind(this, protomux)
|
|
3178
3178
|
|
package/lib/session-state.js
CHANGED
|
@@ -153,8 +153,9 @@ class SessionState {
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
async _updateSnapshotStorage(storage) {
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
const atom = this.atomized
|
|
157
|
+
if (!atom || !atom.flushing) return this.treeInfo()
|
|
158
|
+
while (atom.flushing) await atom.flushed()
|
|
158
159
|
|
|
159
160
|
let rx = storage.read()
|
|
160
161
|
const headPromise = rx.getHead()
|
|
@@ -597,11 +598,12 @@ class SessionState {
|
|
|
597
598
|
const [left, right] = flat.spans(b)
|
|
598
599
|
const s = left / 2
|
|
599
600
|
const e = right / 2 + 1
|
|
600
|
-
const has =
|
|
601
|
+
const has = start <= s && e <= end ? false : this.core.bitfield.hasSet(s, e - s)
|
|
601
602
|
if (has) break
|
|
602
603
|
|
|
603
604
|
tx.deleteTreeNode(a)
|
|
604
|
-
|
|
605
|
+
// delete the whole sibling subtree as it isnt needed per `has` check
|
|
606
|
+
tx.deleteTreeNodeRange(left, right + 1)
|
|
605
607
|
}
|
|
606
608
|
|
|
607
609
|
if (this.isDefault()) {
|