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

feat!: update to libp2p@2.x.x #273

Merged
merged 4 commits into from
Sep 11, 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
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@
"lint": "aegir run lint",
"dep-check": "aegir run dep-check",
"release": "run-s build docs:no-publish npm:release docs",
"npm:release": "aegir release",
"npm:release": "aegir run release --concurrency 1",
"docs": "aegir docs",
"docs:no-publish": "aegir docs --publish false"
"docs:no-publish": "aegir docs --publish false",
"postinstall": "patch-package && rm -rf node_modules/@chainsafe/libp2p-gossipsub/node_modules"
},
"dependencies": {
"aegir": "^44.1.0",
"npm-run-all": "^4.1.5"
"npm-run-all": "^4.1.5",
"patch-package": "^8.0.0",
"rimraf": "^6.0.1"
},
"workspaces": [
"packages/*"
Expand Down
15 changes: 8 additions & 7 deletions packages/libp2p-daemon-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,24 @@
"build": "aegir build",
"pretest": "npm run build",
"test": "aegir test -t node",
"test:node": "aegir test -t node"
"test:node": "aegir test -t node",
"release": "aegir release"
},
"dependencies": {
"@libp2p/daemon-protocol": "^6.0.0",
"@libp2p/interface": "^1.1.2",
"@libp2p/logger": "^4.0.5",
"@libp2p/peer-id": "^4.0.5",
"@libp2p/tcp": "^9.0.13",
"@libp2p/interface": "^2.0.0",
"@libp2p/logger": "^5.0.0",
"@libp2p/peer-id": "^5.0.0",
"@libp2p/tcp": "^10.0.0",
"@multiformats/multiaddr": "^12.1.14",
"it-protobuf-stream": "^1.1.2",
"multiformats": "^13.0.1"
},
"devDependencies": {
"@chainsafe/libp2p-gossipsub": "^13.0.0",
"@libp2p/daemon-server": "^7.0.0",
"@libp2p/interface-compliance-tests": "^5.2.0",
"@libp2p/kad-dht": "^12.0.5",
"@libp2p/interface-compliance-tests": "^6.0.0",
"@libp2p/kad-dht": "^13.0.0",
"aegir": "^44.1.0",
"it-all": "^3.0.4",
"it-pipe": "^3.0.1",
Expand Down
57 changes: 29 additions & 28 deletions packages/libp2p-daemon-client/src/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
DHTRequest,
DHTResponse
} from '@libp2p/daemon-protocol'
import { CodeError } from '@libp2p/interface'
import { InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface'
import { isPeerId, type PeerId, type PeerInfo } from '@libp2p/interface'
import { logger } from '@libp2p/logger'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { peerIdFromMultihash } from '@libp2p/peer-id'
import { multiaddr } from '@multiformats/multiaddr'
import { CID } from 'multiformats/cid'
import type { DaemonClient } from './index.js'
import * as Digest from 'multiformats/hashes/digest'
import { OperationFailedError, type DaemonClient } from './index.js'

const log = logger('libp2p:daemon-client:dht')

Expand All @@ -26,11 +27,11 @@ export class DHT {
*/
async put (key: Uint8Array, value: Uint8Array): Promise<void> {
if (!(key instanceof Uint8Array)) {
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
throw new InvalidParametersError('invalid key received')
}

if (!(value instanceof Uint8Array)) {
throw new CodeError('value received is not a Uint8Array', 'ERR_INVALID_VALUE')
throw new InvalidParametersError('value received is not a Uint8Array')
}

const sh = await this.client.send({
Expand All @@ -49,7 +50,7 @@ export class DHT {
await sh.unwrap().close()

if (response.type !== Response.Type.OK) {
throw new CodeError(response.error?.msg ?? 'DHT put failed', 'ERR_DHT_PUT_FAILED')
throw new ProtocolError(response.error?.msg ?? 'DHT put failed')
}
}

Expand All @@ -58,7 +59,7 @@ export class DHT {
*/
async get (key: Uint8Array): Promise<Uint8Array> {
if (!(key instanceof Uint8Array)) {
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
throw new InvalidParametersError('invalid key received')
}

const sh = await this.client.send({
Expand All @@ -74,11 +75,11 @@ export class DHT {
await sh.unwrap().close()

if (response.type !== Response.Type.OK) {
throw new CodeError(response.error?.msg ?? 'DHT get failed', 'ERR_DHT_GET_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT get failed')
}

if (response.dht?.value == null) {
throw new CodeError('Invalid DHT get response', 'ERR_DHT_GET_FAILED')
throw new OperationFailedError('Invalid DHT get response')
}

return response.dht.value
Expand All @@ -89,14 +90,14 @@ export class DHT {
*/
async findPeer (peerId: PeerId): Promise<PeerInfo> {
if (!isPeerId(peerId)) {
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
throw new InvalidParametersError('invalid peer id received')
}

const sh = await this.client.send({
type: Request.Type.DHT,
dht: {
type: DHTRequest.Type.FIND_PEER,
peer: peerId.toBytes()
peer: peerId.toMultihash().bytes
}
})

Expand All @@ -105,15 +106,15 @@ export class DHT {
await sh.unwrap().close()

if (response.type !== Response.Type.OK) {
throw new CodeError(response.error?.msg ?? 'DHT find peer failed', 'ERR_DHT_FIND_PEER_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT find peer failed')
}

if (response.dht?.peer?.addrs == null) {
throw new CodeError('Invalid response', 'ERR_DHT_FIND_PEER_FAILED')
throw new OperationFailedError('Invalid response')
}

return {
id: peerIdFromBytes(response.dht.peer.id),
id: peerIdFromMultihash(Digest.decode(response.dht.peer.id)),
multiaddrs: response.dht.peer.addrs.map((a) => multiaddr(a))
}
}
Expand All @@ -123,7 +124,7 @@ export class DHT {
*/
async provide (cid: CID): Promise<void> {
if (cid == null || CID.asCID(cid) == null) {
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
throw new InvalidParametersError('invalid cid received')
}

const sh = await this.client.send({
Expand All @@ -139,7 +140,7 @@ export class DHT {
await sh.unwrap().close()

if (response.type !== Response.Type.OK) {
throw new CodeError(response.error?.msg ?? 'DHT provide failed', 'ERR_DHT_PROVIDE_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT provide failed')
}
}

Expand All @@ -148,7 +149,7 @@ export class DHT {
*/
async * findProviders (cid: CID, count: number = 1): AsyncIterable<PeerInfo> {
if (cid == null || CID.asCID(cid) == null) {
throw new CodeError('invalid cid received', 'ERR_INVALID_CID')
throw new InvalidParametersError('invalid cid received')
}

const sh = await this.client.send({
Expand All @@ -165,7 +166,7 @@ export class DHT {

if (response.type !== Response.Type.OK) {
await sh.unwrap().close()
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
}

while (true) {
Expand All @@ -180,13 +181,13 @@ export class DHT {
// Stream values
if (dhtResponse.type === DHTResponse.Type.VALUE && dhtResponse.peer?.addrs != null) {
yield {
id: peerIdFromBytes(dhtResponse.peer.id),
id: peerIdFromMultihash(Digest.decode(dhtResponse.peer.id)),
multiaddrs: dhtResponse.peer.addrs.map((a) => multiaddr(a))
}
} else {
// Unexpected message received
await sh.unwrap().close()
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
throw new ProtocolError('unexpected message received')
}
}
}
Expand All @@ -196,7 +197,7 @@ export class DHT {
*/
async * getClosestPeers (key: Uint8Array): AsyncIterable<PeerInfo> {
if (!(key instanceof Uint8Array)) {
throw new CodeError('invalid key received', 'ERR_INVALID_KEY')
throw new InvalidParametersError('invalid key received')
}

const sh = await this.client.send({
Expand All @@ -212,7 +213,7 @@ export class DHT {

if (response.type !== Response.Type.OK) {
await sh.unwrap().close()
throw new CodeError(response.error?.msg ?? 'DHT find providers failed', 'ERR_DHT_FIND_PROVIDERS_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT find providers failed')
}

while (true) {
Expand All @@ -226,7 +227,7 @@ export class DHT {

// Stream values
if (dhtResponse.type === DHTResponse.Type.VALUE && dhtResponse.value != null) {
const peerId = peerIdFromBytes(dhtResponse.value)
const peerId = peerIdFromMultihash(Digest.decode(dhtResponse.value))

yield {
id: peerId,
Expand All @@ -235,7 +236,7 @@ export class DHT {
} else {
// Unexpected message received
await sh.unwrap().close()
throw new CodeError('unexpected message received', 'ERR_UNEXPECTED_MESSAGE_RECEIVED')
throw new InvalidMessageError('unexpected message received')
}
}
}
Expand All @@ -245,14 +246,14 @@ export class DHT {
*/
async getPublicKey (peerId: PeerId): Promise<Uint8Array | undefined> {
if (!isPeerId(peerId)) {
throw new CodeError('invalid peer id received', 'ERR_INVALID_PEER_ID')
throw new InvalidParametersError('invalid peer id received')
}

const sh = await this.client.send({
type: Request.Type.DHT,
dht: {
type: DHTRequest.Type.GET_PUBLIC_KEY,
peer: peerId.toBytes()
peer: peerId.toMultihash().bytes
}
})

Expand All @@ -261,11 +262,11 @@ export class DHT {
await sh.unwrap().close()

if (response.type !== Response.Type.OK) {
throw new CodeError(response.error?.msg ?? 'DHT get public key failed', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
throw new OperationFailedError(response.error?.msg ?? 'DHT get public key failed')
}

if (response.dht == null) {
throw new CodeError('Invalid response', 'ERR_DHT_GET_PUBLIC_KEY_FAILED')
throw new InvalidMessageError('Invalid response')
}

return response.dht.value
Expand Down
Loading
Loading