Skip to content

Commit

Permalink
fix!: update libp2p deps to 2.x.x
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires libp2p@2.x.x
  • Loading branch information
achingbrain committed Sep 11, 2024
1 parent ce9e82d commit 7cfc09a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
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)
}

Check warning on line 299 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L298-L299

Added lines #L298 - L299 were not covered by tests
}
}

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')
}

Check warning on line 60 in src/peer.ts

View check run for this annotation

Codecov / codecov/patch

src/peer.ts#L59-L60

Added lines #L59 - L60 were not covered by tests

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 7cfc09a

Please sign in to comment.