hypercore 11.13.0 → 11.13.2

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.
@@ -679,6 +679,7 @@ class MerkleTree {
679
679
  // TODO: we could prop use a read batch here and do this in blocks of X for perf
680
680
  while (!ite.contains(head) && !(await hasTreeNode(session.storage, ite.index))) {
681
681
  cnt++
682
+ if (cnt >= 1024) throw ASSERTION('Bad arguments to missingNodes index=' + index + ' at length=' + length)
682
683
  ite.parent()
683
684
  }
684
685
 
@@ -742,6 +743,7 @@ function getByteOffsetBatch (roots, index, rx) {
742
743
  if ((index & 1) === 1) index = flat.leftSpan(index)
743
744
 
744
745
  let head = 0
746
+ let cnt = 0
745
747
 
746
748
  const promises = []
747
749
 
@@ -756,6 +758,8 @@ function getByteOffsetBatch (roots, index, rx) {
756
758
  const ite = flat.iterator(node.index)
757
759
 
758
760
  while (ite.index !== index) {
761
+ if (++cnt >= 1024) throw ASSERTION('Bad getByteOffsetSession index=' + index)
762
+
759
763
  if (index < ite.index) {
760
764
  ite.leftChild()
761
765
  } else {
@@ -930,8 +934,10 @@ async function seekTrustedTree (session, root, bytes, padding) {
930
934
  if (!bytes) return root
931
935
 
932
936
  const ite = flat.iterator(root)
937
+ let cnt = 0
933
938
 
934
939
  while ((ite.index & 1) !== 0) {
940
+ if (++cnt >= 1024) throw ASSERTION('Bad seekTrusted bytes=' + bytes + ', paddding=' + padding)
935
941
  const l = await getTreeNodeFromStorage(session.storage, ite.leftChild())
936
942
 
937
943
  if (l) {
@@ -972,11 +978,13 @@ async function seekUntrustedTree (session, root, bytes, padding) {
972
978
 
973
979
  function seekProof (session, rx, seekRoot, root, p) {
974
980
  const ite = flat.iterator(seekRoot)
981
+ let cnt = 0
975
982
 
976
983
  p.seek = []
977
984
  p.seek.push(getTreeNodeOrError(rx, ite.index))
978
985
 
979
986
  while (ite.index !== root) {
987
+ if (++cnt >= 1024) throw ASSERTION('Bad seekProof seekRoot=' + seekRoot + ', root=' + root)
980
988
  ite.sibling()
981
989
  p.seek.push(getTreeNodeOrError(rx, ite.index))
982
990
  ite.parent()
@@ -987,11 +995,13 @@ function blockAndSeekProof (session, rx, node, seek, seekRoot, root, p) {
987
995
  if (!node) return seekProof(session, rx, seekRoot, root, p)
988
996
 
989
997
  const ite = flat.iterator(node.index)
998
+ let cnt = 0
990
999
 
991
1000
  p.node = []
992
1001
  if (!node.value) p.node.push(getTreeNodeOrError(rx, ite.index))
993
1002
 
994
1003
  while (ite.index !== root) {
1004
+ if (++cnt >= 1024) throw ASSERTION('Bad blockAndSeekProof seekRoot=' + seekRoot + ', root=' + root)
995
1005
  ite.sibling()
996
1006
 
997
1007
  if (seek && ite.contains(seekRoot) && ite.index !== seekRoot) {
@@ -1017,10 +1027,13 @@ function upgradeProof (session, rx, node, seek, from, to, subTree, p) {
1017
1027
 
1018
1028
  const root = ite.index
1019
1029
  const target = from - 2
1030
+ let cnt = 0
1020
1031
 
1021
1032
  ite.seek(target)
1022
1033
 
1023
1034
  while (ite.index !== root) {
1035
+ if (++cnt >= 1024) throw ASSERTION('Bad upgradeProof target=' + target + ', root=' + root)
1036
+
1024
1037
  ite.sibling()
1025
1038
  if (ite.index > target) {
1026
1039
  if (p.node === null && p.seek === null && ite.contains(subTree)) {
@@ -1064,10 +1077,12 @@ function additionalUpgradeProof (session, rx, from, to, p) {
1064
1077
 
1065
1078
  const root = ite.index
1066
1079
  const target = from - 2
1080
+ let cnt = 0
1067
1081
 
1068
1082
  ite.seek(target)
1069
1083
 
1070
1084
  while (ite.index !== root) {
1085
+ if (++cnt >= 1024) throw ASSERTION('Bad arguments to additionalUpgradeProof root=' + root + ' target=' + target)
1071
1086
  ite.sibling()
1072
1087
  if (ite.index > target) {
1073
1088
  p.additionalUpgrade.push(getTreeNodeOrError(rx, ite.index))
package/lib/replicator.js CHANGED
@@ -30,7 +30,7 @@ const ReceiverQueue = require('./receiver-queue')
30
30
  const HotswapQueue = require('./hotswap-queue')
31
31
  const RemoteBitfield = require('./remote-bitfield')
32
32
  const { MerkleTree } = require('./merkle-tree')
33
- const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE } = require('hypercore-errors')
33
+ const { REQUEST_CANCELLED, REQUEST_TIMEOUT, INVALID_CAPABILITY, SNAPSHOT_NOT_AVAILABLE, ASSERTION } = require('hypercore-errors')
34
34
  const m = require('./messages')
35
35
  const caps = require('./caps')
36
36
 
@@ -1235,13 +1235,20 @@ class Peer {
1235
1235
 
1236
1236
  _hasTreeParent (index) {
1237
1237
  if (this.remoteLength >= this.core.state.length) return true
1238
+ if (!(index >= 0)) throw ASSERTION('bad index to _hasTreeParent: ' + index)
1238
1239
 
1239
1240
  const ite = flatTree.iterator(index * 2)
1240
1241
 
1241
1242
  let span = 2
1242
1243
  let length = 0
1244
+ let cnt = 0
1243
1245
 
1244
1246
  while (true) {
1247
+ if (++cnt >= 64) {
1248
+ throw ASSERTION('_hasTreeParent is stuck, index=' + index + ', length=' + length +
1249
+ ', span=' + span + ', remoteLength=' + this.remoteLength + ', length=' + this.core.state.length)
1250
+ }
1251
+
1245
1252
  ite.parent()
1246
1253
 
1247
1254
  const left = (ite.index - ite.factor / 2 + 1) / 2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.13.0",
3
+ "version": "11.13.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {