Skip to content

Commit

Permalink
fix!: update libp2p deps to 2.x.x (#146)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires libp2p@2.x.x
  • Loading branch information
achingbrain authored Sep 11, 2024
1 parent fee737a commit 2579986
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ updates:
schedule:
interval: daily
time: "10:00"
open-pull-requests-limit: 10
open-pull-requests-limit: 20
commit-message:
prefix: "deps"
prefix-development: "deps(dev)"
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# @libp2p/pubsub-peer-discovery

[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-pubsub-peer-discovery.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-pubsub-peer-discovery)
Expand All @@ -7,6 +9,21 @@
# About

<!--
!IMPORTANT!
Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.
To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts
To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.
-->

When the discovery module is started by libp2p it subscribes to the discovery pubsub topic(s)

It will immediately broadcast your peer data via pubsub and repeat the broadcast on the configured `interval`
Expand Down Expand Up @@ -104,7 +121,7 @@ $ npm i @libp2p/pubsub-peer-discovery

## Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `Libp2pPubsubPeerDiscovery` in the global namespace.
Loading this module through a script tag will make its exports available as `Libp2pPubsubPeerDiscovery` in the global namespace.

```html
<script src="https://unpkg.com/@libp2p/pubsub-peer-discovery/dist/index.min.js"></script>
Expand All @@ -118,8 +135,8 @@ Loading this module through a script tag will make it's exports available as `Li

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
- Apache 2.0, ([LICENSE-APACHE](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/libp2p/js-libp2p-pubsub-peer-discovery/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

# Contribution

Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,24 @@
"docs": "aegir docs"
},
"dependencies": {
"@libp2p/interface": "^1.0.1",
"@libp2p/interface-internal": "^1.0.1",
"@libp2p/peer-id": "^4.0.1",
"@libp2p/crypto": "^5.0.0",
"@libp2p/interface": "^2.0.0",
"@libp2p/interface-internal": "^2.0.0",
"@libp2p/peer-id": "^5.0.0",
"@multiformats/multiaddr": "^12.0.0",
"protons-runtime": "^5.0.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^5.0.2"
},
"devDependencies": {
"@libp2p/interface-compliance-tests": "^5.0.5",
"@libp2p/logger": "^4.0.1",
"@libp2p/peer-id-factory": "^4.0.0",
"@types/sinon": "^17.0.2",
"aegir": "^42.2.5",
"@libp2p/interface-compliance-tests": "^6.0.0",
"@libp2p/logger": "^5.0.0",
"@types/sinon": "^17.0.3",
"aegir": "^44.1.1",
"p-defer": "^4.0.0",
"p-wait-for": "^5.0.2",
"protons": "^7.3.1",
"sinon": "^17.0.1",
"sinon": "^18.0.1",
"sinon-ts": "^2.0.0"
}
}
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@
* The default pubsub topic the module subscribes to is `_peer-discovery._p2p._pubsub`, which is also set on `PubsubPeerDiscovery.TOPIC`.
*/

import { publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import { TypedEventEmitter, peerDiscoverySymbol } from '@libp2p/interface'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { peerIdFromPublicKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { Peer as PBPeer } from './peer.js'
import type { PeerDiscovery, PeerDiscoveryEvents, PeerId, PeerInfo, Message, PubSub, Startable, ComponentLogger, Logger } from '@libp2p/interface'
Expand Down Expand Up @@ -239,7 +240,7 @@ export class PubSubPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents>
}

const peer = {
publicKey: peerId.publicKey,
publicKey: publicKeyToProtobuf(peerId.publicKey),
addrs: this.components.addressManager.getAddresses().map(ma => ma.bytes)
}

Expand Down Expand Up @@ -275,9 +276,11 @@ export class PubSubPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents>
return
}

const peer = PBPeer.decode(message.data)
try {
const peer = PBPeer.decode(message.data)
const publicKey = publicKeyFromProtobuf(peer.publicKey)
const peerId = peerIdFromPublicKey(publicKey)

void peerIdFromKeys(peer.publicKey).then(peerId => {
// Ignore if we received our own response
if (peerId.equals(this.components.peerId)) {
return
Expand All @@ -291,9 +294,9 @@ export class PubSubPeerDiscovery extends TypedEventEmitter<PeerDiscoveryEvents>
multiaddrs: peer.addrs.map(b => multiaddr(b))
}
})
}).catch(err => {
this.log.error(err)
})
} catch (err) {
this.log.error('error handling incoming message', err)
}
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
/* eslint-disable @typescript-eslint/no-empty-interface */

