Skip to content

Commit

Permalink
Unslab publicKey and topic in PeerInfo (#182)
Browse files Browse the repository at this point in the history
* Unslab publicKey and topic in PeerInfo

* Add unslab

* unslab in PeerInfo constructor

* Postpone unslab(topic) to when it's stored

* Undo previous commit

* Add TODO on removing topics array
  • Loading branch information
HDegroote authored Jul 30, 2024
1 parent 04b2004 commit 70bfa00
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { EventEmitter } = require('events')
const DHT = require('hyperdht')
const spq = require('shuffled-priority-queue')
const b4a = require('b4a')
const unslab = require('unslab')

const PeerInfo = require('./lib/peer-info')
const RetryTimer = require('./lib/retry-timer')
Expand Down Expand Up @@ -423,6 +424,8 @@ module.exports = class Hyperswarm extends EventEmitter {
// TODO: When you rejoin, it should reannounce + bump lookup priority
join (topic, opts = {}) {
if (!topic) throw new Error(ERR_MISSING_TOPIC)
topic = unslab(topic)

const topicString = b4a.toString(topic, 'hex')

let discovery = this._discovery.get(topicString)
Expand Down
5 changes: 3 additions & 2 deletions lib/peer-info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { EventEmitter } = require('events')
const b4a = require('b4a')
const unslab = require('unslab')

const MIN_CONNECTION_TIME = 15000

Expand All @@ -13,7 +14,7 @@ module.exports = class PeerInfo extends EventEmitter {
constructor ({ publicKey, relayAddresses }) {
super()

this.publicKey = publicKey
this.publicKey = unslab(publicKey)
this.relayAddresses = relayAddresses

this.reconnecting = true
Expand All @@ -28,7 +29,7 @@ module.exports = class PeerInfo extends EventEmitter {
// Set by the Swarm
this.queued = false
this.client = false
this.topics = []
this.topics = [] // TODO: remove on next major (check with mafintosh for context)

this.attempts = 0
this.priority = NORMAL_PRIORITY
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"bare-events": "^2.2.0",
"hyperdht": "^6.11.0",
"safety-catch": "^1.0.2",
"shuffled-priority-queue": "^2.1.0"
"shuffled-priority-queue": "^2.1.0",
"unslab": "^1.3.0"
},
"devDependencies": {
"brittle": "^3.0.2",
Expand Down
43 changes: 43 additions & 0 deletions test/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,49 @@ test('peerDiscovery has unslabbed closestNodes', async (t) => {
t.is(hasUnslabbeds, false, 'sanity check: all are unslabbed')
})

test('topic and peer get unslabbed in PeerInfo', async (t) => {
const { bootstrap } = await createTestnet(3, t.teardown)

const swarm1 = new Hyperswarm({ bootstrap })
const swarm2 = new Hyperswarm({ bootstrap })

t.plan(3)

t.teardown(async () => {
await swarm1.destroy()
await swarm2.destroy()
})

swarm2.on('connection', (conn) => {
t.is(
[...swarm2.peers.values()][0].publicKey.buffer.byteLength,
32,
'unslabbed publicKey in peerInfo'
)
t.is([...swarm2.peers.values()][0].topics[0].buffer.byteLength,
32,
'unslabbed topic in peerInfo'
)

conn.on('error', noop)
conn.end()
})
swarm1.on('connection', (conn) => {
t.is(
[...swarm1.peers.values()][0].publicKey.buffer.byteLength,
32,
'unslabbed publicKey in peerInfo'
)

conn.on('error', noop)
conn.end()
})

const topic = Buffer.alloc(32).fill('hello world')
await swarm1.join(topic, { server: true, client: false }).flushed()
swarm2.join(topic, { client: true, server: false })
})

function noop () {}

function eventFlush () {
Expand Down

0 comments on commit 70bfa00

Please sign in to comment.