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: expose metrics and registrar, use dht for peer discovery #1183

Merged
merged 1 commit into from
Apr 2, 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: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type { ConnectionEncrypter } from '@libp2p/interfaces/connection-encrypte
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
import type { PubSub } from '@libp2p/interfaces/pubsub'
import type { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
import type { MetricsInit } from '@libp2p/interfaces/metrics'
import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { DialerInit } from '@libp2p/interfaces/dialer'
import type { KeyChain } from './keychain/index.js'
Expand Down Expand Up @@ -152,6 +152,8 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
contentRouting: ContentRouting
keychain: KeyChain
connectionManager: ConnectionManager
registrar: Registrar
metrics?: Metrics

pubsub?: PubSub
dht?: DualDHT
Expand Down
14 changes: 11 additions & 3 deletions src/libp2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type { Connection } from '@libp2p/interfaces/connection'
import type { PeerRouting } from '@libp2p/interfaces/peer-routing'
import type { ContentRouting } from '@libp2p/interfaces/content-routing'
import type { PubSub } from '@libp2p/interfaces/pubsub'
import type { ConnectionManager, StreamHandler } from '@libp2p/interfaces/registrar'
import type { ConnectionManager, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
import { validateConfig } from './config.js'
Expand All @@ -43,6 +43,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
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'

const log = logger('libp2p')

Expand All @@ -59,6 +60,8 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
public peerRouting: PeerRouting
public keychain: KeyChain
public connectionManager: ConnectionManager
public registrar: Registrar
public metrics?: Metrics

private started: boolean
private readonly services: Startable[]
Expand All @@ -78,7 +81,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {

// Create Metrics
if (init.metrics.enabled) {
this.components.setMetrics(this.configureComponent(new DefaultMetrics(init.metrics)))
this.metrics = this.components.setMetrics(this.configureComponent(new DefaultMetrics(init.metrics)))
}

this.components.setConnectionGater(this.configureComponent({
Expand Down Expand Up @@ -117,7 +120,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
this.connectionManager = this.components.setConnectionManager(this.configureComponent(new DefaultConnectionManager(this.components, init.connectionManager)))

// Create the Registrar
this.components.setRegistrar(this.configureComponent(new DefaultRegistrar(this.components)))
this.registrar = this.components.setRegistrar(this.configureComponent(new DefaultRegistrar(this.components)))

// Setup the transport manager
this.components.setTransportManager(this.configureComponent(new DefaultTransportManager(this.components, init.transportManager)))
Expand Down Expand Up @@ -180,6 +183,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
if (this.dht != null) {
// add dht to routers
peerRouters.push(this.configureComponent(new DHTPeerRouting(this.dht)))

// use dht for peer discovery
this.dht.addEventListener('peer', (evt) => {
this.onDiscoveryPeer(evt)
})
}

this.peerRouting = this.components.setPeerRouting(this.configureComponent(new DefaultPeerRouting(this.components, {
Expand Down