import { type Codec, decodeMessage, encodeMessage, message } from 'protons-runtime'
import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, MaxLengthError, message } from 'protons-runtime'
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
import type { Uint8ArrayList } from 'uint8arraylist'

Expand Down Expand Up @@ -38,7 +38,7 @@ export namespace Peer {
if (opts.lengthDelimited !== false) {
w.ldelim()
}
}, (reader, length) => {
}, (reader, length, opts = {}) => {
const obj: any = {
publicKey: uint8ArrayAlloc(0),
addrs: []
Expand All @@ -55,6 +55,10 @@ export namespace Peer {
break
}
case 2: {
if (opts.limits?.addrs != null && obj.addrs.length === opts.limits.addrs) {
throw new MaxLengthError('Decode error - map field "addrs" had too many elements')
}

obj.addrs.push(reader.bytes())
break
}
Expand All @@ -76,7 +80,7 @@ export namespace Peer {
return encodeMessage(obj, Peer.codec())
}

export const decode = (buf: Uint8Array | Uint8ArrayList): Peer => {
return decodeMessage(buf, Peer.codec())
export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Peer>): Peer => {
return decodeMessage(buf, Peer.codec(), opts)
}
}
14 changes: 8 additions & 6 deletions test/compliance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env mocha */

import { CustomEvent } from '@libp2p/interface'
import { generateKeyPair, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import tests from '@libp2p/interface-compliance-tests/peer-discovery'
import { defaultLogger } from '@libp2p/logger'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { stubInterface } from 'sinon-ts'
import { pubsubPeerDiscovery, TOPIC } from '../src/index.js'
Expand All @@ -16,9 +16,11 @@ describe('compliance tests', () => {

tests({
async setup () {
const peerId = await createEd25519PeerId()
const subscriber = await createEd25519PeerId()
await new Promise(resolve => setTimeout(resolve, 10))
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)

const subscriberPrivateKey = await generateKeyPair('Ed25519')
const subscriber = peerIdFromPrivateKey(subscriberPrivateKey)

const addressManager = stubInterface<AddressManager>()
addressManager.getAddresses.returns([
Expand All @@ -40,7 +42,7 @@ describe('compliance tests', () => {

intervalId = setInterval(() => {
const peer = PBPeer.encode({
publicKey: subscriber.publicKey,
publicKey: publicKeyToProtobuf(subscriber.publicKey),
addrs: [
multiaddr('/ip4/166.10.1.2/tcp/80').bytes
]
Expand Down
20 changes: 12 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env mocha */

import { CustomEvent, start, stop } from '@libp2p/interface'
import { generateKeyPair, publicKeyFromProtobuf, publicKeyToProtobuf } from '@libp2p/crypto/keys'
import { start, stop } from '@libp2p/interface'
import { defaultLogger } from '@libp2p/logger'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { peerIdFromPrivateKey, peerIdFromPublicKey } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import defer from 'p-defer'
Expand All @@ -23,8 +23,11 @@ describe('PubSub Peer Discovery', () => {
let components: PubSubPeerDiscoveryComponents

beforeEach(async () => {
const peerId = await createEd25519PeerId()
const subscriber = await createEd25519PeerId()
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)

const subscriberPrivateKey = await generateKeyPair('Ed25519')
const subscriber = peerIdFromPrivateKey(subscriberPrivateKey)

mockPubsub = stubInterface<PubSub>({
getSubscribers: () => {
Expand Down Expand Up @@ -72,7 +75,7 @@ describe('PubSub Peer Discovery', () => {
}

const peer = PB.Peer.decode(eventData)
const peerId = await peerIdFromKeys(peer.publicKey)
const peerId = peerIdFromPublicKey(publicKeyFromProtobuf(peer.publicKey))
expect(peerId.equals(components.peerId)).to.equal(true)
expect(peer.addrs).to.have.length(1)
peer.addrs.forEach((addr) => {
Expand All @@ -97,7 +100,8 @@ describe('PubSub Peer Discovery', () => {
discovery = pubsubPeerDiscovery()(components)
await start(discovery)

const peerId = await createEd25519PeerId()
const privateKey = await generateKeyPair('Ed25519')
const peerId = peerIdFromPrivateKey(privateKey)
const expectedPeerData: PeerInfo = {
id: peerId,
multiaddrs: [
Expand All @@ -106,7 +110,7 @@ describe('PubSub Peer Discovery', () => {
]
}
const peer = {
publicKey: peerId.publicKey,
publicKey: publicKeyToProtobuf(peerId.publicKey),
addrs: expectedPeerData.multiaddrs.map(ma => multiaddr(ma).bytes)
}

Expand Down

0 comments on commit 2579986

Please sign in to comment.