Skip to content

Commit

Permalink
fix: use interface-libp2p to ensure the correct services are set (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Apr 27, 2023
1 parent 26e99b2 commit 8602a70
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 51 deletions.
1 change: 1 addition & 0 deletions packages/libp2p-daemon-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions packages/libp2p-daemon-client/test/dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -21,15 +23,15 @@ function match (cid: CID): sinon.SinonMatcher {
describe('daemon dht client', function () {
this.timeout(30e3)

let libp2p: StubbedInstance<Libp2p>
let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let server: Libp2pServer
let client: DaemonClient
let dht: StubbedInstance<DualDHT>

beforeEach(async function () {
dht = stubInterface<DualDHT>()
libp2p = stubInterface<Libp2p>()
libp2p.dht = dht
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p.services.dht = dht

server = createServer(defaultMultiaddr, libp2p)

Expand Down
9 changes: 6 additions & 3 deletions packages/libp2p-daemon-client/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

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'
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<Libp2p>
let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let server: Libp2pServer
let client: DaemonClient

beforeEach(async function () {
libp2p = stubInterface<Libp2p>()
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p.peerStore = stubInterface<PeerStore>()

server = createServer(defaultMultiaddr, libp2p)
Expand Down
10 changes: 6 additions & 4 deletions packages/libp2p-daemon-client/test/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Libp2p>
let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let server: Libp2pServer
let client: DaemonClient
let pubsub: StubbedInstance<PubSub>

beforeEach(async function () {
pubsub = stubInterface<PubSub>()
libp2p = stubInterface<Libp2p>()
libp2p.pubsub = pubsub
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p.services.pubsub = pubsub

server = createServer(defaultMultiaddr, libp2p)

Expand Down
9 changes: 6 additions & 3 deletions packages/libp2p-daemon-client/test/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<Libp2p>
let libp2p: StubbedInstance<Libp2p<{ dht: DHT, pubsub: PubSub }>>
let server: Libp2pServer
let client: DaemonClient

beforeEach(async function () {
libp2p = stubInterface<Libp2p>()
libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()
libp2p.peerStore = stubInterface<PeerStore>()

server = createServer(defaultMultiaddr, libp2p)
Expand Down
1 change: 1 addition & 0 deletions packages/libp2p-daemon-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p-daemon-server/src/dht.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ 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'

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
Expand Down
40 changes: 11 additions & 29 deletions packages/libp2p-daemon-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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<Connection>
handle: (protocol: string | string[], handler: StreamCallback) => Promise<void>
start: () => void | Promise<void>
stop: () => void | Promise<void>
getMultiaddrs: () => Multiaddr[]
}

export interface DaemonInit {
multiaddr: Multiaddr
libp2pNode: any
libp2pNode: Libp2p<{ dht: DHT, pubsub: PubSub }>
}

export interface Libp2pServer {
Expand All @@ -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
Expand All @@ -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 })
}
}

Expand Down Expand Up @@ -277,7 +259,7 @@ export class Server implements Libp2pServer {
*/
async * handlePubsubRequest (request: PSRequest): AsyncGenerator<Uint8Array, void, undefined> {
try {
if (this.libp2p.pubsub == null || (this.pubsubOperations == null)) {
if (this.libp2p.services.pubsub == null || (this.pubsubOperations == null)) {
throw new Error('PubSub not configured')
}

Expand Down Expand Up @@ -320,7 +302,7 @@ export class Server implements Libp2pServer {
*/
async * handleDHTRequest (request: DHTRequest): AsyncGenerator<Uint8Array, void, undefined> {
try {
if (this.libp2p.dht == null || (this.dhtOperations == null)) {
if (this.libp2p.services.dht == null || (this.dhtOperations == null)) {
throw new Error('DHT not configured')
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions packages/libp2p-daemon-server/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Libp2p>()
const libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()

const server = createServer(ma, libp2p)

Expand All @@ -22,7 +25,7 @@ describe('server', () => {
})

it('should stop', async () => {
const libp2p = stubInterface<Libp2p>()
const libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()

const server = createServer(ma, libp2p)

Expand All @@ -33,7 +36,7 @@ describe('server', () => {
})

it('should return multiaddrs', async () => {
const libp2p = stubInterface<Libp2p>()
const libp2p = stubInterface<Libp2p<{ dht: DHT, pubsub: PubSub }>>()

const server = createServer(ma, libp2p)

Expand Down

0 comments on commit 8602a70

Please sign in to comment.