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

fix: use placeholder dht/pubsub #1193

Merged
merged 7 commits into from
Apr 21, 2022
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
6 changes: 3 additions & 3 deletions examples/discovery-mechanisms/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { Gossipsub } from '@achingbrain/libp2p-gossipsub'
import { FloodSub } from '@libp2p/floodsub'
import { Bootstrap } from '@libp2p/bootstrap'
import { PubSubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'

Expand All @@ -16,7 +16,7 @@ const createNode = async (bootstrappers) => {
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()],
pubsub: new Gossipsub(),
pubsub: new FloodSub(),
peerDiscovery: [
new Bootstrap({
list: bootstrappers
Expand All @@ -40,7 +40,7 @@ const createNode = async (bootstrappers) => {
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()],
pubsub: new Gossipsub(),
pubsub: new FloodSub(),
peerDiscovery: [
new PubSubPeerDiscovery({
interval: 1000
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"license": "MIT",
"dependencies": {
"@achingbrain/libp2p-gossipsub": "^0.13.5",
"@libp2p/pubsub-peer-discovery": "^5.0.1",
"@libp2p/floodsub": "^1.0.5",
"execa": "^2.1.0",
"fs-extra": "^8.1.0",
"libp2p": "../",
Expand Down
17 changes: 9 additions & 8 deletions examples/pubsub/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { Gossipsub } from '@achingbrain/libp2p-gossipsub'
import { FloodSub } from '@libp2p/floodsub'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { CustomEvent } from '@libp2p/interfaces'

const createNode = async () => {
const node = await createLibp2p({
Expand All @@ -17,7 +16,7 @@ const createNode = async () => {
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()],
pubsub: new Gossipsub()
pubsub: new FloodSub()
})

await node.start()
Expand All @@ -36,17 +35,19 @@ const createNode = async () => {
await node1.peerStore.addressBook.set(node2.peerId, node2.getMultiaddrs())
await node1.dial(node2.peerId)

node1.pubsub.addEventListener(topic, (evt) => {
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)}`)
node1.pubsub.subscribe(topic)
node1.pubsub.addEventListener('message', (evt) => {
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})

// Will not receive own published messages by default
node2.pubsub.addEventListener(topic, (evt) => {
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)}`)
node2.pubsub.subscribe(topic)
node2.pubsub.addEventListener('message', (evt) => {
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})

// node2 publishes "news" every second
setInterval(() => {
node2.pubsub.dispatchEvent(new CustomEvent(topic, { detail: uint8ArrayFromString('Bird bird bird, bird is the word!') }))
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
}, 1000)
})()
9 changes: 4 additions & 5 deletions examples/pubsub/message-filtering/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { Gossipsub } from '@achingbrain/libp2p-gossipsub'
import { FloodSub } from '@libp2p/floodsub'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { CustomEvent } from '@libp2p/interfaces'

const createNode = async () => {
const node = await createLibp2p({
Expand All @@ -17,7 +16,7 @@ const createNode = async () => {
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()],
pubsub: new Gossipsub()
pubsub: new FloodSub()
})

await node.start()
Expand Down Expand Up @@ -45,7 +44,7 @@ const createNode = async () => {
// Will not receive own published messages by default
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)}`)
})
await node1.pubsub.subscribe(topic)
node1.pubsub.subscribe(topic)

node2.pubsub.addEventListener(topic, (evt) => {
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)}`)
Expand Down Expand Up @@ -75,7 +74,7 @@ const createNode = async () => {
// car is not a fruit !
setInterval(() => {
console.log('############## fruit ' + myFruits[count] + ' ##############')
node1.pubsub.dispatchEvent(new CustomEvent<Uint8Array>(topic, { detail: uint8ArrayFromString(myFruits[count]) }))
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
count++
if (count == myFruits.length) {
count = 0
Expand Down
47 changes: 23 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@
},
"dependencies": {
"@achingbrain/nat-port-mapper": "^1.0.0",
"@libp2p/connection": "^1.1.4",
"@libp2p/crypto": "^0.22.9",
"@libp2p/interfaces": "^1.3.21",
"@libp2p/logger": "^1.1.3",
"@libp2p/multistream-select": "^1.0.3",
"@libp2p/peer-id": "^1.1.8",
"@libp2p/peer-id-factory": "^1.0.8",
"@libp2p/connection": "^1.1.5",
"@libp2p/crypto": "^0.22.11",
"@libp2p/interfaces": "^1.3.22",
"@libp2p/logger": "^1.1.4",
"@libp2p/multistream-select": "^1.0.4",
"@libp2p/peer-id": "^1.1.10",
"@libp2p/peer-id-factory": "^1.0.9",
"@libp2p/peer-record": "^1.0.8",
"@libp2p/peer-store": "^1.0.6",
"@libp2p/utils": "^1.0.9",
"@libp2p/peer-store": "^1.0.10",
"@libp2p/tracked-map": "^1.0.5",
"@libp2p/utils": "^1.0.10",
"@multiformats/mafmt": "^11.0.2",
"@multiformats/multiaddr": "^10.1.8",
"abortable-iterator": "^4.0.2",
Expand All @@ -128,6 +129,7 @@
"it-length-prefixed": "^7.0.1",
"it-map": "^1.0.6",
"it-merge": "^1.0.3",
"it-pair": "^2.0.2",
"it-pipe": "^2.0.3",
"it-sort": "^1.0.1",
"it-stream-types": "^1.0.4",
Expand All @@ -154,25 +156,23 @@
"xsalsa20": "^1.1.0"
},
"devDependencies": {
"@achingbrain/libp2p-gossipsub": "^0.13.5",
"@chainsafe/libp2p-noise": "^6.0.1",
"@libp2p/bootstrap": "^1.0.2",
"@libp2p/daemon-client": "^1.0.0",
"@libp2p/daemon-server": "^1.0.0",
"@libp2p/bootstrap": "^1.0.3",
"@libp2p/daemon-client": "^1.0.2",
"@libp2p/daemon-server": "^1.0.2",
"@libp2p/delegated-content-routing": "^1.0.2",
"@libp2p/delegated-peer-routing": "^1.0.2",
"@libp2p/floodsub": "^1.0.2",
"@libp2p/interface-compliance-tests": "^1.1.20",
"@libp2p/floodsub": "^1.0.5",
"@libp2p/interface-compliance-tests": "^1.1.23",
"@libp2p/interop": "^1.0.3",
"@libp2p/kad-dht": "^1.0.5",
"@libp2p/mdns": "^1.0.3",
"@libp2p/mplex": "^1.0.1",
"@libp2p/pubsub": "^1.2.14",
"@libp2p/tcp": "^1.0.6",
"@libp2p/kad-dht": "^1.0.7",
"@libp2p/mdns": "^1.0.4",
"@libp2p/mplex": "^1.0.3",
"@libp2p/pubsub": "^1.2.18",
"@libp2p/tcp": "^1.0.8",
"@libp2p/topology": "^1.1.7",
"@libp2p/tracked-map": "^1.0.4",
"@libp2p/webrtc-star": "^1.0.3",
"@libp2p/websockets": "^1.0.3",
"@libp2p/webrtc-star": "^1.0.7",
"@libp2p/websockets": "^1.0.6",
"@nodeutils/defaults-deep": "^1.1.0",
"@types/node": "^16.11.26",
"@types/node-forge": "^1.0.0",
Expand All @@ -187,7 +187,6 @@
"go-libp2p": "^0.0.6",
"into-stream": "^7.0.0",
"ipfs-http-client": "^56.0.1",
"it-pair": "^2.0.2",
"it-pushable": "^2.0.1",
"nock": "^13.0.3",
"npm-run-all": "^4.1.5",
Expand Down
51 changes: 51 additions & 0 deletions src/dht/dummy-dht.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { DualDHT, QueryEvent, SingleDHT } from '@libp2p/interfaces/dht'
import type { PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
import errCode from 'err-code'
import { messages, codes } from '../errors.js'
import { EventEmitter } from '@libp2p/interfaces'

export class DummyDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
get wan (): SingleDHT {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

get lan (): SingleDHT {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

get (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

findProviders (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

findPeer (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

getClosestPeers (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

provide (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

put (): AsyncIterable<QueryEvent> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

async getMode (): Promise<'client' | 'server'> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

async setMode (): Promise<void> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}

async refreshRoutingTable (): Promise<void> {
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
}
}
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum messages {
NOT_STARTED_YET = 'The libp2p node is not started yet',
DHT_DISABLED = 'DHT is not available',
PUBSUB_DISABLED = 'PubSub is not available',
CONN_ENCRYPTION_REQUIRED = 'At least one connection encryption module is required',
ERR_TRANSPORTS_REQUIRED = 'At least one transport module is required',
ERR_PROTECTOR_REQUIRED = 'Private network is enforced, but no protector was provided',
Expand All @@ -9,6 +10,7 @@ export enum messages {

export enum codes {
DHT_DISABLED = 'ERR_DHT_DISABLED',
ERR_PUBSUB_DISABLED = 'ERR_PUBSUB_DISABLED',
PUBSUB_NOT_STARTED = 'ERR_PUBSUB_NOT_STARTED',
DHT_NOT_STARTED = 'ERR_DHT_NOT_STARTED',
CONN_ENCRYPTION_REQUIRED = 'ERR_CONN_ENCRYPTION_REQUIRED',
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,8 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
connectionManager: ConnectionManager
registrar: Registrar
metrics?: Metrics

pubsub?: PubSub
dht?: DualDHT
pubsub: PubSub
dht: DualDHT

/**
* Load keychain keys from the datastore.
Expand Down
14 changes: 10 additions & 4 deletions src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import errCode from 'err-code'
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
import type { Metrics } from '@libp2p/interfaces/metrics'
import { DummyDHT } from './dht/dummy-dht.js'
import { DummyPubSub } from './pubsub/dummy-pubsub.js'

const log = logger('libp2p')

export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
public peerId: PeerId
public dht?: DualDHT
public pubsub?: PubSub
public dht: DualDHT
public pubsub: PubSub
public identifyService?: IdentifyService
public fetchService: FetchService
public pingService: PingService
Expand Down Expand Up @@ -168,19 +170,23 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
// dht provided components (peerRouting, contentRouting, dht)
if (init.dht != null) {
this.dht = this.components.setDHT(this.configureComponent(init.dht))
} else {
this.dht = new DummyDHT()
}

// Create pubsub if provided
if (init.pubsub != null) {
this.pubsub = this.components.setPubSub(this.configureComponent(init.pubsub))
} else {
this.pubsub = new DummyPubSub()
}

// Attach remaining APIs
// peer and content routing will automatically get modules from _modules and _dht

const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map(component => this.configureComponent(component))

if (this.dht != null) {
if (init.dht != null) {
// add dht to routers
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))

Expand All @@ -197,7 +203,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {

const contentRouters: ContentRouting[] = (init.contentRouters ?? []).map(component => this.configureComponent(component))

if (this.dht != null) {
if (init.dht != null) {
// add dht to routers
contentRouters.push(this.configureComponent(new DHTContentRouting(this.dht)))
}
Expand Down
51 changes: 51 additions & 0 deletions src/pubsub/dummy-pubsub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { EventEmitter } from '@libp2p/interfaces'
import type { PeerId } from '@libp2p/interfaces/peer-id'
import type { PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interfaces/pubsub'
import errCode from 'err-code'
import { messages, codes } from '../errors.js'

export class DummyPubSub extends EventEmitter<PubSubEvents> implements PubSub {
isStarted (): boolean {
return false
}

start (): void | Promise<void> {

}

stop (): void | Promise<void> {

}

get globalSignaturePolicy (): typeof StrictSign | typeof StrictNoSign {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

get multicodecs (): string[] {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

getPeers (): PeerId[] {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

getTopics (): string[] {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

subscribe (): void {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

unsubscribe (): void {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

getSubscribers (): PeerId[] {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}

publish (): void {
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
}
}
Loading