blind-peer 2.7.17 → 2.8.1

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 (4) hide show
  1. package/bin.js +16 -5
  2. package/index.js +47 -0
  3. package/lib/db.js +24 -3
  4. package/package.json +3 -3
package/bin.js CHANGED
@@ -60,6 +60,15 @@ const cmd = command('blind-peer',
60
60
  logger.info(record)
61
61
  }
62
62
  })
63
+ blindPeer.on('delete-blocked', (stream, { key }) => {
64
+ logger.info(`Blocked delete-core request from untrusted peer ${streamToStr(stream)} for core ${idEnc.normalize(key)}`)
65
+ })
66
+ blindPeer.on('delete-core', (stream, { key, existing }) => {
67
+ logger.info(`Received delete-core request from trusted peer ${streamToStr(stream)} for core ${idEnc.normalize(key)}. Existing: ${existing}`)
68
+ })
69
+ blindPeer.on('delete-core-end', (stream, { key, announced }) => {
70
+ logger.info(`Completed delete-core request from trusted peer ${streamToStr(stream)} for core ${idEnc.normalize(key)}. Was announced: ${announced}`)
71
+ })
63
72
 
64
73
  blindPeer.on('downgrade-announce', ({ record, remotePublicKey }) => {
65
74
  try {
@@ -70,13 +79,13 @@ const cmd = command('blind-peer',
70
79
  })
71
80
 
72
81
  blindPeer.on('announce-core', core => {
73
- logger.info(`Started announcing core ${coreToInfo(core)}`)
82
+ logger.info(`Started announcing core ${coreToInfo(core, true)}`)
74
83
  })
75
84
  blindPeer.on('core-downloaded', core => {
76
- logger.info(`Announced core fully downloaded: ${coreToInfo(core)}`)
85
+ logger.info(`Announced core fully downloaded: ${coreToInfo(core, true)}`)
77
86
  })
78
87
  blindPeer.on('core-append', core => {
79
- logger.info(`Detected announced-core length update: ${coreToInfo(core)}`)
88
+ logger.info(`Detected announced-core length update: ${coreToInfo(core, true)}`)
80
89
  })
81
90
 
82
91
  blindPeer.on('gc-start', ({ bytesToClear }) => {
@@ -220,9 +229,11 @@ function streamToStr (stream) {
220
229
  return `${pubKey}`
221
230
  }
222
231
 
223
- function coreToInfo (core) {
232
+ function coreToInfo (core, includePublicKey = false) {
224
233
  const discKey = hypCrypto.discoveryKey(core.key)
225
- return `Discovery key ${idEnc.normalize(discKey)} (${core.contiguousLength} / ${core.length}, ${core.peers.length} peers)`
234
+ let res = `Discovery key ${idEnc.normalize(discKey)} (${core.contiguousLength} / ${core.length}, ${core.peers.length} peers)`
235
+ if (includePublicKey) res += `. Public key: ${idEnc.normalize(core.key)}`
236
+ return res
226
237
  }
227
238
 
228
239
  cmd.parse()
package/index.js CHANGED
@@ -15,6 +15,7 @@ const IdEnc = require('hypercore-id-encoding')
15
15
  const BlindPeerDB = require('./lib/db.js')
16
16
 
17
17
  const { AddCoreEncoding } = require('blind-peer-encodings')
18
+ const { DeleteCoreEncoding } = require('blind-peer-encodings')
18
19
 
19
20
  class CoreTracker {
20
21
  constructor (blindPeer, core) {
@@ -365,6 +366,7 @@ class BlindPeer extends ReadyResource {
365
366
  })
366
367
 
367
368
  rpc.respond('add-core', AddCoreEncoding, this._onaddcore.bind(this, conn))
369
+ rpc.respond('delete-core', DeleteCoreEncoding, this._ondeletecore.bind(this, conn))
368
370
  }
369
371
 
370
372
  async _activateCore (stream, record) {
@@ -454,6 +456,51 @@ class BlindPeer extends ReadyResource {
454
456
  return coreRecord
455
457
  }
456
458
 
459
+ async _ondeletecore (stream, { key }) {
460
+ if (!this._isTrustedPeer(stream.remotePublicKey)) {
461
+ this.emit('delete-blocked', stream, { key })
462
+ throw new Error('Only trusted peers can delete cores')
463
+ }
464
+
465
+ const existing = await this.db.getCoreRecord(key) !== null
466
+ this.emit('delete-core', stream, { key, existing })
467
+ if (!existing) return false
468
+
469
+ const core = this.store.get({ key })
470
+ await core.ready()
471
+
472
+ const announced = this.announcedCores.has(core.id)
473
+ if (announced) {
474
+ this.swarm.leave(core.discoveryKey)
475
+ try {
476
+ // Closes the download session
477
+ await this.announcedCores.get(core.id).close()
478
+ } catch (e) {
479
+ safetyCatch(e)
480
+ }
481
+ this.announcedCores.delete(core.id)
482
+ }
483
+
484
+ const hexId = b4a.toString(core.discoveryKey, 'hex')
485
+ const tracker = this.activeReplication.get(hexId)
486
+ if (tracker) {
487
+ try {
488
+ // cancel the download request and trigger the cleanup logic
489
+ // which removes it from the active replication map
490
+ await tracker.core.close()
491
+ } catch (e) {
492
+ safetyCatch(e)
493
+ }
494
+ }
495
+
496
+ await core.clear(0, core.length)
497
+
498
+ this.db.deleteCore(key)
499
+ await this.flush()
500
+ this.emit('delete-core-end', stream, { key, announced })
501
+ return true
502
+ }
503
+
457
504
  async _close () {
458
505
  clearInterval(this.flushInterval)
459
506
  if (this.ownsWakeup) this.wakeup.destroy()
package/lib/db.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const HyperDB = require('hyperdb')
2
2
  const crypto = require('hypercore-crypto')
3
3
  const ReadyResource = require('ready-resource')
4
+ const IdEnc = require('hypercore-id-encoding')
4
5
  const { definition: spec } = require('blind-peer-encodings')
5
6
 
6
7
  // LOW, NORMAL, HIGH
@@ -18,6 +19,7 @@ module.exports = class BlindPeerDB extends ReadyResource {
18
19
  this.swarmingKeyPair = null
19
20
 
20
21
  this.coresAdding = []
22
+ this.coresDeleting = []
21
23
  this.coresUpdated = new Map()
22
24
 
23
25
  this.stats = {
@@ -66,17 +68,27 @@ module.exports = class BlindPeerDB extends ReadyResource {
66
68
 
67
69
  const coresAdding = this.coresAdding
68
70
  const coresUpdated = this.coresUpdated
71
+ const coresDeleting = this.coresDeleting
69
72
 
70
73
  this.coresAdding = []
71
74
  this.coresUpdated = new Map()
75
+ this.coresDeleting = []
72
76
 
73
77
  const tx = this.db.transaction()
74
78
  const time = Date.now()
75
79
 
76
80
  let addedCores = 0
81
+ let deletedCores = 0
77
82
  let addedReferrers = 0
78
83
  let bytesAllocated = 0
79
84
 
85
+ for (const key of coresDeleting) {
86
+ const record = await tx.get('@blind-peer/cores', { key })
87
+ deletedCores++
88
+ bytesAllocated -= record.bytesAllocated
89
+ await tx.delete('@blind-peer/cores', { key })
90
+ }
91
+
80
92
  for (const info of coresAdding) {
81
93
  const key = info.key
82
94
  const existing = await tx.get('@blind-peer/cores', { key })
@@ -128,8 +140,8 @@ module.exports = class BlindPeerDB extends ReadyResource {
128
140
  await tx.insert('@blind-peer/cores', c)
129
141
  }
130
142
 
131
- if (addedCores || addedReferrers || bytesAllocated || this.closing) {
132
- this.digest.cores += addedCores
143
+ if (addedCores || deletedCores || addedReferrers || bytesAllocated || this.closing) {
144
+ this.digest.cores += addedCores - deletedCores
133
145
  this.digest.referrers += addedReferrers
134
146
  this.digest.bytesAllocated += bytesAllocated
135
147
  this.digest.flushed = !!this.closing
@@ -141,16 +153,25 @@ module.exports = class BlindPeerDB extends ReadyResource {
141
153
  await tx.flush()
142
154
  }
143
155
 
156
+ async hasCore (key) {
157
+ key = IdEnc.decode(key)
158
+ return await this.db.get('@blind-peer/cores', { key }) !== null
159
+ }
160
+
144
161
  addCore (info) {
145
162
  this.coresAdding.push(info)
146
163
  }
147
164
 
165
+ deleteCore (key) {
166
+ this.coresDeleting.push(key)
167
+ }
168
+
148
169
  updateCore (core, id) { // TODO: id is technically optional
149
170
  this.coresUpdated.set(id, core)
150
171
  }
151
172
 
152
173
  updated () {
153
- return this.coresAdding.length > 0 || this.coresUpdated.size > 0
174
+ return this.coresAdding.length > 0 || this.coresUpdated.size > 0 || this.coresDeleting.length > 0
154
175
  }
155
176
 
156
177
  createGcCandidateReadStream () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blind-peer",
3
- "version": "2.7.17",
3
+ "version": "2.8.1",
4
4
  "description": "Blind peers help keep hypercores available",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -10,7 +10,7 @@
10
10
  "autobase": "^7.0.18",
11
11
  "autobase-discovery": "^1.0.0",
12
12
  "b4a": "^1.6.7",
13
- "blind-peer-encodings": "^3.0.0",
13
+ "blind-peer-encodings": "^3.1.0",
14
14
  "compact-encoding": "^2.16.0",
15
15
  "corestore": "^7.4.4",
16
16
  "graceful-goodbye": "^1.3.3",
@@ -33,7 +33,7 @@
33
33
  "tiny-byte-size": "^1.1.0"
34
34
  },
35
35
  "devDependencies": {
36
- "blind-peering": "^1.10.1",
36
+ "blind-peering": "^1.12.0",
37
37
  "brittle": "^3.7.0",
38
38
  "debounceify": "^1.1.0",
39
39
  "hyperdht": "^6.20.1",