Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unslab publicKey and topic in PeerInfo #182

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading