From 8602a704e45cfa768ad55974d025b2d4be6f42a9 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 27 Apr 2023 18:17:53 +0100 Subject: [PATCH] fix: use interface-libp2p to ensure the correct services are set (#203) --- packages/libp2p-daemon-client/package.json | 1 + .../libp2p-daemon-client/test/dht.spec.ts | 12 +++--- .../libp2p-daemon-client/test/index.spec.ts | 9 +++-- .../libp2p-daemon-client/test/pubsub.spec.ts | 10 +++-- .../libp2p-daemon-client/test/stream.spec.ts | 9 +++-- packages/libp2p-daemon-server/package.json | 1 + packages/libp2p-daemon-server/src/dht.ts | 6 +-- packages/libp2p-daemon-server/src/index.ts | 40 +++++-------------- .../libp2p-daemon-server/test/index.spec.ts | 11 +++-- 9 files changed, 48 insertions(+), 51 deletions(-) diff --git a/packages/libp2p-daemon-client/package.json b/packages/libp2p-daemon-client/package.json index 0d497acc..a385ecfd 100644 --- a/packages/libp2p-daemon-client/package.json +++ b/packages/libp2p-daemon-client/package.json @@ -151,6 +151,7 @@ "devDependencies": { "@libp2p/daemon-server": "^5.0.0", "@libp2p/interface-dht": "^2.0.0", + "@libp2p/interface-libp2p": "3.0.0", "@libp2p/interface-mocks": "^11.0.0", "@libp2p/interface-peer-store": "^2.0.0", "@libp2p/interface-pubsub": "^4.0.0", diff --git a/packages/libp2p-daemon-client/test/dht.spec.ts b/packages/libp2p-daemon-client/test/dht.spec.ts index 2d494658..5db0a587 100644 --- a/packages/libp2p-daemon-client/test/dht.spec.ts +++ b/packages/libp2p-daemon-client/test/dht.spec.ts @@ -3,14 +3,16 @@ import { expect } from 'aegir/chai' import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createServer, Libp2p, Libp2pServer } from '@libp2p/daemon-server' +import { createServer, Libp2pServer } from '@libp2p/daemon-server' import { createClient, DaemonClient } from '../src/index.js' import { multiaddr } from '@multiformats/multiaddr' import { StubbedInstance, stubInterface } from 'sinon-ts' -import { DualDHT, ValueEvent, FinalPeerEvent, PeerResponseEvent, MessageType, EventTypes } from '@libp2p/interface-dht' +import { DualDHT, ValueEvent, FinalPeerEvent, PeerResponseEvent, MessageType, EventTypes, DHT } from '@libp2p/interface-dht' import { peerIdFromString } from '@libp2p/peer-id' import { CID } from 'multiformats/cid' import all from 'it-all' +import type { Libp2p } from '@libp2p/interface-libp2p' +import type { PubSub } from '@libp2p/interface-pubsub' const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/12345') @@ -21,15 +23,15 @@ function match (cid: CID): sinon.SinonMatcher { describe('daemon dht client', function () { this.timeout(30e3) - let libp2p: StubbedInstance + let libp2p: StubbedInstance> let server: Libp2pServer let client: DaemonClient let dht: StubbedInstance beforeEach(async function () { dht = stubInterface() - libp2p = stubInterface() - libp2p.dht = dht + libp2p = stubInterface>() + libp2p.services.dht = dht server = createServer(defaultMultiaddr, libp2p) diff --git a/packages/libp2p-daemon-client/test/index.spec.ts b/packages/libp2p-daemon-client/test/index.spec.ts index 2449af44..801eb6ec 100644 --- a/packages/libp2p-daemon-client/test/index.spec.ts +++ b/packages/libp2p-daemon-client/test/index.spec.ts @@ -2,7 +2,7 @@ import { expect } from 'aegir/chai' import sinon from 'sinon' -import { createServer, Libp2p, Libp2pServer } from '@libp2p/daemon-server' +import { createServer, Libp2pServer } from '@libp2p/daemon-server' import { createClient, DaemonClient } from '../src/index.js' import { multiaddr } from '@multiformats/multiaddr' import { StubbedInstance, stubInterface } from 'sinon-ts' @@ -10,18 +10,21 @@ import { isPeerId } from '@libp2p/interface-peer-id' import { peerIdFromString } from '@libp2p/peer-id' import { mockConnection, mockDuplex, mockMultiaddrConnection } from '@libp2p/interface-mocks' import type { PeerStore } from '@libp2p/interface-peer-store' +import type { Libp2p } from '@libp2p/interface-libp2p' +import type { DHT } from '@libp2p/interface-dht' +import type { PubSub } from '@libp2p/interface-pubsub' const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/0') describe('daemon client', function () { this.timeout(30e3) - let libp2p: StubbedInstance + let libp2p: StubbedInstance> let server: Libp2pServer let client: DaemonClient beforeEach(async function () { - libp2p = stubInterface() + libp2p = stubInterface>() libp2p.peerStore = stubInterface() server = createServer(defaultMultiaddr, libp2p) diff --git a/packages/libp2p-daemon-client/test/pubsub.spec.ts b/packages/libp2p-daemon-client/test/pubsub.spec.ts index b21e6af9..03dab7ac 100644 --- a/packages/libp2p-daemon-client/test/pubsub.spec.ts +++ b/packages/libp2p-daemon-client/test/pubsub.spec.ts @@ -3,27 +3,29 @@ import { expect } from 'aegir/chai' import sinon from 'sinon' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createServer, Libp2p, Libp2pServer } from '@libp2p/daemon-server' +import { createServer, Libp2pServer } from '@libp2p/daemon-server' import { createClient, DaemonClient } from '../src/index.js' import { multiaddr } from '@multiformats/multiaddr' import { StubbedInstance, stubInterface } from 'sinon-ts' import type { PubSub } from '@libp2p/interface-pubsub' import { peerIdFromString } from '@libp2p/peer-id' +import type { Libp2p } from '@libp2p/interface-libp2p' +import type { DHT } from '@libp2p/interface-dht' const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/12345') describe('daemon pubsub client', function () { this.timeout(30e3) - let libp2p: StubbedInstance + let libp2p: StubbedInstance> let server: Libp2pServer let client: DaemonClient let pubsub: StubbedInstance beforeEach(async function () { pubsub = stubInterface() - libp2p = stubInterface() - libp2p.pubsub = pubsub + libp2p = stubInterface>() + libp2p.services.pubsub = pubsub server = createServer(defaultMultiaddr, libp2p) diff --git a/packages/libp2p-daemon-client/test/stream.spec.ts b/packages/libp2p-daemon-client/test/stream.spec.ts index 5683513a..3bdced90 100644 --- a/packages/libp2p-daemon-client/test/stream.spec.ts +++ b/packages/libp2p-daemon-client/test/stream.spec.ts @@ -2,7 +2,7 @@ import { expect } from 'aegir/chai' import sinon from 'sinon' -import { createServer, Libp2p, Libp2pServer } from '@libp2p/daemon-server' +import { createServer, Libp2pServer } from '@libp2p/daemon-server' import { createClient, DaemonClient } from '../src/index.js' import { multiaddr } from '@multiformats/multiaddr' import { StubbedInstance, stubInterface } from 'sinon-ts' @@ -13,18 +13,21 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import all from 'it-all' import { pipe } from 'it-pipe' +import type { DHT } from '@libp2p/interface-dht' +import type { PubSub } from '@libp2p/interface-pubsub' +import type { Libp2p } from '@libp2p/interface-libp2p' const defaultMultiaddr = multiaddr('/ip4/0.0.0.0/tcp/0') describe('daemon stream client', function () { this.timeout(50e3) - let libp2p: StubbedInstance + let libp2p: StubbedInstance> let server: Libp2pServer let client: DaemonClient beforeEach(async function () { - libp2p = stubInterface() + libp2p = stubInterface>() libp2p.peerStore = stubInterface() server = createServer(defaultMultiaddr, libp2p) diff --git a/packages/libp2p-daemon-server/package.json b/packages/libp2p-daemon-server/package.json index 4241667b..a3b76aac 100644 --- a/packages/libp2p-daemon-server/package.json +++ b/packages/libp2p-daemon-server/package.json @@ -141,6 +141,7 @@ "@libp2p/daemon-protocol": "^4.0.0", "@libp2p/interface-connection": "^5.0.1", "@libp2p/interface-dht": "^2.0.0", + "@libp2p/interface-libp2p": "^3.0.0", "@libp2p/interface-peer-id": "^2.0.0", "@libp2p/interface-peer-store": "^2.0.0", "@libp2p/interface-pubsub": "^4.0.0", diff --git a/packages/libp2p-daemon-server/src/dht.ts b/packages/libp2p-daemon-server/src/dht.ts index 2579f25a..1b63f815 100644 --- a/packages/libp2p-daemon-server/src/dht.ts +++ b/packages/libp2p-daemon-server/src/dht.ts @@ -5,7 +5,7 @@ import { } from '@libp2p/daemon-protocol' import { ErrorResponse, OkResponse } from './responses.js' import type { PeerId } from '@libp2p/interface-peer-id' -import type { DualDHT } from '@libp2p/interface-dht' +import type { DHT } from '@libp2p/interface-dht' import type { CID } from 'multiformats/cid' import drain from 'it-drain' import { logger } from '@libp2p/logger' @@ -13,11 +13,11 @@ import { logger } from '@libp2p/logger' const log = logger('libp2p:daemon-server:dht') export interface DHTOperationsInit { - dht: DualDHT + dht: DHT } export class DHTOperations { - private readonly dht: DualDHT + private readonly dht: DHT constructor (init: DHTOperationsInit) { const { dht } = init diff --git a/packages/libp2p-daemon-server/src/index.ts b/packages/libp2p-daemon-server/src/index.ts index 5393fe04..db5f11ce 100644 --- a/packages/libp2p-daemon-server/src/index.ts +++ b/packages/libp2p-daemon-server/src/index.ts @@ -17,17 +17,14 @@ import { } from '@libp2p/daemon-protocol' import type { Listener, Transport } from '@libp2p/interface-transport' import type { Connection, MultiaddrConnection, Stream } from '@libp2p/interface-connection' -import type { PeerId } from '@libp2p/interface-peer-id' -import type { AbortOptions } from '@libp2p/interfaces' -import type { StreamHandler as StreamCallback } from '@libp2p/interface-registrar' -import type { DualDHT } from '@libp2p/interface-dht' +import type { DHT } from '@libp2p/interface-dht' import type { PubSub } from '@libp2p/interface-pubsub' -import type { PeerStore } from '@libp2p/interface-peer-store' import { ErrorResponse, OkResponse } from './responses.js' import { DHTOperations } from './dht.js' import { peerIdFromBytes } from '@libp2p/peer-id' import { PubSubOperations } from './pubsub.js' import { logger } from '@libp2p/logger' +import type { Libp2p } from '@libp2p/interface-libp2p' const LIMIT = 1 << 22 // 4MB const log = logger('libp2p:daemon-server') @@ -37,24 +34,9 @@ export interface OpenStream { connection: Stream } -export interface Libp2p { - peerId: PeerId - peerStore: PeerStore - pubsub?: PubSub - dht?: DualDHT - - getConnections: (peerId?: PeerId) => Connection[] - getPeers: () => PeerId[] - dial: (peer: PeerId | Multiaddr, options?: AbortOptions) => Promise - handle: (protocol: string | string[], handler: StreamCallback) => Promise - start: () => void | Promise - stop: () => void | Promise - getMultiaddrs: () => Multiaddr[] -} - export interface DaemonInit { multiaddr: Multiaddr - libp2pNode: any + libp2pNode: Libp2p<{ dht: DHT, pubsub: PubSub }> } export interface Libp2pServer { @@ -65,7 +47,7 @@ export interface Libp2pServer { export class Server implements Libp2pServer { private readonly multiaddr: Multiaddr - private readonly libp2p: Libp2p + private readonly libp2p: Libp2p<{ dht: DHT, pubsub: PubSub }> private readonly tcp: Transport private readonly listener: Listener private readonly dhtOperations?: DHTOperations @@ -83,12 +65,12 @@ export class Server implements Libp2pServer { }) this._onExit = this._onExit.bind(this) - if (libp2pNode.dht != null) { - this.dhtOperations = new DHTOperations({ dht: libp2pNode.dht }) + if (libp2pNode.services.dht != null) { + this.dhtOperations = new DHTOperations({ dht: libp2pNode.services.dht }) } - if (libp2pNode.pubsub != null) { - this.pubsubOperations = new PubSubOperations({ pubsub: libp2pNode.pubsub }) + if (libp2pNode.services.pubsub != null) { + this.pubsubOperations = new PubSubOperations({ pubsub: libp2pNode.services.pubsub }) } } @@ -277,7 +259,7 @@ export class Server implements Libp2pServer { */ async * handlePubsubRequest (request: PSRequest): AsyncGenerator { try { - if (this.libp2p.pubsub == null || (this.pubsubOperations == null)) { + if (this.libp2p.services.pubsub == null || (this.pubsubOperations == null)) { throw new Error('PubSub not configured') } @@ -320,7 +302,7 @@ export class Server implements Libp2pServer { */ async * handleDHTRequest (request: DHTRequest): AsyncGenerator { try { - if (this.libp2p.dht == null || (this.dhtOperations == null)) { + if (this.libp2p.services.dht == null || (this.dhtOperations == null)) { throw new Error('DHT not configured') } @@ -539,7 +521,7 @@ export class Server implements Libp2pServer { /** * Creates a daemon from the provided Daemon Options */ -export const createServer = (multiaddr: Multiaddr, libp2pNode: Libp2p): Libp2pServer => { +export const createServer = (multiaddr: Multiaddr, libp2pNode: Libp2p<{ dht: DHT, pubsub: PubSub }>): Libp2pServer => { const daemon = new Server({ multiaddr, libp2pNode diff --git a/packages/libp2p-daemon-server/test/index.spec.ts b/packages/libp2p-daemon-server/test/index.spec.ts index 53a27867..e48fdb86 100644 --- a/packages/libp2p-daemon-server/test/index.spec.ts +++ b/packages/libp2p-daemon-server/test/index.spec.ts @@ -3,14 +3,17 @@ import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' -import { createServer, Libp2p } from '../src/index.js' +import { createServer } from '../src/index.js' import { stubInterface } from 'sinon-ts' +import type { Libp2p } from '@libp2p/interface-libp2p' +import type { DHT } from '@libp2p/interface-dht' +import type { PubSub } from '@libp2p/interface-pubsub' const ma = multiaddr('/ip4/0.0.0.0/tcp/0') describe('server', () => { it('should start', async () => { - const libp2p = stubInterface() + const libp2p = stubInterface>() const server = createServer(ma, libp2p) @@ -22,7 +25,7 @@ describe('server', () => { }) it('should stop', async () => { - const libp2p = stubInterface() + const libp2p = stubInterface>() const server = createServer(ma, libp2p) @@ -33,7 +36,7 @@ describe('server', () => { }) it('should return multiaddrs', async () => { - const libp2p = stubInterface() + const libp2p = stubInterface>() const server = createServer(ma, libp2p)