Skip to content

Commit

Permalink
fix: peers were not published on pubsub, updated dependencies (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrunic authored Aug 31, 2022
1 parent 4136998 commit a4b9a69
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,24 @@
"release": "semantic-release"
},
"dependencies": {
"@libp2p/components": "^2.0.1",
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interface-pubsub": "^2.0.0",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^1.1.8",
"@multiformats/multiaddr": "^10.1.7",
"protons-runtime": "^2.0.2"
"@libp2p/components": "^2.0.4",
"@libp2p/interface-peer-discovery": "^1.0.1",
"@libp2p/interface-peer-info": "^1.0.2",
"@libp2p/interface-pubsub": "^2.0.1",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/logger": "^2.0.1",
"@libp2p/peer-id": "^1.1.15",
"@multiformats/multiaddr": "^10.4.0",
"protons-runtime": "^3.1.0"
},
"devDependencies": {
"@libp2p/interface-address-manager": "^1.0.1",
"@libp2p/interface-peer-discovery-compliance-tests": "^1.0.0",
"@libp2p/peer-id-factory": "^1.0.8",
"@libp2p/interface-address-manager": "^1.0.2",
"@libp2p/interface-peer-discovery-compliance-tests": "^1.0.1",
"@libp2p/peer-id-factory": "^1.0.18",
"aegir": "^37.2.0",
"p-defer": "^4.0.0",
"p-wait-for": "^5.0.0",
"protons": "^4.0.1",
"protons": "^5.1.0",
"sinon": "^14.0.0",
"ts-sinon": "^2.0.2"
}
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ export class PubSubPeerDiscovery extends EventEmitter<PeerDiscoveryEvents> imple

for (const topic of this.topics) {
log('broadcasting our peer data on topic %s', topic)
pubsub.dispatchEvent(new CustomEvent('message', {
detail: encodedPeer
}))
void pubsub.publish(topic, encodedPeer)
}
}

Expand Down
70 changes: 63 additions & 7 deletions src/peer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,80 @@
/* eslint-disable import/export */
/* eslint-disable @typescript-eslint/no-namespace */

import { encodeMessage, decodeMessage, message, bytes } from 'protons-runtime'
import type { Codec } from 'protons-runtime'
import { encodeMessage, decodeMessage, message } from 'protons-runtime'
import type { Uint8ArrayList } from 'uint8arraylist'
import type { Codec } from 'protons-runtime'

export interface Peer {
publicKey: Uint8Array
addrs: Uint8Array[]
}

export namespace Peer {
let _codec: Codec<Peer>

export const codec = (): Codec<Peer> => {
return message<Peer>({
0: { name: 'publicKey', codec: bytes },
1: { name: 'addrs', codec: bytes, repeats: true }
})
if (_codec == null) {
_codec = message<Peer>((obj, writer, opts = {}) => {
if (opts.lengthDelimited !== false) {
writer.fork()
}

if (obj.publicKey != null) {
writer.uint32(2)
writer.bytes(obj.publicKey)
} else {
throw new Error('Protocol error: required field "publicKey" was not found in object')
}

if (obj.addrs != null) {
for (const value of obj.addrs) {
writer.uint32(10)
writer.bytes(value)
}
} else {
throw new Error('Protocol error: required field "addrs" was not found in object')
}

if (opts.lengthDelimited !== false) {
writer.ldelim()
}
}, (reader, length) => {
const obj: any = {
publicKey: new Uint8Array(0),
addrs: []
}

const end = length == null ? reader.len : reader.pos + length

while (reader.pos < end) {
const tag = reader.uint32()

switch (tag >>> 3) {
case 0:
obj.publicKey = reader.bytes()
break
case 1:
obj.addrs.push(reader.bytes())
break
default:
reader.skipType(tag & 7)
break
}
}

if (obj.publicKey == null) {
throw new Error('Protocol error: value for required field "publicKey" was not found in protobuf')
}

return obj
})
}

return _codec
}

export const encode = (obj: Peer): Uint8ArrayList => {
export const encode = (obj: Peer): Uint8Array => {
return encodeMessage(obj, Peer.codec())
}

Expand Down
14 changes: 7 additions & 7 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ describe('PubSub Peer Discovery', () => {
await discovery.start()
await discovery.afterStart()

expect(mockPubsub.dispatchEvent.callCount).to.equal(1)
expect(mockPubsub.publish.callCount).to.equal(1)
discovery._broadcast()
expect(mockPubsub.dispatchEvent.callCount).to.equal(2)
expect(mockPubsub.publish.callCount).to.equal(2)

const event = mockPubsub.dispatchEvent.getCall(0).args[0] as CustomEvent<Uint8Array>
const eventData = mockPubsub.publish.getCall(0).args[1]

if (!('byteLength' in event.detail)) {
if (!('byteLength' in eventData)) {
throw new Error('Wrong argument type passed to dispatchEvent')
}

const peer = PB.Peer.decode(event.detail)
const peer = PB.Peer.decode(eventData)
const peerId = await peerIdFromKeys(peer.publicKey)
expect(peerId.equals(components.getPeerId())).to.equal(true)
expect(peer.addrs).to.have.length(1)
Expand All @@ -78,7 +78,7 @@ describe('PubSub Peer Discovery', () => {
detail: {
type: 'unsigned',
topic: TOPIC,
data: event.detail
data: eventData
}
}))
expect(spy.callCount).to.equal(0)
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('PubSub Peer Discovery', () => {
discovery.init(components)
await start(discovery)

await pWaitFor(() => mockPubsub.dispatchEvent.callCount >= 2)
await pWaitFor(() => mockPubsub.publish.callCount >= 2)
})

it('should be able to add and remove peer listeners', async () => {
Expand Down

0 comments on commit a4b9a69

Please sign in to comment.